简体   繁体   English

主键和外键?

[英]primary keys and foreign keys?

When creating a database what happens if one table only contains 2 primary keys from other tables, i'm assuming they're both foreign keys. 创建数据库时,如果一个表仅包含来自其他表的2个主键,那会发生什么情况,我假设它们都是外键。 Does there have to be a primary key in the table? 表中是否必须有主键?

A link table with just two foreign keys and nothing else (no surrogate key added to make a simple meaningless primary key) will usually be constrained to be unique (otherwise you will not be able to distinguish duplicates - and this is also a violation of normal form) so you will often just go ahead and have those two keys together as a composite make up the primary key (PKs have to be unique by definition, and they form a natural choice for the PK of such a link table). 通常只有两个外键且没有别的链接表(没有添加代理键以构成简单的毫无意义的主键)通常会被约束为唯一的(否则您将无法区分重复项-这也违反了常规表格),因此您通常会继续将这两个键组合在一起构成主键(根据定义,PK必须是唯一的,并且它们是此类链接表PK的自然选择)。 The order of those columns in the primary key is usually determined by the most frequent order of search - ie personid, accountid might have personid first in a composite primary key on personid, accountid. 主键中这些列的顺序通常由最频繁的搜索顺序决定-即,personid,accountid在personid,accountid的复合主键中可能首先具有personid。

No; 没有; tables don't have to have primary keys. 表不必具有主键。

They often (usually?) don't when they're linking/mapping tables like this. 当他们像这样链接/映射表时,它们通常(通常?)不这样做。

When you have an associative entity or table, it is good practice to create a composite primary key consisting of the keys of both parents. 当您具有关联实体或表时,最好的做法是创建一个由两个父级键组成的复合主键。 In the example, there would be a uniqueness constraint on the Album-Song-Map, on the composite of Album and Song. 在该示例中,在专辑-歌曲-地图上,专辑和歌曲的组合上将存在唯一性约束。 If you do not make these the primary key and make the combination unique, then you can have duplicates in Album-Song-Map. 如果您不将这些作为主键并使组合唯一,则可以在Album-Song-Map中创建重复项。 The composite key of Album-Song-Map is the key of both parents, but individually they are foreign keys back to their parent, Album and Song respectively. Album-Song-Map的复合键是父母双方的键,但分别地,它们分别是返回其父母的外键,即Album和Song。 In my experience, the associative entity is usually not just for mapping but also contains some business attributes. 以我的经验,关联实体通常不仅用于映射,而且还包含一些业务属性。 For example, say the song has a different Duration (playing time) on one album than another. 例如,假设某首歌在专辑中的持续时间(播放时间)与另一张专辑不同。 This attribute would have to go in Album-Song-Map. 该属性必须在专辑歌曲地图中。 I use a lot of music for dancing, and often the duration of a song is different on different albums. 我使用很多音乐来跳舞,而且不同专辑上的歌曲持续时间通常会有所不同。

A Table doesn't necessarily need to have a primary key associated. 表不一定需要关联主键。 It i completely valid as follows 我完全有效如下

Table_Album Table_Album

pkey | name
1    | name1
2    | name2

Table_Song Table_Song

pkey | name
1    | song1
2    | song2
3    | song3

You can then have a table which states 然后,您可以有一个表,其中指出

Table_Album_Song_Map Table_Album_Song_Map

id | Album | Song  # Here id is just row number and not primary key
1  | name1 | song1
2  | name1 | song2
3  | name2 | song3

Hope that helps 希望能有所帮助

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

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