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

后端 未结 6 1129
温柔的废话
温柔的废话 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:57

    Store all income in a Payments table. Each Payment specifies a payment method from the PaymentMethods table and is related to an order in the Orders table.

    In the past I stored payment methods in a table, but after some thinking I conclude that an IPaymentMethod interface with derived classes (eg. class CreditCard : IPaymentMethod), would be easier to extend and maintain.

    Database Schema

    1. Partial PaymentMethods Table

    • Method ID (primary key)
    • Name (eg. CreditCard, Check, WireTransfer)

    2. Partial Orders Table

    • Order ID (primary key)
    • User ID (relates to Users table)
    • Status (Pending or Completed)
    • Total

    3. Partial Payments Table

    • Payment ID (primary key)
    • Order ID (related to Orders table)
    • Method ID (relates to PaymentMethods table)
    • Date (for record-keeping)
    • Amount

    Order Saldo Query

    To determine the total value of payments received for any given Order, simply sum the Amount the Payments table, grouped by Order ID:

    SELECT
        OrderID,
        SUM(Amount) as Saldo
    FROM Payments
    GROUP BY
        Payments.OrderID
    

提交回复
热议问题