zjcxc(邹建)和各位朋友请再进!!,急急

twism 2004-05-07 11:30:17
请教一个问题:
数据库:ggxx,表yd的ID列与bm表的ID列应满足参照完整性规则:向yd表添加一个记录时,该记录的ID值在表bm中应存在.通过一触发器实现
(1)向yd表插入或修改一条记录时,通过触发器检查记录的ID值在bm表中是否存在,若不存在,则取消插入或修改操作.

如何建立这个触角器?急,帮帮忙
两表情况如下:
yb表:
enployee_id char(6),
name char(10),
address char(20) 允许空,
id char(3)

bm表:
id char(3),
department_name char(10)

我已经按照你所说的创建了触发器了,但我不能在yb表中插入数据.在bm表中能插入数据,
在yb表插入数据时,yb.id在bm中也存在,还是不能插入数据.
删除了这个触发器后就能插入数据.

use yggl
go
if exists (select name from sysobjects
where name='tr_process' and type='tr')
drop trigger tr_process
go
create trigger tr_process on jbqk
for insert,update
as
if exists(select 1 from bmxx a where not exists(select 1 from inserted where department_id=a.department_id))
rollback tran

还有想问一下,rollback tran是什么意思?
...全文
38 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tongtianming 2004-05-08
  • 打赏
  • 举报
回复
rollback tran
如果事务没有成功完成则回滚为初始状态
你插入的数据可能有问题,看看是不是有重复主键之类的毛病
chenxdb 2004-05-08
  • 打赏
  • 举报
回复
if object_id('tr_process') is not null drop trigger tr_process
go
create trigger tr_process on jbqk for insert,update as
declare @id int
select top 1 @id=id inserted where not exists(select * from bm where id=a.id)
if @id is not null begin
raiserror('bm中没有找到id=%d的记录。',16,1,@id) -- 必须有让客户端捕获这个error。
rollback tran
end
go
zjcxc 元老 2004-05-08
  • 打赏
  • 举报
回复
--改触发器(原来的错了):

if exists (select name from sysobjects
where name='tr_process' and type='tr')
drop trigger tr_process
go
create trigger tr_process on jbqk
for insert,update
as
if exists(select 1 from inserted a where not exists(select 1 from bm where id=a.id))
rollback tran

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