主键与唯一键区别

来源:互联网 发布:北京域名快速备案 编辑:程序博客网 时间:2024/04/28 16:52
【注】 转载原文地址:http://blog.sina.com.cn/s/blog_54eeb5d9010005e1.html
1、一个表中可以有多个唯一索引,但是只能有一个主键
2、主键一定是唯一性索引,唯一性索引并不一定就是主键
主键不允许为空,唯一键允许为空,空值不受唯一约束,也就是说可以有多个空值。
注:可以多列组合成一个唯一索引或者一个主键,即组合索引或组合主键
 
代码:

SQLcreate table t (a number(5not nullb number(5)); Table created SQLalter table t add constraint pk_t primary key (ausing indexTable altered SQLcreate unique index idx_t_b on t (b); Index created SQLinsert into t values (11); 1 row inserted SQLinsert into t values (21); insert into t values (21ORA-00001unique constraint (DEMO.IDX_T_Bviolated SQLinsert into t values (2null); 1 row inserted SQLinsert into t values (3null); 1 row inserted SQLinsert into t values (4null); 1 row inserted SQLinsert into t values (42); insert into t values (42ORA-00001unique constraint (DEMO.PK_Tviolated SQLinsert into t values (null2); insert into t values (null2ORA-01400cannot insert NULL into ("DEMO"."T"."A"SQLselect from t;           ------ ------           1      2      3      4
0 0
原创粉丝点击