MySQL 如何查看表的 auto_increment 值

MySQL 如何查看表的 auto_increment 值

要查看表的 auto_increment 值,可以使用 SHOW TABLE 命令。

语法如下:

SHOW TABLE STATUS LIKE 'yourTableName'\G
Mysql

语法如下:

SELECT `AUTO_INCREMENT`
   FROM `information_schema`.`TABLES`
   WHERE `TABLE_SCHEMA` = yourDatabaseName
   AND `TABLE_NAME` =’yourTableName';
Mysql

要理解上述语法,让我们创建一个表。 创建表的查询语句如下:

mysql> create table viewAutoIncrementDemo
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserName varchar(20)
   -> );
Query OK, 0 rows affected (0.59 sec)
Mysql

现在你可以使用插入命令在表中插入一些记录。 查询语句如下:

mysql> insert into viewAutoIncrementDemo(UserName) values('John');
Query OK, 1 row affected (0.18 sec)
mysql> insert into viewAutoIncrementDemo(UserName) values('Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into viewAutoIncrementDemo(UserName) values('Bob');
Query OK, 1 row affected (0.08 sec)
mysql> insert into viewAutoIncrementDemo(UserName) values('Sam');
Query OK, 1 row affected (0.12 sec)
mysql> insert into viewAutoIncrementDemo(UserName) values('Mike');
Query OK, 1 row affected (0.14 sec)
mysql> insert into viewAutoIncrementDemo(UserName) values('David');
Query OK, 1 row affected (0.16 sec)
mysql> insert into viewAutoIncrementDemo(UserName) values('Larry');
Query OK, 1 row affected (0.11 sec)
Mysql

使用 select 语句显示表的所有记录。 查询语句如下:

mysql> select *from viewAutoIncrementDemo;
Mysql

下面是输出结果:

+--------+----------+
| UserId | UserName |
+--------+----------+
|      1 | John     |
|      2 | Carol    |
|      3 | Bob      |
|      4 | Sam      |
|      5 | Mike     |
|      6 | David    |
|      7 | Larry    |
+--------+----------+
7 rows in set (0.00 sec)
Mysql

下面是查看表的 auto_increment 值的查询语句:

mysql> SHOW TABLE STATUS LIKE 'viewAutoIncrementDemo'\G
Mysql

下面是输出结果:

*************************** 1. row ***************************
           Name: viewautoincrementdemo
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
          Rows: 7
 Avg_row_length: 2340
     Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: 8
    Create_time: 2019-03-02 04:05:20
    Update_time: 2019-03-02 04:06:11
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.08 sec)
Mysql

下面是第二个查询语句:

mysql> SELECT `AUTO_INCREMENT`
   -> FROM `information_schema`.`TABLES`
   -> WHERE `TABLE_SCHEMA` = 'sample'
   -> AND `TABLE_NAME` = 'viewAutoIncrementDemo';
Mysql

下面是输出结果:

+----------------+
| AUTO_INCREMENT |
+----------------+
|              8 |
+----------------+
1 row in set (0.00 sec)
Mysql

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册