Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a view in the database updatable?

Can you update a view in a database? If so, how? If not, why not?

like image 911
Avadhesh Avatar asked Sep 23 '10 11:09

Avadhesh


People also ask

Is a view updatable in SQL?

SQL Updating a ViewA view can be updated with the CREATE OR REPLACE VIEW statement.

Can you update a DB view?

Yes, possible to insert,update and delete to view. view is a virtual table. Same Perform as insert,update,delete query.. A view can be defined as a virtual table or a stored query and the data accessible through a view is not stored in the database as a distinct object.

Why a view is not updatable?

It means that the server always knows whether a view is updatable. If a view is not updatable, statements such UPDATE , DELETE , and INSERT are illegal and are rejected. (Even if a view is updatable, it might not be possible to insert into it, as described elsewhere in this section.)

Are views automatically updated?

Yes, they are updated, every time you use them.


2 Answers

The actual answer is "it depends", there are no absolutes.

The basic criteria is it has to be an updateable view in the opinion of the database engine, that is to say can the engine uniquely identify the row(s) to be updated and secondly are the fields updateable. If your view has a calculated field or represents the product of a parent/child join then the default answer is probably no.

However its also possible to cheat... in MS SQL Server and Oracle (to take just two examples) you can have triggers that fire when you attempt to insert or update a view such that you can make something that the server doesn't think updateable into something that is - usually because you have knowledge that the server can't easily infer from the schema.

like image 159
Murph Avatar answered Oct 04 '22 08:10

Murph


The correct answer is "it depends". You can't update an aggregate column in a view for example. For Oracle views you can Google for "updatable join view" for some examples of when you can and cannot update a view.

like image 20
Lord Peter Avatar answered Oct 04 '22 07:10

Lord Peter