简体   繁体   English

Oracle SQL检查存在约束

[英]Oracle SQL Check Exists Constraint

I have two simple tables in Oracle SQL Developer (Train and Driver) each with a common attribute (driverid). 我在Oracle SQL Developer中有两个简单的表(Train和Driver),每个表都有一个公共属性(driverid)。 Essentially what i want to do is to not allow any updates on a particular driver tuple in the driver table if their driverid attribute exists in the train table. 本质上,我想要做的是,如果火车表中存在特定的驱动程序元组的driverid属性,则不允许对其进行任何更新。 I have tried to add the following constraint however it throws back an error of 'subquery not allowed here.' 我尝试添加以下约束,但是它抛出了“此处不允许子查询”的错误。

alter table driver add constraint drivcheck CHECK 
 (NOT EXISTS(select driverid from train))

I did a bit of digging around and the general feeling is that this condition should be checked with a trigger so I have tried to create a trigger to do the job but not having a lot of success. 我做了一些挖掘,总体感觉是应该使用触发器来检查这种情况,因此我尝试创建一个触发器来完成这项工作,但没有取得很大的成功。 The below trigger is what i have come up with already. 下面的触发器是我已经提出的。

create trigger drivcheck4
before update on driver
for each row
begin
declare
cursor dri is
select driverid from train where 'N' IN  
          (select availability 
           from driver 
           inner join train on    driver.driverid=train.driverid
           );
dri2 NUMBER;
begin
open dri;
loop
fetch dri into dri2;
exit when dri%NOTFOUND;
if check (exists (select * from dri2)) THEN
//Throw Error Section
else
//Allow update operation to take place
end if;
end loop;
close dri;
end;

I'm aware the trigger may be performing something different other than as described, but this is just the result of me experimenting. 我知道触发器可能执行的操作与上述操作有所不同,但这只是我进行实验的结果。 My initial description is what i am trying to achieve. 我最初的描述是我想要达到的目标。 If anyone has any thoughts, I would be very grateful! 如果有人有什么想法,我将不胜感激!

Andrew, 安德鲁,

Is there any error that you are facing? 您遇到任何错误吗? Something along the following lines should work. 遵循以下内容应该可以。 If you could post some sample data as well, it would be easy to test. 如果您还可以发布一些示例数据,则将很容易进行测试。

    create or replace trigger trg_bu_driver2
    before update on driver
    for each row
    declare
      l_cnt number;
    begin
    select count(*)
    into   l_cnt
    from   train
    where  driver_id = :new.driver_id;
    if (l_cnt > 0) then
       raise_application_error (-20001, 'new driver id exists in train table');
    end if;
    end trg_bu_driver2;
    /

testing: 测试:

    SQL> select * from driver;

     DRIVER_ID COLUMN2
    ---------- --------------------
            10 driver1

    SQL> select * from train;

     DRIVER_ID   TRAIN_ID COLUMN3
    ---------- ---------- --------------------
            20        100 train1
  • Updating to a driver id that does not exist in train table. 更新到火车表中不存在的驾驶员ID。

      1 update driver 2 set driver_id = 30 3* where driver_id = 10 SQL> / 1 row updated. No issues. SQL> rollback; Rollback complete. 

--updating to a driver id that exists in train table, raises an error. -更新到火车表中存在的驾驶员ID会引发错误。

    SQL> update driver
      2  set    driver_id = 20
      3  where  driver_id = 10
      4  ;
    update driver
           *
    ERROR at line 1:
    ORA-20001: new driver id exists in train table
    ORA-06512: at "DMART_ETL.TRG_BU_DRIVER2", line 9
    ORA-04088: error during execution of trigger 'DMART_ETL.TRG_BU_DRIVER2'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM