Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A table with many null value vs 2 separate tables

I have a table (X) with 10 columns, 6 of which are nullable foreign keys (bigint data type) and in every row 5 of them will be NULL.

SOLUTION: Divide table (X) into 2 tables: (X) and (XType) so that (X) includes a big int column (not foreign key) for insert one of six ID and a XType_id column for determining types of 6 data.

Is this solution optimal or the first table with 10 columns is better?

like image 461
FS-DBA Avatar asked Oct 27 '25 05:10

FS-DBA


1 Answers

I prefer your first option.

When you have those 6 individual foreign key columns, then you can enforce referential integrity by having actual foreign key constraints to those 6 referenced tables.

If you have a super-smart approach with a single ID and an ID_Type, you cannot enforce referential integrity anymore.

The benefit of being able to truly enforce referential integrity for me far outweighs the "benefit" of having just a single ID column; having a few columns with NULL values isn't bad

like image 112
marc_s Avatar answered Oct 28 '25 19:10

marc_s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!