当前位置:网站首页>MySQL笔记4_主键自增长(auto_increment)
MySQL笔记4_主键自增长(auto_increment)
2022-04-23 06:07:00 【小叶很笨呐!】
主键自增涨(auto_increment)
当主键定义为自增长后,这个主键会由数据库系统根据定义的数据类型,自动赋值。
- 默认情况auto_increment的初始值为1
- 一个表中只能有一个字段使用auto_increment约束,且该字段必须有唯一索引,为了避免序号重复(即为主键或主键的一部分)。
- auto_increment约束的字段必须具备not null属性
- auto_increment约束的字段只能是整数类型(tinyint、smallint、int、bigint等)
- auto_increment约束的最大值受该字段的数据类型约束,如果达到上限就会失效。
取消字段的自增长
alter table <数据表的名称> modify column <字段名称> <新的类型(新的长度)>
- 使用默认值
alter table personal modify <字段名> <字段数据类型> primary key auto_increment;
mysql> alter table personal modify _id int(10) primary key auto_increment;
Query OK, 0 rows affected (0.38 sec)
Records: 0 Duplicates: 0 Warnings: 0
- 指定自增字段初始值
*在创建表的时候,指定主键从10开始自增长
mysql> create table peronsal(
-> _id int not null auto_increment,
-> name varchar(10),
-> primary key(_id))
-> auto_increment=10;
Query OK, 0 rows affected (0.04 sec)
- 修改自增字段的初始值(只对后添加的数据有效)
alter table <数据表名> auto_increment=值
mysql> alter table teacher auto_increment=10;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into teacher (name,phone) values('张三','12345678911');
Query OK, 1 row affected (0.00 sec)
mysql> insert into teacher (name,phone) values('张三','12345678911');
Query OK, 1 row affected (0.00 sec)
mysql> select * from teacher;
+-----+------+-------------+
| _id | name | phone |
+-----+------+-------------+
| 1 | 张三 | 12345678911 |
| 2 | 张三 | 12345678911 |
| 10 | 张三 | 12345678911 |
| 11 | 张三 | 12345678911 |
+-----+------+-------------+
4 rows in set (0.00 sec)
- 自增字段值不连续
这个暂时不太会,直接拷贝过来的
创建表 tb_student3,其中 id 是自增主键字段,name 是唯一索引
mysql> CREATE TABLE tb_student3(
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20) UNIQUE KEY,
-> age INT DEFAULT NULL
-> );
Query OK, 0 rows affected (0.04 sec)
- 删除带有自增长的主键约束
1、alter table <数据表的名称> modify column <字段名称> <新的类型(新的长度)>
2、alter table <数据表的名称> drop primary key;
- 手动给设置为自增长值的字段插入一条数据
1、初始表
mysql> select * from student;
+-----+------+-----------+
| _id | name | phone |
+-----+------+-----------+
| 1 | 张三 | 111111111 |
| 2 | 张三 | 111111111 |
| 3 | 张三 | 111111111 |
| 4 | 张三 | 111111111 |
| 5 | 李四 | 22222222 |
+-----+------+-----------+
5 rows in set (0.00 sec)
2、添加数据
mysql> insert into student values(10,"tom","22222222");
Query OK, 1 row affected (0.00 sec)
mysql> insert into student(name,phone) values("jerry","22222222");
Query OK, 1 row affected (0.00 sec)
3、此时自增长的值会根据最后一条数据自增长
mysql> select * from student;
+-----+-------+-----------+
| _id | name | phone |
+-----+-------+-----------+
| 1 | 张三 | 111111111 |
| 2 | 张三 | 111111111 |
| 3 | 张三 | 111111111 |
| 4 | 张三 | 111111111 |
| 5 | 李四 | 22222222 |
| 10 | tom | 22222222 |
| 11 | jerry | 22222222 |
+-----+-------+-----------+
7 rows in set (0.00 sec)
版权声明
本文为[小叶很笨呐!]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_50957373/article/details/120943937
边栏推荐
- iTOP4412 HDMI显示(4.0.3_r1)
- AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
- 19C环境ORA-01035登陆报错处理
- 常用于融合去重的窗口函数row_number
- Information:2021/9/29 10:01 - Build completed with 1 error and 0 warnings in 11s 30ms Error异常处理
- JS format current time and date calculation
- pg库查看某模式下某表的分布键
- Apache SeaTunnel 2.1.0部署及踩坑
- iTOP4412内核反复重启
- iTOP4412 SurfaceFlinger(4.4.4_r1)
猜你喜欢
Itop4412 LCD backlight drive (PWM)
Oracle Job定时任务的使用详解
Itop4412 HDMI display (4.0.3_r1)
Prometheus thanos Quick Guide
Prometheus cortex Architecture Overview (horizontally scalable, highly available, multi tenant, long-term storage)
接口幂等性问题
Typical application scenarios of alicloud log service SLS
Chaos takes you to the chaos project quickly
专用窗口函数rank, dense_rank, row_number
mysql和pgsql时间相关操作
随机推荐
Apache SeaTunnel 2.1.0部署及踩坑
iTOP4412 FramebufferNativeWindow(4.0.3_r1)
PG SQL截取字符串到指定字符位置
RAC环境数据库节点参数设置不当导致监听无法连接问题排查
When switch case, concatenate the error case and if of the conventional judgment expression and use L
Prometheus alarm record persistence (historical alarm saving and Statistics)
Exception record-6
mysql和pg库遇到冲突数据时的两种处理方式
实习做了啥
iTOP4412 FramebufferNativeWindow(4.0.3_r1)
oracle用delete删除数据所需时间测试
iTOP4412 LCD背光驱动(PWM)
Exception record-8
从0开始封装一套项目的网络请求框架
Abnormal record-21
[exynos4412] [itop4412] [android-k] add product options
基于ECS搭建云上博客(体验有礼)
org.xml.sax.SAXParseException; lineNumber: 141; columnNumber: 252; cvc-complex-type.2.4.a: 发现了以元素 ‘b
Exception record-5
"Write multi tenant" implementation of Prometheus and thanos receiver