如何在 mysql 中同时更改多个表?
我正在尝试更改多个表并将 username
VARCHAR 列的大小更改为 999,因为它当前的大小太小,现在事情搞砸了。我该怎么做?
我已尝试以下操作,它适用于一个表,但当尝试更新多个表名称时,它返回错误:
ALTER TABLE `TABLE_NAME` CHANGE `username` VARCHAR( 999 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL
I am trying to alter multiple tables and change the size of the username
VARCHAR column to 999 as its current size is too small and now things are screwed up. How can I do this?
I have tried the following and it worked for one table but when trying to update multiple table names it returned errors:
ALTER TABLE `TABLE_NAME` CHANGE `username` VARCHAR( 999 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法通过单个查询来完成此操作。您需要查询
information_schema
视图以获取要更改的表和列的列表。然后,您将使用生成的结果集创建ALTER
查询(在外部应用程序/脚本中或在 MySQL 中使用游标和准备好的语句)You can't do it with a single query. You need to query
information_schema
views to get the list of tables and columns to change. You will then use the resulting resultset to createALTER
queries (either in an external application/script or within MySQL using cursors and prepared statements)我发现执行此操作的唯一方法是通过外部文件。这是我的暗示:
然后我就可以轻松修改我想要的任何表格。
I found the only way to do this was via an external file. This is my implimentation :
I then was able to modify any table I wanted easily.
编写一个查询文件来更改所有表并执行该文件。
Write a query file to alter all tables and execute that file.