MYSQL的primary key和unique key的区别_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
2
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2334 | 回复: 1   主题: MYSQL的primary key和unique key的区别        下一篇 
xuefeng
注册用户
等级:上士
经验:315
发帖:69
精华:0
注册:2011-8-17
状态:离线
发送短消息息给xuefeng 加好友    发送短消息息给xuefeng 发消息
发表于: IP:您无权察看 2015-7-30 16:21:19 | [全部帖] [楼主帖] 楼主

Primary key 与Unique Key都是唯一性约束。但二者有很大的区别:

1、Primary key的1个或多个列 必须为NOT NULL,如果列为NULL,在增加PRIMARY KEY时,列自动更改为NOT NULL。而UNIQUE KEY 对列没有此要求。

2、一个表只能有一个PRIMARY KEY,但可以有多个UNIQUE KEY。

下面以测试说明:

SQL> create table t (a int,b int,c int,d int);
Table created.
SQL> desc t
Name                                      Null?    Type
----------------------------------------- -------- -----------
A                                                  NUMBER(38)
B                                                  NUMBER(38)
C                                                  NUMBER(38)
D                                                  NUMBER(38)
SQL> alter table t add constraint pk_t primary key (a,b);
Table altered.
SQL> desc t
Name                                      Null?    Type
----------------------------------------- -------- ----------------
A                                         NOT NULL NUMBER(38)
B                                         NOT NULL NUMBER(38)
C                                                  NUMBER(38)
D                                                  NUMBER(38)


可以看到A、B两个列都自动改为了NOT NULL

SQL> alter table t modify (a int null);
alter table t modify (a int null)
*
ERROR at line 1:
ORA-01451: column to be modified to NULL cannot be modified to NULL


可以看到,列A不允许改为 NULL

SQL> alter table t drop constraint pk_t;
Table altered.
SQL> alter table t add constraint uk_t_1 unique (a,b);
Table altered.
SQL> desc t
Name                                      Null?    Type
----------------------------------------- -------- -----------
A                                                  NUMBER(38)
B                                                  NUMBER(38)
C                                                  NUMBER(38)
D                                                  NUMBER(38)


我们看到列A又变回了NULL。

注意到,在删除主键时,列的NULLABLE会回到原来的状态。如果在创建主键后,对原来为NULL的主键列,显式设为NOT NULL,在删除主键后仍然是NOT NULL。比如在创建主键后,执行下面的操作,可以看到:

SQL> alter table t modify (b int not null);
Table altered.
SQL> alter table t drop constraint pk_t;
Table altered.
SQL> desc t
Name                                      Null?    Type
----------------------------------------- -------- ----------
A                                                 NUMBER(38)
B                                         NOT NULL NUMBER(38)
C                                                  NUMBER(38)
D                                                  NUMBER(38)


再做如下的实验:

SQL> drop table t;
Table dropped.
SQL> create table t (a int,b int,c int,d int);
Table created.
SQL> alter table t add constraint uk_t_1 unique (a,b);
Table altered.
SQL> alter table t add constraint uk_t_2 unique (c,d);
Table altered.


可以看到可以增加两个UNIQUE KEY。看看能不能增加两个主键:

SQL> alter table t add constraint pk_t primary key (c);
Table altered.
SQL> alter table t add constraint pk1_t primary key (d);
alter table t add constraint pk1_t primary key (d)
*
ERROR at line 1:
ORA-02260: table can have only one primary key


由此可以看到一个表只能有一个主键。

SQL> alter table t drop constraint pk_t;
Table altered.
SQL> insert into t (a ,b ) values (null,null);
1 row created.
SQL> /
1 row created.
SQL> insert into t (a ,b ) values (null,1);
1 row created.
SQL> /
insert into t (a ,b ) values (null,1)
*
ERROR at line 1:
ORA-00001: unique constraint (SYS.UK_T_1) violated
SQL> insert into t (a ,b ) values (1,null);
1 row created.
SQL> /
insert into t (a ,b ) values (1,null)
*
ERROR at line 1:
ORA-00001: unique constraint (SYS.UK_T_1) violated


主键和唯一键约束是通过参考索引实施的,如果插入的值均为NULL,则根据索引的原理,全NULL值不被记录在索引上,所以插入全NULL值时,可以有重复的,而其他的则不能插入重复值

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
联动大白
注册用户
等级:列兵
经验:91
发帖:0
精华:0
注册:2015-5-27
状态:离线
发送短消息息给联动大白 加好友    发送短消息息给联动大白 发消息
发表于: IP:您无权察看 2019-6-29 0:30:00 | [全部帖] [楼主帖] 2  楼

为了方便大家阅读,我对文章中错误号来解释一下吧!

Error Id: ORA-00001

Title: unique constraint (string.string) violated

Description:

unique constraint (string.string) violated

Action:

Either remove the unique restriction or do not insert the key.

Cause:

An UPDATE or INSERT statement attempted to insert a duplicate key. For Trusted Oracle configured in DBMS MAC mode, you may see this message if a duplicate entry exists at a different level.


Error Id: ORA-01451

Title: column to be modified to NULL cannot be modified to NULL

Description:

column to be modified to NULL cannot be modified to NULL

Action:

if a primary key or check constraint is enforcing the NOT NULL constraint, then drop that constraint.

Cause:

the column may already allow NULL values, the NOT NULL constraint is part of a primary key or check constraint.


Error Id: ORA-02260

Title: table can have only one primary key

Description:

table can have only one primary key

Action:

Remove the extra primary key.

Cause:

Self-evident.


也许你已明白,但对一个人有用也是我存在的理由!^_^ By:持之以恒的大白

-- 来自: 北京联动北方科技有限公司



赞(0)    操作        顶端 
总帖数
2
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论