postgresql外來鍵on delete cascade
阿新 • • 發佈:2018-12-24
級聯刪除
A表中欄位依賴於B表中對應欄位,
如果刪除B表中的記錄,則會級聯刪除A表中對應記錄
create table test(id serial primary key, name varchar(10));
create table test01(id serial primary key, tid int, constraint fk_test01_tid foreign key(tid) references test(id)
on delete cascade);
imos=# insert into test(name) values('a'),('b'),('c'); INSERT 0 3 imos=# insert into test01(tid) values(1),(2),(3); INSERT 0 3 imos=# imos=# select * from test; id | name ----+------ 1 | a 2 | b 3 | c (3 rows) imos=# select * from test01; id | tid ----+----- 1 | 1 2 | 2 3 | 3 imos=# delete from test where id=1; DELETE 1 imos=# select * from test01; id | tid ----+----- 2 | 2 3 | 3 (2 rows) imos=# select * from test; id | name ----+------ 2 | b 3 | c (2 rows) imos=# delete from test01 where id=2; DELETE 1 imos=# select * from test; id | name ----+------ 2 | b 3 | c (2 rows) imos=# select * from test01; id | tid ----+----- 3 | 3 (1 row)