将列名转换为小写的 MYSQL 脚本
我正在寻找一个 MYSQL 脚本来一次性将数据库中的所有列名转换为小写...
我继承了一个 MYSQL 数据库,该数据库有很多混合大小写的列名(150 个具有奇怪命名约定的表)并且我不想手动逐个表地检查每个表来执行此操作。
有人有这样的脚本吗?
谢谢
I am looking for a single MYSQL script to convert ALL column names in a database to lowercase in one go...
I have inherited a MYSQL database that has a lot of mixed case column names (150 tables with a strange naming convention) and I don't want to go through manually each table by table to do this.
Has anyone got such a script?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果其他人想要下面是已完成查询的示例,请在使用之前进行测试......
按要求编辑完整的解决方案
HTH 将来会有人...
BTW 视图也在这里编写脚本,因此您可能需要将它们从最终的 SQL 代码中删除
In case anybody else wants this below is an example of the completed query, please test before you use.....
EDIT COMPLETED SOLUTION AS REQUESTED
HTH somebody in the future...
BTW views are also scripted here so you may need to take them out of your final SQL code
您可以通过将以下正则表达式应用于 SQL 转储(例如,
mysqldump
生成的转储)来将所有表名和列名重命名为小写:这有效,因为所有表名和列名都用 ` `(反引号)。最好仅在模式上执行此操作,与数据分开(仅使用表结构,然后进行插入)。
要在 Vim 中执行此操作,请打开 SQL 转储并输入以下命令:
或者使用 sed 从命令行执行此操作:
如果需要重复执行此操作,请将表达式存储在文本文件中,然后调用它如下:
You can rename all table and column names to lowercase by applying the following regular expression to a SQL dump (for example, the dump generated by
mysqldump
):This works because all table and column names are wrapped by `` (backticks). It is better to do this on the schema only, separate from the data (work with table structures only and then do the inserts).
To do this in Vim, open the SQL dump and enter the following command:
Or do it from the command line using
sed
:If you need to do it repeatedly, store the expression in a text file, and invoke it as follows:
lepe提出的解决方案确实是唯一安全的方法。脚本方法太危险,容易导出或处理错误的数据定义。上面的所有示例脚本都遗漏了几种数据类型,因此它们并不完整。
我做了一个 sqldump,在表和列名称周围放置反引号,然后使用 Notepad++ 搜索 (`.*`) 并替换为 \L\1。这使得我的所有表名和列名都变成小写。
然后我备份数据库,清除所有表,然后执行 .sql 文件进行重建。我并不担心将结构与数据分开,因为我发现任何数据中都没有出现反引号符号。
就我而言,我需要将列名全部小写,因为我的开发环境自动将first_name 转换为First Name: 作为数据输入的字段标签。如果我将它们保留为大写字母(我继承的),它们将转换为名字,这不是我想要的,我必须更改所有字段标签。
The solution proposed by lepe is really the only safe way to go. The scripting methods are too dangerous, easy to export or process the wrong data definition. All of the example scripts above leave out several data types, so they are incomplete.
I did a sqldump which places backticks around the table and column names, then used Notepad++ to Search on (`.*`) and Replace With \L\1. That rendered all of my table and column names to lower case.
Then I backed up my database, wiped out all of the tables and then executed my .sql file to rebuild. I did not worry about doing structure separate from data as I found no occurances of the backtick symbol in any of my data.
In my case, I need my column names all lower case because my development environment automatically converts first_name to First Name: as my field label for data entry. If I left them as caps (which I inherited), they would convert to FIRST NAME which is not what I want, I'd have to alter all of my field labels.
您可以通过构建脚本来解决此任务,从该语句的输出开始:
有关此功能的详细信息可以在此处找到“MYSQL::INFORMATION_SCHEMA COLUMNS 表"
然后您可以使用 ALTER TABLE .. CHANGE 功能来更改列的名称,
例如
另请参阅“MYSQL::ALTER TABLE 语法”
不同的数据类型有不同的要求,因此您需要 UNION:
You can solve this task by building a script, starting with the output from this statement:
Details about this feature can be found here "MYSQL::The INFORMATION_SCHEMA COLUMNS Table"
Then you can use the ALTER TABLE .. CHANGE feature to change the name of the columns
e.g.
See also "MYSQL::ALTER TABLE Syntax"
Different datatype have different requirements so you need the UNIONs: