简体   繁体   English

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

[英]How would I make a trigger that will check a value to make sure that it is the correct value or else it would throw up an error?

 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;​

What I want to do is make it so that if an inspector ID is entered for an officer that doesn't match up to an officer that has the rank of inspector then it will throw an error that states they aren't an inspector & will prompt them to enter a valid ID. 我要做的是,如果输入的检查人员ID与具有检查人员等级的人员不匹配,则会抛出错误,指出他们不是检查人员,并且会提示他们输入有效的ID。

no trigger required 无需触发

What you are describing is a multi-foreign key constraint from Officer(Inspector_id, Inspector_rank) to Officer(Staff_id, rank) 您所描述的是从Officer(Inspector_id,Inspector_rank)到Officer(Staff_id,rank)的多外键约束

you need to add the inspector_rank field to officer table 您需要将inspector_rank字段添加到高级人员表

then you need to link both id and rank to officer together so they are both the same record 那么您需要同时将id和rank链接到高级人员以便他们都是同一条记录

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

then you need a simple value constraint of inspector_rank CHECK inspector_rank = INSEPECTOR 那么你需要一个简单的值约束inspector_rank CHECK inspector_rank = INSEPECTOR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修改触发器并确保触发器正常运行以增加 OrderTotal 值 - How can I modify the trigger and make sure that the trigger runs properly to increase the OrderTotal value 如何确保正在返回触发器生成的值? - how to make sure that trigger generated value is being returned? 我将如何使其成为UDF - How would I make this a UDF 我将如何添加检查约束以确保日期不在未来 - How would I add a check constraint for making sure a date is not in the future 如何让触发器返回一个值? - How can I make the trigger return a value? 我将如何加入此统计信息? - How would I make this join on this statistic? 我将如何在Microsoft Access中进行此查询 - how would I make this query in microsoft access 如何使此查询运行得更快? - How would I make this query run faster? 当字段值出现在 SQLite 中时,我将如何确定它的出现率? - How would I determine the rate that a field value shows up, when it shows up at all in SQLite? 我将如何做到(如果Sector [1] ==“ l”和BRCTerritories [1] ==“。” :)调出表中的所有结果? - How would I make it so (if Sector[1]==“l” and BRCTerritories[1]==“.”:) brings up all the results in the table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM