推荐几个设计网站,网站开发知识体系,自媒体平台是什么意思,asp网站怎么下载源码文章目录 自增长唯一键外键 自增长
auto_increment#xff1a;当对应的字段#xff0c;不给值#xff0c;会自动的被系统触发#xff0c;系统会从当前字段中已经有的最大值1操作#xff0c;得到一个新的不同的值。通常和主键搭配使用#xff0c;作为逻辑主键。 自增长的… 文章目录 自增长唯一键外键 自增长
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_increment500;
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的班级里面还有学生由于外键约束导致删除失败。