1

I have a table with 2 foreign keys, 1 leading to a individual person table and 1 leading to a company table.

I want to set it up so that mysql will allow any 1 of the foreign keys to be null but fail to insert if both are null.

If that is not the case I will just enforce the rule on the front end.

EDIT: This is different than other questions is that the key could be one or the other or both. From what I read in the answers they are only talking about it being one table or the other table not both

Thank you for your time

grandpa_sam
  • 119
  • 6
  • We need to see table schema, bro. I mean grandpa – Phiter Mar 16 '16 at 18:05
  • Possible duplicate of [Possible to do a MySQL foreign key to one of two possible tables?](http://stackoverflow.com/questions/441001/possible-to-do-a-mysql-foreign-key-to-one-of-two-possible-tables) – philipxy Mar 17 '16 at 00:04
  • `FOREIGN KEYs` are rather limited. I often thing they are not worth the effort. – Rick James Mar 17 '16 at 06:50

1 Answers1

0

No, don't do it like that.

You need better abstraction. Read up on table inheritance, and The Party Model.

Have a single table parties and a type column for individuals and organizations. Point your foreign key at this table.

You should read http://www.slideshare.net/billkarwin/sql-antipatterns-strike-back/33-Polymorphic_Assocations_Objective_reference_multiple

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152