简体   繁体   English

两个主键

[英]Two Primary Keys

这个

here is the pictorial representation of my partial database. 这是我的部分数据库的图形表示。 BrandNo is a primary key in Suppliar Table, that is being used as Foreign Key in others. BrandNoSuppliar Table中的主键,在Suppliar表中用作外键。

In LotDetails Table I need BrandName as Foreign Key. LotDetails表中,我需要LotDetails作为外键。 This sounds absurd as i can make either 这听起来很荒谬,因为我可以做到

  1. a Single Primary Key OR 单个主键
  2. a Composite Key 复合键

that will be used as Foreign Key. 将用作外键。

I know that I can use BrandNo as Foreign Key and Display BrandName, but for the sake of KNOWLEDGE (and yes EASE ofcourse) i want to know that 我知道我可以使用BrandNo作为外键并显示BrandName,但是为了知道(并且是EASE ofcourse)我想知道

Is it possible to use two attributes of a table as Foreign Keys separately in different Tables? 是否可以在不同的表中分别使用表的两个属性作为外键?

EDITTED EDITTED

BrandNo is just a Serial Number and Brand Name can be the Name of any Brand. BrandNo只是一个序列号,品牌名称可以是任何品牌的名称。 BrandNo is needed in 4 Tables as shown, whereas BrandName is needed in only one table. 如图所示,4个表中需要BrandNo,而只有一个表需要BrandName。 Thanks! 谢谢!

Yes! 是! A FK don't need to reference a PK, you even don't need to reference a indexed column but for the sake of relational integrity (and sanity) you must reference a unique valued column (thus is why we "like" to reference a PK or at least a unique non clustered indexed column). FK不需要引用PK,你甚至不需要引用索引列,但为了关系完整性(和健全性),你必须引用一个唯一值列(这就是为什么我们“喜欢”引用PK或至少一个唯一的非聚簇索引列)。

It's sound a bit weird but you can build a relational tableAB holding IdA, IdB and tableA and tableB referencing tableAB respective columns. 这听起来有点奇怪,但您可以构建一个关系表AB,其中包含IdA,IdB和tableA以及引用tableAB各自列的tableB。

btw: a table don't need to own a PK but cannot exist two PK. 顺便说一句:一张桌子不需要拥有PK但不能存在两个PK。 In general the table is physical ordered by the PK. 通常,该表是由PK物理排序的。

Yes that's quite possible. 是的,这很有可能。 Assuming BrandName is a candidate key on its own then in principle you can reference it in just the same way as BrandNo. 假设BrandName本身就是候选键,那么原则上你可以像BrandNo一样引用它。 In that case BrandName and BrandNo would not be a composite key, they would both be separate candidate keys . 在这种情况下,BrandName和BrandNo不是复合键,它们都是单独的候选键

By convention and for simplicity and ease of use it is usual to choose just one key per table to be used for foreign key references. 按照惯例,为了简单和易于使用,通常每个表只选择一个密钥用于外键引用。 Usually (not always) that is the one designated as "primary" key but it doesn't have to be so if you find a good reason to do otherwise. 通常(并非总是)指定为“主要”密钥的密钥,但如果您找到其他原因,则不一定如此。

When you create foreign key references, the key in the referenced table should be a primary key . 创建外键引用时,引用表中的应该是主键 It doesn't make sense to have a "partial" reference. 拥有“部分”引用是没有意义的。

Instead, you should have a Brands table that has a primary key (perhaps BrandId , perhaps BrandName -- I prefer the former). 相反,你应该有一个Brands ,有一个主键(或者表BrandId ,也许BrandName -我更喜欢前者)。 Then tables that need information about a brand can directly reference this table. 然后,需要有关品牌信息的表格可以直接引用此表格。

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

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