简体   繁体   中英

Creating new table vs adding new field

I am having following data :

Fabric Cost

time     |   No fabric|BangloreSilk|Chanderi|.... <- fabric types
--------------------------------------------
01/15    |         40 |         25 |...
02/15    |         45 |         30 |...
.....    |        ... |        ... |...

Dyeing Cost

time     |   No fabric|BangloreSilk|Chanderi|.... <- fabric types
--------------------------------------------
01/15    |         40 |         25 |...
02/15    |         45 |         30 |...
.....    |        ... |        ... |...

And here list of fabric types will be same for both the data.
Now to add this data I created following tables :

fabric_type

id                  int
fabric_type_name    varchar

And then I have two approaches .

Approach 1 :

fabric_cost

id                  int
fabric_type_id      int (foreign key to fabric_type)
cost                int

deying_cost

id                  int
fabric_type_id      int (foreign key to fabric_type)
cost                int

Approach 2 :

fabric_overall_cost

id                  int
fabric_type_id      int (foreign key to fabric_type)
cost                int
fabric_or_dyeing    bit (to represent 0 for fabric cost and 1 for dyeing cost)

Now the question is which approach will be better??

Maybe you can create another table - cost_subjects

cost_subjects

id                  byte
subject             varchar

costs

id                  int
fabric_type_id      int (foreign key to fabric_type)
cost                int
cost_subject        byte (foreign key to cost_subjects table)

And then you can extend the table with more subjects to include in costs of fabric

It really depends on your requirements. Are there other columns that are unique only for the fabric_cost table? Are there other columns that are unique only for the dyeing_cost table? Meaning will your 2 tables grow independently? If yes, approach 1 is better. Otherwise, approach 2 is better because you won't need to do CRUD on 2 separate tables (for easier maintenance). Another approach would be:

id                  int
fabric_type_id      int (foreign key to fabric_type)
fabric_cost         float/double/decimal
dyeing_cost         float/double/decimal

This third approach is if you always have both costs. You might not want to use int for cost. Again, it depends on your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM