SQL Advanced Apps (VIEW)

Source: Internet
Author: User

SQL VIEW

A view is a table that is visualized

In SQL, the view is a visual table based on the result set of the SQL statement

A view contains rows and columns, as if it were a real table. A field in a view is a field from a real table in one or more databases. We can add SQL functions, WHERE, and join statements to the view, or we can submit the data as if they came from a single table

Note: The design and structure of a database is not affected by functions, where, or join statements in the view

SQL CREATE VIEW  

CREATE VIEW view_name asselect column_name (S) from table_namewhere condition

Note: views always display the most recent data. The database engine rebuilds data through SQL statements whenever the user queries the view

  

SQL CREATE VIEW Instance

You can use views from within a query, inside a stored procedure, or from within another view. By adding functions, joins, and so on to the view, you can accurately submit the data you want to submit to the user

Sample database Northwind has some views that are installed by default. The view "Current Product List" lists all products that are in use from the product table. This view is created using the following SQL:

CREATE VIEW [Current Product List] asselect ProductID, Productnamefrom productswhere discontinued=no

You can query the view above

SELECT * FROM [current Product List]

Another view of the Northwind sample database selects products with all units in the product table that are higher than the average unit price:

You can query the view above like this:

SELECT * FROM [Products Above Average Price]

Another view instance from the Northwind database calculates the total sales for each category in 1997. Notice that this view picks up the data from another view called Product Sales for 1997:

You can query the view above like this:

SELECT * FROM [Category Sales for 1997]

You can also add conditions to the query. Now, we just need to see all the sales of the "Beverages" Category:

SELECT * FROM [Category Sales for 1997]where categoryname= ' beverages '

  

Update view

You can use the following syntax

CREATE OR REPLACE VIEW view_name asselect column_name (s) from table_namewhere condition

Now, add the Category column to the current Product List view

CREATE VIEW [Current Product List] Asselect ProductID, ProductName, Categoryfrom productswhere discontinue=no

  

Undo View

You can use the following command

DROP VIEW view_name

  

SQL Advanced Apps (VIEW)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.