我在设计新的数据库表时遇到问题。是否可以像ID一样存储数据?
假设我有一张桌子在哪里:
ID type
1 client_id
2 item_id
3 price
4 date
像这样制作表可能会出错:
ID transaction foreign_id data
1 222 1 14
2 222 2 25
3 222 3 $255
4 222 4 5/17/2020
5 528 1 14
6 528 2 187
7 528 3 $14
8 528 4 5/16/2020
等等..
我要制作一个更大的数据库,可能超过20列。我在想这是否是(更好的)解决方案。这种结构是否有可能出错的地方?
PS:Postgre数据库
您正在描述实体属性值模型。
我实际上不建议这样做,因为它有很多陷阱,例如:
integrity of attribute names cannot be enforced (what if someone creates a new attribute called
id_client
, whileclient_id
exists already?)all values are stored in a single column so you cannot use proper sql datatypes for each attriute (what if someone sets date
2020-00-00
?)not possible to enforce the notion of mandatory attribute, or any other type of constraint
On the other hand, all above points are properly managed by creating a table with the relevant number of columns, with proper datatypes and
not null
constraints. 20 columns is not that many - and alternative solutions are possible while use relational tables, such as inheritance.