再谈表的约束

文章目录

自增长

auto_increment:当对应的字段,不给值,会自动的被系统触发,系统会从当前字段中已经有的最大值+1操作,得到一个新的不同的值。通常和主键搭配使用,作为逻辑主键。

自增长的特点:

  • 任何一个字段要做自增长,前提是本身是一个索引(key一栏有值)
  • 自增长字段必须是整数
  • 一张表最多只能有一个自增长
bash 复制代码
mysql> create table if not exists tt21( id int unsigned primary key auto_increment, name varchar(20) not null );
Query OK, 0 rows affected (0.22 sec)

mysql> desc tt21;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)  | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.06 sec)

mysql> insert into tt21 (name) values ('a');
Query OK, 1 row affected (0.00 sec)

mysql> insert into tt21 (name) values ('b');
Query OK, 1 row affected (0.00 sec)

mysql> insert into tt21 (name) values ('c');
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt21;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
+----+------+
3 rows in set (0.00 sec)

如果在插入时没有设定自增值,那么默认从1开始,如果插入了一个自增值,那么后面如果没有插入自增值,就从上一个继续开始:

也可以自己设定一个起始值:

bash 复制代码
mysql> create table tt22( id int  unsigned primary key auto_increment, name varchar(20) not null )auto_increment=500;
Query OK, 0 rows affected (0.03 sec)

mysql> insert into tt22 (name) values ('a');
Query OK, 1 row affected (0.01 sec)

mysql> insert into tt22 (name) values ('b');
Query OK, 1 row affected (0.01 sec)

mysql> insert into tt22 (name) values ('c');
Query OK, 1 row affected (0.01 sec)

mysql> select * from tt22;
+-----+------+
| id  | name |
+-----+------+
| 500 | a    |
| 501 | b    |
| 502 | c    |
+-----+------+
3 rows in set (0.00 sec)

在插入后获取上次插入的 AUTO_INCREMENT 的值(批量插入获取的是第一个值):

bash 复制代码
mysql> select last_insert_id();

唯一键

一张表中有往往有很多字段需要唯一性,数据不能重复,但是一张表中只能有一个主键:唯一键就可以解决表中有多个字段需要唯一性约束的问题。

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

关于唯一键和主键的区别:我们可以简单理解成,主键更多的是标识唯一性的。而唯一键更多的是保证在业务上,不要和别的信息出现重复。

bash 复制代码
mysql> create table stu( id char(20) unique comment '这是学生的唯一键', name varchar(32) not null );
Query OK, 0 rows affected (0.03 sec)

mysql> desc stu;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | char(20)    | YES  | UNI | NULL    |       |
| name  | varchar(32) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

如果插入的id是一样的,就会插入失败:

唯一键可以为空:

外键

外键用于定义主表和从表之间的关系:外键约束主要定义在从表上,主表则必须是有主键约束或unique约束。当定义外键后,要求外键列数据必须在主表的主键列存在或为null。

语法:

bash 复制代码
foreign key (字段名) references 主表(列)

实例:

主表创建:

bash 复制代码
mysql> create table class(
    -> id int primary key,
    -> name varchar(32) not null
    -> );
Query OK, 0 rows affected (0.03 sec)

从表创建:

bash 复制代码
mysql> create table student( id int unsigned primary key, name varchar(20) not null, telephone varchar(32) unique key, class_id int, foreign key(class_id) references class(id) );
Query OK, 0 rows affected (0.06 sec)

主表中含有的信息:

bash 复制代码
mysql> select * from class;
+----+--------------+
| id | name         |
+----+--------------+
|  1 | 物联网101    |
|  2 | 物联网102    |
+----+--------------+
2 rows in set (0.00 sec)

在从表中插入信息:

在从表中插入班级id为1和2都是可以的,但是插入的班级id为3,由于外键约束,导致插入失败。

删除主表中班级id为1 的班级:

id为1的班级里面还有学生,由于外键约束导致删除失败。

相关推荐
qq_529835359 分钟前
对计算机中缓存的理解和使用Redis作为缓存
数据库·redis·缓存
勤奋的凯尔森同学2 小时前
webmin配置终端显示样式,模仿UbuntuDesktop终端
linux·运维·服务器·ubuntu·webmin
月光水岸New3 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6753 小时前
数据库基础1
数据库
莫忘初心丶3 小时前
在 Ubuntu 22 上使用 Gunicorn 启动 Flask 应用程序
python·ubuntu·flask·gunicorn
我爱松子鱼3 小时前
mysql之规则优化器RBO
数据库·mysql
chengooooooo3 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Rverdoser4 小时前
【SQL】多表查询案例
数据库·sql
Galeoto4 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)5 小时前
MySQL主从架构
服务器·数据库·mysql