SQL must know the fifth part

Lesson 18    

        This lesson describes what views are, how they work, and when to use them; it also describes how views can be used to simplify some of the SQL operations performed in previous lessons

        The main code is as follows:

create view ProductCustomers as
select cust_name, cust_contact, prod_id
from Customers, Orders, OrderItems
where Customers.cust_id = Orders.cust_id
 and OrderItems.order_num = Orders.order_num;
select cust_name, cust_contact
from ProductCustomers
where prod_id = 'RGAN01';
create view vendorlocations as
select rtrim(vend_name) + '(' + rtrim(vend_country) + ')' as vend_title
from vendors;
select *
from vendorlocations;
create view customeremaillist as
select cust_id, cust_name, cust_email
from Customers
where cust_email is not null;
select *
from customeremaillist;
create view orderitemsexpanded as
select order_num, prod_id, quantity, item_price, quantity*item_price as expanded_price
from orderitems;
select *
from orderitemsexpanded
where order_num = 20008;

        1. Views are virtual tables. Unlike tables that contain data, views only contain queries that retrieve data using dynamic

        2. Some common applications of views:

  • Reuse SQL Statements
  • Simplify complex SQL operations
  • Use part of the table instead of the whole table
  • protect data
  • Change the data format and presentation, the view can return data that is different from the presentation and format of the underlying table

        3. Once views are created, they can be used in much the same way as tables

        4. The data returned by the view is retrieved from other tables. When changing the data in these tables, the view will return the changed data

        5. Before deploying an application that uses a lot of views, it should be tested

        6. Some of the most common rules and restrictions on view creation and use

  • Like tables, views must be uniquely named
  • There is no limit to the number of views that can be created
  • Creating a view must have sufficient access rights
  • Views can be nested, i.e. views can be constructed using queries that retrieve data from other views
  • Many DBMSs prohibit the use of ORDER BY clauses in view queries
  • Some DBMSs require all columns returned to be named    
  • Views cannot be indexed and cannot have associated triggers or defaults
  • Some DBMSs treat views as read-only queries, which means that data can be retrieved from the view but not written back to the underlying table
  • Some DBMSs allow the creation of views that cannot make inserts or updates that would result in a row no longer belonging to the view

        7. Views are created with the CREATE VIEW statement, dropped with the DROP VIEW statement, overwritten (or updated)

        8. Extending the scope of a view makes it reusable and potentially more useful

        9. If a WHERE clause is used when retrieving data from a view, the two sets of clauses are automatically combined

Summary: Views are virtual tables. They contain not data but queries that need to retrieve data. Views provide a layer of encapsulation of SELECT statements that can be used to simplify data processing, reformat or protect the underlying data


Lesson 19

        This lesson introduces what a stored procedure is, why you should use a stored procedure, how to use a stored procedure, and the basic syntax for creating and using stored procedures

        The main code is as follows:

        1. Simply put, a stored procedure is one or more SQL statements that are saved for later use. Think of them as batch files, although their role is not limited to batch processing

        2. Main reasons to use stored procedures

  • Complex operations can be simplified by encapsulating processing in an easy-to-use unit
  • Data consistency is guaranteed because it does not require repeated establishment of a series of processing steps
  • Simplify change management
  • Less effort required by the DBMS to process commands, improving performance
  • There are SQL elements and features that can only be used in a single request, and stored procedures can use them to write more functional and flexible code

        3. Defects

  • Stored procedure syntax differs in different DBMSs
  • Generally speaking, writing stored procedures is more complex than writing basic SQL statements, requiring higher skills and more experience

        4. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851181&siteId=291194637