将列名转换为小写的 MYSQL 脚本

发布于 2024-08-08 15:10:25 字数 156 浏览 4 评论 0原文

我正在寻找一个 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 技术交流群。

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

发布评论

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

评论(4

错爱 2024-08-15 15:10:25

如果其他人想要下面是已完成查询的示例,请在使用之前进行测试......

按要求编辑完整的解决方案

SELECT CONCAT(
'ALTER TABLE ', table_name, 
' CHANGE ', column_name, ' ', 
LOWER(column_name), ' ', column_type, ' ', extra,
CASE WHEN IS_NULLABLE = 'YES' THEN  ' NULL' ELSE ' NOT NULL' END, ';') AS line
FROM information_schema.columns
WHERE table_schema = '<DBNAME>' 
AND data_type IN ('char', 'varchar','INT', 'TINYINT', 'datetime','text','double','decimal')
ORDER BY line;

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

SELECT CONCAT(
'ALTER TABLE ', table_name, 
' CHANGE ', column_name, ' ', 
LOWER(column_name), ' ', column_type, ' ', extra,
CASE WHEN IS_NULLABLE = 'YES' THEN  ' NULL' ELSE ' NOT NULL' END, ';') AS line
FROM information_schema.columns
WHERE table_schema = '<DBNAME>' 
AND data_type IN ('char', 'varchar','INT', 'TINYINT', 'datetime','text','double','decimal')
ORDER BY line;

HTH somebody in the future...
BTW views are also scripted here so you may need to take them out of your final SQL code

灵芸 2024-08-15 15:10:25

您可以通过将以下正则表达式应用于 SQL 转储(例如,mysqldump 生成的转储)来将所有表名和列名重命名为小写:

s/`\(\w\+\)`/\L&/g

这有效,因为所有表名和列名都用 ` `(反引号)。最好仅在模式上执行此操作,与数据分开(仅使用表结构,然后进行插入)。

要在 Vim 中执行此操作,请打开 SQL 转储并输入以下命令:

:%s/`\(\w\+\)`/\L&/g

或者使用 sed 从命令行执行此操作:

sed 's/`\(\w\+\)`/\L&/g' input.sql > output.sql

如果需要重复执行此操作,请将表达式存储在文本文件中,然后调用它如下:

sed -f regex.txt input.sql > output.sql

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):

s/`\(\w\+\)`/\L&/g

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:

:%s/`\(\w\+\)`/\L&/g

Or do it from the command line using sed:

sed 's/`\(\w\+\)`/\L&/g' input.sql > output.sql

If you need to do it repeatedly, store the expression in a text file, and invoke it as follows:

sed -f regex.txt input.sql > output.sql
蔚蓝源自深海 2024-08-15 15:10:25

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.

静水深流 2024-08-15 15:10:25

您可以通过构建脚本来解决此任务,从该语句的输出开始:

SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'dbname';
ORDER BY table_name

有关此功能的详细信息可以在此处找到“MYSQL::INFORMATION_SCHEMA COLUMNS 表"

然后您可以使用 ALTER TABLE .. CHANGE 功能来更改列的名称,

例如

ALTER TABLE mytable CHANGE old_name new_name varchar(5);

另请参阅“MYSQL::ALTER TABLE 语法

不同的数据类型有不同的要求,因此您需要 UNION:

SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(character_maximum_length)||');' AS Line
    FROM information_schema.columns
    WHERE table_schema = dbname and datatype in ( 'CHAR', 'VARCHAR' )
    ORDER BY table_name
    UNION
SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(numeric_precision)||');' AS Line
    FROM information_schema.columns
    WHERE table_schema = dbname and datatype in ( 'INTEGER' )
    ORDER BY table_name
    UNION
SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(numeric_precision)||','||CHAR(numeric_scale)|');' AS Line
    FROM information_schema.columns
    WHERE table_schema = dbname and datatype in ( 'FLOAT' )
    ORDER BY table_name
    UNION
SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||');' AS Line
    FROM information_schema.columns
    WHERE table_schema = dbname and datatype in ( 'DATE' )
    ORDER BY table_name

You can solve this task by building a script, starting with the output from this statement:

SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'dbname';
ORDER BY table_name

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.

ALTER TABLE mytable CHANGE old_name new_name varchar(5);

See also "MYSQL::ALTER TABLE Syntax"

Different datatype have different requirements so you need the UNIONs:

SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(character_maximum_length)||');' AS Line
    FROM information_schema.columns
    WHERE table_schema = dbname and datatype in ( 'CHAR', 'VARCHAR' )
    ORDER BY table_name
    UNION
SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(numeric_precision)||');' AS Line
    FROM information_schema.columns
    WHERE table_schema = dbname and datatype in ( 'INTEGER' )
    ORDER BY table_name
    UNION
SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||'('||CHAR(numeric_precision)||','||CHAR(numeric_scale)|');' AS Line
    FROM information_schema.columns
    WHERE table_schema = dbname and datatype in ( 'FLOAT' )
    ORDER BY table_name
    UNION
SELECT 'ALTER TABLE '||table_name||' CHANGE '|| column_name||' '||lower(column_name)||' '||datatype||');' AS Line
    FROM information_schema.columns
    WHERE table_schema = dbname and datatype in ( 'DATE' )
    ORDER BY table_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文