seema sirpal delhi university computer centrepeople.du.ac.in/~ssirpal/presentations/session...
TRANSCRIPT
![Page 1: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/1.jpg)
SQL
Seema Sirpal
Delhi University Computer Centre
![Page 2: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/2.jpg)
Views
• In SQL, a VIEW is a virtual table based on the In SQL, result-set of a
SELECT statement.
• Views are database objects which contain no data of its own.
• The fields in a view are fields from one or more real tables in the
database. You can add SQL functions, WHERE, and JOIN statements to
a view and present the data as if the data were coming from a single
table.
• User cannot distinguish between a table and a view.
• Views restricts access to the database.
![Page 3: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/3.jpg)
Views
Syntax:
CREATE VIEW view_name
AS SELECT column_name(s) FROM ) FROM table_name
WHERE condition
Example:
CREATE VIEW dept20 AS SELECT * FROM emp WHERE deptno=20;
SELECT * FROM dept20;
![Page 4: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/4.jpg)
Views
Example:
CREATE OR REPLACE VIEW empview
(eno, dno, ename,sal,dname)
AS SELECT empno, emp.deptno, ename, sal, dname
FROM emp, dept
WHERE emp.deptno = dept.deptno;
![Page 5: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/5.jpg)
Views
Manipulating the Base Table thru View is possible if:
- There are no aggregate functions
- No DISTINCT is used
- No GROUP BY or HAVING
Example : (Read only View)
CREATE VIEW empview (empno, tot) AS
SELECT empno, count(*) FROM incr GROUP BY empno;
![Page 6: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/6.jpg)
Views
With Check Options
CREATE VIEW empdept AS
SELECT empno, ename, deptno FROM emp WHERE deptno=30
WITH CHECK OPTION;
It will not allow to insert the following row:
INSERT INTO empdept VALUES (1234, ‘JAMILA’, 20)
![Page 7: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/7.jpg)
Updatable Views
![Page 8: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/8.jpg)
Updatable Views
![Page 9: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/9.jpg)
GRANT
Granting Privileges on Columns
INSERT, UPDATE or REFERENCES privileges can be
granted on individual columns
Example :
GRANT UPDATE (acct_no) ON accts TO user1;
![Page 10: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/10.jpg)
![Page 11: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/11.jpg)
![Page 12: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/12.jpg)
![Page 13: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/13.jpg)
![Page 14: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/14.jpg)
![Page 15: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/15.jpg)
![Page 16: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/16.jpg)
ROWID
Every tuple in a database has a pseudo-column ROWID that is used to
identify tuples.
Example:
To select a particular row (2nd) –
Select * from emp a where 2=(select count(rowid) from emp b where
a.rowid>=b.rowid);
To select a range of rows (91 to 100) –
Select * from (select ename,rownum rn from emp where rownum<101)
where rn between 91 and 100;
![Page 17: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/17.jpg)
![Page 18: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/18.jpg)
![Page 19: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/19.jpg)
![Page 20: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/20.jpg)
![Page 21: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/21.jpg)
![Page 22: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/22.jpg)
![Page 23: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/23.jpg)
![Page 24: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/24.jpg)
![Page 25: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/25.jpg)
![Page 26: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/26.jpg)
![Page 27: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/27.jpg)
![Page 28: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/28.jpg)
![Page 29: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/29.jpg)
![Page 30: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/30.jpg)
![Page 31: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/31.jpg)
![Page 32: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/32.jpg)
![Page 33: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/33.jpg)
To create a trigger for the emp table, which makes the entry in ename
column in UPPERCASE
CREATE OR REPLACE TRIGGER upper_trig
BEFORE INSERT OR UPDATE OF ename ON emp
FOR EACH ROW
BEGIN
:new.ename := UPPER (:new.ename);
END;
:new – keyword refer to new value of the column
:old – keyword refer to old value of the column
EXAMPLE
![Page 34: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/34.jpg)
Write a trigger total_salary to maintain a derived column totsal that stores
total salary of all the members in a department.
EXAMPLE
![Page 35: Seema Sirpal Delhi University Computer Centrepeople.du.ac.in/~ssirpal/presentations/Session 7-Views-Triggers-etc.… · • The fields in a view are fields from one or more real tables](https://reader034.vdocuments.mx/reader034/viewer/2022042121/5e9bb62ae8ed6d6c0c1e1e88/html5/thumbnails/35.jpg)
DROP TRIGGER t1;
DROPPING A TRIGGER