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值时,可以有重复的,而其他的则不能插入重复值。
分享到:
相关推荐
主要介绍了mysql中key 、primary key 、unique key 与index区别的相关资料,需要的朋友可以参考下
那这个问题就可以简化为 PRIMARY KEY,UNIQUE KEY 和 INDEX 的区别。而这三者也正好是索引的划分,主键索引,唯一索引和普通索引(INDEX)。 使用 INDEX 来加速从数据库中读取数据。INDEX 通常加在那些 JOIN, WHERE...
定义了UNIQUE约束的字段中不能包含重复值,可以为一个或多个字段定义UNIQUE约束,因此,UNIQUE即可以在字段级也可以在表级定义,在...简单的说, primary key = unique + not null unique就是唯一,当你需要限定你的某
UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。 PRIMARY KEY 约束拥有自动定义的 UNIQUE 约束。 请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。 CREATE TABLE 时...
在课程结束时,使用SQL查询分析器用PRIMARY KEY、CHECK、FOREIGN KEY……REFERENCES、NOT NULL、UNIQUE等关键字验证MYSQL的实体完整性、参照完整性及用户定义完整性。 二、实验内容 1、在查询分析器中用PRIMARY KEY...
UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。 PRIMARY KEY 拥有自动定义的 UNIQUE 约束。 请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。 下面的 SQL 在 ...
Check,Unique,Primary key,not null,Foreign key)。 示例: create table stu( s_id number(8) PRIMARY KEY, s_name varchar2(20) not null, s_sex varchar2(8), clsid number(8), constraint u_1 unique(s_name), ...
1、建表语句: CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, ... UNIQUE KEY `idxunique_first_name_gender` (`first_name`
set pause "please put an enter key" 且 set pause on:设置带有提示的分屏 oerr ora 904 :查看错误 set head off :去掉表头 set feed off :去掉表尾 保存在oracle数据库中的所有操作细节: spool ...
id int (5)auto_increment primary key, username varchar(20), userpassword varchar(20), studentmaggerrole varchar(10) ) //班级信息表 create table class ( id int (5)auto_increment primary key, deptid ...
a%b表⽰以a开头b结尾的任意长度的字符串 _(下横线) a_b表⽰以a开头b结尾的长度为3的任意字符, PRIMARY KEY UNIQUE NOT NULL AUTO_INCREMENT DEFAULT default_value PRIMARY KEY 与UNIQUE 数据库操作: show ...
补充: (3)创建课程表 CREATE TABLE courses -> ( cno char(4) PRIMARY KEY NOT NULL, -> cname varchar(40) UNIQUE, -> cperiod INT, -> credit DECIMAL(3,1), -> ctno char(4), -> CONSTRAINT fk_tescher_...
若表有primary key或unique key,则分区表的分区列必须包含在primary key或unique key列表里,这是为了确保主键的效率,否则同一主键区的东西一个在A分区,一个在B分区,显然会比较麻烦。 2、各分区类型条件 ...
mysql “ON DUPLICATE KEY UPDATE” 语法如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则在出现重复值的行执行UPDATE;如果不会导致唯一值列...
-- Create/Recreate primary, unique and foreign key constraints alter table JBPM4_DEPLOYMENT add primary key (DBID_) using index tablespace CSSCIS pctfree 10 initrans 2 maxtrans 255 storage ...
UNIQUE KEY `provice_id` (`province_id`) ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8 COMMENT='省份数据库'; CREATE TABLE `position_city` ( `id` int(11) NOT NULL AUTO_INCREMENT, `province_...
Alter table tabname add primary key(col) 说明:删除主键:Alter table tabname drop primary key(col) 创建索引 create [unique] index idxname on tabname(col…。) 删除索引:drop index idxname on ...
PRIMARY KEY (callnumber, supplier_id, ponum), FOREIGN KEY (callnumber) REFERENCES TITLE4(callnumber), FOREIGN KEY (supplier_id) REFERENCES SUPPLIER4(supplier_id), FOREIGN KEY (ponum) REFERENCES ...