> 文章列表 > MySQL--表的约束--0410

MySQL--表的约束--0410

MySQL--表的约束--0410

目录

1. 空属性

1.2 限制不为空

2.默认值

2.2 default 和 null

3.列描述

4.zeorfill

5.主键

5.1 在创建表时直接在字段上指定主键

5.2 在表创建好以后添加主键

5.3 删除主键

5.4 复合主键

6.自增长

7.唯一键

 8.外键


1. 空属性

在插入数据的时候,如果某列中允许为null,并且插入时没有传入对应列的参数。那么这个值就会被设置为null。

mysql> desc t1-> ;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id      | int(11)       | YES  |     | NULL    |       |
| salary  | float(10,8)   | YES  |     | NULL    |       |
| salary2 | decimal(10,8) | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql> select * from t1;
+------+-------------+-------------+
| id   | salary      | salary2     |
+------+-------------+-------------+
|  100 | 23.12345695 | 23.12345612 |
+------+-------------+-------------+
1 row in set (0.00 sec)mysql> insert into t1 (id) values(101);
Query OK, 1 row affected (0.00 sec)mysql> select * from t1;
+------+-------------+-------------+
| id   | salary      | salary2     |
+------+-------------+-------------+
|  100 | 23.12345695 | 23.12345612 |
|  101 |        NULL |        NULL |
+------+-------------+-------------+

数据库默认字段基本都是字段为空,但是实际开发时,尽量保证字段不为空。因为空数据没办法参与运算。

所以我们在设计数据库表的时候,一定要在表中进行限制,满足上面条件的数据就不能插入到表中。这就是“约束”。

1.2 限制不为空

在列属性后面加上not null,约束该列不能填入null

mysql> create table myclass ( id int(10), class_room varchar(10) not null);

mysql> create table myclass ( id int(10), class_room varchar(10) not null);
Query OK, 0 rows affected (0.01 sec)mysql> desc myclass;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(10)     | YES  |     | NULL    |       |
| class_room | varchar(10) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into myclass (class_room) values('abc');
Query OK, 1 row affected (0.00 sec)
mysql> select * from myclass;
+------+------------+
| id   | class_room |
+------+------------+
| NULL | abc        |
+------+------------+
mysql> insert into myclass (id) values(1);
ERROR 1364 (HY000): Field 'class_room' doesn't have a default value

2.默认值

默认值:某一种数据会经常性的出现某个具体的值,可以在一开始就指定好,在需要真实数据的时候,用户可以选择性的使用默认值。

在列后面加上default 加默认值 即可完成

mysql> create table t5(-> name varchar(10) not null,-> age tinyint unsigned default 18,-> sex char(2) default '保密');
mysql> insert into t5 (name) values('张三');
mysql> insert into t5 (name,age) values('李四',19);
mysql> insert into t5 (name,age,sex) values('王五',17,'男');
mysql> select * from t5;
+--------+------+--------+
| name   | age  | sex    |
+--------+------+--------+
| 张三   |   18 | 保密   |
| 李四   |   19 | 保密   |
| 王五   |   17 | 男     |
+--------+------+--------+

默认值的生效:数据在插入的时候不给该字段赋值,就使用默认值
注意:只有设置了default的列,才可以在插入值的时候,对列进行省略

2.2 default 和 null

default 当我们不显示的向指定列中插入,default会自动生效。

null 当我们显示的向一列插入,如果插入的是正常值,就正常工作,否则插入NULL。 not null则约束不让插入NULL。

3.列描述

列描述:comment,没有实际含义,专门用来描述字段,会根据表创建语句保存,相当于是创建表时自己写下的注释。

需要通过如下指令查看。

show create table tablename \\G

4.zeorfill

填充零

当我们建表时,采用了如下语句

mysql> create table t6(-> id int,-> stusnum int(8));
Query OK, 0 rows affected (0.01 sec)mysql> desc t6-> ;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| id      | int(11) | YES  |     | NULL    |       |
| stusnum | int(8)  | YES  |     | NULL    |       |
+---------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

这个Type列中,int(11) int(8),里面的这个数字代表的是总共用多少位来存储这个数据。

对于unsigned 这个默认值会变成10 而不是11 。 这是因为int类型所能表示的最大值,用10位数字就能表示下来,所以对于有符号数,需要多一位放置符号,所以是11

第一种修改方案
mysql> alter table t6 modify stusnum int(8) zerofill;第二种修改方案
mysql> alter table t6 change stusnum stunum int(8) zerofill;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc t6;
+--------+--------------------------+------+-----+---------+-------+
| Field  | Type                     | Null | Key | Default | Extra |
+--------+--------------------------+------+-----+---------+-------+
| id     | int(11)                  | YES  |     | NULL    |       |
| stunum | int(8) unsigned zerofill | YES  |     | NULL    |       |
+--------+--------------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from t6;
+------+----------+
| id   | stunum   |
+------+----------+
|    3 | 00000128 |
+------+----------+

