级联删除
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)