Table of Contents
创建测试表
客户端3的操作
注意
客户端2的操作-续
其他操作
小结
Home Database Mysql Tutorial MySQL自增长主键探究_MySQL

MySQL自增长主键探究_MySQL

Jun 01, 2016 pm 12:59 PM

MySQL自动增长使用的关键字是 AUTO_INCREMENT; 因为属于 DDL,所以不区分大小写. 使用的列,必须被定义为 key, 比如主键,唯一键等。

本文中使用的数据库是 MariaDB 5.5.5

默认事务隔离界别是 REPEATABLE-READ

客户端是安装 Windows版本 MariaDB时附带安装的 HeidiSQL .

社区免费版的下载页面为: https://downloads.mariadb.org/mariadb/

创建测试表

使用客户端连接到服务器, 用户为 root,密码也是 root 如:

mysql -h localhost -P 3306 -u root -proot
Copy after login
Copy after login
Copy after login
Copy after login

先选择切换 database:

USE `test`;
Copy after login
Copy after login
Copy after login
Copy after login

创建测试表:

DROP TABLE IF EXISTS `test_auto`;

CREATE TABLE `test_auto` (
    `id` INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`id`)
)
COMMENT='测试自动增长'
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Copy after login

客户端1的操作

使用新的客户端1连接到服务器, 用户为 root,密码也是 root 如:

mysql -h localhost -P 3306 -u root -proot
Copy after login
Copy after login
Copy after login
Copy after login

切换 database:

USE `test`;
Copy after login
Copy after login
Copy after login
Copy after login

然后,在客户端1之中, 开启事务, 插入一些数据, 但是不提交.

# 在客户端1中执行
begin ;
insert into test_auto() values();
insert into test_auto() values();
insert into test_auto() values();
insert into test_auto() values();
insert into test_auto() values();
insert into test_auto() values();
Copy after login

此时,可以使用查询语句

SELECT * FROM `test`.`test_auto`;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

可以看到, 得到了6条数据, id 是 1-6, 对应着我们插入数据的SQL数。因为我们没有提交, 所以这个结果只能在客户端1中看见。

客户端2的操作

使用新的客户端2连接到服务器, 用户为 root,密码也是 root 如:

mysql -h localhost -P 3306 -u root -proot
Copy after login
Copy after login
Copy after login
Copy after login

切换 database:

USE `test`;
Copy after login
Copy after login
Copy after login
Copy after login

然后,在客户端2之中, 开启事务, 插入一些数据, 也不提交.

# 在客户端2中执行
begin ;
insert into test_auto() values();
insert into test_auto() values();
insert into test_auto() values();
insert into test_auto() values();
insert into test_auto() values();
Copy after login

此时,可以使用查询语句

SELECT * FROM `test`.`test_auto`;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

可以看到, 得到了5条数据, id 是 7-11, 对应着我们插入数据的SQL数。因为我们没有提交, 所以这个结果只能在客户端2中看见。

这里我们可以看到,自增的主键是全局唯一的,如果有事务回滚,那么已经自增的部分,是不会受影响的。多个事务之间的自增主键也不会互相影响, 能保证唯一,但不能保证最终的记录是连续的。

注意

通过客户端1和客户端2的操作,可以发现没提交的事务操作其他客户端是不能看到的。

这是 REPEATABLE-READ 事务隔离级别, 在开启事务后, 还没提交前, 客户端看到的记录, 是 事务开启那一刻的快照, 加上本次会话中执行操作的结果。保证在事务执行过程中,不受其他会话所提交事务的影响。

如果事务的隔离级别是 READ COMMITtED , 只能看到提交成功的记录。

查询事务隔离级别: select @@tx_isolation

客户端3的操作

使用新的客户端3连接到服务器, 用户为 root,密码也是 root 如:

mysql -h localhost -P 3306 -u root -proot
Copy after login
Copy after login
Copy after login
Copy after login

切换 database:

USE `test`;
Copy after login
Copy after login
Copy after login
Copy after login

然后,在客户端3之中, 先使用查询语句:

SELECT * FROM `test`.`test_auto`;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

可以看到, 一条数据也没有,因为还没有数据被提交。

客户端1的操作-续

回到客户端1的窗口, 执行查询语句:

SELECT * FROM `test`.`test_auto`;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

可以看到还是原先的6条记录。接着提交事务

commit;
Copy after login

客户端2的操作-续

回到客户端2的窗口, 执行查询语句:

SELECT * FROM `test`.`test_auto`;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

可以看到还是原先的5条记录。接着回滚事务

rollback;
Copy after login

再执行查询语句:

SELECT * FROM `test`.`test_auto`;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

可以看到6条记录(ID为1-6),为什么是6条? 因为回滚时本次事务就结束,然后也不读取快照版本,而是读取所有可见的数据,即客户端1提交的数据。

其他操作

接着在客户端1中看到的也是6条记录.

也可以继续执行几次插入或者事务操作,中途查询数据,并分析结果。

小结

MySQL的自动增长列, 保证了不重复,不保证中间不跳号(当然,不跳号只有某些特殊业务有需求)。特别是在事务执行环境里执行时, 为了不影响逻辑与性能,也只能采用这种处理方式。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

See all articles