SQL Learning to create, modify, and delete views using the command method

Source: Internet
Author: User
Tags getdate

1. Create a View

(1) General format:

Create VIEW View name

[WITH Encryption]

As

SELECT statement

[WITH CHECK option]


(2) Define a single source table view:

Establish a view of the student's number, name, gender and age in the Department of Information Management

Create View is_student (Studentid,studentname,sex,birth)

As

Select Studentid,studentname,sex,getdate ()-birth

From student

where sdept = ' Information Management Department '


(3) Define a multi-source table view:

To establish a view of the student's number, name, and result of the C001 course in the Department of Information Management

Create View v_is_s1 (Studentid,studentname,grade)

As

Select S.studentid,studentname,grade

From student S

Join grade G on G.studentid = S.studentid

where sdept = ' Information Management Department ' and G.courseid = ' C001 '


(4) Define a new view on an existing view:

Create a view of the student's number, name, and age in the information management department on the view set up on (2) with age less than 20

Create View Is_student_sage (Studentid,studentname,birth)

As

Select Studentid,studentname,getdate ()-birth

From Is_student

where GETDATE ()-birth >20


(5) A view with an expression

Defines a view of a student's year of birth, including number, name, and year of birth

Create View bt_s (Studentid,studentname,birth)

As

Select Studentid,studentname,getdate ()-birth

From student


(6) Views with grouped statistics:

Define a view of each student's academic number and average score

Create View S_g (Studentid,grade)

As

Select Studentid,avg (Grade)

From grade

GROUP BY StudentID


2. Modify the View:

Alter VIEW view name

As

Query statements


Revised to count each student's test scores and the total number of elective courses.

Alter View S_g (Studentid,grade,count_coursename)

As

Select Studentid,avg (Grade), COUNT (*)

From grade

GROUP BY StudentID


3. Delete the view:

Drop View Name

This article from "A Growing small Tree" blog, reproduced please contact the author!

SQL Learning to create, modify, and delete views using the command method

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.