更新 SELECT 语句的结果
Oracle 允许您更新 SELECT 语句的结果。
UPDATE (<SELECT Statement>)
SET <column_name> = <value>
WHERE <column_name> <condition> <value>;
我想这可以用于根据另一个表中匹配行的值更新一个表中的列。
这个功能如何称呼,它是否可以有效地用于大型更新,当 SELECT 连接多个表时它是否有效?如果可以,如何?
Oracle lets you update the results of a SELECT statement.
UPDATE (<SELECT Statement>)
SET <column_name> = <value>
WHERE <column_name> <condition> <value>;
I suppose that this could be used for updating columns in one table based on the value of a matching row in another table.
How is this feature called, can it efficiently be used for large updates, does it work when the SELECT joins multiple tables, and if so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的评论,我认为这是标准的 Sql...:(
对于 Oracle,您可以在表上编写更新,在其中使用连接检索信息,例如:
对于 Sql Server,它是:
如果有人知道可行的方法关于 Oracle、Sql Server 和 MySql 我很感兴趣。
Thanks for comments, I thought this was standard Sql... :(
For Oracle you can write an update on a table where you retrieve information with a join like:
For Sql Server, it's:
If anyone knows a way to do this that works on Oracle, Sql Server and MySql I'd be interested.
我还没有看到这个的正式名称。 Oracle SQL Reference 只是指更新子查询。 我倾向于将其视为“视图更新”的一种形式,子查询位于内联视图中。
是的,当多个表连接时它可以工作,但要遵守视图更新的规则。 这意味着只能更新视图的一个基表,并且该表必须在视图中“键保留”:即它的行只能在视图中出现一次。 这要求视图(子查询)中的任何其他表都通过要更新的表的外键约束进行引用。
一些例子可能会有所帮助。 使用标准的 Oracle EMP 和 DEPT 表,将 EMP.EMPNO 定义为 EMP 的主键,并将 EMP.DEPTNO 定义为 DEPT.DEPTNO 的外键,则允许此更新:
不是:
但这 性能:优化器将(必须)在解析期间识别要更新的基表,并且与其他表的联接将被忽略,因为它们对要执行的更新没有任何影响 - 正如此 AUTOTRACE 输出所示:(
请注意,表即使 DEPT.DNAME 出现在子查询中,DEPT 也永远不会被访问。
I haven't seen a formal name for this. The Oracle SQL Reference just refers to updating a subquery. I tend to think of it as a form of "view updating", with the subquery being in in-line view.
Yes, it works when a number of tables are joined, but subject to the rules of view updating. This means that only one of the view's base tables can be updated, and this table must be "key-preserved" in the view: i.e. its rows should only be able to appear once in the view. This requires that any other tables in the view (subquery) are referenced via foreign key constraints on the table to be updated.
Some examples may help. Using the standard Oracle EMP and DEPT tables, with EMP.EMPNO being defined as the primary key of EMP, and EMP.DEPTNO being defined as a foreign key to DEPT.DEPTNO, then this update is allowed:
But this is not:
As for performance: the optimiser will (must) identify the base table to be updated during parsing, and joins to other table will be ignored since they do not have any bearing on the update to be performed - as this AUTOTRACE output shows:
(Note that table DEPT is never accessed even though DEPT.DNAME appears in the subquery).
您提到的表格没有具体名称 AFAIK。 只是更新 select 语句的结果。
还有另一种形式称为相关更新(具有单列或多列更新)
多列形式
还有一种形式也可以从中返回值,称为使用返回子句更新
以及一些具体信息使用嵌套表进行更新。 最好至少检查这两页
Oracle ® 数据库 SQL 语言参考 SELECT
Oracle® 数据库 SQL 语言参考更新
The form you mention has no specific name AFAIK. Just updating the result of a select statement.
There is another form called Correlated update (with single or multicolumn update)
The multicolumn form
There is also a from which also returning of values called Update with returning clause
And some specifics for updates with nested tables. Best is to check at least this two pages
Oracle® Database SQL Language Reference SELECT
Oracle® Database SQL Language Reference UPDATE