簡體   English   中英

關系數據庫結構-最佳實踐?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM