if object_id('comic') is not null
drop table comic
go
if object_id('title') is not null
drop table title
gocreate table title(titleId int primary key,titleName varchar(50))
insert into title select 1,'test'create table comic(titleId int not null,comicId int)
insert into comic select 1,10
insert into comic select 1,11alter table comic add constraint comic_conname
foreign key(titleId) references title(titleId) on update cascade on delete cascadeupdate title set titleid=2 where titleid=1select * from comicdelete title where titleid=1外键约束
drop table comic
go
if object_id('title') is not null
drop table title
gocreate table title(titleId int primary key,titleName varchar(50))
insert into title select 1,'test'create table comic(titleId int not null,comicId int)
insert into comic select 1,10
insert into comic select 1,11alter table comic add constraint comic_conname
foreign key(titleId) references title(titleId) on update cascade on delete cascadeupdate title set titleid=2 where titleid=1select * from comicdelete title where titleid=1外键约束
GO
INSERT b SELECT 'a'
UNION ALL SELECT 'b'
GOCREATE TABLE c(objname VARCHAR(10) PRIMARY KEY)
GO
INSERT c SELECT 'e'
GOCREATE FUNCTION f
(@objname VARCHAR(10))
RETURNS BIT
AS
BEGIN
DECLARE @b BIT
IF EXISTS(SELECT 1 FROM
(SELECT objname FROM b
UNION
SELECT objname FROM c) x
WHERE objname=@objname
)
SET @b=1
ELSE
SET @b=0
RETURN @b
END
GO
CREATE TABLE a(x INT,objname VARCHAR(10) CHECK(dbo.f(objname)=1))
GO
INSERT a SELECT 1,'a'
/*
插入成功(所影响的行数为 1 行)
*/
INSERT a SELECT 1,'v'
/*
服务器: 消息 547,级别 16,状态 1,行 1
INSERT 语句与 COLUMN CHECK 约束 'CK__a__objname__6A86975B' 冲突。该冲突发生于数据库 'test',表 'a', column 'objname'。
语句已终止。
*/DROP TABLE a,b,c
DROP FUNCTION f
GO