When indexed view updated - sql

I create a indexed view View1 on Table1 and Table2 and have instead of trigger and after trigger on Table1 that use of View1. It seem that data of View1 not change when I use this view on instead of trigger and after trigger. But I want to use View1 with new data when use this view on after trigger. How can I do it. It should be noted that I use with Noexpand hint when use indexed view (when don't use with noexpand hint in SQL Server 2008 R2 act this indexed view such as no indexed view).

The indexed view is updated after all triggers on the base tables are executed. You can see that in the query plan. There will be only one clustered index update on the view.
However, you can create INSTEAD OF triggers on the view and then make all your operations on the indexed view. However,there are some restrictions when updating/inserting/deleting through an updateable view. You can read about them here.
Note that views allow only INSTEAD OF triggers, not AFTER too.
The most important thing to know about updating indexed views is that any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.

Related

What's the difference between a view and a query?

In SSMS and MySQLWorkbench a view is the SQL that generates a result set. In Access those are called queries. Is a view the same as a query?
A view is essentially a stored/predefined query. Depending on the database in question, views may additionally have indexes defined against them, have referential integrity defined on them, and/or be materialized (pre-queried and persisted to disk).

How to update master table while updating materialized view

I have three sites,one site contians the employees table, while the other sites have materialized view of employees table .
This is how i created the materialized views on the other sites.
CREATE MATERIALIZED VIEW employeesMV
REFRESH FAST
FOR UPDATE
AS
SELECT * FROM manager.employees#managerlink;
so i just want to know how to update the master table employees after i make changes such as (insert or update) on the materialized view.
Thank you in advance.
By default, materialized view can't be updated. However, if you use FOR UPDATE clause, you can do it, but those changes aren't reflected in MV's source table. Moreover, as soon as you refresh the MV, changes you've made will be lost.
Advanced replication covers it, but Oracle says that it is deprecated in 12cR1.
There's a walkthrough on Vinayaga Consultancy's blog, Updatable Materialized View, based on Oracle 11.2 (source) and 10.2 (target database) so - have a look. It isn't that trivial at all.

How to update a view in oracle

I have create view without any problem in Oracle. But I can not update it.I want update view in oracle, please help. Is it possible anyway?
You can create an instead of trigger on the view, as described in the documentation:
A view presents the output of a query as a table. If you want to change a view as you would change a table, you must create INSTEAD OF triggers. Instead of changing the view, they change the underlying tables.
Once you have that trigger in place, you can update the view using the same syntax as if it was a table.
You haven't shown your view or table definitions so there isn't enough information to provide a useful example; fortunately the documentation has one you can use as a starting point.
View is a select of your table my friend, there for if you want to update your view you must update your table and then you will see the changes in your view.

Why we need trigger to delete from View?

Sometimes I found some code that makes triggers to allow user to delete from View ?
But why we need to delete from View ,, because simply we can Go to Table > and execute delete like this simple code :
delete from table where x=y ;
My question why we use deletion on views by using TRIGGERS ؟
in another words what is the advantages of using delete over a View !
The reason we use view are mainly to hide the complexity of data
sources , Hide away the actual tables (For security reasons), saving
us writing the same code over and over again.
Now if you havent let your users access the tables directly and they
only work through View in this case they will be executing Deletes
against views.
Triggers are only used when your View has more than One underlying
table. You cannot do update, insert or delete operations on view if it
effects multiple underlying tables. Therefore we make use of Instead
of Delete/Update/Insert Triggers to do these operations against
views.
When you have multiple Underlying tables if you Update,Delete or
Insert effects only One underlying table it does allow you to execute
the statement but it is not guaranteed that it will be done correctly.
therefore if you have a situation where your view is based on
multiple underlying tables you should always do update/delete/inserts
1) Directly to underlying tables. 2) Use Instead of triggers.
Also Read here for more details on why you shouldnt do update/delete/insert operations on views with multiple underlying tables.

Sql Server: Execute delete against updateable view with joins

In SQL it is possible to run inserts and updates against a view, as long as the view only selects data from one table. However, deletes don't seem to work quite so well. Can anyone help out?
Take this view for example:
CREATE VIEW v_MyUpdatableView
AS
SELECT x.* FROM MyPrimaryTable x
LEFT OUTER JOIN AnotherTable y ON y.MyPrimaryTableId = x.Id
I can run updates and inserts against this view and they happily pass through to MyPrimaryTable.
However, if I run a delete I receive the following exception:
View or function 'v_MyUpdatableView' is not updatable because the modification affects multiple base tables.
Quote:
DELETE statements remove data in one or more of the member tables through the partitioned view. The DELETE statements must adhere to this rule:
DELETE statements are not allowed if there is a self-join with the same view, or any of the member tables.
Data Modification Rules - Creating a Partitioned View
I would just create a stored procedure that would delete the data from two tables. I know it's not pretty, but it would work or do logical deletes, where you update a column to be "deleted".