将整个数据库的空字符串更新为 NULL
我正在执行一些数据库清理,并注意到有很多列在各个列中同时具有空字符串和 NULL 值。
是否可以编写一条 SQL 语句来将数据库中每个表的每一列的空字符串更新为 NULL,除了不允许 NULL 的列之外?
我查看了 information_schema.COLUMNS
表并认为这可能是起点。
I'm performing some database clean up and have noticed that there are a lot of columns that have both empty strings and NULL values in various columns.
Is it possible to write an SQL statement to update the empty strings to NULL for each column of each table in my database, except for the ones that do not allow NULL's?
I've looked at the information_schema.COLUMNS
table and think that this might be the place to start.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用一条简单的 SQL 语句不可能做到这一点。
但您可以对每一列使用一个语句来完成此操作。
或者,如果您想清空也有空格的项目:
It's not possible to do this with one simple SQL statement.
But you can do it using one statement for each column.
or, if you want to null out the items that also have whitespace:
我认为这在 MySQL 中是不可能的,但使用您选择的脚本语言当然是可能的。
SHOW TABLES
DESC TABLE
、SHOW CREATE TABLE 或 SELECT * FROM information_schema.COLUMNS
,选择您更愿意解析的那个准备花一些时间等待:)
I don't think it's possible within MySQL but certainly with a script language of your choice.
SHOW TABLES
DESC TABLE
,SHOW CREATE TABLE
orSELECT * FROM information_schema.COLUMNS
, take the one you rather parsePrepare to spend some time waiting :)
我想出了如何使用存储过程来做到这一点。下次我肯定会考虑使用脚本语言。
I figured out how to do this using a stored procedure. I'd definitely look at using a scripting language next time.