更新 MySQL 数据库中所有表的 AUTO_INCRMENT 值

发布于 2024-09-16 22:56:20 字数 363 浏览 6 评论 0原文

可以通过

ALTER TABLE some_table AUTO_INCRMENT = 1000

设置/重置MySQL表的AUTO_INCRMENT值,但是我需要在其上设置AUTO_INCRMENT现有值(用于修复 MM 复制),例如:

ALTER TABLE some_table SET AUTO_INCRMENT = AUTO_INCRMENT + 1 ,这不起作用

实际上,我想对数据库中的所有表运行此查询。但实际上这并不是很重要。

除了手动运行查询之外,我找不到解决此问题的方法。请您提出一些建议或给我指出一些想法好吗?

谢谢

It is possbile set/reset the AUTO_INCREMENT value of a MySQL table via

ALTER TABLE some_table AUTO_INCREMENT = 1000

However I need to set the AUTO_INCREMENTupon its existing value (to fix M-M replication), something like:

ALTER TABLE some_table SET AUTO_INCREMENT = AUTO_INCREMENT + 1 which is not working

Well actually, I would like to run this query for all tables within a database. But actually this is not very crucial.

I could not find out a way to deal with this problem, except running the queries manually. Will you please suggest something or point me out to some ideas.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

话少心凉 2024-09-23 22:56:20

使用:

ALTER TABLE some_table AUTO_INCREMENT = 0

...将根据 auto_increment 列中现有的最高值将 auto_increment 值重置为下一个值。

要在所有表上运行此命令,您需要使用 MySQL 的动态 SQL 语法称为“PreparedStatements”,因为您无法将 ALTER TABLE 语句的表名作为变量提供。您必须循环遍历以下输出:

SELECT t.table_name
  FROM INFORMATION_SCHEMA.TABLES t
 WHERE t.table_schema = 'your_database_name'

...为每个表运行上面的 ALTER TABLE 语句。

Using:

ALTER TABLE some_table AUTO_INCREMENT = 0

...will reset the auto_increment value to be the next value based on the highest existing value in the auto_increment column.

To run this over all the tables, you'll need to use MySQL's dynamic SQL syntax called PreparedStatements because you can't supply the table name for an ALTER TABLE statement as a variable. You'll have to loop over the output from:

SELECT t.table_name
  FROM INFORMATION_SCHEMA.TABLES t
 WHERE t.table_schema = 'your_database_name'

...running the ALTER TABLE statement above for each table.

浪菊怪哟 2024-09-23 22:56:20
set @db = 'your_db_name';

SELECT concat('ALTER TABLE ', @db, '.', TABLE_NAME, ' AUTO_INCREMENT = 0;') 
FROM information_schema.TABLES WHERE TABLE_SCHEMA = @db AND TABLE_TYPE = 'BASE TABLE'

然后复制粘贴并运行得到的输出。

set @db = 'your_db_name';

SELECT concat('ALTER TABLE ', @db, '.', TABLE_NAME, ' AUTO_INCREMENT = 0;') 
FROM information_schema.TABLES WHERE TABLE_SCHEMA = @db AND TABLE_TYPE = 'BASE TABLE'

Then copy-paste and run the output you get.

潜移默化 2024-09-23 22:56:20

在下面的说明中,您需要将 [括号] 中的所有内容替换为正确的值。尝试之前进行备份。

如果您可以通过命令行以 root 身份登录 mysql,那么您可以执行以下操作来重置所有表上的 auto_increment,首先我们将构建我们想要运行的查询:

进行数据库备份:

mysqldump -u [uname] -p [dbname] | gzip -9 > [backupfile.sql.gz]

登录:

mysql -u root -p

将 group_concat_max_length 设置为更高的值,这样我们的查询列表就不会被截断:

SET group_concat_max_len=100000;

使用以下命令创建我们的查询列表:

SELECT GROUP_CONCAT(CONCAT("ALTER TABLE ", table_name, " AUTO_INCREMENT = 0") SEPARATOR ";") FROM information_schema.tables WHERE table_schema = "[DATABASENAME]";

然后您将收到一长串 mysql 查询,后跟一堆破折号。将查询字符串复制到剪贴板,它看起来类似于:

ALTER table1 AUTO_INCREMENT = 0;ALTER table2 AUTO_INCREMENT = 0;...continued...

更改为要运行命令的数据库:

USE [DATABASENAME];

然后粘贴剪贴板上的字符串并按 Enter 运行它。这应该对数据库中的每个表运行更改。

搞砸了吗?从您的备份恢复,请务必在运行以下命令之前注销 mysql(只需输入 exit; 即可),

gzip -d < [backupfile.sql.gz] | mysql -u [uname] -p [dbname]

我不会对您使用任何这些命令造成的任何损害承担责任,请使用风险由您自行承担。

In the below instructions you will need to replace everything that is in [brackets] with your correct value. BACKUP BEFORE ATTEMPTING.

If you can login to mysql as root through the command line then you could do the following to reset the auto_increment on all tables, first we will construct our queries which we want to run:

Make a database backup:

mysqldump -u [uname] -p [dbname] | gzip -9 > [backupfile.sql.gz]

Login:

mysql -u root -p

Set the group_concat_max_length to a higher value so our list of queries doesn't get truncated:

SET group_concat_max_len=100000;

Create our list of queries by using the following:

SELECT GROUP_CONCAT(CONCAT("ALTER TABLE ", table_name, " AUTO_INCREMENT = 0") SEPARATOR ";") FROM information_schema.tables WHERE table_schema = "[DATABASENAME]";

Then you will receive a long string of mysql queries followed by a bunch of dashes. Copy the string of queries to your clipboard, it will look something similar to:

