MySQL数据库_约束条件
目录
1.概述:
-
约束是一种限制,它通过限制表中的数据,来确保数据的完整性和唯一性。
-
约束可以用来管理如何插入或处理数据库数据。
2.分类:
-
主键约束(Primary Key)
-
外键约束(Foreign Key)
-
唯一约束(Unique Key)
-
非空约束(Not Null)
-
默认值约束(Default)
-
检查约束(Check)
注意:以上6种约束中,一个数据表中只能有一个主键约束,其它约束可以有多个。
3.主键约束(Primary Key)
(1)概述:
-
主键约束是使用最频繁的约束。在设计数据表时,一般情况下,都会要求表中设置一个主键。主键是表的一个特殊字段,该字段能唯一标识该表中的每条信息。
-
例如,学生信息表中的学号是唯一的。
(2)分类:
-
单字段主键
-
多字段联合主键
(3)规则说明:
-
每个表只能定义一个主键。
-
一个字段名只能在联合主键字段表中出现一次。
-
主键值必须唯一标识表中的每一行,且不能为NULL,即表中不可能存在有相同主键值的两行数据,这是唯一性原则。
-
联合主键不能包含不必要的多余字段。当把联合主键的某一字段删除后,如果剩下的字段构成的主键仍然满足唯一性原则,那么这个联合主键是不正确的,这是最小化原则。
(4)主键约束管理:
1)在创建表时设置主键约束
-
设置单字段主键
用法说明1:
MariaDB [db_test]> create table 表名
-> (
-> 字段名1 数据类型1 primary key,
-> ……
-> 字段名n 数据类型n,
-> );
用法说明2:
MariaDB [db_test]> create table 表名
-> (
-> 字段名1 数据类型1,
-> ……
-> [字段n] [数据类型n],
-> primary key (字段名)
-> );
实例:
MariaDB [db_test]> create table tab_students #创建tab_courses数据表
-> (
-> stu_id int(4) primary key,
-> stu_name varchar(20),
-> );
或者:
MariaDB [db_test]> create table tab_students #创建tab_courses数据表
-> (
-> stu_id int(4),
-> stu_name varchar(20),
-> primary key(stu_id)
-> );
MariaDB [db_test]> desc tab_students;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| stu_id | int(4) | NO | PRI | NULL | |
| stu_name | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
-
设置多字段联合主键
用法说明:
MariaDB [db_test]> create table 表名
-> (
-> 字段名1 数据类型1,
-> ……
-> 字段名n 数据类型n,
-> primary key (字段名1,字段2)
-> );
实例:
MariaDB [db_test]> create table tab_emp
-> (
-> name varchar(20),
-> deptID int(10),
-> salary float,
-> primary key(name,deptID)
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [db_test]> desc tab_emp;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(20) | NO | PRI | | |
| deptID | int(10) | NO | PRI | 0 | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
2)在修改表时添加主键约束
用法说明:
MariaDB [db_test]> alter table 表名
-> add primary key(字段名);
实例:
MariaDB [db_test]> desc tab_users;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| age | int(5) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)
MariaDB [db_test]> alter table tab_users
-> add primary key(name);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> desc tab_users;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | NO | PRI | | |
| age | int(5) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
3)删除主键约束
用法说明:
MariaDB [db_test]> alter table 表名
-> drop primary key;
实例:
MariaDB [db_test]> desc tab_users;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | NO | PRI | | |
| age | int(5) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [db_test]> alter table tab_users drop primary key; #由于主键约束在一个表中只能有一个,因此不需要指定主键名就可以删除一个表中的主键约束
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> desc tab_users;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | NO | | | |
| age | int(5) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
4.外键约束(Foreign Key)
(1)概述:
-
外键约束经常和主键约束一起使用,用来确保数据的一致性。
-
外键约束用来在两个表的数据之间建立链接,它可以是一列或者多列。一个表可以有一个或多个外键。
-
外键对应的是参照完整性,一个表的外键可以为空值,若不为空值,则每一个外键的值必须等于另一个表中主键的某个值。
-
外键是表的一个字段,不是本表的主键,但对应另一个表的主键。定义外键后,不允许删除另一个表中具有关联关系的行。
-
例如,一个水果摊,只有苹果、桃子、李子、西瓜4种水果,那么,你来到水果摊要买水果只能选择苹果、桃子、李子和西瓜,不能购买其它的水果。
(2)规则说明:
-
主表(父表):对于两个具有关联关系的表而言,相关联字段中主键所在的表就是主表。
-
从表(子表):对于两个具有关联关系的表而言,相关联字段中外键所在的表就是从表。
-
父表必须已经存在于数据库中,或者是当前正在创建的表。如果是后一种情况,则父表与子表是同一个表,这样的表称为自参照表,这种结构称为自参照完整性。
-
必须为父表定义主键。主键不能包含空值,但允许在外键中出现空值。也就是说,只要外键的每个非空值出现在指定的主键中,这个外键的内容就是正确的。
-
在父表的表名后面指定列名或列名的组合。这个列或列的组合必须是父表的主键或候选键。
-
外键中列的数目必须和父表的主键中列的数目相同。且数据类型也必须和父表主键中对应列的数据类型相同。
(3)外键约束管理:
1)在创建表时设置外键约束
用法说明:
MariaDB [db_test]> create table 从表名
-> (
-> 字段名1 数据类型1 [primary key],
-> ……
-> 字段名n 数据类型n,
-> constraint 外键名 foreign key(外键字段)
-> references 主表名(主键字段)
-> );
实例:
MariaDB [db_test]> create table tab_dep #创建主表,设定主键
-> (
-> id int(10) primary key,
-> name varchar(20) not null,
-> location varchar(50)
-> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [db_test]> create table tab_emp #创建从表,设置主键和外键
-> (
-> id int(10) primary key,
-> name varchar(20),
-> depID int(10), #关联字段的数据类型必须匹配
-> salary float,
-> constraint fk_emp_dep foreign key(depID) #外键名为fk_emp_dep,外键为dep_ID字段
-> references tab_dep(id) #与主表的主键(id)相关联
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [db_test]> show create table tab_emp;
+---------+------------------------------------------------------------------+
| Table | Create Table
+---------+------------------------------------------------------------------+
| tab_emp | CREATE TABLE `tab_emp` (
`id` int(10) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`depID` int(10) DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_emp_dep` (`depID`),
CONSTRAINT `fk_emp_dep` FOREIGN KEY (`depID`) REFERENCES `tab_dep` (`id`) #外键设定成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+------------------------------------------------------------------+
1 row in set (0.00 sec)
2)在修改表时添加外键约束
用法说明:
MariaDB [db_test]> alter table 从表名
-> add constraint 外键名 foreign key(外键字段)
-> references 主表名(主键字段);
MariaDB [db_test]> alter table tab_emp
-> add constraint fk_emp_dep foreign key(depID) #外键名为fk_emp_dep,外键为dep_ID字段
-> references tab_dep(id); #与主表的主键(id)相关联
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> show create table tab_emp;
+---------+------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------+
| tab_emp | CREATE TABLE `tab_emp` (
`id` int(10) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`depID` int(10) DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_emp_dep` (`depID`),
CONSTRAINT `fk_emp_dep` FOREIGN KEY (`depID`) REFERENCES `tab_dep` (`id`) #外键设定成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+------------------------------------------------------------------+
1 row in set (0.00 sec)
3)删除外键约束
用法说明:
MariaDB [db_test]> alter table 从表名
-> drop foreign key(外键字段);
实例:
MariaDB [db_test]> alter table tab_emp
-> drop foreign key fk_emp_dep;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> show create table tab_emp;
+---------+------------------------------+
| Table | Create Table |
+---------+------------------------------+
| tab_emp | CREATE TABLE `tab_emp` (
`id` int(10) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`depID` int(10) DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_emp_dep` (`depID`),
#删除外键成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+------------------------------+
1 row in set (0.00 sec)
5.唯一约束(Unique Key)
(1)概述:
-
唯一约束与主键约束有一个相似的地方,就是它们都能够确保列的唯一性。与主键约束不同的是,唯一约束在一个表中可以有多个,并且设置唯一约束的列是允许有空值的,虽然只能有一个空值。
-
例如,在用户信息表中,要避免表中的用户名重名,就可以把用户名列设置为唯一约束。
(2)规则说明:
-
一个表可以有多个字段声明为unique,但只能有一个primary key声明。
-
声明为unique的字段允许一个空值的存在,但是声明为primary key的字段不允许有任何空值。
(3)唯一约束管理:
1)在创建表时设置唯一约束
用法说明:
MariaDB [db_test]> create table 表名
-> (
-> 字段名1 数据类型1 primary key,
-> 字段名2 数据类型2 unnique,
-> ……
-> 字段名n 数据类型n
-> );
实例:
MariaDB [db_test]> create table tab_dep
-> (
-> id int(10) primary key,
-> name varchar(20) unnique,
-> location varchar(50)
-> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [db_test]> desc tab_dep;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
2)在修改表时添加唯一约束
用法说明:
MariaDB [db_test]> alter table tab_dep
-> add constraint 唯一约束名 unique(唯一约束字段);
实例:
MariaDB [db_test]> alter table tab_dep
-> add constraint unique_name unique(name);
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> show create table tab_dep;
+---------+---------------------------+
| Table | Create Table |
+---------+---------------------------+
| tab_dep | CREATE TABLE `tab_dep` (
`id` int(10) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`location` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_name` (`name`) #唯一约束名为unique_name
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+----------------------------+
1 row in set (0.00 sec)
3)删除唯一约束
用法说明:
MariaDB [db_test]> alter table 表名
-> drop index 唯一约束名;
实例:
MariaDB [db_test]> alter table tab_dep
-> drop index unique_name;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> show create table tab_dep;
+---------+---------------------------+
| Table | Create Table |
+---------+---------------------------+
| tab_dep | CREATE TABLE `tab_dep` (
`id` int(10) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`location` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
#唯一约束名unique_name已经被删除
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+----------------------------+
1 row in set (0.00 sec)
6.非空约束(Not Null)
(1)概述:
-
非空约束用来约束表中的字段不能为空。对于使用了非空约束的字段,如果用户在添加数据时没有指定值,数据库系统就会报错。
-
例如,在学生信息表中,如果不添加学生姓名,那么这条记录是没有用的。
(2)非空约束管理:
1)在创建表时设置非空约束
用法说明:
MariaDB [db_test]> create table 表名
-> (
-> 字段名1 数据类型1 [primary key],
-> 字段名2 数据类型2 not null,
-> ……
-> 字段名n 数据类型n
-> );
实例:
MariaDB [db_test]> create table tab_dep
-> (
-> id int(10) primary key,
-> name varchar(20) not null,
-> location varchar(50)
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [db_test]> desc tab_dep;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
2)在修改表时添加非空约束
用法说明:
MariaDB [db_test]> alter table 表名
-> change column 字段名
-> 字段名 数据类型 not null;
实例:
MariaDB [db_test]> alter table tab_dep
-> change column location
-> location varchar(50) not null;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> desc tab_dep;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| location | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
3)删除非空约束
用法说明:
MariaDB [db_test]> alter table 表名
-> change column 字段名
-> 字段名 数据类型 null;
实例:
MariaDB [db_test]> alter table tab_dep change column location location varchar(50) null;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> desc tab_dep;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
7.默认值约束(Default)
(1)概述:
-
默认值约束用来约束当数据表中某个字段不输入值时,自动为其添加一个已经设置好的值。默认值约束通常用在已经设置了非空约束的列,这样能够防止数据表在录入数据时出现错误。
-
例如,在注册学生信息时,如果不输入学生的性别,那么会默认设置一个性别或者输入一个“未知”。
(2)默认值约束管理:
1)在创建表时设置默认值约束
用法说明:
MariaDB [db_test]> create table 表名
-> (
-> 字段名1 数据类型1 [primary key],
-> 字段名2 数据类型2,
-> ……
-> 字段名n 数据类型n,
-> 字段名 数据类型 default '默认值'
-> );
实例:
MariaDB [db_test]> create table tab_dep
-> (
-> id int(10) primary key,
-> name varchar(20),
-> location varchar(50) default 'Beijing'
-> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [db_test]> desc tab_dep;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| location | varchar(50) | YES | | Beijing | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
2)在修改表时添加默认值约束
用法说明:
MariaDB [db_test]> alter table 表名
-> change coulmn 字段名
-> 字段名 数据类型 default '默认值';
实例:
MariaDB [db_test]> alter table tab_dep
-> change column location
-> location varchar(50) default 'Shanghai';
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> desc tab_dep;
+----------+-------------+------+-----+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+----------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| location | varchar(50) | YES | | Shanghai | |
+----------+-------------+------+-----+----------+-------+
3 rows in set (0.01 sec)
3)删除默认值约束
用法说明:
MariaDB [db_test]> alter table 表名
-> change coulmn 字段名
-> 字段名 数据类型 default null;
实例:
MariaDB [db_test]> alter table tab_dep
-> change column location
-> location varchar(50) default null;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [db_test]> desc tab_dep;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
8.检查约束(Check)
(1)概述:
-
检查约束是用来检查数据表中,字段值是否有效的一个手段。在设置字段的检查约束时要根据实际情况进行设置,这样能够减少无效数据的输入。
-
check子句会被分析,但是会被忽略。目前官方所有存储引擎都不再支持check检查约束了。
-
例如,学生信息表中的年龄字段是没有负数的,并且数值也是有限制的。如果是大学生,年龄一般应该在18~30岁之间。
(2)规则说明:
-
在更新表数据的时候,系统会检查更新后的数据行是否满足check约束中的限定条件。
-
若将check约束子句置于所有列的定义以及主键约束和外键定义之后,则这种约束也称为基于表的check约束。该约束可以同时对表中多个列设置限定条件。
(3)检查约束管理:
1)在创建表时设置检查约束
用法说明:
MariaDB [db_test]> create table 表名
-> (
-> 字段名1 数据类型1 [primary key],
-> 字段名2 数据类型2 ,
-> ……
-> 字段名n 数据类型n ,
-> check(字段名的约束条件1 and 字段名的约束条件2)
-> );
实例:
MariaDB [db_test]> create table tab_dep
-> (
-> id int(10) primary key,
-> name varchar(20),
-> depID int(10),
-> salary float,
-> check(salary>0 and salary<100)
-> );
Query OK, 0 rows affected (0.00 sec)
2)在修改表时添加检查约束
用法说明:
MariaDB [db_test]> alter table 表名
-> add constraint 检查约束名 check(字段名的约束条件);
MariaDB [db_test]> alter table tab_dep
-> add constraint check_depID check(depID>0);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
3)删除检查约束
用法说明:
MariaDB [db_test]> alter table 表名
-> drop constraint 检查约束名;
实例:
MariaDB [db_test]> alter table tab_dep
-> drop constraint check_depID;