1.表的约束
真正约束字段的是数据类型,但是数据类型约束很单一,需要一些格外的约束,更好的保证数据的合法性,比如有一个字段是身份证号,要求是唯一的。
表的约束有很多,主要学习以下几种:
null/not null;
default;
comment;
primary key;
auto_increment;
unique key;
1.1空属性null/not null
1)null:字段为空(默认的)
2).not null:字段不为空
案例:创建一张班级表,包含班级名和班级所在教室(如果没有班级名,就不知道你所在班级;如果没有教室名字,就不知道你在哪上课)
mysql> create table myclass(
-> class_name varchar(10) not null,
-> class_room varchar(10) not null);
mysql> desc myclass;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| class_name | varchar(10) | NO | | NULL | |
| class_room | varchar(10) | NO | | NULL | |
+------------+-------------+------+-----+---------+-------+
//插入数据时,没有给教室数据,所以插入失败
mysql> insert into myclass(class_name) values('class1');
ERROR 1364 (HY000): Field 'class_room' doesn't have a default value
1.2默认值default
默认值:某一种数据会经常性的出现某个具体的值,可以在一开始就指定好,在需要真实数据的时候,用户可以选择性的使用默认值。
mysql> create table tt10(
-> name varchar(20) not null,
-> age tinyint unsigned default 0,
-> sex char(2) default '男');
mysql> desc tt10;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| sex | char(2) | YES | | 男 | |
+-------+---------------------+------+-----+---------+-------+
数据在插入的时候不给该字段赋值,就是用定义好的默认值
mysql> insert into tt10(name) values('zhangsan');
mysql> select * from tt10;
+----------+------+------+
| name | age | sex |
+----------+------+------+
| zhangsan | 0 | 男 |
+----------+------+------+
1.3列描述commet
comment:备注,专门用来描述字段
mysql> create table tt11(
-> name varchar(20) not null comment'姓名',
-> age tinyint unsigned default 0 comment '年龄',
-> sex char(2)default '男' comment '性别'
-> );
可以通过show查看注释的列信息
mysql> show create table tt11;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tt11 | CREATE TABLE `tt11` (
`name` varchar(20) NOT NULL COMMENT '姓名',
`age` tinyint(3) unsigned DEFAULT '0' COMMENT '年龄',
`sex` char(2) DEFAULT '男' COMMENT '性别'
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1.4主键primary key
I.一个主键
特点:唯一,不能重复,不能为空,一般在整型字段列上定义
create table tt12(
-> id int primary key,
-> name varchar(10));
或create table tt13(
-> id int,
-> name varchar(10),
-> primary key(id));
mysql> desc tt12;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
key中的PRI表示该字段是主键
II.复合主键
create table tt13(
-> id int,
-> name varchar(10),
-> primary key(id,name));
mysql> desc tt13;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(10) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
III.表已建好,再添加主键
alter table 表名 add primary key(字段列表)
- 主键约束:主键对应的字段不可以重复,一旦重复,操作失败。
mysql> insert into tt13 values(1, 'aaa');
Query OK, 1 row affected (0.01 sec)
//插入相同数据时,无法插入
mysql> insert into tt13 values(1, 'aaa');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
- 删除主键
alter table table_name drop primary key
mysql> alter table tt13 drop primary key;
mysql> desc tt13;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
+-------+------------------+------+-----+---------+-------+
1.5自增长
- auto_increment:当对应的字段,不给值,会自动的被系统触发,系统会从当前字段中已经有的最大值+1操作,得到一个新的不同的值。通常和主键搭配使用,作为逻辑主键。
- 特点:一张表最多只能有一个自增长;自增长字段必须是整数;任何一个字段要做自增长,前提是本身就是一个索引(key一栏有值)
//建表
mysql> create table tt2(
-> id int unsigned primary key auto_increment,
-> name varchar(10) not null default '');
//插入a
mysql> insert into tt2(name) values('a');
//插入b
mysql> insert into tt2(name) values('b');
//查看表内容
mysql> select * from tt2;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
+----+------+
1.6唯一键UNIQUE
一张表中往往有很多字段需要唯一性,数据不能重复,但是一张表中只能有一个主键;唯一键就可以解决表中有多个字段需要唯一性性约束的问题。
一张表可以有多个唯一键约束,而且可以为空
create table tt16(
-> id int primary key,
-> name varchar(10) unique);
mysql> desc tt16;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(10) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
1.7外键
外键用于定义主表和从表之间的关系:外键约束主要定义在从表上,主表则必须是有主键约束或unique约束。当定义外键后,要求外键列数据必须在主表的主键列存在或为null。
foreign key (字段名) references 主表(列)
根据案例学习外键的用法
- 先创建主表
mysql> create table myclass(
-> id int primary key,
-> name varchar(30) not null comment '班级名');
- 再创建从表
mysql> create table stu(
-> id int primary key,
-> name varchar(30) not null comment'学生名',
-> class_id int,
-> foreign key(class_id) references myclass(id))
-> ;
- 正常插入数据
mysql> insert into myclass values(10,'软工01班'),(20,'计科02班');
mysql> insert into stu values(100,'张三',10),(101,'李四',20);
//查看两张表
mysql> select * from myclass;
+----+-------------+
| id | name |
+----+-------------+
| 10 | 软工01班 |
| 20 | 计科02班 |
+----+-------------+
mysql> select * from stu;
+-----+--------+----------+
| id | name | class_id |
+-----+--------+----------+
| 100 | 张三 | 10 |
| 101 | 李四 | 20 |
+-----+--------+----------+
- 插入一个班级号为30的学生,因为没有30这个班级,所以无法插入
mysql> insert into stu values(102,'王五',30);
ERROR 1452 (23000): Cannot add or update a child row:
a foreign key constraint fails (`test1`.`stu`, CONSTRAINT `stu_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `myclass` (`id`))
- 插入班级id为null(即新来一名学生,还没有分配班级)
mysql> insert into stu values(102,'赵六',null);
mysql> select * from stu;
+-----+--------+----------+
| id | name | class_id |
+-----+--------+----------+
| 100 | 张三 | 10 |
| 101 | 李四 | 20 |
| 102 | 赵六 | NULL |
+-----+--------+----------+