四、UNIQUE ---- 唯一约束

唯一键可以是单个字段,也可以是多个字段的组合,设置唯一约束后,INSERT或UPDATE时如果表中唯一键字段中已存在该数据,则拒绝该行数据的INSERT或UPDATE。但是数据库中NULL并不等于NULL,所以唯一键中如果没有NOT NULL约束,则可以在唯一键中INSERT或UPDATE任意多个NULL。

1.创建测试表

唯一约束为组合键(a,b),即a和b的组合必须是唯一的。

create table tbl_unique(
a int not null,
b int,
c varchar(10) not null default 'catch u',
constraint uk_tbl_unique_a_b unique(a,b)
);

向tbl_unique表中写入数据(1,,1,'test')

test=# insert into tbl_unique (a,b,c) values(1,1,'test');
INSERT 0 1

再次写入(a,b)组合(1,1)时,则会返回错误。

test=# insert into tbl_unique (a,b,c) values(1,1,'u see');
ERROR: duplicate key value violates unique constraint "uk_tbl_unique_a_b"
DETAIL: Key (a, b)=(1, 1) already exists.

那么唯一键中出现NULL呢?唯一键中可以写入任意多个NULL!

test=# insert into tbl_unique (a) values(2);
INSERT 0 1
test=# insert into tbl_unique (a) values(2);
INSERT 0 1
test=# insert into tbl_unique (a) values(2);
INSERT 0 1
test=# \pset null 'NULL'
Null display is "NULL".
test=# select * from tbl_unique ;
a | b | c
---+------+---------
1 | 1 | test
2 | NULL | catch u
2 | NULL | catch u
2 | NULL | catch u
(4 rows)

2.唯一键约束删除

test=# alter table tbl_unique drop constraint uk_tbl_unique_a_b ;
ALTER TABLE

3.唯一键约束增加

如果你想向表中增加唯一约束,必须要考虑表中已存在的数据可能存在重复数据。重复的数据有两种理解方式:

方式一:严格意义上的唯一,NULL不等于NULL,即(1,NULL)和(1,NULL)不是重复数据。

方式二:非严格意义上的唯一,NULL等于NULL,即(1,NULL)和(1,NULL)是重复数据。

所以向表中增加唯一约束必须要删除这些重复数据,或者将重复数据删除到唯一。

情况一:删除严格意义上的重复

第一步:清空测试表,写入一些测试数据。

test=# delete from tbl_unique ;
DELETE 4
test=# insert into tbl_unique (a,b) values (1,1),(1,1),(1,1);
INSERT 0 3
test=# insert into tbl_unique (a) values (2),(2),(2);
INSERT 0 3
test=# select * from tbl_unique ;
a | b | c
---+------+---------
1 | 1 | catch u
1 | 1 | catch u
1 | 1 | catch u
2 | NULL | catch u
2 | NULL | catch u
2 | NULL | catch u
(6 rows)

从结果中看,严格意义上的唯一有1个(1,1,'catch u')和3个(2,NULL,'catch u'),删除重复数据即是要删除所有的(1,1,'catch u')。这种情况下只要使用下面的语句删除即可。

delete from tbl_unique where a= 1 and b = 1;

但是如果表中存在成千上万个这种重复数据,这么一条一条的删除岂不显得低级?!

第二步:查询(a,b)存在重复的数据

test=# select a,b from tbl_unique where a is not null and b is not null group by a,b having count(*) > 1;
a | b
---+---
1 | 1
(1 row)

第三步:查询所有(a,b)重复的数据

test=# select * from tbl_unique where exists(select null from (select a,b from tbl_unique where a is not null and b is not null group by a,b having count(*) > 1)tbl_temp where tbl_temp.a=tbl_unique.a and tbl_temp.b=tbl_unique.b) ;
a | b | c
---+---+---------
1 | 1 | catch u
1 | 1 | catch u
1 | 1 | catch u
(3 rows)

第四步:删除所有(a,b)重复的数据

把上面的语句中select *替换成 delete就可以了。

test=# delete from tbl_unique where exists(select null from (select a,b from tbl_unique where a is not null and b is not null group by a,b having count(*) > 1)tbl_temp where tbl_temp.a=tbl_unique.a and tbl_temp.b=tbl_unique.b) ;
DELETE 3
test=# select * from tbl_unique ;
a | b | c
---+------+---------
2 | NULL | catch u
2 | NULL | catch u
2 | NULL | catch u
(3 rows)

第五步:增加唯一约束

test=# alter table tbl_unique add constraint uk_tbl_unique_a_b unique (a,b);
ALTER TABLE

情况二:删除非严格意义重复数据

第一步:删除约束,清空数据,写入测试数据

test=# alter table tbl_unique drop constraint uk_tbl_unique_a_b ;
ALTER TABLE
test=# delete from tbl_unique ;
DELETE 3
test=# insert into tbl_unique (a,b) values (1,1),(1,1),(1,1);
INSERT 0 3
test=# insert into tbl_unique (a) values (2),(2),(2);
INSERT 0 3
test=# select * from tbl_unique ;
a | b | c
---+------+---------
1 | 1 | catch u
1 | 1 | catch u
1 | 1 | catch u
2 | NULL | catch u
2 | NULL | catch u
2 | NULL | catch u
(6 rows)

