I create a table from a junction between 3 tables.
The issue that I'm facing is that this table becomes outdated once a new modification affects once of the original table used to supply its data.
How should solve this problem? Is a trigger the only solution?
You have several options:
Live with the outdated data and periodically (say once a day or once an hour) update the table.
Use a trigger to update the associated values.
Use a view.
Use an indexed view if possible (see here).
The last option is a way to define a view that SQL Server updates automatically -- what is called a materialized view in other databases. That said, not all queries are supported in materialized views.
"There are two problems in computer science. Cache coherency, and naming things." Tables derived from other tables exemplify both.
SQL Server supports materialized views, if you actually need them. Before you decide you do, do what every good carpenter does: measure twice and cut once. That is, write an ordinary view, and see how fast it is. If it's too slow -- however defined -- find out why. Most of the time, slow views can be sped up using indexes. If you have 100s of millions of rows, you might convert the view to a function that takes an argument applied to the WHERE clause, making it a "parameterized" view.
Can any one explain in detail with an example if possible, why AFTER triggers are not supported for views in SQL Server?
I know we use AFTER triggers after insert, update or delete on a table, then why not on a view, too?
Because it is only possible to do something instead of an DDL statement on a view.
Views themselves don't contain data, so it is not possible to do something before or after a change since it can't be sure how one change on the view affects the data below it.
Example:
If you have a view resulting in one row, and you update that row, that row might disappear, 'another' row might pop up, and there is no way to tell the relation between the first and the last.
I know that deleting a row from view will delete that row from base table also. But I think it will be more surprised to know that how does deleting a row from base table affect on view?
Deleting a row in a table will affect the results from regular views.
Views are not executed when they are created. They are executed when they are referenced. Each time you reference the view in the query, it is run again. So, any updates to the data in the underlying tables will affect the view.
This does not apply to materialized views, which are executed when they are created. To reflect changes in the underlying data, you need to refresh a materialized view.
I have a question about views. Consider we have a view that I insert a record in it's base table. Does my view update after this insert or I should do a SELECT to update?
I think my question obvious - is view just a SELECT or it's result save in database and if it's base table, so when it's updated it then becomes update?
Normal Views are not persisted. If an updateable view is inserted into then selecting from the view (or the affected underlying tables) will show your results.
Not entirely sure what problem you are trying to solve. Views (non-indexed) suffice for most applications.
Have a look at Indexed Views: Improving Performance with SQL Server 2008 Indexed Views:
In the case of a nonindexed view, the portions of the view necessary
to solve the query are materialized at run time. Any computations such
as joins or aggregations are done during query execution for each
query referencing the view. After a unique clustered index is
created on the view, the view's result set is materialized immediately
and persisted in physical storage in the database, saving the overhead
of performing this costly operation at execution time.
The typical use of an indexed view is when you have expensive aggregations to perform.
Think of a view as a select statement. Instead of having to write out the entire select statement, you just select the view and it runs that select statement for you. So yes, anything you do to the underlying tables will automatically be visible in the view.
Can you update a view in a database?
If so, how?
If not, why not?
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.
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.
Yes, they are updatable but not always. Views can be updated under followings:
If the view consists of the primary key of the table based on which the view has been created.
If the view is defined based on one and only one table.
If the view has not been defined using groups and aggregate functions.
If the view does not have any distinct clause in its definition.
If the view that is supposed to be updated is based on another view, the later should be updatable.
If the definition of the view does not have any sub queries.
PostgreSQL has RULEs to create updatable VIEWs. Check the examples in the manual to see how to use them.
Ps. In PostgreSQL a VIEW is a RULE, a select rule.
In the past it wasn't possible to update any views. The main purpose of a view is to look at data, hence the name. It could also have been called a stored query.
Today, many database engines support to update views. It's bound to restrictions, some updates are virtually impossible (eg. calculated columns, group by etc).
There are two approaches:
INSTEAD OF trigger, which basically shifts the problem to the user. You write some procedural code that does the job. Certainly, no guarantees is made about correctness, consistency, etc. From RDBMS engine perspective a trigger that deletes everything from the base tables, no matter what update is made in the view, is perfectly fine.
Much more ambitious is view updates handled exclusively by RDBMS engine. Not much progress is made here: to put it mildly, if you have some good ideas there, then you can roll out PhD thesis. In practice, your favorite RDBMS might allow some limiting ad-hock view updates; check the manual:-)
Yes you can, but have a look at CREATE VIEW (Transact-SQL) and see the section Updatable Views
http://msdn.microsoft.com/en-us/library/ms187956.aspx
See Remarks\updateable view
Yes they are - the syntax is the same as updating a table
Update MyView
Set Col1 = "Testing"
Where Col2 = 3
Go
There a few conditions to creating an View that can be updated. They can be found here
EDIT:
I must add that is based on MS SQL
When a view is created in SQL Server, metadata for the referenced table columns (column name and ordinal position) is persisted in the database. Any change to the referenced base table(s) (column re-ordering, new column addition, etc) will not be reflected in the view until the view is either:
•Altered with an ALTER VIEW statement
•Recreated with DROP VIEW/CREATE VIEW statements
•Refreshed using system stored procedure sp_refreshview
Yes, using an INSTEAD OF trigger.
We generally don't update a view. A view is written to fetch data from the various tables based on joins and where conditions put.
View is just a logic put in place which gives the desired data set on invoking it.
But not sure on what scenario one needs to update a view.