SQL Server 2008:如何将FK设置为PK?
我正在整理自己的数据库,从我见过的示例来看,外键也可以设置为主键。
我正在创建我的桌子,以便我所有的 FK 也都是 PK。这是错误的吗?什么时候FK应该成为PK?一定要PK吗?
主键在它们自己的表中有意义...作为 Id 和 Identity。但是当使用的Id是另一个表时,它也必须是PK吗?
I'm putting together my own database and from examples I've seen, Foriegn Key can also be set as Primary Keys.
I was creating my Tables so that all of my FK were also PK. Is this wrong? When should a FK be a PK? Does it have to be a PK?
Primary Key's make sense in their own table... as the Id and Identity. But when using the Id is another table, does it have to be a PK as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您尝试创建
1 到 1
或1 到 0/1
映射时,外键只能作为主键。示例:
我有一个“人员”表、一个“雇员”表和一个“承包商”表。所有员工都是人,所有承包商都是人,每个人要么是员工,要么是承包商
本质上你最终会得到这样的结果。
为了响应您的人员有多个地址,您应该创建一个关联表。这是一个图表。
正如您所看到的,现在每个人都可以有多个地址,并且由于每个员工都是一个人,因此每个员工可以有多个地址地址。对于承包商来说也是如此。
编辑:这是来自 SQL Server 的更改脚本
A Foreign Key should only be the Primary Key when your trying to create a
1 to 1
or1 to zero/1
mapping.Example:
I have a Person table, an Employee table, and a Contractor table. All Employees are people, all Contractors are people and every Person is either an employee or a Contractor
Essentially you would end up with something like this.
In response to your people have multiple addresses you should create an association table. Here is a diagram.
As you can see now every person can have many addresses and since each Employee is a person then every Employee can have many addresses. This is the same for Contractor as well.
Edited: Here is the Change Script from SQL Server
只有一种情况需要 FK 兼 PK。
当该表表示 PK 表中事物的子类或子集时,例如,SalariedEmployees 表具有与Employees 表的FK...
There is only one scenarios that would require a FK to also be a PK.
When the table represents a subclass, or subset, of the things in the PK table, for example, Eg, SalariedEmployees table which has a FK to the Employees table...
FK是指向另一个表的PK的字段。就是这样。
链接到自身的表可以包含指向其自身 PK 的 FK。
PK 兼 FK 只能发生在 1 对 1 关系的子表中。
A FK is a field pointing to the PK of another table. That's it.
A table linked to itself could contain a FK pointing to its own PK.
A PK that is also a FK can only happen in the child table of a 1 to 1 relationship.
仅当两个表具有一对一关系并且添加第二个表是因为第一个表太宽且大多数查询中并不总是需要这些项目时,FK 才应该是 PK。
如果你有一对多关系或多对多关系,它根本不起作用。
FK 通常不是 PK。如果我有一个人员表和一个相关的地址表,如果我将 PK 和 FK 设置为同一对象,那么我只能存储一个地址,但大多数地址表允许同一个人或组织存储多个地址。在这种情况下,您将使用 AddressID 作为 PK,并使用 person_id 作为 person 表的 FK。这是最常见的 PK/FK 场景。
An FK should only also be a PK if the two tables have a one-to-one relationship and the second table was added because the first was too wide and these were items not always needed in most queries.
It will not work at all if you have a one-to-many relationship or a many-to-many relationship.
FKs are much more often not also the PK. If I havea person table and a related address table, If I make the PK and the FK the same thing, then I can only store one address, but most address tables allow for mulitple addresses for the same person or organization. IN that case you would have and AddressID as the PK and a person_id as the FK to the person table. This is the most common PK/FK scenario.
给定列既是 PK 又是 FK 的一种情况是 gen-spec 设计模式的关系模型。在其他响应之一中,“Employees”是“Persons”的特化。employees 表中的 PK 引用 Persons 表中的 PK。因此,专用表中的 PK 也是一个 FK。
这允许创建一个视图将员工和个人结合起来,在一个视图中提供有关员工的所有数据,无论该数据是员工特有的(如“雇用日期”)还是所有人共有的,无论他们是否是员工(如“出生日期”) )
使每个 PK 也成为 FK 并不是一个好的做法。如果逻辑模型不合逻辑,那么您就会遇到麻烦。
One situation where a given column is both a PK and an FK is the relational model for the gen-spec design pattern. In one of the other responses "Employees" is a specialization of Persons". The PK in the employees tables references the PK in the Persons table. So the PK in the specialized table is also an FK.
This allows the creation of a view that joins employees and persons to provide in a single view all the data about employees, whether that data is peculiar to employees (like "Hire Date") or is common to all persons, whether or not they are employees (like "Date of Birth").
It is not good practice to make every PK also be an FK. The FKs should reflect the logical structure of the data. If the logical model is illogical, you're headed for trouble.