View study notes

1, the concept of view
Relational database systems is a view provided to the user to observe an important mechanism for multi-angle data in the database. Not only does it meet the individual needs of each user, it also provides a protection mechanism to shield unauthorized data or irrelevant data, or the user's query processing operations defined on the data processed or his right to a query.
Views are derived from one or more base tables and views the virtual table, it is only the definition, there is no corresponding physical data. View the content data can only exist in the corresponding base table, the base table when the data changes, the query from the view out of the data has changed. So the view is like a window, through which you can see and change the data they are interested in. Use view has the following advantages:
① conducive to data security. For different user-defined different views, so that users see only the data relevant to their own.
② simplify queries. Create a view for complex queries, users do not have to type a complex query, you can just do a simple query against this view.
③ guarantee logical data independence. For the operation of the view, such as querying, only depends on the view definition. When the base view table configured to modify, simply modify subquery section view definition. The view-based query without changing. This independence is introduced between the first chapter and the outer pattern mode, i.e., a logical data independence.
View and basic tables both similarities, there are differences. View, once defined, can and base tables, like being queried, removed, can then define a new view on the basis of a view on, but generally not allowed to modify the view definition, and update operations on the view of the data also have certain limitations.
2, the view is defined
CREATE VIEW <View name> [(<column name> [{<column name>}])]
AS <subquery>
[ WITH CHECK OPINION ] ;
Wherein <column name> that is the name of a column built view contained herein entirely omitted <column name> indicates the view by a subquery target column names; <subquery> may be arbitrarily complex SELECT query but usually not allowed to contain an ORDER BY clause and DISTINCT phrase, specific SELECT query syntax in detail in section 5.3.1 of this book; when WITH CHECK OPINION expressed the view update operation (such as uPDATE, INSERT, etc.), to ensure that modify, insert recording satisfies conditional expression subquery, so that the user data can be prevented to add, delete, update operations and other changes, intentionally or unintentionally not destroy data in the base table view through the view range.
Example 6 establish basic information view High_price1 a price not less than 100.00 rental car.
CREATE VIEW High_price1
AS
SELECT Auto_order,Auto_ID,Auto_name,Lease_price,Purchase_date
FROM Automobile
WHERE Lease_price>=100.00;
 
Still be in view to ensure the rental price of not less than 100.00 Example 7 to establish a record of not less than rental price view High_price2 car 100.00 basic information, and requires modification and insertion.
CREATE VIEW High_price2
AS
SELECT Auto_order,Auto_ID,Auto_name,Lease_price,Purchase_date
FROM Automobile
WHERE Lease_price>=100.00
WITH CHECK OPINION;
 
View may also be derived from a plurality of base tables and views.
Example 8 Leased_Auto1 established view all the information a rented car.
CREATE VIEW Leased_Auto1
AS
SELECT Automobile.Auto_order,Auto_ID,Auto_name,Lease_price,Purchase_date, Client_code, Lease_date
FROM Automobile,Lease
WHERE Automobile.Auto_order=Lease.Auto_order;
 
Example 9 establish a leased and the lease price is not less than the basic information view Leased_Auto2 automobile 100.00 on the basis of Automobile tables and views High_price1 on.
CREATE VIEW Leased_Auto2
AS
SELECT Automobile.Auto_order,Auto_ID,Auto_name,Lease_price,Purchase_date, Client_code, Lease_date
FROM Automobile, Lease, High_price1
WHERE Automobile.Auto_order= Lease.Auto_order AND Automobile.Auto_order=High_price1.Auto_order;
 
3. Delete View
When the view is substantially derived table is deleted, the view will fail, but generally will not be automatically deleted, usually you need to explicitly delete view of a DROP VIEW statement, the statement of the form:
DROP VIEW <view name>;
Example 10 Delete View Leased_Auto2.
DROP VIEW Leased_Auto2;
 
Once you delete a view, other view derived from that view will be invalid, and therefore basic and delete tables, be careful when deleting views.
 
4, the update data through the view
Conditions must be the updated data through the view
1, SELECT statement or function is not used in the polymerization Group by, Union, Distinct or clause Top
2, INSERT, UPDATE, DELETE statements must meet certain conditions in order to refer to a view updatable
 
By relatively few scenes view updated data, we understand just under view can be updated on the line.
 
5, view encryption
Encryption view what good is it?
If a hacker to crack the database, and then he wanted to call the system stored procedure sp_help look at the contents of the view. This time will get the message, the text is encrypted.
 

Guess you like

Origin www.cnblogs.com/schangxiang/p/11294447.html