SQL Server series: views

Source: Internet
Author: User

A view is a virtual table in a database that, like a real table, contains a series of row and column data with names. Row and column data are referenced by the query that is used to customize the view, and are generated dynamically when the view is referenced.

1. Concept of views

A view is exported from one or more tables and behaves like a table, but the view is a virtual table. Views can be defined on the basis of a view that already exists. On a view, the user can query the database using the SELECT statement and modify the record using INSERT, UPDATE, and DELETE statements. The operation for the view is ultimately translated into operations on the base data table.

Usage: 1. Assemble multiple tables into a single table such as the first page data display.

2. Create a View

To create a view syntax using T-sql:

CREATE VIEW [schema_name] view_name [(column [,... n])] [with <view_attribute> [,... N]] as Select_statem ent [with CHECK OPTION] [;]

Instance:

Single-Table View

CREATE VIEW [dbo]. [V_product] As    SELECT [productid],[productname] from    [dbo].[ Product]

Multi-table View

CREATE VIEW [dbo]. [V_productcategory] As    SELECT [productid],[categoryname],[productname] from    [dbo].[ Product] INNER JOIN [dbo]. [Category]    on [dbo]. [Product]. [CategoryID] = [dbo]. [Category]. [CategoryID]

Create a sort view

CREATE VIEW [dbo]. [V_product] As    SELECT TOP PERCENT [productid],[productname] from    [dbo].[ Product]    ORDER by [UnitPrice]

3. Modify the View

To modify the view syntax using T-sql:

ALTER VIEW [schema_name] view_name [(column [,... n])] [with <view_attribute> [,... N]] as Select_statem ent [with CHECK OPTION] [;]

4. View View

To view the view structure:

EXEC sp_help [V_product]

To view the view text:

EXEC sp_helptext [V_product]

5. Delete a view

T-SQL Delete view syntax:

DROP VIEW [schema_name.] view_name [..., n] [;]

To delete a single view:

DROP VIEW [V_product]

To delete multiple views:

DROP VIEW [V_product],[v_productcategory]

SQL Server series: views

Related Article

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.