非严格意义上该表中的数据全部是重复数据,和情况一比只需要把NOT NULL过滤条件去掉即可。

第二步:查询(a,b)重复数据

test=# select a,b from tbl_unique group by a,b having count(*) > 1;
a | b
---+------
2 | NULL
1 | 1
(2 rows)

第三步:查询所有(a,b)重复数据

test=# select * from tbl_unique where exists(select null from (select a,b from tbl_unique group by a,b having count(*) > 1)tbl_temp where (tbl_temp.a=tbl_unique.a and tbl_temp.b=tbl_unique.b) or (tbl_temp.a is null and tbl_unique.a is null) or (tbl_temp.b is null and tbl_unique.b is null)) ;
a | b | c
---+------+---------
1 | 1 | catch u
1 | 1 | catch u
1 | 1 | catch u
2 | NULL | catch u
2 | NULL | catch u
2 | NULL | catch u
(6 rows)

第四步:删除所有(a,b)重复数据

同样把上面语句的select * 替换成delete即可。

test=# delete from tbl_unique where exists(select null from (select a,b from tbl_unique group by a,b having count(*) > 1)tbl_temp where (tbl_temp.a=tbl_unique.a and tbl_temp.b=tbl_unique.b) or (tbl_temp.a is null and tbl_unique.a is null) or (tbl_temp.b is null and tbl_unique.b is null)) ;
DELETE 6
test=# select * from tbl_unique ;
a | b | c
---+---+---
(0 rows)

第五步:增加唯一键约束

test=# alter table tbl_unique add constraint uk_tbl_unique_a_b unique (a,b);
ALTER TABLE

如果表中没有主键或NOT NULL的唯一键,那么可以利用表的OID属性,将表的oid列显示出来,该列类似主键的功能。利用该列,可以将重复数据删除到只剩一条,先使用下面的SQL语句,修改表的属性。

test=# alter table tbl_unique set with oids;
ALTER TABLE

情况三:将严格意义上重复数据删除到只有一条

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

test=# alter table tbl_unique drop constraint uk_tbl_unique_a_b ;
ALTER TABLE
test=# delete from tbl_unique ;
DELETE 0
test=# insert into tbl_unique (a,b) values (1,1),(1,1),(1,1);
INSERT 0 3
test=# insert into tbl_unique (a) values (2),(2),(2);
INSERT 0 3
test=# select oid,* from tbl_unique ;
oid | a | b | c
-------+---+------+---------
16399 | 1 | 1 | catch u
16400 | 1 | 1 | catch u
16401 | 1 | 1 | catch u
16402 | 2 | NULL | catch u
16403 | 2 | NULL | catch u
16404 | 2 | NULL | catch u
(6 rows)

严格意义上的重复数据是3条(1,1,'catch u'),现在要将三条的重复数据,删除到只剩一条。

第二步:查询重复数据的最小oid

test=# select min(oid) from tbl_unique where a is not null and b is not null group by a,b;
min
-------
16399
(1 row)

第三步:查询oid不是最小的重复数据

test=# select oid,* from tbl_unique where oid not in(select min(oid) from tbl_unique where a is not null and b is not null group by a,b) and a is not null and b is not null;
oid | a | b | c
-------+---+---+---------
16400 | 1 | 1 | catch u
16401 | 1 | 1 | catch u
(2 rows)

第四步:删除oid不是最小的重复数据

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

test=# delete from tbl_unique where oid not in(select min(oid) from tbl_unique where a is not null and b is not null group by a,b) and a is not null and b is not null;
DELETE 2
test=# select oid,* from tbl_unique ;
oid | a | b | c
-------+---+------+---------
16399 | 1 | 1 | catch u
16402 | 2 | NULL | catch u
16403 | 2 | NULL | catch u
16404 | 2 | NULL | catch u
(4 rows)

第五步:增加唯一键约束

test=# alter table tbl_unique add constraint uk_tbl_unique_a_b unique (a,b);
ALTER TABLE

情况四:将非严格意义上重复数据删除到只有一条

第一步:删除唯一约束,清空表,写入测试数据

test=# alter table tbl_unique drop constraint uk_tbl_unique_a_b ;
ALTER TABLE
test=# delete from tbl_unique ;
DELETE 4
test=# insert into tbl_unique (a,b) values (1,1),(1,1),(1,1);
INSERT 0 3
test=# insert into tbl_unique (a) values (2),(2),(2);
INSERT 0 3
test=# select oid,* from tbl_unique ;
oid | a | b | c
-------+---+------+---------
16407 | 1 | 1 | catch u
16408 | 1 | 1 | catch u
16409 | 1 | 1 | catch u
16410 | 2 | NULL | catch u
16411 | 2 | NULL | catch u
16412 | 2 | NULL | catch u
(6 rows)

