| Views |
Sorry the information on this page is outdated as it relates to older versions of SQL Server.
We have new content and free videos at this location, http://www.learningcomputer.com/id29.htm Please note that rest of the SQL Server videos can be bought from this page Buy Tutorials. Please send any comments or suggestion to kashi@learningcomputer.com
A view is a virtual table that does not exist physically in the database. You can use views as security mechanisms by granting permission on a view but not on underlying tables. Also the views can perform complicated joins on base tables giving the end user an easier way to look at all the data. The syntax for creating a view is:
CREATE VIEW [owner.]view_name[(column_name [, column_name]...)]
[WITH ENCRYPTION] AS
select_statement
[WITH CHECK OPTION]
where
view_name:Is the name of the view. View names must follow the rules for identifiers.
column_name:Is the name to be used for a column in a view. Naming a column in CREATE VIEW is always legal but only necessary when a column is derived from an arithmetic expression, a function, or a constant, when two or more columns could otherwise have the same name (usually because of a join), or when you want to give a column in a view a name different from the column from which it is derived. Column names can also be assigned in the SELECT statement.
ENCRYPTION:Encrypts the syscomments entries that contain the text of the CREATE VIEW statement. This command also hides the view syntax from the user
AS select_statement:Is the SELECT statement that defines the view. It can use more than one table and other views. You must have permission to select from the objects referenced in the SELECT clause of a view you are creating.
WITH CHECK OPTION:Forces all data modification statements executed against the view to adhere to the criteria set within the select_statement defining the view. When a row is modified through a view, the WITH CHECK OPTION guarantees that the data will remain visible through the view after the modification has been committed.
Example 1:
Lets us look at the following table mrm_patient with five fields and one primary key (sysno_patient)
| Address | Zip_Code | |||
| 146498 | Mitchel | John | 85023 | |
| 146499 | Spencer | Teri | 53219 | |
| 146500 | James | Taylor | 23 N. Atlantic Blvd | 76215 |
| 146501 | Pewinsky | Lewis | 85023 | |
| 146502 | James | Sue | 3567 E Tatum Blvd | 85032 |
| exam_date | ||||
| 107425 | 146494 | 7 | 7 | |
| 107426 | 168314 | 5 | 3 | |
| 107427 | 135264 | 6 | 8 | |
| 107428 | 146502 | 8 | 9 | |
| 107428 | 146500 | 7 | 2 |
| Last_Name | First_Name | exam_date | sysno_trans | |
| James | Sue | 8 | 9 | |
| James | Taylor | 7 | 2 |

