目录
约束的认识
约束是一种限制,用于限制表中的数据,为了保证表中的数据的准确和可靠性 , MySQL数据库通过约束防止无效的数据进入到表中,以保护数据的实体完整性 , 在 MySQL 中一共分为以下几种约束类型
约束类型 | 说明 |
---|---|
not null | 指示某列不能存储 NULL 值。 |
unique | 保证某列的每行必须有唯一的值。 |
default | 规定没有给列赋值时的默认值 |
primary key | not null 和 unique 的结合。 确保某列或多个列的结合有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。 |
foreign key | 保证一个表中的数据匹配另一个表中的值的参照完整性 |
约束的应用场景一般分为以下两种
-
创建数据表的时候添加约束
create table <数据表名>(<列名1> <约束类型> , <列名2> <约束类型>,.......)
-
修改现有表的约束类型
-- 给现有数据表添加字段并设置约束条件 alter table <数据表名> add <字段名> <类型> <约束条件>; -- 修改现有数据表的约束条件 alter table <数据表名> modify <字段名> <类型> <约束条件>; -- alter table <数据表名> drop <约束名> <约束键名>;
非空约束 ( not null )
创建表时,可以指定某列添加非空约束 , 使其在添加数据时该列必须要添加有效的数据.
1.创建数据表添加非空约束
命令
create table <数据表名>(<列名> not null);
示例
在数据库中创建一张 student 数据表 , 并把 id 字段设置为非空约束
-- 重新设置学生表结构
drop table if exists student;
-- 创建 student 数据表
create table student (
id int not null,
sn int,
name varchar(20),
qq_mail varchar(20)
);
查看 student 表结构观察变化 , 可以看到 , 在 id 字段中的 Null 此时的状态是 NO , 意味着 , 在添加数据的时候 , id 字段不接受 null 值
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| sn | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| qq_mail | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
如果在添加数据时 id 列为 null 或者不添加 id 列, 此时就会出现报错
-- 全列添加
mysql> insert into student values(null,2001,'张三','123@qq.com');
ERROR 1048 (23000): Column 'id' cannot be null
-- error : 列'id'不能为空
-- 指定列添加
mysql> insert into student(sn,name,qq_mail) values(2001,'张三','123@qq.com');
ERROR 1364 (HY000): Field 'id' doesn't have a default value
-- error : 字段id没有默认值
2.现有数据表添加非空约束
命令
alter table <数据表名> modify <字段名> <类型> not null;
示例
在现有 student 表中的 sn 字段添加非空约束
alter table student modify sn int not null;
查看表结构 , 可以看到 sn 字段中的 Null 选项的状态也变成了 NO
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| sn | int(11) | NO | | NULL | |
| name | varchar(20) | YES | | NULL | |
| qq_mail | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
3.删除非空约束
命令
alter table <数据表名> modify <字段名> <类型>;
示例
在现有 student 表中 , 删除 sn 字段的非空约束
alter table student modify sn int;
查看表结构 , 可以看到 sn 字段中的 Null 选项状态变回了 YES
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| sn | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| qq_mail | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
唯一约束 ( unique )
在创建数据表时 , 可以指定某列添加唯一约束 , 使该列在添加数据时 , 不能添加重复的数据
1.创建表添加唯一约束
命令
create table <数据表名>(<列名> unique);
示例
在数据库中创建一张 student 数据表 , 并把 id 字段设置为唯一约束
-- 重新设置学生表结构
drop table if exists student;
-- 创建 student 数据表
create table student (
id int unique,
sn int,
name varchar(20),
qq_mail varchar(20)
);
查看 student 表结构观察变化 , 可以看到 , 在 id 字段中的 Key 此时的状态是 UNI ,代表当前 id 字段的值是唯一的 , 不接受重复值
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| sn | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| qq_mail | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in se