2014-09-05 36 views
0

情况:我是一个有数据库的新手,刚刚插入python(很长)的MySQLdb。我在如何构建我的数据表方面寻找基本技巧,当断裂成一个新表等关系数据库结构 - 最佳实践?

例:说我在看宠物的主人,他们的宠物,和他们的宠物' 玩具。我最终对玩具的属性感兴趣。

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. 

问:这应该留一个表,或者我应该有几个表连接业主的宠物,然后用宠物玩具等?是否有一条硬性规定?

在此先感谢。

+0

http://en.wikipedia.org/wiki/Database_normalization – Rob 2014-09-05 12:14:40

回答

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