读书人

用什么步骤能够实现触发器的操作

发布时间: 2012-09-29 10:30:01 作者: rapoo

用什么方法能够实现触发器的操作
详细点说就是不允许使用触发器,有什么办法实现触发器的效果,即删除类别中的记录评论表、新闻表相关的记录都删除
表如下:
--类别表
create table category
(
id int identity(1,1) primary key,
caname varchar(50)
)
--新闻表
create table news
(
id int identity(1,1) primary key,
...
caid int reference category (id)
)
--评论表
carete table comments
(
id int identity(1,1) primary key,
...
newsId int reference news (id)
)

[解决办法]

SQL code
create table a   (id varchar(20) primary key,password varchar(20) not null)  create table b   (id int identity(1,1) primary key, name varchar(50) not null, userId varchar(20), foreign key (userId) references a(id) on delete cascade )  -- 表B创建了外码userId 对应A的主码ID,声明了级联删除   测试数据:   insert a values ('11','aaa')   insert a values('23','aaa')   insert b values('da','11')   insert b values('das','11')   insert b values('ww','23')   --删除A表内id为'11'的数据,发现B表内userId 为“11”也被数据库自动删除了,这就是级联删除   delete a where id='11'--drop table a--drop table b 

读书人网 >SQL Server

热点推荐