简体   繁体   中英

ERD: how to store data for three different payment types

Let's say a user has 3 different ways for payment. Cash, IBAN, and through providing certain_document. Each payment type requires its own different details.

How can I store this in my database?

Let's say the user has chosen to pay using his IBAN, assuming this picture is the current database, do I fill the fields associated with the IBAN option and set the others to Null? Or is there a more professional way to store the data without having these Null values?

UPDATE

I found a solution to this problem in the answer to this question , however, the answer is still not sufficient. If anybody has a link to a more detailed documents please let me know.

As @philipxy noted, you're asking about representing inheritance in a RDBMS. There are a few different ways to do this:

  1. Have all of your attributes in one table (which, based on your screenshot, is what you have now). With this approach, it would be best to store NULL s in non-applicable columns---if nothing else, the default settings for InnoDB tables uses the compact row format, which means NULL columns don't take up extra storage. Of course, your queries can get complex, and maintaining these tables can become cumbersome.
  2. Have child tables to store your details:

    • Payments (PaymentID, PaymentDate, etc.)
    • CashPaymentDetails(PaymentID, Cash_Detail_1, Cash_Detail_2, etc.)
    • IBANPaymentDetails(PaymentID, IBAN_Detail_1, etc.)
    • You can get the information for each payment by joining the base payment table with one of the "subsidiary" tables:

      SELECT * FROM Payments P INNER JOIN CashPaymentDetails C ON C.PaymentID = P.PaymentID

  3. Your third option is to use the entity-attribute-value (EAV) model. Like with Option 2, you have a base Payment table. However, instead of having one table for each payment method, you have one subsidiary table that contains the payment details. For more information, here's the Wiki page , and here's a blog with some additional information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM