Oracle 外來鍵約束

2021-09-01 00:04:28 字數 795 閱讀 5975

新增主鍵約束:

alter table ga_airline add constraint pk_airline_id primary key(airline_id);

有三種形式的外來鍵約束:

1、普通外來鍵約束(如果存在子表引用父表主鍵,則無法刪除父表記錄)

alter table t_invoice_detail add constraint fk_invoice_id foreign key(invoice_id ) references t_invoice(id);

2、級聯外來鍵約束(可刪除存在引用的父表記錄,而且同時把所有有引用的子表記錄也刪除)

alter table t_invoice_detail add constraint fk_invoice_id foreign key(invoice_id ) references t_invoice(id) on delete cascade;

3、置空外來鍵約束(可刪除存在引用的父表記錄,同時將子表中引用該父表主鍵的外來鍵字段自動設為null,但該字段應允許空值)

alter table t_invoice_detail add constraint fk_invoice_id foreign key(invoice_id ) references t_invoice(id) on delete set null;

注:父表t_invoice 主鍵 id

子表t_invoice_detail 外來鍵字段 invoice_id

oracle外來鍵約束

新建父表 sql create table teacher 2 3 id number primary key,4 name varchar2 10 5 table created.新建子表 sql 1 create table student 2 3 id number primary key,4...

Oracle 外來鍵約束

下面的語句建立department 20表,並定義和啟用department id列上的外來鍵,該外來鍵引用departments表的department id列上的主鍵 create table dept 20 employee id number 4 last name varchar2 10 ...

oracle新增外來鍵約束

alter table gjjy.jy dic crop drop constraint fk zmlb alter table gjjy.jy dic crop drop constraint fk zwlb alter table gjjy.jy dic crop add constraint ...