--创建表的示例
--检查对象是否存在,如果存在,删除
if exists (select * from dbo.sysobjects where id = object_id(n[主表]) and objectproperty(id, nisusertable) = 1)
drop table [主表]
go
if exists (select * from dbo.sysobjects where id = object_id(n[从表]) and objectproperty(id, nisusertable) = 1)
drop table [从表]
go
--创建表环境
create table 主表(id varchar(40) not null constraint pk_主表 primary key
,createdate datetime
)
create table 从表(id varchar(40) not null
,indexid varchar(40) not null
,value varchar(500)
)
alter table 从表 add constraint pk_从表 primary key clustered (id,indexid)
--添加级联更新
alter table dbo.从表 add constraint
fk_从表_主表 foreign key
(
id
) references dbo.主表
(
id --主表id
) on update cascade
on delete cascade 发表者:zjcxc--创建表的示例2
--检查对象是否存在,如果存在,删除
if exists (select * from dbo.sysobjects where id = object_id(n[主表]) and objectproperty(id, nisusertable) = 1)
drop table [主表]
go
if exists (select * from dbo.sysobjects where id = object_id(n[从表]) and objectproperty(id, nisusertable) = 1)
drop table [从表]
go
--创建表环境
create table 主表(id varchar(40) not null constraint pk_主表 primary key
,createdate datetime
)
create table 从表(id varchar(40) not null
,indexid varchar(40) not null
,value varchar(500)
,constraint pk_从表 primary key clustered (id,indexid)
,constraint fk_从表_主表 foreign key(id) references dbo.主表(id)
on update cascade --级联更新
on delete cascade --级联删除
) 发表者:zjcxc--就这个嘛.
--添加级联更新
alter table dbo.从表 add constraint
fk_从表_主表 foreign key
(
id
) references dbo.主表
(
id --主表id
) on update cascade
on delete cascade |