Suppose I have three tables
orders
table, fields: name, address, country, paid, shipped, amountpaypal
payment table, fields: order_idstripe
payment table, fields: customer_id, cvc_valid, address_valid, zip_valid
Each order should be associated with only one payment table, paypal
or stripe
Question is: How do I associate an Order with its payment when it is possible that the payment is either in the Paypal table or in the Stripe table?
I thought of adding an enum field to the Orders table to decide whether to look up the payment in the Paypal table or to look up in the Stripe table. Are there better methods than this?
I would like to use either MariaDB, MySQL, or SQLite.
Best Answer
There are several options:
Paypal
andStripe
tables into a singlePayment
table, with fields to hold either type of payment;paypal_id
and astripe_id
in theOrder
table; use a condition on the table to ensure that at least one of them is NULL.Paypal
orStripe
as you propose.The first two options allow for the payment ID(s) to be foreign keys to the payment table(s), the third does not.
I would lean towards a single table with all payment data, but that may be a personal preference. Which of the first two options is better would depend on whether there are any common columns between
Paypal
andStripe
, and just how large the distinct columns are.UPDATE: joanolo's answer lays out how to actually implement my option 2; take a look!