更新 MySQL 数据库中所有表的 AUTO_INCRMENT 值
可以通过
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_INCREMENT
upon 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用:
...将根据 auto_increment 列中现有的最高值将 auto_increment 值重置为下一个值。
要在所有表上运行此命令,您需要使用 MySQL 的动态 SQL 语法称为“PreparedStatements”,因为您无法将 ALTER TABLE 语句的表名作为变量提供。您必须循环遍历以下输出:
...为每个表运行上面的 ALTER TABLE 语句。
Using:
...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:
...running the ALTER TABLE statement above for each table.
然后复制粘贴并运行得到的输出。
Then copy-paste and run the output you get.
在下面的说明中,您需要将 [括号] 中的所有内容替换为正确的值。尝试之前进行备份。
如果您可以通过命令行以 root 身份登录 mysql,那么您可以执行以下操作来重置所有表上的 auto_increment,首先我们将构建我们想要运行的查询:
进行数据库备份:
登录:
将 group_concat_max_length 设置为更高的值,这样我们的查询列表就不会被截断:
使用以下命令创建我们的查询列表:
然后您将收到一长串 mysql 查询,后跟一堆破折号。将查询字符串复制到剪贴板,它看起来类似于:
更改为要运行命令的数据库:
然后粘贴剪贴板上的字符串并按 Enter 运行它。这应该对数据库中的每个表运行更改。
搞砸了吗?从您的备份恢复,请务必在运行以下命令之前注销 mysql(只需输入
exit;
即可),我不会对您使用任何这些命令造成的任何损害承担责任,请使用风险由您自行承担。
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:
Login:
Set the group_concat_max_length to a higher value so our list of queries doesn't get truncated:
Create our list of queries by using the following:
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:
Change to the database you would like to run the command on:
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)I will not take responsibility for any damage cause by your use of any of these commands, use at your own risk.
我在 github 上找到了这个要点,它对我来说就像一个魅力: https://gist.github.com /abhinavlal/4571478
命令:
如果您的数据库需要密码,不幸的是您必须将其放入命令中才能使其工作。一种解决方法(仍然不是很好,但有效)是将密码放在安全文件中。您可以随时删除该文件,这样密码就不会保留在您的命令历史记录中:
I found this gist on github and it worked like a charm for me: https://gist.github.com/abhinavlal/4571478
The command:
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:
假设您必须通过修改自动增量列而不是表中分解 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.
我编写了下面的过程,更改数据库名称并执行该过程
使用下面的行执行上面的过程
I have written below procedure change the database name and execute the procedure
Execute the procedure above using below line
在 MySQL 数据库中更新/重置 AUTO_INCRMENT 的最快解决方案
确保
AUTO_INCRMENT
列尚未在另一个表上用作FOREIGN_KEY
。首先,将
AUTO_INCRMENT
COLUMN 删除为:示例:
ALTER TABLE payment DROP payment_id
然后重新添加该列,并将其移动为表中的第一列
示例:<代码>更改表付款添加 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 aFOREIGN_KEY
on another table.Firstly, Drop the
AUTO_INCREMENT
COLUMN as:Example:
ALTER TABLE payments DROP payment_id
Then afterward re-add the column, and move it as the first column in the table
Example:
ALTER TABLE payments ADD payment_id INT AUTO_INCREMENT PRIMARY KEY FIRST
重置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.