[MySQL] Can anybody help answer my stack overflow question more clearly or point me in the direction of some good topics on the matter?
http://stackoverflow.com/questions/34927924/how-to-add-related-properties-to-a-table-in-mysql-correctly
0
Upvotes
2
u/muchargh Jan 25 '16
You are right to want to avoid NULL columns and the headaches that can bring.
The solution is another table (although not the table you have mocked up). The idea that joins are inherently bad misses the point entirely. Joins are generally a good thing.
This is the table you need:
WidgetId is the primary key and has a foreign key constraint to the widget table.
You'll want to avoid nonsense for packaging type, so this will allow a foreign key constraint:
Semantically no packaging is type of packaging, hence its inclusion in the packaging table. It says we know the item is not packaged.
If no packaging information is present, we know it hasn't been entered/checked/whatever.
I'd use CHAR(4) for the PackagingType column as it allows someone to use the table without going back to lookup what "4" is. Same space as an integer, except it has meaning and eliminates the need to join to or query the reference.
So why this way and not a nullable column?
Here is the code to find widgets without packaging information:
1
u/specialcrayon Jan 22 '16
Add actual database schema to the question.