2 Views
2 Views
A view is like a virtual table produced by executing a query. The relational database management
system (RDBMS) stores a view as a named SELECT in the database catalog.
Whenever you issue a SELECT statement that contains a view name, the RDBMS executes the view-
defining query to create the virtual table. That virtual table then is used as the source table of the
query.
Why do you need to use the views
2 defining-query
Example:
1 CREATE VIEW employee_contacts AS
2 SELECT
3 first_name, last_name, email, phone_number, department_name
4 FROM
5 employees e
6 INNER JOIN
7 departments d ON d.department_id = e.department_id
8 ORDER BY first_name;
By default, the names of columns of the view are the same as column specified in
the SELECT statement. If you want to rename the columns in the view, you include the new column
names after the CREATE VIEW clause as follows:
1 CREATE VIEW view_name(new_column_list)
2 AS
3 SELECT-statement;
Example:
1 CREATE VIEW payroll (first_name , last_name , job, compensation) AS
2 SELECT
3 first_name, last_name, job_title, salary
4 FROM
5 employees e
6 INNER JOIN
7 jobs j ON j.job_id= e.job_id
8 ORDER BY first_name;
Exercise:
The following statement creates a view named employee_yos based on the employees table.
|CREATE VIEW employee_yos AS
SELECT employee_id, first_name || last_name full_name, months_between (sysdate,hire_date) /12 as yos
FROM employees
WHERE email LIKE ‘A%’;
Try To Do This:
1. UPDATE employee_yos set full_name = ‘Ahmad’;
ERROR ORA-01733: virtual column not allowed here
2. UPDATE employee_yos set email=’aa’;
ERROR ORA-00001: unique constraint (HR.EMP_EMAIL_UK) violated
3. UPDATE employee_yos set email=email || ‘a’;
107 rows updated
4. When using [WITH READ ONLY]
ERROR ORA-42399: cannot perform a DML operation on a read-only view
5. UPDATE employee_yos SET email=’B’ || email ; [WITH CHECK OPTION]
ERROR ORA-01402: view WITH CHECK OPTION where-clause violation