问题
Say I have a project ~10 tables with ~30 entries each, which enumerate values. What are possible advantages/disadvantages with regard to scenario 1 vs scenario 2?
And is there a turning point for these (dis)advantages based on the amount of enumeration tables/amount of rows per table?
Scenario 1:
Table 1:
ID Value
---------
1 - Value1
2 - Value2
3 - Value3
Table2:
ID Value
---------
1 - Value4
2 - Value5
3 - Value6
Scenario 2:
Table1:
ID Value Category
---------------------
1 - Value1 - 1
2 - Value2 - 1
3 - Value3 - 1
4 - Value4 - 2
5 - Value5 - 2
6 - Value6 - 2
回答1:
I would go with option one.
The main reason is that the first option enables you to use specific foreign key constraints, meaning that every attribute in the main table(s) can be linked only to it's relevant table, while to acheive the same thing with the second option you will have to also use check constraint, that will force you to create a more cumbersome database schema.
The second reason is that database tables should reflect business entities - and the first option enables you to do that more accurately.
The third reason is that it allows you to save "enums" with the most correct data type for each "enum".
Let's review a more detailed example - suppose your database is for selling shirts. Shirts have a veriaty of details that may verify - like color and size for example.
Your first option would translate to something like this:
Create table Shirts
(
Id int,
Model_Id int,
Color_Id int,
Size_Id int
)
Create table Models
(
Id int,
Name varchar(50)
)
Create table Colors
(
Id int,
Name varchar(20)
)
Create table Sizes
(
Id int,
Name varchar(5)
)
The second one would translate to this:
Create table Shirts
(
Id int,
Model_Id int,
Color_Id int,
Size_Id int
)
Create table Enums
(
Id int,
Name varchar(50),
Categoery_Id int
)
Create table Categories
(
Id int,
Name --(Model, Color, Size etc`)
)
Now say someone changes the category_id for a specific record in the enums table - your data is no longer correct - that can't happen with the first option.
来源:https://stackoverflow.com/questions/42854130/what-are-the-pros-and-cons-of-make-seperate-enum-tables-in-sql-server-vs-one-tab