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 Views Database Objects 1-TABLE: Physical unite 2-VIEW: Logical representation 3-SEQUENCE 4-SYNONYM 5-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, GPA FROM Student WHERE 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 VIEWCOSCStudent AS SELECTID COSCid, name COSCName, GPA FROMStudent WHEREMajor=COSC; Views
8 Retrieving Data from View: SELECT* FROMCOSCStudent; SELECTCOSCid, COSCname FROMCOSCStudent; Views
9 Results COSCID COSCNAMEGPA James John3.32
10 Modifying a View: CREATE OR REPLACE VIEW COSCStudent (Field1, Field2) //alias names for Major and Minor AS SELECTMajor, Minor FROMstudent WHEREmajor =COSC; Views
11 Example: CREATE VIEW COSCData (minsal, maxsal, avgsal) AS SELECTMIN(Salary), MAX (Salary), AVG (Salary) FROMFaculty WHEREdept =COSC; Views
12 SELECT* FROM COSCData; MINSAL MAXSAL AVGSAL
13 Modifying a View (continued): CREATE VIEW studentgrade AS SELECTName, ID, c_num, grade FROMstudent, student_course; CREATE VIEW majors AS SELECTmajor, count (*) total FROMstudent GROUP BYmajor; Views
14 SELECT* FROM majors; MAJOR TOTAL COSC 2 ENGL 1 MATH 4
15 CREATE FORCE VIEW COSCStudent AS SELECT ID, Name, GPA FROM NewStudent WHERE Major=COSC; Views
16 Check Option: CREATE VIEWCOSCStudent AS SELECTID COSCid, name COSCName, GPA FROMStudent WHEREMajor=COSC WITH CHECK OPTION CONSTRAINT cosc_ck; //You can only update the COSC students records. Views
17 Read Only Option: CREATE VIEWCOSCStudent AS SELECTID COSCid, name COSCName, GPA FROMStudent WHEREMajor=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, GPA FROM COSCStudent WHERE Major=COSC; Views
24 Viewing constraints: SELECT constraint_name, constraint_type, search_condition, status,last_change FROM user_constraints WHERE table_name=STUDENT; Result: CONSTRAINT_NAME CSTATUS LAST_CHANGE SYS_C 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_C ID CS640M2 This is list of columns that can be updated USER_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 student AS SELECT * Materialized Views
28 Creating a Materialized View: CREATE MATERIALIZED VIEW name [REFRESH clause ] AS Subquery; Materialized Views
29 Creating a Materialized View: Example: CREATE MATERIALIZED VIEW localstudents REFRESH FAST START WITH SYSDATE NEXT SYSDATE+10 WITH PRIMARY KEY AS SELECT * FROM Materialized Views