简体   繁体   English

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

[英]Relational database structure - best practice?

Situation: I am a bit of a newbie with databases, and have just plugged MySQLdb into python (at great length). 情况:我是数据库的新手,并且刚刚将MySQLdb插入python(很长)。 I am looking for basic tips in terms of how to structure my data tables, when to break off into a new table, etc. 我正在寻找有关如何构造数据表,何时分解为新表等方面的基本技巧。

Example: Say I'm looking at pet owners, their pets, and their pets' toys. 示例:假设我正在看宠物主人,他们的宠物和他们的宠物玩具。 I'm ultimately interested in the properties of the toys. 我最终对玩具的属性感兴趣。

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.

Question: Should this stay as one table, or should I have several tables linking owners with pets, then pets with toys, etc.? 问题:这应该是一张桌子,还是我应该有几张桌子将主人与宠物联系起来,然后将宠物与玩具联系起来,等等? Is there a hard and fast rule? 有硬性规定吗?

Thanks in advance. 提前致谢。

I would think of each object as it's own table, then look for one to many relationships and give them their own table, so something like this: 我会将每个对象都视为自己的表,然后查找一对多关系并为其提供自己的表,因此如下所示:

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

Pets 宠物用品

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)

Toys 玩具

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