Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Views automatically updated

Tags:

sql

view

If I JOIN or CROSS APPLY two tables and create a VIEW, will the view automatically gets updated when I update either of the two tables or I add records to either of them?

Will these new records show up in the VIEW?

like image 382
Hammad Khan Avatar asked Oct 13 '11 14:10

Hammad Khan


People also ask

Can views be updated?

yes it is possible to insert,Update and delete using views. Mostly it is used to show limited data to user not whole table. View is nothing but an interface between Original table and user. yes it is possible to insert,Update and delete using views.

Will view be updated when table is updated?

Yes , View gets updated. Please note that View is a logical statement and the data in view is not stored anywhere, the data is returned from database every time we select data from view.

Do SQL views need to be refreshed?

Views need to be refreshed if the underlying tables change at all. That can change the datatypes of the view's columns or rearrange its indexes.


2 Answers

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

I think Microsoft sums up what a View is quite clearly:

A view can be thought of as either a virtual table or a stored query.

http://msdn.microsoft.com/en-us/library/aa214068%28v=sql.80%29.aspx

Views are not automatically cached.

When you SELECT from a view, the database has to run the query stored in the view to get the result set to use in your statement

The data you 'see' in a view, is not actually stored anywhere, and is generated from the tables on the fly.

Because of this be careful running views which are very complex. Always take into account that the view will have to be executed before its result set is accessed.

like image 117
Curtis Avatar answered Oct 07 '22 12:10

Curtis


A view is basically a stored query, it holds no data so no, it won't get updated when the tables it's built on are. However as soon as you reference the view the query it's based on will run, so you will see the changes made to the base tables.

like image 42
OTTA Avatar answered Oct 07 '22 12:10

OTTA