Ian*_*oyd 6 sql-server table-relationships sql-server-2008-r2 relationships
我有一个"主桌",称之为Customers:
CREATE TABLE Customers (
CustomerID int PRIMARY KEY NOT NULL,
FirstName nvarchar(50),
LastName nvarchar(50)
)
Run Code Online (Sandbox Code Playgroud)

我有一个"卫星表",称之为Customer_IllegallyObtainedInformation:
CREATE TABLE Customer_IllegallyObtainedInformation (
CustomerID int PRIMARY KEY NOT NULL,
CellPhonePin int,
SexualOrientation varchar(20),
EmailPassword varchar(50)
)
Run Code Online (Sandbox Code Playgroud)

现在,我想要的是从Illegal表回到主Customers表的外键约束:

换一种说法:
Customer 没有一个Illegal条目Illegal没有进入Customer我的本能是在SQL Server数据库图中拖动
Illegal表TO的Customers表指示SQL Server Customers_IllegallyObtainedInformation是关系中的"子".相反,SQL Server中发生的事情使它成为一对一的关系:

这意味着如果您尝试插入a Customer,它将失败,因为没有现有Illegal信息.
如何在SQL Server中创建"父子"或"一对一可选"关系?
注意:不要将示例与问题混淆.我可以在Illegal表格中创建一个牺牲的主要代理键:

但这不是我的问题.
似乎设计师正在相反的方向创建外键.
只需自己编写代码:
CREATE TABLE Customers (
CustomerID int PRIMARY KEY NOT NULL,
FirstName nvarchar(50),
LastName nvarchar(50)
)
CREATE TABLE Customer_IllegallyObtainedInformation (
CustomerID int PRIMARY KEY NOT NULL,
CellPhonePin int,
SexualOrientation varchar(20),
EmailPassword varchar(50),
constraint fk_Customers foreign key (CustomerId) references dbo.Customers
)
-- succeeds:
insert into dbo.Customers
values(1, 'One', 'One'), (2, 'Two', 'Two')
--fails:
insert into dbo.Customer_IllegallyObtainedInformation
values(3, 1, '', '');
--succeeds:
insert into dbo.Customer_IllegallyObtainedInformation
values(1, 1, '', '');
Run Code Online (Sandbox Code Playgroud)
PRIMARY KEY可以参与出站FOREIGN KEY关系.
CREATE TABLE Customer_IllegallyObtainedInformation (
CustomerID int PRIMARY KEY NOT NULL,
CellPhonePin int,
SexualOrientation varchar(20),
EmailPassword varchar(50)
)
ALTER TABLE Customer_IllegallyObtainedInformation ADD FOREIGN KEY (CustomerId)
REFERENCES Customer(CustomerId)
Run Code Online (Sandbox Code Playgroud)
其他人已经指出如何通过使用 SQL 脚本设置关系来实现您想要的目的。我想我只想补充一下设计师正在做的事情......
基本上你拖错了方向。
外键本身始终是一对多的。这是一种通知 DBMS 您有一个表(子表)的方式,您希望其中的列(或列的组合)始终与另一个表中的键相对应。有了这些信息,DBMS 就可以承担起确保子表中的每一行实际上满足此要求的责任。
By making the column a key also in the child table the relationship can be made de facto One-To-One, but from the DBMS perspective this is not really a property of the relationship. Rather it is just one more restriction on the data that can be inserted into the child table.
When creating a relationship in the designer, it seems someone decided that the primary key should be dragged into the child table. So when you drag from Customers_IllegallyObtainedInformation to Customers, the designer figures that the Customers_IllegallyObtainedInformation is the table containing the primary key.
But wait, why did it work with the second sample where you had introduced a surrogate key? Probably because the people making the designer decided to make it smart. In that case you are dragging a column that is not a key in the table. That cannot form the primary key in the relation so the designer checks if the relation can be formed in the opposite direction. And since it can, thats what it offers...