对表中的数据进行限定 , 保证数据的正确性 , 有效性 , 完整性。
1、not null
非空约束,表示该字段不能为空;插入数据值必须要给传值。
-- 增加约束条件
create table t2(
id int(3) not null -- 该字段传值的时候不能为空
);
举例:
mysql> create table t2(id int(2) not null,name char(4));
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int | NO | | NULL | |
| name | char(4) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
mysql> insert into t2 values(100); -- 前边不指定字段,数据插入不完整,不管是不是设定了非空,报错
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into t2 values('逆天而行'); -- 前边不指定字段,数据插入不完整,不管是不是设定了非空,报错
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into t2(name) values('随风起舞'); -- 前边指定,非空不插入数据,报错
ERROR 1364 (HY000): Field 'id' doesn't have a default value
mysql> insert into t2 values(1,'逆天而行'); -- 全部数据插入就没问题
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2(id) values(2); -- 只要非空插入数据,其他不管也没事
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+----+--------------+
| id | name |
+----+--------------+
| 1 | 逆天而行 |
| 2 | NULL |
+----+--------------+
mysql> alter table t3 modify sex char;
Query OK, 4 rows affected (0.03 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> desc t3; -- 修改数据类型不小心会导致约束条件丢失,加上就好了
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name | char(4) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2、default
默认值 , 在该字段中如果没有数据的传入 , 会默认的将默认值进行填充
mysql> create table t3(name char ,sex enum('男','女') default('男'));
Query OK, 0 rows affected (0.01 sec)
mysql> desc t3;
+-------+-------------------+------+-----+--------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------------+------+-----+--------------------+-------------------+
| name | char(1) | YES | | NULL | |
| sex | enum('男','女') | YES | | _utf8mb4\'ç·\' | DEFAULT_GENERATED |
+-------+-------------------+------+-----+--------------------+-------------------+
mysql> insert into t3 values('穷凶极恶','男'),('星星有泪','女');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> alter table t3 modify column name char(4);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t3 values('穷凶极恶','男'),('星星有泪','女');
Query OK, 2 rows affected (0.00 sec)
mysql> select * from t3;
+--------------+------+
| name | sex |
+--------------+------+
| 穷凶极恶 | 男 |
| 星星有泪 | 女 |
+--------------+------+
-- **************************************************************************
-- **************************************************************************
-- 如下,默认值填不填都没事,不填自动给填了,倒是没有任何约束的数据不填就会为空,但是都不会报错
mysql> insert into t3(name) values('穷凶极恶');
Query OK, 1 row affected (0.02 sec)
mysql> insert into t3(sex) values('男');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t3;
+--------------+------+
| name | sex |
+--------------+------+
| 穷凶极恶 | 男 |
| 星星有泪 | 女 |
| 穷凶极恶 | 男 |
| NULL | 男 |
+--------------+------+
3、unique
字段值是唯一的 , 不能重复
mysql> create table t4(id int(4) unique, name char(4));
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> insert into t4 values(1,'铁血无双'),(1,'寒风凛凛');
ERROR 1062 (23000): Duplicate entry '1' for key 't4.id'
-- 报错,因为id的1重复
mysql> insert into t4 values(1,'铁血无双'),(2,'寒风凛凛');
Query OK, 2 rows affected (0.00 sec)
mysql> desc t4;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int | YES | UNI | NULL | |
| name | char(4) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
mysql> select * from t4;
+------+--------------+
| id | name |
+------+--------------+
| 1 | 铁血无双 |
| 2 | 寒风凛凛 |
+------+--------------+
4、primary key
主键约束,确保字段数据唯一却不能为空(相当于not null + unique)
添加数据的时候,既一定要填,而且不能重复的一列数据
5、auto_increment
自动增加(首先要把字段设置成一个主键),会默认设置一个int类型的值从1开始 , 每增加一条数据 , 会在上一条的基础上+1.
mysql> create table t5(id int primary key auto_increment,name char(4));
Query OK, 0 rows affected (0.01 sec)
mysql> desc t5;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | char(4) | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+
mysql> insert into t5(name) values('乱舞狂刀');
Query OK, 1 row affected (0.02 sec)
mysql> select * from t5;
+----+--------------+
| id | name |
+----+--------------+
| 1 | 乱舞狂刀 |
+----+--------------+
mysql> insert into t5(id) values(3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t5(name) values('鬼魅森林');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t5;
+----+--------------+
| id | name |
+----+--------------+
| 1 | 乱舞狂刀 |
| 3 | NULL |
| 4 | 鬼魅森林 |
+----+--------------+