怎样用SQL给生日添加约束

[复制链接]
查看11 | 回复0 | 2007-12-29 13:57:16 | 显示全部楼层 |阅读模式
--创建表的示例
--检查对象是否存在,如果存在,删除
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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行