--这就是级联表的创建方法,主表id更新时,从表a_id自动更新,主表id删除时,自动删除从表a_id对应主表id的记录--检查对象是否存在,如果存在,删除
if exists (select * from dbo.sysobjects where id = object_id(N'[从表]') and objectproperty(id, N'isusertable') = 1)
drop table [从表]if exists (select * from dbo.sysobjects where id = object_id(N'[主表]') and objectproperty(id, N'isusertable') = 1)
drop table [主表]
go--创建表环境
create table 主表(id int primary key ,name varchar(10))create table 从表(id int identity(1,1) primary key
,a_id int constraint FK_从表_主表 foreign key
references 主表(id) on update cascade on delete cascade
,name varchar(10))
go