1.约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性、唯一性。
2.按照约束的数目划分——分为表级约束和列级约束。ps:如果约束针对一个字段我们称为列级约束,针对两个或两个以上我们称为表级约束
3.按照功能划分——约束分为:
- NOT NULL(非空约束)
- PRIMARY KEY(主键约束)
- UNIQUE KEY(唯一约束)
- DEFAULT(默认约束)
- FOREIGN KEY (外键约束)
列级约束即可在列定义时声明,也可以在列定义后声明,表级约束只能在列定义后声明。
NOT NULL,DEFAULT只有列级约束。
check 约束不起作用。
其他约束看这里
保持数据的一致性,完整性。
实现一对一或一对多关系。(把数据库称为关系型数据库的原因)
1.父表(子表参照的表)和子表(具有外键列的表)必须使用相同的存储引擎,而且禁止使用临时表。
2.数据表的存储引擎只能为InnoDB。
3.外键列和参照列必须具有相似的数据类型。其中数字的长度或是否有符号必须相同;而字符的长度则可以不同。
4.外键列和参照列必须创建索引。如果外键列不存在索引的话,MySQL将自动创建索引。
MySQL配置文件
default-storage-engine = INNODB
mysql> \q
mysql> use ti
Database changed
mysql> CREATE TABLE provinces(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> pname VARCHAR(20) NOT NULL
-> );
Query OK, 0 rows affected (0.23 sec)
mysql> SHOW CREATE TABLE provinces;
+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| provinces | CREATE TABLE `provinces` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`pname` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
FOREIGN KEY(pid) REFERENCES provinces(id)
foreign references
//父表
mysql> CREATE TABLE provinces(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> pname VARCHAR(20) NOT NULL
-> );
Query OK, 0 rows affected (0.23 sec)
//子表
mysql> CREATE TABLE users(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(10) NOT NULL,
-> pid SMALLINT,
-> FOREIGN KEY(pid) REFERENCES provinces(id)
-> );
ERROR 1215 (HY000): Cannot add foreign key constraint
//其中数字的长度或是否有符号必须相同
mysql> CREATE TABLE users(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(10) NOT NULL,
-> pid SMALLINT UNSIGNED,
-> FOREIGN KEY(pid) REFERENCES provinces(id)
-> );
Query OK, 0 rows affected (0.19 sec)
show INDEXES FROM tab_name;
show INDEXES FROM tab_name\g; 以网格的形式
mysql> show INDEXES FROM provinces;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| provinces | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
// 以网格的形式
mysql> show INDEXES FROM provinces\G;
*************************** 1. row ***************************
Table: provinces
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
ERROR:
No query specified
表有两个索引
mysql> show INDEXES FROM users\G;
*************************** 1. row ***************************
Table: users
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: users
Non_unique: 1
Key_name: pid
Seq_in_index: 1
Column_name: pid
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
2 rows in set (0.00 sec)
ERROR:
No query specified
mysql> SHOW CREATE TABLE users;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`pid` smallint(5) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `provinces` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> CREATE TABLE user1(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(10) NOT NULL,
-> pid SMALLINT UNSIGNED,
-> FOREIGN KEY(pid) REFERENCES provinces(id) ON DELETE CASCADE
-> );
Query OK, 0 rows affected (0.20 sec)
mysql> SHOW CREATE TABLE USER1;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| USER1 | CREATE TABLE `user1` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`pid` smallint(5) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`),
CONSTRAINT `user1_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `provinces` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
做插入记录操作时要先在父表中插入再在子表中插入,因为子表参考的父表。
mysql> insert provinces(pname) VALUES('A');
Query OK, 1 row affected (0.03 sec)
mysql> insert provinces(pname) VALUES('b');
Query OK, 1 row affected (0.03 sec)
mysql> insert provinces(pname) VALUES('c');
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM provinces;
+----+-------+
| id | pname |
+----+-------+
| 1 | A |
| 2 | b |
| 3 | c |
+----+-------+
3 rows in set (0.00 sec)
mysql> INSERT user1(username,pid) VALUES('Tom',1)
-> ;
Query OK, 1 row affected (0.03 sec)
mysql> INSERT user1(username,pid) VALUES('Josn',3);
Query OK, 1 row affected (0.02 sec)
mysql> INSERT user1(username,pid) VALUES('Bob',7);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`ti`.`user1`, CONSTRAINT `user1_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `provinces` (`id`) ON DELETE CASCADE)
mysql> insert provinces(pname) VALUES('b');
Query OK, 1 row affected (0.03 sec)
mysql> insert provinces(pname) VALUES('c');
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM provinces;
+----+-------+
| id | pname |
+----+-------+
| 1 | A |
| 2 | b |
| 3 | c |
+----+-------+
3 rows in set (0.00 sec)
mysql> INSERT user1(username,pid) VALUES('Tom',1)
-> ;
Query OK, 1 row affected (0.03 sec)
mysql> INSERT user1(username,pid) VALUES('Josn',3);
Query OK, 1 row affected (0.02 sec)
mysql> INSERT user1(username,pid) VALUES('Bob',7);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`ti`.`user1`, CONSTRAINT `user1_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `provinces` (`id`) ON DELETE CASCADE)
mysql> INSERT user1(username,pid) VALUES('Bob',3);
Query OK, 1 row affected (0.03 sec)
mysql> SELECT *FROM USER1;
+----+----------+------+
| id | username | pid |
+----+----------+------+
| 1 | Tom | 1 |
| 2 | Josn | 3 |
| 4 | Bob | 3 |
+----+----------+------+
3 rows in set (0.00 sec)
mysql> DELETE FROM provinces where id = 3;
Query OK, 1 row affected (0.05 sec)
mysql> SELECT *FROM USER1;
+----+----------+------+
| id | username | pid |
+----+----------+------+
| 1 | Tom | 1 |
+----+----------+------+
1 row in set (0.00 sec)