Module - 8 Database Objects - Part 1 - View
2026-03-31 17:25
Tags: #ADV_DBMS
Author: Duke Hsu
Key Concepts
Objectives
- Table
- Basic unit of storage; composed of rows
-
View
- Logically represents subsets of data from one or more tables.
-
Sequence
- Generates numeric values
- Index
- Improves the performance of data retrieval queries
- Synonym
- Gives alternative names to objects
View
- An SQL view is a virtual table based on the result-set of an SQL statement.
- An SQL view contains rows and columns, just like a real table .
- The fields in the view are fields from one or more real tables in the database.
Two tables of Views
| Feature | Simple Views | Complex Views |
|---|---|---|
| Number of tables | One | One or more |
| Contain functions | No | Yes |
| Contain groups of data | No | Yes |
| DML Operations through a view | Yes | Not always |
| ## Create a View From One Table |
Syntax:
1 2 3 4 5 6 7 | |
Example:
Create a View named empuv80 from the table employees.
1 2 3 4 5 6 7 | |
Retrieving Data from a View
1 2 3 4 | |
Output
employee_id last_name salary
202 Mitchell 8500.00 203 Johnson 8200.00 207 Brown 8000.00
Creating a View by using column aliases in the subquery
1 2 3 4 5 6 | |
Retrieving Data from a View
1 2 3 | |
Output
ID_NUMBER LNAME ANN_SALARY
120 Weiss 96000.00
121 Fripp 98400.00
122 Kaufling 94800.00
123 Vollman 78000.00
124 Mourgos 69600.00
Create a View From Multiple Tables
- To create a view from multiple tables we can simply include multiple tables in the SELECT statement.
Syntax:
1 2 3 4 | |
Example:
1 2 3 4 | |
Retrieving Data from a View
1 2 3 | |
Output
REGION_ID COUNTRY_NAME REGION_NAME
2 Argentina Americas 3 Australia Asia 1 Belgium Europe
ALTER view Statement (SQL Server)
- In SQL server , a view can be updated with the ALTER VIEW statement.
Syntax:
1 2 3 4 | |
Example:
The following SQL adds the job_id column to the salvu50 view.
1 2 3 4 5 6 7 | |
Output
ID_NUMBER LNAME ANN_SALARY JOB_TITLE
120 Weiss 96000.00 ST_MAN 121 Fripp 98400.00 ST_MAN 122 Kaufling 94800.00 ST_MAN 123 Vollman 78000.00 ST_MAN 124 Mourgos 69600.00 ST_MAN
Creating a Complex View
Warning
Complex views are mostly ready-only . Can include: INNER JOIN, LEFT JOIN, GROUP BY, HAVING , Subqueries.
- Complex view - A saved SELECT statement that combines multiple tables and performs calculations.
Syntax:
1 2 3 4 5 6 7 | |
Example:
Create a complex view that contains group functions to display values from two tables.
1 2 3 4 5 6 7 8 9 10 11 | |
Retrieving Data from a View
1 2 | |
output
department_id department_name TotalEmployees AvgSalary
20 Marketing 2 9500.000000 30 Purchasing 6 4150.000000 40 Human Resources 1 6800.000000 50 Shipping 6 6600.000000 60 IT 8 5912.500000 80 Sales 9 10033.333333 90 Executive 3 19333.333333 100 Finance 7 8300.000000
Key Rules for DML on Views
-
Single Base Table: The view must reference only one underlying table for simple inserts and updates.
-
No Aggregation or Grouping: Views containing GROUP BY, HAVING, or DISTINCT are not updatable.
-
No Computed Columns: You cannot update a column that is defined by an expression or a function (e.g., salary * 12).
-
NOT NULL Constraints: If a NOT NULL column in the base table is omitted from the view, you cannot perform an INSERT.
-
WITH CHECK OPTION: If defined with this clause, you cannot update or insert rows that do not satisfy the view’s WHERE clause.
-
Multi-Table Views: Updates can sometimes be performed on a single table within a join, but inserting is typically prohibited unless it meets strict key-preserved table rules.
-
Read-Only Views: Views created with the READ ONLY clause or using complex queries cannot be used for DML operations
Using the WITH CHECK OPTION Clause
- The WITH CEHCK OPTION is used when creating a view to enforce the view's condition during INSERT or UPDATE
- It ensures that any data modified through the view must still satisfy the WHERE condition of the view.
WithOut CHECK OPTION
You can insert data that is NOT visible in the view.
With CHECK OPTION
You can ONLY insert / update data that remains visible in the view.
Adding vs Not Adding WITH CHECK OPTION
| Situation | with (WITH CHECK OPTION) | with Out (WITH CHECK OPTION) |
|---|---|---|
| INSERT / UPDATE operations | Only allows you to insert or update data that matches the view's conditions | Lets you insert or update any data, even if it doesn't match the view's conditions |
| Data disappearing | Won't happen — your changes stay visible in the view | Can happen — your changes might not match the conditions anymore, so they disappear from the view |
| Error message | Throws an error (Msg 550) if you try to break the rules | No checking — the operation just goes through |
Syntax:
1 2 3 4 5 6 | |
Example:
1 2 3 4 5 6 | |
Retrieving Data from a View
1 2 | |
Output
employee_id first_name job_id
120 Matthew ST_MAN 121 Adam ST_MAN 122 Payam ST_MAN 123 Shanta ST_MAN 124 Kevin ST_MAN
Real Life Example for WITH CHECK OPTION
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
Using the WITH READ ONLY Clause
| - Feature | - MS SQL Support |
| - WITH READ ONLY | - ❌ Not supported |
| - Complex View | - ✔ Automatically read-only |
| - Permission Control | - ✔ Recommended |
| - WITH CHECK OPTION | - ✔ Restricts data validity |
Removing a View
Syntax:
1 | |
Example:
1 | |
Error msg:
1 2 | |
1 2 3 4 5 6 | |
When Should You Use WITH CHECK OPTION?
Use it in these situations:
- Data accuracy matters a lot — Like in banking or inventory systems, where you can't have data suddenly vanish after an update
- Control what people can change — For example, a manager should only be able to edit employees in their own department, not accidentally touch other departments
- The view filters data in a specific way — Like showing only "active orders" or "non-expired coupons"
- You have views built on top of other views — This protects the whole chain of filters
You don't really need it if:
- The view is just for looking at data — You never insert, update, or delete through it
- The view shows everything — It has no WHERE clause to filter anything
- You want to allow data beyond the view's conditions — You're okay with data disappearing from the view