欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > 再谈表的约束

再谈表的约束

2025/2/24 16:43:08 来源:https://blog.csdn.net/weixin_73397765/article/details/141155800  浏览:    关键词:再谈表的约束

在这里插入图片描述

文章目录

  • 自增长
  • 唯一键
  • 外键

自增长

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

  • 任何一个字段要做自增长,前提是本身是一个索引(key一栏有值)
  • 自增长字段必须是整数
  • 一张表最多只能有一个自增长
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开始,如果插入了一个自增值,那么后面如果没有插入自增值,就从上一个继续开始:

在这里插入图片描述

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

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 的值(批量插入获取的是第一个值):

mysql> select last_insert_id();

唯一键

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

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

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

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。

语法:

foreign key (字段名) references 主表()

实例:
在这里插入图片描述

主表创建:

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

从表创建:

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)

主表中含有的信息:

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的班级里面还有学生,由于外键约束导致删除失败。

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词