mysql auto_increment的查询与修改

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 ;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值