六、FOREIGN KEY ---- 外键约束

外键可以是单个字段,也可以是多个字段。所谓的外键约束就是引用字段必须在被引用字段中存在,除非引用字段部分为NULL或全部为NULL(由MATCH TYPE决定),否则INSERT或UPDATE时将返回失败,且被引用字段必须有唯一约束或是主键。

外键约束语法相对较复杂一点,创建外键的语法如下:

ALTER TABLE tbl_foreign CONSTRAINT fk_constraint FOREIGN KEY(col1,col2) REFERENCES tbl_foreign_refd(refd_col1,refd_col2) MATCH [SIMPLE|FULL] ON DELETE [CASCADE|NO ACTION] ON UPDATE [CASCADE|NO ACTION];

其中:

tbl_foreign             : 引用表

fk_constraint           : 外键约束名称

(col1,col2)             : 引用表中引用字段

tbl_foreign_refd        : 被引用表

(refd_col1,refd_col2)   : 被引用表中被引用字段,和(col1,col2)对应

MATCH [SIMPLE|FULL]     : 外键匹配模式,如果引用字段全部不是NULL,则强匹配,否则根据匹配模式进行弱匹配。

--SIMPLE,默认值,只要引用字段中任一字段为NULL,则不要求与被引用字段强匹配;

--FULL,只有引用字段全部为NULL,才不要求与被引用字段强匹配。

ON DELETE [CASCADE | NO ACTION] : 默认NO ACTION

--CASCADE,删除被引用表数据级联删除引用表数据

--NO ACTION,删除被引用表数据必须先删除引用表数据,否则,如果引用表如果存在数据,直接删除被引用表数据返回失败。

ON UPDATE [CASCADE | NO ACTION] : 默认NO ACTION

--CASCADE,更新被引用表时级联更新引用表数据

--NO ACTION,更新被引用表时必须先删除引用表数据,否则,如果引用表存在数据,直接更新被引用表数据返回失败。

1.创建测试表(引用表tbl_foreign和被引用表tbl_foreign_refd)

create table tbl_foreign_refd(
a int not null,
b int not null,
c varchar
);
alter table tbl_foreign_refd add constraint pk_tbl_foreign_refd_a_b primary key(a,b); create table tbl_foreign(
a int,
b int,
c varchar
);
alter table tbl_foreign add constraint fk_tbl_foreign_a_b foreign key(a,b) references tbl_foreign_refd(a,b);

上表中完整外键其实如下,因为match,on delete,on update会自动使用默认值。

test=# alter table tbl_foreign add constraint fk_tbl_foreign_a_b foreign key(a,b) references tbl_foreign_refd(a,b) match simple on delete no action on update no action;

测试例1.match simple on delete no action on update no action

test=# insert into tbl_foreign_refd (a,b) values (1,1),(1,2),(1,3);
INSERT 0 3
test=# insert into tbl_foreign(a,b) values (1,1),(1,2);
INSERT 0 2 test=# insert into tbl_foreign(a,b) values (2,1);
ERROR: insert or update on table "tbl_foreign" violates foreign key constraint "fk_tbl_foreign_a_b"
DETAIL: Key (a, b)=(2, 1) is not present in table "tbl_foreign_refd". test=# insert into tbl_foreign(a) values (2);
INSERT 0 1
test=# insert into tbl_foreign(a) values (1);
INSERT 0 1
test=# select * from tbl_foreign;
a | b | c
---+------+------
1 | 1 | NULL
1 | 2 | NULL
2 | NULL | NULL
1 | NULL | NULL
(4 rows)
test=# delete from tbl_foreign_refd where a=1 and b=1;
ERROR: update or delete on table "tbl_foreign_refd" violates foreign key constraint "fk_tbl_foreign_a_b" on table "tbl_foreign"
DETAIL: Key (a, b)=(1, 1) is still referenced from table "tbl_foreign". test=# update tbl_foreign_refd set a=3 where a=1 and b=1;
ERROR: update or delete on table "tbl_foreign_refd" violates foreign key constraint "fk_tbl_foreign_a_b" on table "tbl_foreign"
DETAIL: Key (a, b)=(1, 1) is still referenced from table "tbl_foreign".

测试例2.match full on delete cascade on update cascade

删除外键约束,清空数据,重新增加外键