zerofill作用

如果宽度小于设定的宽度,自动填充0。要注意的是,这只是最后显示的结果,在MySQL中实际存储的还是1。
 

5.主键

primary key用来唯一的约束该字段里面的数据,不能重复,不能为空,一张表中最多只能有一个
主键。

就类似每个人都有身份证号,这个身份证号不能重复,不能为空。在需要查询信息的时候,可以通过身份证号来确定对象,但是不一定修改身份证号。

5.1 在创建表时直接在字段上指定主键

mysql> create table t7(-> id int unsigned primary key,-> name varchar(10));
Query OK, 0 rows affected (0.01 sec)mysql> desc t7;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   | PRI | NULL    |       |
| name  | varchar(10)      | YES  |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

5.2 在表创建好以后添加主键

mysql> alter table t6 add primary key (id);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc t6;;
+--------+--------------------------+------+-----+---------+-------+
| Field  | Type                     | Null | Key | Default | Extra |
+--------+--------------------------+------+-----+---------+-------+
| id     | int(11)                  | NO   | PRI | NULL    |       |
| stunum | int(8) unsigned zerofill | YES  |     | NULL    |       |
+--------+--------------------------+------+-----+---------+-------+

5.3 删除主键

mysql> alter table t6 drop primary key;
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> desc t6;
+--------+--------------------------+------+-----+---------+-------+
| Field  | Type                     | Null | Key | Default | Extra |
+--------+--------------------------+------+-----+---------+-------+
| id     | int(11)                  | NO   |     | NULL    |       |
| stunum | int(8) unsigned zerofill | YES  |     | NULL    |       |
+--------+--------------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

5.4 复合主键

在创建表的时候,在所有字段之后,使用primary key(主键字段列表)来创建主键,如果有多个字段
作为主键,可以使用复合主键

mysql> create table t8(-> id int,-> name varchar(6),-> phone int(11),-> primary key(id,phone));
Query OK, 0 rows affected (0.01 sec)mysql> desc t8;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(11)    | NO   | PRI | NULL    |       |
| name  | varchar(6) | YES  |     | NULL    |       |
| phone | int(11)    | NO   | PRI | NULL    |       |
+-------+------------+------+-----+---------+-------+

6.自增长

auto_increment:当对应的字段,不给值,会自动的被系统触发,系统会从当前字段中已经有的最大值+1操作,得到一个新的不同的值。

自增长的特点:

  • 任何一个字段要做自增长,前提是本身是一个索引(key一栏有值)
  • 自增长字段必须是整数
  • 一张表最多只能有一个自增长

 auto_increment可以跟想要的起始数。当手动插入了一个大于auto_increment的数时,自增长的起点也会变为这个大值。

mysql> create table t9( ->id int unsigned primary key auto_increment,-> name varchar(5))auto_increment=100;
mysql> insert into t9 (name) values('abc');
Query OK, 1 row affected (0.00 sec)mysql> insert into t9 (name) values('defg');
Query OK, 1 row affected (0.01 sec)mysql> select * from t9;
+-----+------+
| id  | name |
+-----+------+
| 100 | abc  |
| 101 | defg |
+-----+------+
2 rows in set (0.00 sec)
mysql> insert into t9 (id,name) values(500,'hijk');
Query OK, 1 row affected (0.00 sec)mysql> insert into t9 (name) values('lmn');
Query OK, 1 row affected (0.00 sec)mysql> select * from t9;
+-----+------+
| id  | name |
+-----+------+
| 100 | abc  |
| 101 | defg |
| 500 | hijk |
| 501 | lmn  |
+-----+------+

7.唯一键

一张表中有往往有很多字段需要唯一性,数据不能重复,但是一张表中只能有一个主键。

  • 唯一键就可以解决表中有多个字段需要唯一性约束的问题。

比如每个人的电话号码都是是不同的。

  • 唯一键的本质和主键差不多,唯一键允许为空,而且可以多个为空,空字段不做唯一性比较

 以学生信息表为例:

学生的属性,有的是可以重复的(身高、体重、姓名),有的是不能重复的(电话、学号、身份证号)。

一般而言主键只是在众多具有唯一性的属性列中被选择称为主键而已,其他字段的唯一性,我们在建表时也需要保证他的唯一性。但是因为主键只能有一个,所以mysql提供了唯一键。

  • 主键的作用,更多的是保证我们在查找的时候,找到唯一的一条记录。
  • 唯一键的作用,保证在表中,各个不同的值,要在mysql层中也保证唯一性。

列名 数据类型 unique

mysql> create table student (
-> id char(10) unique comment '学号,不能重复,但可以为空',
-> name varchar(10)
-> );
mysql> insert into student(id, name) values('01', 'aaa');
mysql> insert into student(id, name) values(null, 'bbb'); 可以为空

 8.外键