[英]Relational database structure - best practice?
情况:我是数据库的新手,并且刚刚将MySQLdb插入python(很长)。 我正在寻找有关如何构造数据表,何时分解为新表等方面的基本技巧。
示例:假设我正在看宠物主人,他们的宠物和他们的宠物玩具。 我最终对玩具的属性感兴趣。
Pet owner 1: has 3 pets: each pet has 5 toys: each toy has unique properties.
Pet owner 2: has 2 pets: each pet has 4 toys: each toy has unique properties.
问题:这应该是一张桌子,还是我应该有几张桌子将主人与宠物联系起来,然后将宠物与玩具联系起来,等等? 有硬性规定吗?
提前致谢。
我会将每个对象都视为自己的表,然后查找一对多关系并为其提供自己的表,因此如下所示:
宠物主人
Column
------
OwnerId --primary key
FirstName
LastName
--any other info you want to track on the owner
主人宠物
Column
------
OwnerId --composite primary key
PetId --composite primarykey
--any other info relating to the ownership
宠物用品
Column
------
PetId --primarykey
PetName
--any other pet properties (color, birthdate, etc...)
宠物玩具
Column
------
PetId --composite primary key
ToyId --composite primarykey
--any other info relating to the pets ownership of the toy (dateOfIntroduction maybe)
玩具
Column
------
ToyId--primarykey
Manufacturer
Type
Cost
DateOfPurchase
--any other toy properties
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.