test=# alter table tbl_foreign drop constraint fk_tbl_foreign_a_b ;
ALTER TABLE
test=# delete from tbl_foreign;
DELETE 4
test=# alter table tbl_foreign add constraint fk_tbl_foreign_a_b foreign key(a,b) references tbl_foreign_refd(a,b) match full on delete cascade on update cascade;
ALTER TABLE
test=# insert into tbl_foreign(a,b) values (1,1),(1,2);
INSERT 0 2 test=# insert into tbl_foreign(a) values (2);
ERROR: insert or update on table "tbl_foreign" violates foreign key constraint "fk_tbl_foreign_a_b"
DETAIL: MATCH FULL does not allow mixing of null and nonnull key values. test=# insert into tbl_foreign(c) values (2);
INSERT 0 1
test=# select * from tbl_foreign;
a | b | c
------+------+------
1 | 1 | NULL
1 | 2 | NULL
NULL | NULL | 2
(3 rows) test=# delete from tbl_foreign_refd where a=1 and b=1;
DELETE 1
test=# update tbl_foreign_refd set a=2 where a=1 and b=2;
UPDATE 1
test=# select * from tbl_foreign;
a | b | c
------+------+------
NULL | NULL | 2
2 | 2 | NULL
(2 rows) test=# update tbl_foreign set a=3 where a=2;
ERROR: insert or update on table "tbl_foreign" violates foreign key constraint "fk_tbl_foreign_a_b"
DETAIL: Key (a, b)=(3, 2) is not present in table "tbl_foreign_refd".

2.删除外键约束

alter table tbl_foreign drop constraint fk_tbl_foreign_a_b ;

3.增加外键约束

和唯一键,主键一样,增加外键约束前首先要删除脏数据,对外键来说脏数据针对不同的match type来说是不一样的。

match simple : 引用字段全部是NOT NULL,且在被引用表中不存在的。

match full   : 引用字段部分是NULL和全部是NOT NULL且在被引用表中不存在的。

情况一:增加match simple类型外键

第一步:删除外键,清空表,写入测试数据

test=# alter table tbl_foreign drop constraint fk_tbl_foreign_a_b ;
ALTER TABLE
test=# delete from tbl_foreign;
DELETE 2
test=# insert into tbl_foreign(a,b) values (1,2),(2,2),(1,1);
INSERT 0 3
test=# insert into tbl_foreign(a) values (3),(4);
INSERT 0 2
test=# insert into tbl_foreign(c) values (5);
INSERT 0 1
test=# select * from tbl_foreign;
a | b | c
------+------+------
1 | 2 | NULL
2 | 2 | NULL
1 | 1 | NULL
3 | NULL | NULL
4 | NULL | NULL
NULL | NULL | 5
(6 rows) test=# select * from tbl_foreign_refd ;
a | b | c
---+---+------
1 | 3 | NULL
2 | 2 | NULL
(2 rows)

对于要增加的外键来说,(1,1,NULL),(1,2,NULL)是脏数据。

第二步:查询脏数据

test=# select * from tbl_foreign where not exists (select null from tbl_foreign_refd where tbl_foreign_refd.a=tbl_foreign.a and tbl_foreign_refd.b=tbl_foreign.b) and a is not null and b is not null;
a | b | c
---+---+------
1 | 1 | NULL
1 | 2 | NULL
(2 rows)

第三步:删除脏数据

将上面SQL语句中的select替换成delete即可。

test=# delete from tbl_foreign where not exists (select null from tbl_foreign_refd where tbl_foreign_refd.a=tbl_foreign.a and tbl_foreign_refd.b=tbl_foreign.b) and a is not null and b is not null;
DELETE 2

第四步:增加外键

test=# alter table tbl_foreign add constraint fk_tbl_foreign_a_b foreign key(a,b) references tbl_foreign_refd(a,b) match simple;
ALTER TABLE

情况二:增加match full类型外键

第一步:删除外键,清空表,写入测试数据

test=# alter table tbl_foreign drop constraint fk_tbl_foreign_a_b ;
ALTER TABLE
test=# delete from tbl_foreign;
DELETE 4
test=# insert into tbl_foreign(a,b) values (1,2),(2,2),(1,1);
INSERT 0 3
test=# insert into tbl_foreign(a) values (3),(4);
INSERT 0 2
test=# insert into tbl_foreign(c) values (5);
INSERT 0 1
test=# select * from tbl_foreign;
a | b | c
------+------+------
1 | 2 | NULL
2 | 2 | NULL
1 | 1 | NULL
3 | NULL | NULL
4 | NULL | NULL
NULL | NULL | 5
(6 rows) test=# select * from tbl_foreign_refd ;
a | b | c
---+---+------
1 | 3 | NULL
2 | 2 | NULL
(2 rows)

对于要增加的外键来说,(1,1,NULL),(1,2,NULL),(3,NULL,NULL),(4,NULL,NULL)是脏数据。

第二步:查询脏数据

test=# select * from tbl_foreign where not exists (select null from tbl_foreign_refd where tbl_foreign_refd.a=tbl_foreign.a and tbl_foreign_refd.b=tbl_foreign.b) and ((a is not null and b is not null) or (a is not null and b is null) or(a is null and b is not null));
a | b | c
---+------+------
1 | 1 | NULL
1 | 2 | NULL
3 | NULL | NULL
4 | NULL | NULL
(4 rows)

