Database Design: Add a foreign key constraint to a dynamic key

The payment_type_id will let my app determine if it should refer to paypal_payments or stripe_payments table for payment details. The reference_id will be used to get the payment details from one of those tables.

I want to make reference_id a foreign key and have a constraint such that it will restrict updates and deletes on those payment ids. The database schema below shows my current setup without the FK on reference_id.

Is there a better way to design my table? Or is there a way for me to implment the FK with this current setup? I'd like to avoid having to have all the payment details within the payments table if I can seaprate it out appropriately based on payment type.

Zoe ZoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
payment_type will only tell you the payment method but to make things easier, i guess in your tables: paypal_payments and stripe_payments, it should include the as the FK.

and I guess, you can't combine both tables: paypal_payments and stripe_payments as they got different attributes (fields)?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Zoe ZoeAuthor Commented:
Great idea! I don't know why I didn't think of that.

What's the general rule for where to put the FK? In your suggestion, you switched it from payments to the paypal_payments and strip_payments. Is there a rule that should be followed?
Ryan ChongCommented:
>>What's the general rule for where to put the FK?
Personally think it's to normalise your tables : ) that's why we try to split one table into many to reduce duplicate data.

>>In your suggestion, you switched it from payments to the paypal_payments and strip_payments. Is there a rule that should be followed?
I don't quite understand what you mean it, but I just want to have an easier way, so that we can link the tables together. Personally, if there are different kind of payment methods are allowed, and you wish to capture different info from these payment methods, I would suggest a database design like this:

Table: payments
id                  int            PK
payment_type_id            int            FK

Table: payment_type
id                  int            PK
name                  varchar(127)

Table: paypal_payments
id                  int            PK (from table: payments)
payments_id            int            FK

Table: stripe_payments
id                  int            PK
payments_id            int            FK (from table: payments)

hope this make sense to you...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.