当前位置:网站首页>MySQL notes 4_ Primary key auto_increment
MySQL notes 4_ Primary key auto_increment
2022-04-23 07:12:00 【Xiaoye is stupid!】
Self increase of primary key (auto_increment)
When the primary key is defined as self growing , The primary key will be defined by the database system according to the data type , Automatic assignment .
- By default auto_increment The initial value of 1
- Only one field can be used in a table auto_increment constraint , And the field must have a unique index , In order to avoid duplication of serial numbers ( It is the primary key or part of the primary key ).
- auto_increment The constrained field must have not null attribute
- auto_increment The field of constraint can only be integer type (tinyint、smallint、int、bigint etc. )
- auto_increment The maximum value of the constraint is constrained by the data type of the field , If the upper limit is reached, it will fail .
Cancel the self growth of the field
alter table < The name of the data table > modify column < Field name > < New type ( New length )>
- Use the default value
alter table personal modify < Field name > < Field data type > 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
- Specify the initial value of the auto increment field
* When creating tables , Specify primary key from 10 Start growing from
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)
- Modify the initial value of the auto increment field ( Only valid for data added later )
alter table < Data table name > auto_increment= value
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(' Zhang San ','12345678911');
Query OK, 1 row affected (0.00 sec)
mysql> insert into teacher (name,phone) values(' Zhang San ','12345678911');
Query OK, 1 row affected (0.00 sec)
mysql> select * from teacher;
+-----+------+-------------+
| _id | name | phone |
+-----+------+-------------+
| 1 | Zhang San | 12345678911 |
| 2 | Zhang San | 12345678911 |
| 10 | Zhang San | 12345678911 |
| 11 | Zhang San | 12345678911 |
+-----+------+-------------+
4 rows in set (0.00 sec)
- Self increasing field values are discontinuous
This is not very good for the time being , It's a direct copy of
Create table tb_student3, among id Is a self incrementing primary key field ,name It's the only index
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)
- Delete the primary key constraint with self growth
1、alter table < The name of the data table > modify column < Field name > < New type ( New length )>
2、alter table < The name of the data table > drop primary key;
- Manually insert a piece of data into the field set as self growth value
1、 Initial table
mysql> select * from student;
+-----+------+-----------+
| _id | name | phone |
+-----+------+-----------+
| 1 | Zhang San | 111111111 |
| 2 | Zhang San | 111111111 |
| 3 | Zhang San | 111111111 |
| 4 | Zhang San | 111111111 |
| 5 | Li Si | 22222222 |
+-----+------+-----------+
5 rows in set (0.00 sec)
2、 Add data
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、 At this time, the self increasing value will self increase according to the last data
mysql> select * from student;
+-----+-------+-----------+
| _id | name | phone |
+-----+-------+-----------+
| 1 | Zhang San | 111111111 |
| 2 | Zhang San | 111111111 |
| 3 | Zhang San | 111111111 |
| 4 | Zhang San | 111111111 |
| 5 | Li Si | 22222222 |
| 10 | tom | 22222222 |
| 11 | jerry | 22222222 |
+-----+-------+-----------+
7 rows in set (0.00 sec)
版权声明
本文为[Xiaoye is stupid!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230606586396.html
边栏推荐
- ./gradlew: Permission denied
- Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
- ViewPager2实现画廊效果执行notifyDataSetChanged后PageTransformer显示异常 界面变形问题
- oracle分区的相关操作
- iTOP4412内核反复重启
- Exception record-9
- JNI中使用open打开文件是返回-1问题
- Exception record-8
- Apache Atlas 编译及安装记录
- Cause: dx. jar is missing
猜你喜欢

Dolphinscheduler调度sql任务建表时The query did not generate a result set异常解决

Oracle Job定时任务的使用详解

记录webView显示空白的又一坑

【2021年新书推荐】Practical IoT Hacking

取消远程依赖,用本地依赖

BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题

Bottomsheetdialogfragment conflicts with listview recyclerview Scrollview sliding

mysql和pgsql时间相关操作

C connection of new world Internet of things cloud platform (simple understanding version)

Build a cloud blog based on ECS (polite experience)
随机推荐
PG SQL截取字符串到指定字符位置
mysql和pg库遇到冲突数据时的两种处理方式
ARGB透明度换算
Using stack to realize queue out and in
oracle计算两日期相差多少秒,分钟,小时,天
adb shell top 命令详解
mysql和pgsql时间相关操作
launcher隐藏不需要显示的app icon
this. getOptions is not a function
取消远程依赖,用本地依赖
oracle视图相关
Itop4412 kernel restarts repeatedly
Dolphinscheduler集成Flink任务踩坑记录
Component learning
Itop4412 LCD backlight drive (PWM)
【2021年新书推荐】Practical IoT Hacking
接口幂等性问题
ffmpeg常用命令
npm ERR code 500解决
iTOP4412 HDMI显示(4.4.4_r1)