a
 
SQLRef.Com 

            SqlRef.Com


SQL BASIC
DATA QUERY LANGUAGE
BASIC FUNCTION
AGGREGATE FUNCTION
DATA MANIPULATION LANGUAGE
DATA DEFINITION LANGUAGE
DATA CONTROL LANGUAGE
OTHERS

CREATE VIEW

CREATE VIEW <view name> AS <SELECT Statement>

View is a virtual table. You can use view to see certain fields or records from a table. You can create VIEW with the CREATE VIEW command. The following SQL statement creates an Employee_VIEW view that only allows user to see EmployeeID, FirstName and LastName from Employee table.

CREATE VIEW Employee_VIEW AS
   SELECT EmpID, FirstName, LastName FROM Employee


Table: Employee
EmpID FirstName LastName Address City Country Phone
EMP001 Maria Anders Obere Str. 57 Berlin Germany 030-0074321
EMP002 Ernst Handel Kirchgasse 6 Graz Austria 7675-3425
EMP003 Helen Bennett Garden House Crowther Way Cowes UK (198) 555-8888
EMP004 Paula Wilson 2817 Milton Dr. Albuquerque USA (505) 555-5939
EMP005 Wong Liu 55 Grizzly Peak Rd. Butte USA (406) 555-5834

View: Employee_VIEW
EmpID FirstName LastName
EMP001 Maria Anders
EMP002 Ernst Handel
EMP003 Helen Bennett
EMP004 Paula Wilson
EMP005 Wong Liu

After creating the view, you can use VIEW as the normal table.

SELECT * FROM Employe_VIEW WHERE EmployeeID = 'EMP003'

The above SQL statement will returns the following Resultset.

ResultSet1
EmpID FirstName LastName
EMP003 Helen Bennett

Besides limiting the number of columns that can be seen, you can also create VIEW that limits the number of records that can be seen. Let's look at the following example:

CREATE VIEW Employee_VIEW_Rec AS
   SELECT * FROM Employee WHERE FirstName = 'Paula'
 
View: Employee_VIEW_Rec
EmpID FirstName LastName Address City Country Phone
EMP004 Paula Wilson 2817 Milton Dr. Albuquerque USA (505) 555-5939



 
| Freebies | Free Trial | Add to Bookmark | Search | Link to Us |
Copyright © 2006 SqlRef.com. All rights reserved.