Chapter 12 Data Manipulation Language (DML) View • Objectives • • • • • Dr. Chitsaz Definition Create a view Retrieve data from a view Insert, delete, update to/from views Drop a view 1 Views Database Objects 1-TABLE: Physical unite 2-VIEW: Logical representation 3-SEQUENCE 4-SYNONYM 5-INDEX 2 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. 3 Views 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 4 Views CREATE VIEW COSCStudent AS SELECT ID, Name, GPA FROM Student WHERE Major=‘COSC’; DESCRIBE COSCStudent; 5 Results Name Null? Type ----------------------------------------------------ID NOT NULL NUMBER(6) NAME VARCHAR2(80) GPA NUMBER(3,2) 6 Views Aliases Column Name: CREATE VIEW COSCStudent AS SELECT ID COSCid, name COSCName, GPA FROM Student WHERE Major=‘COSC’; 7 Views Retrieving Data from View: SELECT * FROM COSCStudent; SELECT FROM COSCid, COSCname COSCStudent; 8 Results COSCID COSCNAME GPA -----------------------------------------------243 James 3.21 102234 John 3.32 9 Views 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’; 10 Views Example: CREATE VIEW COSCData (minsal, maxsal, avgsal) AS SELECT MIN(Salary), MAX (Salary), AVG (Salary) FROM Faculty WHERE dept =‘COSC’; 11 SELECT FROM * COSCData; MINSAL MAXSAL AVGSAL --------------------------------------------------20000 45000 33800 12 Views Modifying a View (continued): CREATE VIEW studentgrade AS SELECT Name, ID, c_num, grade FROM student, student_course; CREATE VIEW majors AS SELECT major, count (*) total FROM student GROUP BY major; 13 SELECT * FROM majors; MAJOR TOTAL ------------------------COSC 2 ENGL 1 MATH 4 14 Views CREATE FORCE VIEW COSCStudent AS SELECT ID, Name, GPA FROM NewStudent WHERE Major=‘COSC’; 15 Views Check Option: CREATE VIEW SELECT COSCStudent AS ID COSCid, name COSCName, GPA FROM Student WHERE Major=‘COSC’ WITH CHECK OPTION CONSTRAINT cosc_ck; //You can only update the COSC students records. 16 Views Read Only Option: CREATE VIEW SELECT COSCStudent AS ID COSCid, name COSCName, GPA FROM Student WHERE Major=‘COSC’ WITH READ ONLY; //Data may not be modified 17 Views Removing a View: DROP VIEW majors; 18 User Views • You can check data dictionary to check for name of view and definition using USER_VIEWS • SELECT * • FROM USER_VIEWS 19 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 20 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 21 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 22 Views Insert Data Into A View: INSERT INTO COSCStudent (COSCid, COSCName, GPA) VALUES (1121,’SANDY’,3.33); SELECT FROM WHERE COSCid, COSCName, GPA COSCStudent Major=‘COSC’; 23 Viewing constraints: SELECT constraint_name, constraint_type, search_condition, status,last_change FROM user_constraints WHERE table_name=‘STUDENT’; Result: CONSTRAINT_NAME C STATUS LAST_CHANGE --------------------------------------------------------------------SYS_C0098772 P ENABLED 18-NOV-05 24 Viewing constraints: SELECT FROM WHERE constraint_name, column_name, owner user_cons_columns table_name=‘STUDENT’; CONSTRAINT_NAME COLUMN_NAME OWNER ------------------------------------------------------------------SYS_C0098772 ID CS640M2 This is list of columns that can be updated USER_UPDATABLE_COLUMNS 25 Materialized Views 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 26 Privilege to create materialized view Materialized Views CREATE TABLE local student AS SELECT * From student@Remote_connect; 27 Materialized Views Creating a Materialized View: CREATE MATERIALIZED VIEW name [REFRESH clause ] AS Subquery; 28 Materialized Views Creating a Materialized View: Example: CREATE MATERIALIZED VIEW localstudents REFRESH FAST START WITH SYSDATE NEXT SYSDATE+10 WITH PRIMARY KEY AS SELECT * FROM student@remote_connect; 29