ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL基础知识—约束(重点)

2021-10-10 22:02:46  阅读:188  来源: 互联网

标签:name kd MySQL 约束 vip sec mysql 基础知识 id


介绍

约束:constraint

在创建表的时候,可以给表中的字段加上约束来保证表中数据的完整性,有效性。

常见约束

非空约束not null

非空约束not null约束的字段不能为NULL。

只有列级约束。

示例

mysql> create table t_vip(id int,name varchar(255) not null);
Query OK, 0 rows affected (0.25 sec)

mysql> desc t_vip;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int          | YES  |     | NULL    |       |
| name  | varchar(255) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.04 sec)
mysql> insert into t_vip(id,name) values(1,'kd');
Query OK, 1 row affected (0.04 sec)

mysql> insert into t_vip(id,name) values(2,'hl');
Query OK, 1 row affected (0.04 sec)

mysql> insert into t_vip(id) values(2);
1364 - Field 'name' doesn't have a default value

可以看到name没有默认值,表示不能为空。

唯一性约束unique

唯一性约束unique约束的字段不能重复,但是可以为NULL。

示例1 列级约束

mysql>  create table t_vip(id int,name varchar(255) unique,email varchar(255));
Query OK, 0 rows affected (0.65 sec)

mysql> insert into t_vip(id,name,email) values(1,'kd','kd@123.com');
Query OK, 1 row affected (0.08 sec)

mysql> select * from t_vip;
+----+------+------------+
| id | name | email      |
+----+------+------------+
|  1 | kd   | kd@123.com |
+----+------+------------+
1 row in set (0.03 sec)

mysql> insert into t_vip(id,name,email) values(2,'kd','kd@123.com');
1062 - Duplicate entry 'kd' for key 't_vip.name'

mysql> insert into t_vip(id) values(2);
Query OK, 1 row affected (0.07 sec)

mysql> select * from t_vip;
+----+------+------------+
| id | name | email      |
+----+------+------------+
|  1 | kd   | kd@123.com |
|  2 | NULL | NULL       |
+----+------+------------+
2 rows in set (0.03 sec)

可以看出,name字段被unique约束,不能重复,一旦重复后会出现如下报错:

1062 - Duplicate entry 'kd' for key 't_vip.name'

但是,name字段可以为NULL,而NULL可以重复。

示例2 表级约束

需求:name和email两个字段联合起来具有唯一性

mysql>  create table t_vip(
			id int,
			name varchar(255),
			email varchar(255),
			unique(name,email));
Query OK, 0 rows affected (0.32 sec)

mysql> desc t_vip;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int          | YES  |     | NULL    |       |
| name  | varchar(255) | YES  | MUL | NULL    |       |
| email | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.03 sec)

mysql> insert into t_vip(id,name,email) values
(1,'kd','kd@123.com');
Query OK, 1 row affected (0.05 sec)

mysql> insert into t_vip(id,name,email) values
(2,'kd','kd1@123.com');
Query OK, 1 row affected (0.04 sec)

mysql> insert into t_vip(id,name,email) values
(3,'kd1','kd@123.com');
Query OK, 1 row affected (0.03 sec)

mysql> insert into t_vip(id,name,email) values(4,'kd','kd@123.com');
1062 - Duplicate entry 'kd-kd@123.com' for key 't_vip.name'

从上面的示例中,可以看出两个字段联合的唯一性约束中,两个字段可以分别不同,只有两个字段完全一样的时候才会报错。

主键约束

primary key(pk)

外键约束

foreign key(fk)

检查约束

check(mysql没有,Oracle有)

not null和unique联合

mysql> create table t_vip(
    	id int,
    	name varchar(255) not null unique);
Query OK, 0 rows affected (0.26 sec)

mysql> desc t_vip;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int          | YES  |     | NULL    |       |
| name  | varchar(255) | NO   | PRI | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

可以看出,name是not null 和unique联合约束后,name变成了主键primary key。(注意:Oracle中不一样!)

标签:name,kd,MySQL,约束,vip,sec,mysql,基础知识,id
来源: https://blog.csdn.net/kuangd_1992/article/details/120693145

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有