高分求救,SQL触发器问题,在线等

来源:百度知道 编辑:UC知道 时间:2024/05/17 00:39:35
我在SQL中 建了两个数据库,分别为ceshi1 和 ceshi2

数据库ceshi1 有1个表news_list
字段是有: id 、 title 、 type(默认值为0) 、 content 、 time

数据库ceshi2 也有1个表shenghe
字段是有: id 、 title 、 content 、 time

现在我想做的是,(1)当我对ceshi1 数据库的news_list表的 一条记录做修改时,也就是对 type字段的值修改为“ 1 ”时,数据库就自动会把其他字段的内容,也就是 title content time 的值传给 ceshi2 数据库的表shenghe 的相应字段中。 (2)当type字段的值又修改为“ 1 ”时,数据库就自动会把ceshi2 数据库的表shenghe的相应字段删除,也就是title 字段的内容相同的记录删除

请问这功能该怎么实现?我只想通过数据库的方式实现(也只能通过这种方式),也就是利用 存储器,或触发器的方式实现?
第一个问题已经解决:
CREATE trigger up3
on dbo.news_list2
for Update
as
insert ceshi2.dbo.shenghe(title,content,[time] )
select title,content,[time] from inserted where type = '1'

请热心人事帮忙写下这第二个问题的程序!!!!!!!!!

请大家一定要看清楚要求,我想我已经说的很明白了,谁先先实现这功能,我就力马加分!!!!!!在线等
(2)当type字段的值又修改为“0”时

不好意思,写错了。。。。。。。

修改现有触发器就行了。

alter trigger up3
on dbo.news_list2
for Update
as
insert ceshi2.dbo.shenghe(title,content,[time] )
select title,content,[time] from inserted where type = '1'
delete ceshi2.dbo.shenghe where title =( select title from inserted where type = '0' )
and [content] = (select [content] from inserted where type = '0' )
and [time] = (select [time] from inserted where type = '0' )

因为你更新的时候没有保存id,所以在删除的时候就要逐个匹配才能确定删除。

(2)当type字段的值又修改为“ 1 ”时 ,应该是 “0”吧.

首先数据库ceshi2 也有1个表shenghe
字段是有: id 、 title 、 content 、 time
要加一个外键指向对应的news_list
id 、newslist_id、 title 、 content 、 time
也就是第一问题时要这样
insert ceshi2.dbo.shenghe(newslist_id,title,content,[time] )
select id,title,content,[time] from inserted where type = '1'

然后当type又修改回0时,就很好办了.
CREATE trigger delete1
on dbo.news_list2
for Update
as
delete fr