当前位置:网站首页>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
边栏推荐
- Abnormal record-22
- npm ERR code 500解决
- Practice using polardb and ECs to build portal websites
- 常用于融合去重的窗口函数row_number
- RAC环境数据库节点参数设置不当导致监听无法连接问题排查
- Using Prom label proxy to implement label based multi tenant reading of Prometheus thanos
- Android-Room数据库快速上手
- ORACLE表有逻辑坏块时EXPDP导出报错排查
- Dolphinscheduler集成Flink任务踩坑记录
- oracle杀死正在执行的sql
猜你喜欢
随机推荐
Abnormal record-13
iTOP4412 SurfaceFlinger(4.4.4_r1)
【机器学习】笔记 4、KNN+交叉验证
Construire un blog Cloud basé sur ECS (bénédiction sur le Code Cloud Xiaobao, explication détaillée de la tâche iphone13 gratuite)
pg库对姓名进行校验
select命令产生redo日志问题的分析
ACFS文件系统创建扩大缩小等配置步骤
Apache SeaTunnel 2.1.0部署及踩坑
关于我
oracle给对象重命名
Itop4412 cannot display boot animation (4.0.3_r1)
iTOP4412无法显示开机动画(4.0.3_r1)
窗口分析函数LAST_VALUE,FIRST_VALUE,lag,lead
10g数据库使用大内存主机时不能启动的问题
oracle存储过程中is和as区别
Winter combat camp hands-on combat - cloud essential environment preparation, hands-on practical operation, quickly build lamp environment, lead mouse cloud Xiaobao backpack without shadow
Prometheus alarm record persistence (historical alarm saving and Statistics)
iTOP4412 LCD背光驱动(PWM)
开篇:双指针仪表盘的识别
Dolphinscheduler配置Datax踩坑记录









