- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我运行底部看到的 SQL 时,为什么返回:
Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table 'pricedex.table_a' that match the referencing column list in the foreign key 'FK_delete_from_parent'.Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
代码:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE table_a
(
[column_1] [int] NULL,
[column_2] [int] NULL
)
CREATE TABLE table_b
(
[column_1] [int] NULL,
[column_2] [int] NULL
)
GO
CREATE NONCLUSTERED INDEX IX_app ON table_a (column_1, column_2)
GO
CREATE NONCLUSTERED INDEX IX_app ON table_b (column_1, column_2)
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE table_b WITH CHECK
ADD CONSTRAINT FK_delete_from_parent
FOREIGN KEY (column_1, column_2) REFERENCES table_a (column_1, column_2)
ON DELETE CASCADE
GO
最佳答案
您需要将PRIMARY KEY
添加到table_a
并将PK 列更改为NOT NULL
:
CREATE TABLE table_a (
[column_1] [int] NOT NULL,
[column_2] [int] NOT NULL,
PRIMARY KEY(column_1, column_2) -- compound primary key
);
CREATE TABLE table_b (
[column_pk] [int] NOT NULL PRIMARY KEY, -- you probably want different pk
[column_1] [int] NULL,
[column_2] [int] NULL
);
-- adding foreign key constraint
ALTER TABLE table_b WITH CHECK
ADD CONSTRAINT FK_delete_from_parent FOREIGN KEY (column_1, column_2)
REFERENCES table_a (column_1, column_2) ON DELETE CASCADE;
编辑:
Create Foreign Key Relationships
:
A foreign key constraint does not have to be linked only to a primary key constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.
CREATE TABLE table_a (
[column_1] [int] NOT NULL, -- with UNIQUE column can be nullable
[column_2] [int] NOT NULL,
UNIQUE(column_1, column_2)
-- anyway this table should have PK
);
请注意,如果列可以为空并且您有 NULL
值,ON DELETE CASCADE
将不会从相应表中删除记录。
关于sql - 如何使用复合键在 SQL Server 中实现参照完整性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34024777/
使用此类和映射: Public class Something { public int Id; public IList Similarthings { get; set; } }
我在Rails 3.2.2中,并且有一个Variant类: class Variant v2 v2.twin_variant => v1 这可能吗?我应该如何建立协会? 最佳答案 给定以下模型 cl
我计划建立一个新的SNS web应用程序,具有以下/跟随功能。我正在考虑使用Ruby on Rails+MySQL,但是我发现如果我使用它们,就需要在一些常用的特性上使用连接查询,比如显示您正在跟踪的
我正在尝试在 SQLAlchemy 中创建一个类似于多对多映射的社交网络。也就是说,我有一个 Character 类和一个 character_relations 交叉表来链接 Character 到
我是一名优秀的程序员,十分优秀!