要查看表的auto_increment值,可以使用INFORMATION_SCHEMA.TABLES。
让我们首先创建一个表-
mysql> create table viewtheauto_incrementValueForATableDemo -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) -> );
以下是使用insert命令在表中插入一些记录的查询-
mysql> insert into viewtheauto_incrementValueForATableDemo(StudentName) values('Ramit'); mysql> insert into viewtheauto_incrementValueForATableDemo(StudentName) values('Bob'); mysql> insert into viewtheauto_incrementValueForATableDemo(StudentName) values('Chris'); mysql> insert into viewtheauto_incrementValueForATableDemo(StudentName) values('Robert'); mysql> insert into viewtheauto_incrementValueForATableDemo(StudentName) values('John');
以下是使用select语句显示表中所有记录的查询-
mysql> select * from viewtheauto_incrementValueForATableDemo;
这将产生以下输出-
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Ramit | | 2 | Bob | | 3 | Chris | | 4 | Robert | | 5 | John | +-----------+-------------+ 5 rows in set (0.00 sec)
以下是查询以查看表的auto_increment值-
mysql> SELECT `AUTO_INCREMENT` AS 'NextAutoIncrementValue' -> FROM `information_schema`.`TABLES` -> WHERE `TABLE_SCHEMA` = 'test' -> AND `TABLE_NAME` = 'viewtheauto_incrementValueForATableDemo';
这将产生以下输出-
+------------------------+ | NextAutoIncrementValue | +------------------------+ | 6 | +------------------------+ 1 row in set (0.00 sec)