oracle級聯操作,oracle級

來源:互聯網
上載者:User

oracle級聯操作,oracle級
在添加foreing key約束時,還可以指定級聯操作的類型,主要用於確定當刪除(on delete) 附表中的一條記錄時,如何處理子表中的外鍵欄位,有如下三種參考型別。

cascade
此關鍵字用於表示當刪除主表中被引用列的資料時,將子表中相應應用列的值置為null值,這種情況要求子表中的引用列支援null值
set null
此關鍵字用於當刪除主表中被引用列的資料時,將子表中相應引用列的值設定為null值。這種情況要求子表中的應用列支援null值。
not action
此關鍵字用於表示當刪除主表中被引用的資料時,如果子表的引用列種包含該值,則禁止操作執行。預設為該選項。

具體的操作如下:
SQL> create table stuclass(
  2  cid number(4) primary key,
  3  cname varchar2(40),
  4  num number(4)
  5  );
表已建立。
SQL> create table stu(
  2  sid number(4),
  3  scode varchar(10),
  4  sname varchar(20),
  5  cid number(4) references stuclass(cid) on delete set null
  6  );
表已建立。
SQL> insert into stuclass values(1,'oracle',35);
已建立 1 行。
SQL> insert into stu values(1,'s001','aaa',1);
已建立 1 行。
SQL> delete stuclass where cid=1;
已刪除 1 行。
SQL> select * from stuclass;
未選定行
SQL> select * from stu;
       SID SCODE      SNAME                       CID
---------- ---------- -------------------- ----------
         1 s001       aaa
SQL>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.