oracle新增外來鍵約束的方法

2021-08-27 05:13:35 字數 531 閱讀 8144

給表新增外來鍵約束分兩種情況,一種是剛建表的時候直接新增外來鍵約束,另一種則是表已經建立好了再新增外來鍵約束。

建表時新增外來鍵約束:

create table test_a(

id number not null primary key,

name varchar2(50)

);create table test_b(

id number not null primary key,

test_aid number references test_a(id), -- 設定外來鍵約束

name varchar2(50)

)

表已經建了再新增外來鍵約束:

alter table test_b  

add constraint fk_testid

foreign key (test_aid) references test_a(id);

--其中 fk_testid 是外來鍵名稱

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 ...

Oracle表中新增外來鍵約束

新增主鍵約束 alter table ga airline add constraint pk airline id primary key airline id 有三種形式的外來鍵約束 1 普通外來鍵約束 如果存在子表引用父表主鍵,則無法刪除父表記錄 2 級聯外來鍵約束 可刪除存在引用的父表記錄,...

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...