第二步:查询重复数据的最小oid

test=# select min(oid) from tbl_unique group by a,b;
min
-------
16410
16407
(2 rows)

第三步:查询oid不是最小的重复数据

test=# select oid,* from tbl_unique where oid not in(select min(oid) from tbl_unique group by a,b);
oid | a | b | c
-------+---+------+---------
16408 | 1 | 1 | catch u
16409 | 1 | 1 | catch u
16411 | 2 | NULL | catch u
16412 | 2 | NULL | catch u
(4 rows)

第四步:删除oid不是最小的重复数据

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

test=# delete from tbl_unique where oid not in(select min(oid) from tbl_unique group by a,b);
DELETE 4
test=# select oid,* from tbl_unique ;
oid | a | b | c
-------+---+------+---------
16407 | 1 | 1 | catch u
16410 | 2 | NULL | catch u
(2 rows)

第五步:增加唯一键约束

test=# alter table tbl_unique add constraint uk_tbl_unique_a_b unique (a,b);
ALTER TABLE

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

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

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

  2. PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库

    最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...

  3. 数据库并发事务控制四:postgresql数据库的锁机制二:表锁 <转>

    在博文<数据库并发事务控制四:postgresql数据库的锁机制 > http://blog.csdn.net/beiigang/article/details/43302947 中后面提 ...

  4. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...

  5. TPC-H生成.tbl文件导入postgresql数据库的坑

    数据库project好好的不用主流的MySQL和Microsoft server而要求用听都没听过的postgresql (当然,可能你三个都没听过) 这里的坑主要是把生成的那八张.tbl的表导入pg ...

  6. PowerDesigner反向工程PostgreSQL数据库

    1. 环境准备: a)         安装PowerDesigner,以PowerDesigner15.1为例 b)         安装java jdk,以jdk-7-windows-i586为例 ...

  7. 视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    说好的给园子里的朋友们录制与<ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库> 这篇博客相对应的视频,由于一个月一来没有时 ...

  8. Enterprise Architect的共享Respository设置,postgresql数据库

    Enterprise Architect有一个很实用的共享,在设计UML图的时候,可以连接到一个数据库服务器,将所有的画图数据共享在上面,所有连到这个server的人,都可以看到 别人的图,图中的元素 ...

  9. PostgreSQL数据库基本配置

    一.安装 首先安装PostgreSQL客户端: sudo apt-get install postgresql-client 然后,安装PostgreSQL服务器: sudo apt-get inst ...

  10. C#访问PostGreSQL数据库的方法 http://www.jb51.net/article/35643.htm

    这次的项目中的一个环节要求我把PostGreSQL数据取出来,然后放到SqlServer里,再去处理分析. http://www.jb51.net/article/35643.htm - 我对Post ...

随机推荐

  1. 关于Cocos2d-x中定时器的使用总结

    1.定义 定时器在大部分游戏中是不可或缺的,即每隔一段时间,就要执行相应的刷新体函数,以更新游戏的画面.时间.进度.敌人的指令等等. cocos2dx为我们提供了定时器schedule相关的操作.其操 ...

  2. VS2013环境生成和调用DLL动态链接库

    http://blog.csdn.net/u010273652/article/details/25514577 创建动态库方法: 创建动态库是生成 .dll .lib 两个个文件 文件 -> ...

  3. 【转】7Z命令行解压缩

    7z.exe在CMD窗口的使用说明如下: 7-Zip (A) 4.57 Copyright (c) 1999-2007 Igor Pavlov 2007-12-06 Usage: 7za <co ...

  4. (转)x264源码分析(1):main、parse、encode、x264_encoder_open函数代码分析

    转自:http://nkwavelet.blog.163.com/blog/static/2277560382013103010312144/ x264版本:   x264-snapshot-2014 ...

  5. nodejs基础 -- Stream流

    nodejs 的 Stream 是一个抽象接口,node中有很多对象实现了这个接口.例如,对http服务器发起请求的request对象就是一个Stream,还有stdout(标准输出)也是一个Stre ...

  6. Linux Vi/Vim 的使用及实例

    什么是 vim? Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用. 简单的来说, vi 是老式的字处理器,不过功能已经很齐全了,但是 ...

  7. OpenGL介绍

    OpenGL是一个开放的三维图形软件包,它独立于窗口系统和操作系统,以它为基础开发的应用程序可以十分方便地在各种平台间移植:OpenGL可以与Visual C++紧密接口,便于实现机械手的有关计算和图 ...

  8. mysql 中查看指定表的字段名 (可根据字段变量生成c#后台代码)

    select DISTINCT data_type  from COLUMNS where table_name='表名' 用ConCat();构造生成代码.....

  9. 演示-JQuery关系选择器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. apache Storm之一-入门学习

    准备工作 这个教程使用storm-starter项目里面的例子.我推荐你们下载这个项目的代码并且跟着教程一起做.先读一下:配置storm开发环境和新建一个strom项目这两篇文章把你的机器设置好. 一 ...