1.查询
很多情况下,我们要提前用到当前某个表的auto_increment自增id,可以通过执行sql语句来查询到这个id值。
show table status where name=’表名’
或者
show table status like ‘表名’
然后从查询到的结果集中获得auto_increment的值
代码实例:
<?php
mysql_connect("localhost","root",'');
mysql_select_db("test");
$sql="show table status where name='members'";
$query=mysql_query($sql);
$res=mysql_fetch_array($query);
echo "当前表的自增id为:".$res['Auto_increment'];
?>
另外一种方法是直接用information_schema.tables来查询具体的值,需要注意的是这种查询的结果可以所有的数据库,不单单是当前数据库,所以需要通过table_schema来指定数据库。
select auto_increment from information_schema.tables where table_name=TABLENAME and table_schema=DB;2.修改
ALTER TABLE tableName auto_increment=number ;
查询与修改MySQL自增ID
本文介绍如何使用SQL查询MySQL表中当前的自增ID值,并提供了两种查询方法及一个修改自增ID的示例。第一种方法是通过执行特定的SQL语句并获取查询结果集中的auto_increment值;第二种方法则是直接从information_schema.tables表中进行查询。此外,还介绍了如何通过ALTER TABLE语句来修改自增ID的起始值。
2508

被折叠的 条评论
为什么被折叠?



