【注】 转载原文地址:http://blog.sina.com.cn/s/blog_54eeb5d9010005e1.html
1、一个表中可以有多个唯一索引,
但是只能有一个主键。
2、
主键一定是唯一性索引,唯一性索引并不一定就是主键
主键
不允许为空,
唯一键允许为空,空值不受唯一约束,也就是说可以有多个空值。
注:可以多列组合成一个唯一索引或者一个主键,即组合索引或组合主键
代码:
SQL> create table t (a number(5) not null, b number(5));
Table created
SQL> alter table t add constraint pk_t primary key (a) using index;
Table altered
SQL> create unique index idx_t_b on t (b);
Index created
SQL> insert into t values (1, 1);
1 row inserted
SQL> insert into t values (2, 1);
insert into t values (2, 1)
ORA-00001: unique constraint (DEMO.IDX_T_B) violated
SQL> insert into t values (2, null);
1 row inserted
SQL> insert into t values (3, null);
1 row inserted
SQL> insert into t values (4, null);
1 row inserted
SQL> insert into t values (4, 2);
insert into t values (4, 2)
ORA-00001: unique constraint (DEMO.PK_T) violated
SQL> insert into t values (null, 2);
insert into t values (null, 2)
ORA-01400: cannot insert NULL into ("DEMO"."T"."A")
SQL> select * from t;
A B
------ ------
1 1
2
3
4