Oracle 外鍵約束子表、父表,oracle約束

來源:互聯網
上載者:User

Oracle 外鍵約束子表、父表,oracle約束

CREATE TABLE employees(
    employee_id      NUMBER(6),
    last_name        VARCHAR2(25) NOT NULL,
    email            VARCHAR2(25),
    salary           NUMBER(8,2),
    commission_pct   NUMBER(2,2),
    hire_date        DATE NOT NULL,
...
    department_id    NUMBER(4),
    CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
      REFERENCES departments(department_id),
    CONSTRAINT emp_email_uk UNIQUE(email));

 

FOREIGN KEY:在表級指定子表中的列
REFERENCES:標識在父表中的列
ON DELETE CASCADE:當父表中的列被刪除時,子表中相對應的列也被刪除
ON DELETE SET NULL:子表中相應的列置空


Oracle 外鍵約束問題

參考一下:
ALTER TABLE titles
ADD
CONSTRAINT FK_pub_id FOREIGN KEY (pub_id) REFERENCES publishers(pub_id)

CONSTRAINT FK_pub_id FOREIGN KEY (pub_id)
--pub_id子表中的欄位
REFERENCES publishers(pub_id)
--pub_id父表中的欄位

一般情況下取一樣的名。也可以不一樣
 
在 oracle中怎刪除有主外鍵關係的表

先刪子表後刪父表,如果兩張表相互有主外鍵約束,drop table tablename cascade constraints即可
 

相關文章

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.