DB design : save different details of payment (credit or check)

后端 未结 6 1107
温柔的废话
温柔的废话 2020-12-30 14:09

I have a member that can pay in three different ways:

  1. credit card
  2. check
  3. transfer from bank account

How can I design a table t

6条回答
  •  隐瞒了意图╮
    2020-12-30 14:54

    The way to do it is to make four tables, if the three ways have all different fields. Make one table for each of the different methods and one table for the order itself. In the order table, you will store the orderID and an enumerated value of which method the user used to pay (in addition to any other fields you need). For instance, if you were programming in .NET, you could create an enum:

    Public Enum PaymentMethod
        CreditCard
        Check
        WireTransfer
    End Enum
    

    This would effectively set CreditCard equal to 0, Check to 1 and WireTransfer to 2. Have a field in your main order table for what type this is and save this integer there. In your payment tables, have a foreign key for the OrderID from your main table. If you wanted, you could keep the ID of the record in the payment table, but it's unnecessary because you can find the records based on the OrderID:

    SELECT o.*, cc.* FROM Orders o 
    INNER JOIN CreditCardPayments cc ON o.OrderID=cc.OrderID
    WHERE cc.OrderID=[some number]
    

    Of course if you have three different methods, you'll have to check first to see which kind of payment it is first and then use the appropriate SELECT statement:

    SELECT PaymentMethod FROM Orders WHERE OrderID=[some number]
    

    Once you determine with method, you can use pre-made SELECT statements that call from your different payment method tables.

    Hope this helps!

提交回复
热议问题