如何更改数据库中的所有表以使用AUTO_INCREMENT = 1 [英] How do I change all tables in my database to use AUTO_INCREMENT=1

查看:511
本文介绍了如何更改数据库中的所有表以使用AUTO_INCREMENT = 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中大约有30张表:

I have about 30 tables in my database:

table1 table2 table3 table4 table5

我希望所有表都使用AUTO_INCREMENT=1,如何修改表?

I want all tables to use AUTO_INCREMENT=1, how do I modify the tables?

这是其中一个表的示例DDL.我从未在任何表中定义AUTO_INCREMENT,默认情况下它正在获取值.

Here is the sample DDL of one of the tables. I have never defined AUTO_INCREMENT in any of the tables, it is fetching the value by default.

CREATE TABLE `amenities` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

推荐答案

要更改要用于新行的AUTO_INCREMENT计数器的值,请执行以下操作:

To change the value of the AUTO_INCREMENT counter to be used for new rows, do this:

ALTER TABLE `table_name` AUTO_INCREMENT = 1;

要更新所有31个表,可以使用以下php脚本:

To update all your 31 tables you can use this php script:

<?php
$tables = array('table1','table2','tableX'); //continue here
foreach($tables as $update)
{
     mysql_query("ALTER TABLE `".$update."` AUTO_INCREMENT = 1;");
}
?>

这篇关于如何更改数据库中的所有表以使用AUTO_INCREMENT = 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