0

I have a mysql database and some tables.

One table is Products: Its columns are prod_id, prod_type.

Prod_types are mobile or laptop or book.

Each of them have different attributes (eg: publisher, author for books, battery_condition, model for laptop etc).

Should I store all the data in the Products table by adding columns like 'author', 'model' etc and keep null for those columns which don't apply to my product.

For example for an entry with prod_type='book' , 'model', 'batter_condition' etc will be null. Or should I create different tables for 'book', 'mobile' and 'laptop' ?

Simo
  • 955
  • 8
  • 18
jishnu
  • 3
  • 1
  • Welcome to SO. Please provide a Minimal, Complete, and Verifiable example. **Show us the code for your latest attempt** and where you got stuck. and explain why the result is not what you expected. https://stackoverflow.com/help/mcve Please try to lay out your question in a way that other people can read it. As it stands, you are unlikely to get help as people won't be able to understand what you have written. – Dragonthoughts Sep 04 '18 at 10:52
  • in general, you shouldn't have a bunch of "optional" fields in a relational database. Create suitable tables for them and link to them. – Dragonthoughts Sep 04 '18 at 10:53
  • Have a read of the answers to https://stackoverflow.com/questions/386652/techniques-for-database-inheritance, which detail a few approaches and their pros and cons. – Matt Raines Sep 04 '18 at 16:46

1 Answers1

1

Yes. Create new tables for Book, Mobile and Laptop. Have the product_id attribute in each of those tables. This product_id attribute is a foreign key to the primary key of table products, which is product_id as well.

Storing all data in products will result in a lot of NULL fields and will make the data storage inefficient.