第三步:删除脏数据

将上面SQL语句的SELECT替换成DELETE即可。

test=# delete from tbl_foreign where not exists (select null from tbl_foreign_refd where tbl_foreign_refd.a=tbl_foreign.a and tbl_foreign_refd.b=tbl_foreign.b) and ((a is not null and b is not null) or (a is not null and b is null) or(a is null and b is not null));
DELETE 4

第四步:增加外键约束

test=# alter table tbl_foreign add constraint fk_tbl_foreign_a_b foreign key(a,b) references tbl_foreign_refd(a,b) match full;
ALTER TABLE

postgresql----数据库表约束----FOREIGN KEY的更多相关文章

  1. MySQL数据库之-foreign key 外键(一对多、多对多、一对一)、修改表、复制表

    摘要: 外键 一对多 外键 多对多 外键 一对一 --------------------------------------------------------------------------- ...

  2. 数据库六大约束用法:主键(primary key)、外键(foreign key)、非空(not null)、默认(default)、检查(check)、唯一(unique)

    1. 数据库有六大约束 主键(primary key) 外键(foreign key):被参照的键必须有唯一约束或是主键 非空(not null) 默认(default) 检查(check):orac ...

  3. 关于数据库主从表、主键PRIMARY KEY 外键约束 FOREIGN KEY 约束----NOT NULL,DEFAULT,CHECK

    如果由两个列共同组成主键,而且一个子表将主键作为可为空值的外键来继承,就可能得到错误的数据.可在一个外键列中插入有效的值,但在另一个外键列中插入空值.然后,可添加一个数据表检查约束,在可为空的外键中检 ...

  4. postgresql数据库primary key约束/not null约束/unique约束及default值的添加与删除、列的新增/删除/重命名/数据类型的更改

    如果在建表时没有加primary key约束.not null约束.unique约束.default值,而是创建完表之后在某个字段添加的话 1.primary key约束的添加与删除 给red_pac ...

  5. MySql数据库插入或更新报错:Cannot add or update a child row: a foreign key constraint fails

    具体报错信息: Cannot add or update a child row: a foreign key constraint fails (`xxx`.`AAA`, CONSTRAINT `t ...

  6. 数据库基本表创建 完整性约束 foreign Key

    理解以下几张表的内容,根据实际情况设计属性名.数据类型.及各种完整性约束(primary key.foreign key.not null.unique.check),用数据定义语言实现,然后设计实验 ...

  7. [MySQL数据库之表的约束条件:primary key、auto_increment、not null与default、unique、foreign key:表与表之间建立关联]

    [MySQL数据库之表的约束条件:primary key.auto_increment.not null与default.unique.foreign key:表与表之间建立关联] 表的约束条件 约束 ...

  8. 数据库中的參照完整性(Foreign Key)

    之前在项目中遇到了这样一个问题,我举得简单的样例来说明. 比方我们有两个表,一个表(department)存放的是部门的信息,比如部门id,部门名称等:还有一个表是员工表(staff),员工表里面肯定 ...

  9. Oracle数据库-primary key/foreign key和references关系

    主要介绍一下个人对主键(primary key).外键(foreign key).候选键(Candidate key).超键(super key).references的总结 概念: 主键:用户选择元 ...

随机推荐

  1. 数组累加兼eval性能测试

    <label> <input type="checkbox" />测试结果 </label> <script> document.g ...

  2. 关于Java获取文件路径的几种方法

    第一种:File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f);  ...

  3. ubuntu16.04 安装caffe以及python接口

    http://blog.csdn.net/qq_25073253/article/details/72571714http://blog.csdn.net/greed7480/article/deta ...

  4. 【转】WCF入门教程二[WCF应用的通信过程]

    一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...

  5. Event Listener's Adapter Classes

    摘自: http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html Refer to the WindowEventDemo, a ...

  6. C#控件方法及属性大全,望补充

    C#控件及常用设计整理 1.窗体 常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体. (2) WindowState属性: 用来获取或设置窗体的窗口状态 ...

  7. thinkphp模板中无法给自定义函数传多个参数

    1.模板的用法 {:function(param1,param2,param3...)} 2.实例 <td>{:getChannelInfo($adminId,$v['sale_id']) ...

  8. iOS: UUID and SSKeyChain

    需要加入SSKeyChain文件 传送门:SSKeyChain // // UniqueIDCreater.h // Housemart // // Created by Haozhen Li on ...

  9. keepalived双BACKUP加nopreempt失效、手动监控服务脚步。

    keepalived双BACKUP加nopreempt不起作用,两个机器同时拥有vip, 排查几天发现是防火墙问题,啃爹. 打开  vi /etc/sysconfig/iptables 插入一条:-A ...

  10. Redis生成Id主键的工具

    public class PrimaryKeyGenerator { private static readonly NedisClient client = new NedisClient(GetR ...