ALTER table1 AUTO_INCREMENT = 0;ALTER table2 AUTO_INCREMENT = 0;...continued...

Change to the database you would like to run the command on:

USE [DATABASENAME];

Then paste the string that is on your clipboard and hit enter to run it. This should run the alter on every table in your database.

Messed up? Restore from your backup, be sure to logout of mysql before running the following (just type exit; to do so)

gzip -d < [backupfile.sql.gz] | mysql -u [uname] -p [dbname]

I will not take responsibility for any damage cause by your use of any of these commands, use at your own risk.

十雾 2024-09-23 22:56:20

我在 github 上找到了这个要点,它对我来说就像一个魅力: https://gist.github.com /abhinavlal/4571478

命令:

mysql -Nsr -e "SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t WHERE t.table_schema = 'DB_NAME'" | xargs -I {} mysql DB_NAME -e "ALTER TABLE {} AUTO_INCREMENT = 1;"

如果您的数据库需要密码,不幸的是您必须将其放入命令中才能使其工作。一种解决方法(仍然不是很好,但有效)是将密码放在安全文件中。您可以随时删除该文件,这样密码就不会保留在您的命令历史记录中:

 ... | xargs -I {} mysql -u root -p`cat /path/to/pw.txt` DB_NAME -e...

I found this gist on github and it worked like a charm for me: https://gist.github.com/abhinavlal/4571478

The command:

mysql -Nsr -e "SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t WHERE t.table_schema = 'DB_NAME'" | xargs -I {} mysql DB_NAME -e "ALTER TABLE {} AUTO_INCREMENT = 1;"

If your DB requires a password, you unfortunately have to put that in the command for it to work. One work-around (still not great but works) is to put the password in a secure file. You can always delete the file after so the password doesn't stay in your command history:

 ... | xargs -I {} mysql -u root -p`cat /path/to/pw.txt` DB_NAME -e...
乖乖兔^ω^ 2024-09-23 22:56:20

假设您必须通过修改自动增量列而不是表中分解 N:M 关系的外键来解决此问题,并且您可以预测正确的值是什么,请尝试使用相关列不存在的临时表自动增量,然后将其映射回原始表的位置,然后将列类型更改为自动增量,或者截断原始表并从临时表加载数据。

Assuming that you must fix this by amending the auto-increment column rather than the foreign keys in the table decomposing the N:M relationship, and that you can predict what the right values are, try using a temporary table where the relevant column is not auto-increment, then map this back in place of the original table and change the column type to auto-increment afterwards, or truncate the original table and load the data from the temp table.

路弥 2024-09-23 22:56:20

我编写了下面的过程,更改数据库名称并执行该过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `setAutoIncrement`()
BEGIN
DECLARE done int default false;
    DECLARE table_name CHAR(255);
DECLARE cur1 cursor for SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t 
        WHERE t.table_schema = "buzzer_verifone";

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    open cur1;

    myloop: loop
        fetch cur1 into table_name;
        if done then
            leave myloop;
        end if;
        set @sql = CONCAT('ALTER TABLE ',table_name, ' AUTO_INCREMENT = 1');
        prepare stmt from @sql;
        execute stmt;
        drop prepare stmt;
    end loop;

    close cur1;
END

使用下面的行执行上面的过程

Call setAutoIncrement();

I have written below procedure change the database name and execute the procedure

CREATE DEFINER=`root`@`localhost` PROCEDURE `setAutoIncrement`()
BEGIN
DECLARE done int default false;
    DECLARE table_name CHAR(255);
DECLARE cur1 cursor for SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t 
        WHERE t.table_schema = "buzzer_verifone";

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    open cur1;

    myloop: loop
        fetch cur1 into table_name;
        if done then
            leave myloop;
        end if;
        set @sql = CONCAT('ALTER TABLE ',table_name, ' AUTO_INCREMENT = 1');
        prepare stmt from @sql;
        execute stmt;
        drop prepare stmt;
    end loop;

    close cur1;
END

Execute the procedure above using below line

Call setAutoIncrement();
不必你懂 2024-09-23 22:56:20

在 MySQL 数据库中更新/重置 AUTO_INCRMENT 的最快解决方案

确保 AUTO_INCRMENT 列尚未在另一个表上用作 FOREIGN_KEY

首先,将 AUTO_INCRMENT COLUMN 删除为:

ALTER TABLE table_name DROP column_name

示例:ALTER TABLE payment DROP payment_id

然后重新添加该列,并将其移动为表中的第一列

ALTER TABLE table_name ADD column_name DATATYPE AUTO_INCREMENT PRIMARY KEY FIRST

示例:<代码>更改表付款添加 payment_id INT AUTO_INCRMENT PRIMARY KEY FIRST

The Quickest solution to Update/Reset AUTO_INCREMENT in MySQL Database

Ensure that the AUTO_INCREMENT column has not been used as a FOREIGN_KEY on another table.

Firstly, Drop the AUTO_INCREMENT COLUMN as:

ALTER TABLE table_name DROP column_name

Example: ALTER TABLE payments DROP payment_id

Then afterward re-add the column, and move it as the first column in the table

ALTER TABLE table_name ADD column_name DATATYPE AUTO_INCREMENT PRIMARY KEY FIRST

Example: ALTER TABLE payments ADD payment_id INT AUTO_INCREMENT PRIMARY KEY FIRST

夏末染殇 2024-09-23 22:56:20

重置mysql表自动增量非常简单,我们可以通过单个查询来完成,请参阅此http://webobserve.blogspot.com/2011/02/reset-mysql-table-autoincrement.html

Reset mysql table auto increment was very easy, we can do it with single query, please see this http://webobserve.blogspot.com/2011/02/reset-mysql-table-autoincrement.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文