简体   繁体   English

如何更新数据库中所有表的所有记录?

[英]How to update all records of all tables in database?

I have a table with a lot of records (could be more than 500 000 or 1 000 000).我有一张有很多记录的表(可能超过 500 000 或 1 000 000)。 I want to update some common columns with the same field name in all tables throughout the database.我想在整个数据库的所有表中更新一些具有相同字段名称的常见列。

I know the traditional way to write separate queries to individual tables but not one query to update all records of all tables.我知道将单独的查询写入单个表的传统方法,而不是一个查询来更新所有表的所有记录。

What is the most efficient way to do this in SQL, without using some dialect-specific features, so it works everywhere (Oracle, MSSQL, MySQL, Postgres etc.)?在 SQL 中执行此操作的最有效方法是什么,而不使用某些特定于方言的功能,因此它可以在任何地方使用(Oracle、MSSQL、MySQL、Postgres 等)?

ADDITIONAL INFO: There are no calculated fields.附加信息:没有计算字段。 There are indexes.有索引。 Used generated SQL statements that update the table row by row.使用生成的 SQL 语句逐行更新表。

(This sounds like the classic case for normalizing that 'column'.) (这听起来像是规范化“列”的经典案例。)

Anyway... No. There is no single query to locate that column across all tables, then perform an UPDATE on each of the tables.无论如何... 不。没有单个查询可以在所有表​​中定位该列,然后对每个表执行UPDATE

In MySQL, you can use the table information_schema.COLUMNS to locate all the tables containing a particular named column.在 MySQL 中,您可以使用表information_schema.COLUMNS来定位包含特定命名列的所有表。 With such a SELECT , you can generate (using CONCAT() , etc) the desired UPDATE statements.使用这样的SELECT ,您可以生成(使用CONCAT()等)所需的UPDATE语句。 But then, you need to manually run them (via copy and paste).但是,您需要手动运行它们(通过复制和粘贴)。

Granted, you could probably write a Stored Procedure to wrap that into a single call, but that is too risky.当然,您可能会编写一个存储过程将其包装到单个调用中,但这太冒险了。 What if some other table has the same column name, but should not be updated?如果其他一些表具有相同的列名,但不应更新怎么办?

As for common code across multiple vendors -- forget it!至于跨多个供应商的通用代码——算了! Virtually every syntax needs some amount of tweaking.几乎每个语法都需要进行一些调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM