5

我正在创建一个视图来向用户显示他/她的数据,但我也希望用户能够在这些视图中的某些字段中进行更改。视图中所做的更改是否也反映在基表中?

另外,我能否更新由多个基表组成的视图?

4

1 回答 1

11

可更新和可插入视图中所述:

一些视图是可更新的。也就是说,您可以在诸如UPDATEDELETE、等语句中使用它们INSERT来更新基础表的内容。要使视图可更新,视图中的行与基础表中的行之间必须存在一对一的关系。还有某些其他构造使视图不可更新。更具体地说,如果视图包含以下任何内容,则它是不可更新的:

  • 聚合函数(SUM()MIN()MAX()COUNT()等)

  • DISTINCT

  • GROUP BY

  • HAVING

  • UNION或者UNION ALL

  • 选择列表中的子查询

  • 某些联接(请参阅本节后面的附加联接讨论)

  • FROM子句中的不可更新视图

  • 子句中的子查询WHERE引用子句中的FROM

  • Refers only to literal values (in this case, there is no underlying table to update)

  • Uses ALGORITHM = TEMPTABLE (use of a temporary table always makes a view nonupdatable)

  • Multiple references to any column of a base table.

[ deletia ]

It is sometimes possible for a multiple-table view to be updatable, assuming that it can be processed with the MERGE algorithm. For this to work, the view must use an inner join (not an outer join or a UNION). Also, only a single table in the view definition can be updated, so the SET clause must name only columns from one of the tables in the view. Views that use UNION ALL are not permitted even though they might be theoretically updatable, because the implementation uses temporary tables to process them.

于 2012-12-10T02:06:28.117 回答