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

18
Views

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

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

Views

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

ObjectivesCreate viewsModify/Drop ViewsInsert/Delete/Update to/from viewsRetrieve data from views

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

FactsViews as logical representation of dataViews are queried like tablesViews are objects similar to tablesViews are stored queriesEvery time a view is uses the query is run

(Views are not stored database)

3

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

Advantages of ViewsRestrict database accessRepresent data from different tablesMake complex query easyCalculate values of formulaRepresent different views of the same dataViews can be created from other views

4

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

Creating ViewsCREATE [OR REPLACE] VIEW

nameAS sub-query[WITH READ ONLY]

5

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

Creating ViewsExample:

CREATE OR REPLACE VIEW salesEmplAS

SELECT *FROM testdata.EmployeeWHERE dept=‘Sales’;

6

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

Check the Structure of ViewsDESC salesEmpl;

Name Null? TypeF_Name VARCHAR2(20)L_Name VARCHAR2(20)SSN NUMBER(9)Dept VARCHAR2(20)B_Date DATE

7

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

Check the Content of Views

SELECT *FROM SalesEmpl;

Retrieve data from Views

SELECT L_Name, F_Name, SSNFROM SalesEmplWHERE TO_CHAR(B_Date, ‘YYYY’)=1973;

8

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

Column Aliases:CREATE VIEW GoodStudents AS

SELECT Name, SSN ID, Major Area, Email Contact

FROM StudentsWhere GPA>3;

Alternative Renaming:

CREATE OR REPLACE VIEW GoodStudents(Name, ID, Area, Contact)AS

SELECT Name, SSN, Major, EmailFROM StudentsWHERE GPA>3;

9

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

Use of Functions:CREATE VIEW EmployeeData

(Dept, MinSal, MaxSal, AvgSal, CountEmp)AS

SELECT Dept, MIN(PayRate), Max(PayRate), AVG (PayRate), Count(*)

FROM Employee;

10

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

Use of Functions:SELECT * FROM EmployeeData;

How to modify Views?

11

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

SELECT only Views:

CREATE View EmployeeDataAS

SELECT L_Name, F_Name, DeptFROM EmployeeWITH READ ONLY;

Why do we need a read only view?

12

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

Delete Views:

DROP VIEW Name)

DROP VIEW EmployeeData;

13

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

Check the list of views

SELECT *FROM USER_VIEWS;

14

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

INSERT Data into View

INSERT INTO GoodStudent(ID, Name, Area, Contact)VALUES (211202111, ‘John Smith’,

‘COSC’, ‘[email protected]’);

15

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

Insert with NULL value:

INSERT INTO GoodStudent(ID, Name, Area, Contact)

VALUES (211202111, ‘John Smith’, NULL, ‘[email protected]’);

16

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

Delete data from views:

DELETE GoodStudentWHERE ID=211202111;

Update Views:UPDATE GoodStudentSET Area=‘MATH’WHERE ID=211202111;

17

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

Create Views from Multiple tableCREATE OR REPLACE VIEW SalesInfo

(Name, ID, Item, Cost)AS

SELECT F_Name || ‘ ‘ || L_Name, ID, HowMany, Price

FROM Employee, SalesWHERE ID=E_ID;

18