简体   繁体   English

数据库架构关系设计

[英]Database Schema relationship design

在此输入图像描述

Hi there, 嗨,您好,

I'm new to database design and I'm trying to design a very simple schema for the model. 我是数据库设计的新手,我正在尝试为模型设计一个非常简单的模式。

Just wondering if this will be the best way to design it, since this is my first time and don't want to build this out without a lookover. 只是想知道这是否是设计它的最佳方式,因为这是我的第一次,并且不想在没有外观的情况下构建它。

Pokemon table:

  • I get the name, id (PK), type(FK), image and regionID(fk) 我得到name, id (PK), type(FK), image and regionID(fk)
  • type is a Foreign Key from the Type table and has a one-to-many relationship. typeType表中的Foreign Key,具有one-to-many关系。 Does that sound right? 听起来不错吗? I'm thinking this because one pokemon can have multiple types ? 我在想这个因为one pokemon can have multiple types Also multiple pokemon can have multiple types makes sense to me. 还有multiple pokemon can have multiple类型对我有意义。
  • regionID is a foreign key from Region table. regionIDRegion表中的外键。 many pokemon can live in many regions makes sense to me. many pokemon can live in many regions对我有意义。 Or should it be one pokemon can live in multiple regions ? 或者它应该是one pokemon can live in multiple regions Or multiple regions can have multiple pokemon ? 或者multiple regions can have multiple pokemon

Region table:

  • I've only got a PK in here. 我这里只有PK Do I need the FK from LocationWithinRegion table as well? 我还需要LocationWithinRegion表中的FK吗?
  • I have a one to many relationship here, Because 1 region can have multiple locations but one location can't have multiple regions . 我在这里有one to many关系,因为1 region can have multiple locations但是one location can't have multiple regions Is this right? 这是正确的吗?

Type table : Type table

  • I made all those types bits so I can represent a boolean value. 我创建了所有这些类型的位,所以我可以表示一个布尔值。 I want to be able to query the pokemon table and look for all related data and find true or false in regions where that pokemon can be found. 我希望能够查询口袋妖怪表并查找所有相关数据,并在可以找到该口袋妖怪的区域中找到真或假。

This is my first time making a database schema so please, let me know how it looks! 这是我第一次制作数据库架构,请让我知道它的外观!

Thanks 谢谢

In general: 一般来说:

  • Provide each table with a singular name. 为每个表提供单数名称。 User not Users as each User不是每个Users
    row represents a User. row代表用户。
  • Provide each table with the smallest possible primary key. 为每个表提供尽可能小的主键。 In most cases an identity int would do. 在大多数情况下,标识int会这样做。
  • Create an index on foreign key columns. 在外键列上创建索引。 This will help joins. 这将有助于加入。

Now in your case: 现在你的情况:

There is a many-to-many relationship between Pokemon and Type . PokemonType之间存在多对多的关系。 So I would drop the PokemonType string from the Pokemon table and I would create a table Type(Id int Identity PK, Description string Unique) and a table PokemonType(PokemonId int FK on Pokemon, TypeId int FK on Type, PK on both columns) 所以我会从Pokemon表中删除PokemonType string ,然后创建一个表Type(Id int Identity PK, Description string Unique)和一个表PokemonType(PokemonId int FK on Pokemon, TypeId int FK on Type, PK on both columns)

Also is a many-to-many relationship between Pokemon and Region . 也是PokemonRegion之间的多对多关系。

In general, to represent a many-to-many relationship, you need a lookup table between the two tables. 通常,要表示多对多关系,您需要在两个表之间使用查找表。

Something like A(id) -> Lookup(A.id, B.id) <- B(id) 类似A(id) - > Lookup(A.id,B.id)< - B(id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM