1
Chapter 12 Data Manipulation Language
(DML)View
Dr. Chitsaz• Objectives
• Definition
• Create a view
• Retrieve data from a view
• Insert, delete, update to/from views
• Drop a view
2
ViewsDatabase Objects
1-TABLE: Physical unite2-VIEW: Logical representation3-SEQUENCE4-SYNONYM5-INDEX
3
Views
VIEW:-What is a view? Logical
representations (Subset of data from one or more tables or views)
-Why use a view?Restrict database access. Make complex queries easy.Represent data from different
tables.Represent different Views of the
same data.
4
Creating a View:
CREATE [OR REPLACE] [FORCE | NOFORCE]VIEW name AS
Subquery[WITH CHECK OPTION [CONSTRAINT name]][WITH READ ONLY];
Use OR REPLACE if view already exists.Use FORCE if the base tables are not existed (view will be created even if the base table
does not exists)Use WITH CHECK OPTION: only rows that accessible by view can be updated.Use WITH READ ONLY for select only
Views
5
CREATE VIEW COSCStudent AS
SELECT ID, Name, GPAFROM StudentWHERE Major=‘COSC’;
DESCRIBE COSCStudent;
Views
6
Results
Name Null? Type
-----------------------------------------------------
ID NOT NULL NUMBER(6)
NAME VARCHAR2(80)
GPA NUMBER(3,2)
7
Aliases Column Name:CREATE VIEW COSCStudent AS
SELECT ID COSCid, name COSCName,GPA
FROM StudentWHERE Major=‘COSC’;
Views
8
Retrieving Data from View:
SELECT *
FROM COSCStudent;
SELECT COSCid, COSCname
FROM COSCStudent;
Views
9
Results
COSCID COSCNAME GPA
------------------------------------------------
243 James 3.21
102234 John 3.32
10
Modifying a View:
CREATE OR REPLACE VIEW COSCStudent(Field1, Field2) //alias names for Major and Minor
AS
SELECT Major, Minor
FROM student
WHERE major =‘COSC’;
Views
11
Example:
CREATE VIEW COSCData
(minsal, maxsal, avgsal)
AS
SELECT MIN(Salary), MAX (Salary),
AVG (Salary)
FROM Faculty
WHERE dept =‘COSC’;
Views
12
SELECT *
FROM COSCData;
MINSAL MAXSAL AVGSAL
---------------------------------------------------
20000 45000 33800
13
Modifying a View (continued):CREATE VIEW studentgrade AS
SELECT Name, ID, c_num, gradeFROM student, student_course;
CREATE VIEW majors ASSELECT major, count (*) totalFROM studentGROUP BY major;
Views
14
SELECT *FROM majors;
MAJOR TOTAL-------------------------COSC 2ENGL 1MATH 4
15
CREATE FORCE VIEW COSCStudent
AS
SELECT ID, Name, GPA
FROM NewStudent
WHERE Major=‘COSC’;
Views
16
Check Option:CREATE VIEW COSCStudent AS
SELECT ID COSCid, name COSCName, GPA
FROM StudentWHERE Major=‘COSC’
WITH CHECK OPTION CONSTRAINT cosc_ck;
//You can only update the COSC students records.
Views
17
Read Only Option:CREATE VIEW COSCStudent AS
SELECT ID COSCid, name COSCName, GPA
FROM StudentWHERE Major=‘COSC’
WITH READ ONLY;//Data may not be modified
Views
18
Removing a View:
DROP VIEW majors;
Views
19
User Views
• You can check data dictionary to check for name of view and definition using USER_VIEWS
• SELECT *
• FROM USER_VIEWS
20
Rules for Performing DML operation on a View:
• Simple view: you can perform DML operation on simple view (not complex; droved from more than one table)
• Can NOT REMOVE a row if view contains:– Group function (aggregated functions including null)
– GROUP BY clause (or having)
– DISTINCT clause
21
Rules for Performing DML operation on a View:
• Can NOT MODIFY data in a view if it contains:
1. The Following:• Group function• GROUP BY clause• DISTINCT clause
2. Column defined by expression The ROWNUM pseudocolumn
22
Rules for Performing DML operation on a View:
• Can NOT ADD data in a view if it contains:
1. The Following:• Group function
• GROUP BY clause
• DISTINCT clause
2. Column defined by expression
3. NOT NULL columns in the tables that are not selected by view
23
Insert Data Into A View:INSERT INTO COSCStudent
(COSCid, COSCName, GPA)VALUES (1121,’SANDY’,3.33);
SELECT COSCid, COSCName, GPAFROM COSCStudentWHERE Major=‘COSC’;
Views
24
Viewing constraints:
SELECT constraint_name, constraint_type, search_condition, status,last_change
FROM user_constraintsWHERE table_name=‘STUDENT’;
Result:CONSTRAINT_NAME C STATUS
LAST_CHANGE---------------------------------------------------------------------SYS_C0098772 P ENABLED 18-NOV-05
25
Viewing constraints:
SELECT constraint_name, column_name, owner
FROM user_cons_columns
WHERE table_name=‘STUDENT’;
CONSTRAINT_NAME COLUMN_NAME OWNER-------------------------------------------------------------------SYS_C0098772 ID CS640M2
This is list of columns that can be updatedUSER_UPDATABLE_COLUMNS
26
To improve the performance of an application, you can make local copies of remote tables.
Definition: Stored local copies of tables
Master table: remote site
Local Table: local site
Refresh interval: how frequently update the tables
You need to have Creating Materialized View Privilege to create materialized view
Materialized Views
27
CREATE TABLE local studentAS
SELECT *From student@Remote_connect;
Materialized Views
28
Creating a Materialized View:
CREATE MATERIALIZED VIEW name [REFRESH clause ]
ASSubquery;
Materialized Views
29
Creating a Materialized View:Example:
CREATE MATERIALIZED VIEW localstudentsREFRESH FASTSTART WITH SYSDATE NEXT SYSDATE+10WITH PRIMARY KEY
ASSELECT *FROM student@remote_connect;
Materialized Views