当前位置:网站首页>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无法显示开机动画(4.0.3_r1)
- 利用栈实现队列的出队入队
- useReducer基本用法
- [SM8150][Pixel4]LCD驱动
- JVM basics you should know
- [sm8150] [pixel4] LCD driver
- pycharm Install packages failed
- Using Prom label proxy to implement label based multi tenant reading of Prometheus thanos
- Implementation of multi tenant read and write in Prometheus cortex
- 19C环境ORA-01035登陆报错处理
猜你喜欢
随机推荐
oracle库恢复数据
Winter combat camp hands-on combat - MySQL database rapid deployment practice lead mouse cloud Xiaobao
Chaos vous emmène au projet chaos.
Android面试计网面经大全【持续更新中。。。】
测试oracle库700万数据量普通方式创建索引所需时间
Antd Design Form表单检验
pycharm Install packages failed
Explore how @ modelandview can forward data and pages through the source code
Practice using polardb and ECs to build portal websites
双指针仪表盘读数(一)
oracle表的约束详解
Static interface method calls are not supported at language level ‘5‘ 异常解决
js时间获取本周一、周日,判断时间是今天,今天前、后
iTOP4412无法显示开机动画(4.0.3_r1)
"Write multi tenant" implementation of Prometheus and thanos receiver
oracle创建表空间和修改用户默认表空间
Abnormal record-10
oracle计算两日期相差多少秒,分钟,小时,天
Oracle redo log产生量大的查找思路与案例
中国各省会城市经纬度位置