问个触发器的问题

花开了叫我 2017-08-11 01:20:35
SQL> select * from test order by id;

ID NAME TID
---------- ---- ----------
1 aa q
1 aa a
1 aa z
2 cc d
2 cc f
3 dd v

6 rows selected

这个表的主键是 id,tid 我现在想要建立一个触发器 限制 name和id一一对应
比如 我要插入 1 aa c 就可以 插入 1 bb c 就报错。

这个触发器该怎么写
提前谢谢
...全文
301 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qyz 2017-08-13
  • 打赏
  • 举报
回复
增加一个以id,name为主键的表test_check,然后在test 上增加一个外键 alter table test add constraint fk_test foreign key (id,name) references test_check(id,name);
imcmuc 2017-08-11
  • 打赏
  • 举报
回复
create or replace trigger tr_test
 BEFORE  insert on test 
for each row
declare
  ECNAME VARCHAR2(1000);
  e_invalid_id exception;

begin
  SELECT MAX(ENAME) INTO ECNAME FROM EMP WHERE ID= :NEW.ID;
  if (:new.ENAME != ECNAME)  then
     raise e_invalid_id;
  end if;

  exception
     when e_invalid_id then
     dbms_output.put_line ('ERR!');
end;
junes06 2017-08-11
  • 打赏
  • 举报
回复
oralce上 ,instead of 触发器是不能直接建立在表上的
花开了叫我 2017-08-11
  • 打赏
  • 举报
回复
引用 3 楼 junes06 的回复:


--存储过程就简单了,先把要插入的值用参数传进来,判断是否符合条件,符合条件的插入就可以了
create or replace procedure pr_test1 (v_id varchar2,v_name varchar2,v_tid varchar2)
as
  e_invalid_id exception;
begin
  if (v_id='1' and v_name='bb')  then      --写判断数据的条件,不符合就抛出错误信息
          raise e_invalid_id;
  else
   insert into test (id,name,tid) values (v_id,v_name,v_tid);  --符合条件就插入数据
   commit;
  end if;
  exception
     when e_invalid_id then
       dbms_output.put_line ('数据错误!,请检查数据!');
end;

--调用存储过程

begin
 pr_test1('1','aa','cc');
end;

好像还是要用触发器 数据是程序里插入的 改程序的话代价大了 还是得想办法在表的触发器上检查
junes06 2017-08-11
  • 打赏
  • 举报
回复


--存储过程就简单了,先把要插入的值用参数传进来,判断是否符合条件,符合条件的插入就可以了
create or replace procedure pr_test1 (v_id varchar2,v_name varchar2,v_tid varchar2)
as
  e_invalid_id exception;
begin
  if (v_id='1' and v_name='bb')  then      --写判断数据的条件,不符合就抛出错误信息
          raise e_invalid_id;
  else
   insert into test (id,name,tid) values (v_id,v_name,v_tid);  --符合条件就插入数据
   commit;
  end if;
  exception
     when e_invalid_id then
       dbms_output.put_line ('数据错误!,请检查数据!');
end;

--调用存储过程

begin
 pr_test1('1','aa','cc');
end;

花开了叫我 2017-08-11
  • 打赏
  • 举报
回复
引用 1 楼 junes06 的回复:
创建 instead of 必须要 建立在视图上,然后对这个视图进行数据新增 create view v_test as select * from test; create or replace trigger tr_test instead of insert on v_test for each row declare e_invalid_id exception; begin if (:new.id='1' and :new.name='bb') then --写判断数据的条件,不符合就抛出错误信息 raise e_invalid_id; else insert into test (id,name,tid) values (:new.id,:new.name,:new.TID); --符合条件就插入数据 end if; exception when e_invalid_id then dbms_output.put_line ('数据错误!,请检查数据!'); end; 建议楼主也可以用存储过程实现
新建视图的话需要改别的程序了 存储过程大概是什么样的呀 好像有个复合触发器 是怎么做呢 望赐教
junes06 2017-08-11
  • 打赏
  • 举报
回复
创建 instead of 必须要 建立在视图上,然后对这个视图进行数据新增 create view v_test as select * from test; create or replace trigger tr_test instead of insert on v_test for each row declare e_invalid_id exception; begin if (:new.id='1' and :new.name='bb') then --写判断数据的条件,不符合就抛出错误信息 raise e_invalid_id; else insert into test (id,name,tid) values (:new.id,:new.name,:new.TID); --符合条件就插入数据 end if; exception when e_invalid_id then dbms_output.put_line ('数据错误!,请检查数据!'); end; 建议楼主也可以用存储过程实现

17,078

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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