关系数据库结构 - 最佳实践?

时间:2014-09-05 12:12:30

标签: mysql data-structures relational-database

情况:我是一个有数据库的新手,刚刚将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.

问题:这应该作为一张桌子,或者我应该有几张桌子将所有者与宠物联系起来,还有带玩具等的宠物?是否存在严格的规则?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我会认为每个对象都是它自己的表,然后查找一对多关系并给它们自己的表,所以像这样:

PetOwner

Column
------
OwnerId --primary key
FirstName
LastName
--any other info you want to track on the owner

OwnersPets

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...)

PetsToys

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