码迷,mamicode.com
首页 > 其他好文 > 详细

19、约束之间的比较:主键约束、联合约束、唯一约束、外键约束

时间:2019-10-15 09:46:21      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:create   exists   ica   not   mod   article   合约   row   extra   

约束之间的比较:主键约束、联合约束、唯一约束、外键约束

1、主键约束,primary_key

能唯一确定一张表中的记录,也就是我们通过给某个字段添加约束,就可以使得改字段不重复且不为空。

create table test1(
id int primary key,
name varchar(20) not null
);

1.2、联合约束,只要联合的主键值加起来不重复就可以了,且不为空

create table test2(
id int,
name varchar(20),
password varchar(20),
primary key(id,name)
);

mysql> desc test2;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(20) | NO   | PRI | NULL    |       |
| password | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set

insert into test2 values(1,‘张三‘,‘000‘);
insert into test2 values(1,‘李四‘,‘000‘);
insert into test2 values(2,‘张三‘,‘000‘);
Query OK, 1 row affected

insert into test2 values (null,‘xiaoming‘,‘000‘);
1048 - Column ‘id‘ cannot be null

1.3 当创建表好了之后添加约束

create table test3(
id int,
name varchar(20)
);

alter table test3 add primary key (id);

或者:

alter table test3 modify id int primary key;

1.4 删除主键约束

alter table test3 drop primary key;

2、唯一约束,unique

约束的字段不为空,当只约束一个字段的时候,显示的是UNI,若修饰多个字段,显示的是MUL

create table test4(
id int,
name varchar(20),
unique(name)
);

create table test4(
id int,
name varchar(20) unique
);

2.1 当创建表好了之后添加约束

create table test4( 
id int,
name varchar(20)
);

alter table test4 add unique(name);

或者

alter table test4 modify name varchar(20) unique;

mysql> desc test4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  | UNI | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set

2.2 约束多个字段,都不能为空,只要组合键不重复就行,单个键重复不影响

create table test4(
id int,
name varchar(20),
unique (id,name)
);

mysql> desc test4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | MUL | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set

insert into test4 values (1,‘lisi‘);
insert into test4 values (2,‘lisi‘);

2.3 删除约束

即使是联合约束,相当于查找汉字的首字母和具体拼写一样,这种联合约束,只能直接删除主要的约束(首字母),并且删除掉这个约束后,所有的约束均删除了 联合约束只能删除主要约束,并且主要约束删除了,所有约束都删除了

alter table test4 drop index name;
1091 - Can‘t DROP ‘name‘; check that column/key exists

alter table test4 drop index id;
Query OK, 2 rows affected
Records: 2  Duplicates: 0  Warnings: 0

3、 primary_key 、 unique 区别

  • primary key = unique + not null
  • UNIQUED 可空,可以在一个表里的一个或多个字段定义;PRIMARY KEY 不可空不可重复,在一个表里可以定义联合主键;

参考资料:
https://www.bilibili.com/video/av39807944/?p=12 https://blog.csdn.net/nanaMasuda/article/details/52543177

19、约束之间的比较:主键约束、联合约束、唯一约束、外键约束

标签:create   exists   ica   not   mod   article   合约   row   extra   

原文地址:https://www.cnblogs.com/Stephanie-boke/p/11675198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!