0% found this document useful (0 votes)
17 views

2 Views

Uploaded by

pierresalam6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

2 Views

Uploaded by

pierresalam6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL 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

 Simplifying data retrieval.

 Maintaining logical data independence.

 Implementing data security.

Creating SQL views


To create a new view in a database, you use the following Oracle CREATE VIEW statement:
1 CREATE [OR REPLACE] VIEW view_name [(column_aliases)] AS

2 defining-query

3 [WITH READ ONLY]

4 [WITH CHECK OPTION]

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;

Querying data from views


Querying data from views is the same as querying data from tables.
SELECT * FROM employee_contacts;
you can apply filtering or grouping as follows:
SELECT job,MIN(compensation),MAX(compensation),AVG(compensation)
FROM payroll WHERE job LIKE ‘A%’ GROUP BY job;
Modifying SQL views
To modify a view use the same CREATE OR REPLACE VIEW statement.
CREATE OR REPLACE view_name AS SELECT-statement
Removing SQL views
DROP VIEW payroll;

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

You might also like