我如何制作一个触发器来检查一个值以确保它是正确的值,否则会引发错误?

时间:2015-01-07 20:10:04

标签: sql oracle

 DROP TABLE Officer cascade constraints;
 CREATE TABLE Officer(
 StaffID        Number(6)  PRIMARY KEY,
 FirstName    VARCHAR2(16),
 MiddleName    VARCHAR2(16),
 Surname    VARCHAR2(16),
 Rank    VARCHAR2(16),
 Department    VARCHAR2(16),
 Place_Of_Work   VARCHAR2(16),
 Hire_Date   VARCHAR2(16),
 Inspector_ID    Number(6));

 alter table Officer
  add constraint Officer_Inspector_ID_FK
  foreign key ( Inspector_ID )
  references Officer ( StaffID )
  on delete set null
  ;

 create sequence Officer_seq start with 100 increment by 1 nomaxvalue; 
 create trigger Officer_trigger
 before insert on Officer
 for each row
 begin
 select Officer_seq.nextval into :new.StaffID from dual;
   end;​

我想要做的是,如果为一名不具备检查员等级的高级职员匹配的高级职员输入检查员ID,则会抛出错误,表明他们不是&# 39;检查员&将提示他们输入有效的身份证。

1 个答案:

答案 0 :(得分:1)

无需触发器

你所描述的是从军官(Inspector_id,Inspector_rank)到军官(Staff_id,rank)的多外键约束

您需要将inspector_rank字段添加到军官表

然后你需要将id和rank连接到官员在一起所以他们都是同一个记录

foreign key (Inspector_id, Inspector_rank) references Officer(Staff_id, rank)

那么你需要一个简单的inspector_rank值约束CHECK inspector_rank = INSEPECTOR