2014-09-05 89 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