Presentation is loading. Please wait.

Presentation is loading. Please wait.

Views. Objectives Create views Modify/Drop Views Insert/Delete/Update to/from views Retrieve data from views.

Similar presentations


Presentation on theme: "Views. Objectives Create views Modify/Drop Views Insert/Delete/Update to/from views Retrieve data from views."— Presentation transcript:

1 Views

2 Objectives Create views Modify/Drop Views Insert/Delete/Update to/from views Retrieve data from views

3 Facts Views as logical representation of data Views are queried like tables Views are objects similar to tables Views are stored queries Every time a view is uses the query is run (Views are not stored database) 3

4 Advantages of Views Restrict database access Represent data from different tables Make complex query easy Calculate values of formula Represent different views of the same data Views can be created from other views 4

5 Creating Views CREATE [OR REPLACE] VIEW name AS sub-query [WITH READ ONLY] 5

6 Creating Views Example: CREATE OR REPLACE VIEW salesEmpl AS SELECT * FROM testdata.Employee WHERE dept=‘Sales’; 6

7 Check the Structure of Views DESC salesEmpl; NameNull?Type F_NameVARCHAR2(20) L_NameVARCHAR2(20) SSNNUMBER(9) DeptVARCHAR2(20) B_DateDATE 7

8 Check the Content of Views SELECT * FROMSalesEmpl; Retrieve data from Views SELECTL_Name, F_Name, SSN FROMSalesEmpl WHERE TO_CHAR(B_Date, ‘YYYY’)=1973; 8

9 Column Aliases: CREATE VIEW GoodStudents AS SELECT Name, SSN ID, Major Area, Email Contact FROM Students Where GPA>3; Alternative Renaming: CREATE OR REPLACE VIEW GoodStudents (Name, ID, Area, Contact) AS SELECT Name, SSN, Major, Email FROM Students WHERE GPA>3; 9

10 Use of Functions: CREATE VIEW EmployeeData (Dept, MinSal, MaxSal, AvgSal, CountEmp) AS SELECT Dept, MIN(PayRate), Max(PayRate), AVG (PayRate), Count(*) FROM Employee; 10

11 Use of Functions: SELECT * FROM EmployeeData; How to modify Views? 11

12 SELECT only Views: CREATE View EmployeeData AS SELECT L_Name, F_Name, Dept FROM Employee WITH READ ONLY; Why do we need a read only view? 12

13 Delete Views: DROP VIEW Name) DROP VIEW EmployeeData; 13

14 Check the list of views SELECT * FROM USER_VIEWS; 14

15 INSERT Data into View INSERT INTO GoodStudent (ID, Name, Area, Contact) VALUES (211202111, ‘John Smith’, ‘COSC’, ‘Jsmith@fsu.edu’); 15

16 Insert with NULL value: INSERT INTO GoodStudent (ID, Name, Area, Contact) VALUES (211202111, ‘John Smith’, NULL, ‘Jsmith@fsu.edu’); 16

17 Delete data from views: DELETE GoodStudent WHERE ID=211202111; Update Views: UPDATE GoodStudent SET Area=‘MATH’ WHERE ID=211202111; 17

18 Create Views from Multiple table CREATE OR REPLACE VIEW SalesInfo (Name, ID, Item, Cost) AS SELECT F_Name || ‘ ‘ || L_Name, ID, HowMany, Price FROM Employee, Sales WHERE ID=E_ID; 18


Download ppt "Views. Objectives Create views Modify/Drop Views Insert/Delete/Update to/from views Retrieve data from views."

Similar presentations


Ads by Google