The database table has a physical existence in the database. A view is a virtual table, that is one that does not actually exist. View is made up of a query on one or many tables in a database.
In database theory, a view is the result set of a stored query on the data, which the database users can query just as they would in a persistent database collection object. This pre-established query command is kept in the database dictionary. Unlike ordinary base tables in a relational database, a view does not form part of the physical schema: as a result set, it is a virtual table computed or collated from data in the database, dynamically when access to that view is requested. Changes applied to the data in a relevantunderlying table are reflected in the data shown in subsequent invocations of the view. In some NoSQL databases, views are the only way to query data.
Views can provide advantages over tables:
Database practitioners can define views as read-only or updatable. If the database system can determine the reverse mapping from the view schema to the schema of the underlying base tables, then the view is updatable. INSERT, UPDATE, and DELETE operations can be performed on updatable views. Read-only views do not support such operations because the DBMS cannot map the changes to the underlying base tables. A view update is done by key preservation.
Some systems support the definition of INSTEAD OF triggers on views.(insert/delete view 必须为其定义trigger) This technique allows the definition of other logic for execution in place of an insert, update, or delete operation on the views. Thus database systems can implement data modifications based on read-only views. However, an INSTEAD OF trigger does not change the read-only or updatable property of the view itself.
In order to insert (update & delete) data to views created using multiple tables, you need to use an ‘Instead of trigger’.
**Please note that ‘After Triggers’ cannot be created for views.
Let’s create an instead of trigger using the following syntax.
CREATE TRIGGER TRGI_VW_PAYMENT ON VW_LAST_PAYMENT_DETAILS INSTEAD OF INSERT AS BEGIN INSERT INTO STUDENT_PAYMENT SELECT STD_ID,PAY_AMT,PAY_DATE FROM INSERTED END
Now using the above insert syntax, you can insert data without getting any error. If you inspect the ‘STUDENT_PAYMENT’ table you can see that the data has been inserted successfully.