7. 6
Triggers
Connected to:
Personal Oracle Database 11g Release 11.1.0.6.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create table student_aduit
2 (student_id Number(8,0),
3 salutation varchar2(5),
4 first_name varchar2(25),
5 last_name varchar2(25),
6 street_address varchar2(50),
7 zip varchar2(5),
8 phone varchar2(15),
9 record_id number,
10 modified_by varchar2(30)
11 );
Table created.
SQL> create sequence studentrecord_aduit_seq
2 start with 09081
3 increment by 45
4 nocycle
5 nocache;
Sequence created.
8. 7
SQL> create or replace trigger student_aduit_RD
2 before insert on student_aduit
3 for each row
4 begin
5 /* fill in the record_id with the primary key values created by sequence*/
6 select studentrecord_aduit_seq.nextval
7 into :NEW.record_id
8 from dual;
9 end student_aduit_RD;
10 /
Trigger created.
SQL> Alter table student_aduit
2 add modified_date date;
Table altered.
SQL> Alter table student_aduit
2 add EMPLOYER varchar2(50);
Table altered.
SQL> Alter table student_aduit
2 add REGISTRATION_DATE date;
Table altered.
SQL> Alter table student_aduit
2 add created_by varchar2(50);
Table altered.
SQL>
SQL> Alter table student_aduit
2 add CREATED_DATE date;
Table altered.
9. 8
SQL>descstudent_aduit;
Name Null? Type
----------------------------------------- -------- ----------------------------
STUDENT_ID NUMBER(8)
SALUTATION VARCHAR2(5)
FIRST_NAME VARCHAR2(25)
LAST_NAME VARCHAR2(25)
STREET_ADDRESS VARCHAR2(50)
ZIP VARCHAR2(5)
PHONE VARCHAR2(15)
RECORD_ID NUMBER
MODIFIED_BY VARCHAR2(30)
MODIFIED_DATE DATE
EMPLOYER VARCHAR2(50)
REGISTRATION_DATE DATE
CREATED_BY VARCHAR2(50)
CREATED_DATE DATE
SQL> Alter table student_aduit
2 add action_typechar(12);
Table altered.
SQL> Create or replace trigger studentrecd_inst_trgg
2 after insert on student
3 for each row
4 begin
5 insert into student_aduit(student_id,salutation,first_name, last_name,
6 street_address, zip, phone, employer, registration_date, created_by, created_date)
7 select :New.student_id,:New.salutation, :New.first_name. :New.last_name,
8 :NEW.street_address, :New.zip, :New.phone, :New.employer, :New.registration_date,
9 :NEW.created_by, :NEW.created_date FROM DUAL;
10 ENDstudentrecd_inst_trgg;
11 /
10. 9
Warning: Trigger created with compilation errors.
SQL> SHOW ERRORS;
Errors for TRIGGER STUDENTRECD_INST_TRGG:
LINE/COL ERROR
-------- -----------------------------------------------------------------
2/4 PL/SQL: SQL Statement ignored
4/60 PL/SQL: ORA-00919: invalid function
SQL> Create or replace trigger studentrecd_4
2 after insert on STUDENT
3 for each row
4 begin
5 insert into student_aduit(student_id,salutation,first_name, last_name, street_address, zip, phone, employer, registration_date, created_by, created_date)
6 select :New.student_id,:New.salutation, :New.first_name, :New.last_name,
7 :NEW.street_address, :New.zip, :New.phone, :New.employer, :New.registration_date,
8 :NEW.created_by, :NEW.created_date FROM DUAL;
9 END studentrecd_4;
10 /
Trigger created.
SQL> Create or replace trigger studentrecd_updt_trg
2 after update on student
3 for each row
11. 10
4 begin
5 insert into student_aduit(student_id,salutation,first_name, last_name, street_address, zip, phone, employer, registration_date, modified_by,
modified_date)
6 select :New.student_id,:New.salutation, :New.first_name, :New.last_name,
7 :NEW.street_address, :New.zip, :New.phone, :New.employer, :New.registration_date,
8 :NEW.modified_by, :NEW.modified_date FROM DUAL;
9 ENDstudentrecd_updt_trg;
10 /
Trigger created.
Create or replace trigger studentrecd_delt_trg
after delete on student
for each row
begin
insert into student_aduit(student_id,salutation,first_name, last_name, street_address, zip, phone, employer, registration_date, modified_by, modified_date)
select :old.student_id,:old.salutation, :old.first_name, :old.last_name,
:old.street_address, :old.zip, :old.phone, :old.employer, :old.registration_date,
:old.modified_by, :old.modified_date FROM DUAL;
END studentrecd_delt_trg;
/
Trigger created.
insert into student(salutation, first_name, last_name, street_address,
zip,phone,employer,registration_date, created_by, created_date, student_id, modified_by ,MODIFIED_DATE)
values('Mr', 'Jackson','Philp','1900 E Bellview Apt 341D','07055',
'7089809812','County Foods',(select sysdate from dual),'Jackson',(select sysdate from dual), 805678, 'NA',
(selectsysdate from dual));
1 row created.
select * from student where student_id = 805678
12. 11
STUDENT_ID SALUTATION FIRST_NAME LAST_NAME STREET_ADDRESS ZIP PHONE EMPLOYER
REGISTRATION_DATE CREATED_BY CREATED_DATE MODIFIED_BY MODIFIED_DATE
---------------------- ---------- ------------------------- ------------------------- -------------------------------------------------- ----- --------------- -------------------------------------------------- -----
-------------------- ------------------------------ ------------------------- ------------------------------ -------------------------
805678 Mr Jackson Philp 1900 E Bellview Apt 341D 07055 7089809812 County Foods
15-MAR-13 Jackson 15-MAR-13 NA 15-MAR-13
update student
set last_name ='Mark',
modified_by='Jackson',
Modified_date = (select sysdate from dual)
wherestudent_id = 805678;
select * from student_aduit;
STUDE
NT_ID
SALUT
ATION
FIRST_
NAME
LAST_
NAME
STREET_ADD
RESS
ZIP PHON
E
RECO
RD_I
D
MODIF
IED_BY
MODIFIE
D_DATE
EMPL
OYER
REGISTRATIO
N_DATE
CREAT
ED_BY
CREATED_D
ATE
ACTION_T
YPE
80567
8
Mr Jackso
n
Philp 1900 E
Bellview Apt
341D
705
5
70898
09812
9171 Coun
ty
Foods
3/15/2013 Jackso
n
3/15/2013
80567
8
Mr Jackso
n
Philp 1900 E
Bellview Apt
341D
705
5
70898
09812
9216 Coun
ty
Foods
3/15/2013 Jackso
n
3/15/2013
80567
8
Mr Jackso
n
Mark 1900 E
Bellview Apt
341D
705
5
70898
09812
9261 Jackso
n
3/15/201
3
Coun
ty
Foods
3/15/2013
13. 12
SQL> delete student where student_id = 805678;
1 row deleted.
SQL> commit;
select * from student_aduit;
STUDE
NT_ID
SALUT
ATION
FIRST_
NAME
LAST_
NAME
STREET_ADDR
ESS
ZI
P
PHON
E
RECOR
D_ID
MODIFI
ED_BY
MODIFIE
D_DATE
EMPLO
YER
REGISTRATI
ON_DATE
CREAT
ED_BY
CREATED
_DATE
ACTION
_TYPE
805678 Mr Jackson Philp 1900 E
Bellview Apt
341D
70
55
70898
09812
9171 County
Foods
3/15/2013 Jackson 3/15/20
13
805678 Mr Jackson Philp 1900 E
Bellview Apt
341D
70
55
70898
09812
9216 County
Foods
3/15/2013 Jackson 3/15/20
13
805678 Mr Jackson Mark 1900 E
Bellview Apt
341D
70
55
70898
09812
9261 Jackson 3/15/201
3
County
Foods
3/15/2013
805678 Mr Jackson Mark 1900 E
Bellview Apt
341D
70
55
70898
09812
9306 Jackson 3/15/201
3
County
Foods
3/15/2013
14. 13
selecttrigger_name, trigger_type, triggering_event, table_name,
column_name, action_type, trigger_body,REFERENCING_NAMES ,CROSSEDITION
fromuser_triggers
TRIGGER_NAM
E
TRIGGE
R_TYPE
TRIGGERIN
G_EVENT
TABLE_
NAME
COLUMN
_NAME
ACTION
_TYPE
TRIGGER_BODY REFERENCI
NG_NAMES
CROSSE
DITION
STUDENTRECD
_INST_TRGG
AFTER
EACH
ROW
INSERT STUDENT PL/SQL begin
insert into
student_aduit(student_id,s
alutation,first_name,
last_name,
street_address,
REFERENCIN
G NEW AS
NEW OLD AS
OLD
NO
STUDENTRECD
_UPDT_TRG
AFTER
EACH
ROW
UPDATE STUDENT PL/SQL begin
insert into
student_aduit(student_id,s
alutation,first_name,
last_name, street_address,
zip,
REFERENCIN
G NEW AS
NEW OLD AS
OLD
NO
STUDENT_ADUI
T_RD
BEFORE
EACH
ROW
INSERT STUDENT
_ADUIT
PL/SQL begin
/* fill in the record_id
with the primary key
values created by
sequence*/
select studentrec
REFERENCIN
G NEW AS
NEW OLD AS
OLD
NO
STUDENTRECD
_4
AFTER
EACH
ROW
INSERT STUDENT PL/SQL begin
insert into
student_aduit(student_id,s
alutation,first_name,
last_name, street_address,
zip,
REFERENCIN
G NEW AS
NEW OLD AS
OLD
NO
STUDENTRECD
_DELT_TRG
AFTER
EACH
ROW
DELETE STUDENT PL/SQL begin
insert into
student_aduit(student_id,s
alutation,first_name,
REFERENCIN
G NEW AS
NEW OLD AS
OLD
NO
16. 15
Stored Procedures
SQL> Create table cities
2 (city_namechar(45),
3 Location_Statechar(45),
4 country char(45)
5 );
Table created.
SQL>
SQL> create or replace procedure city
2 (city_namecity.city_name%TYPE,
3 Location_statecity.Location_State%TYPE,
4 country city.country%TYPE
5 )
6 AS BEGIN
7 INSERT INTO city(city_name, location_state, country)
8 values(city_name, location_state, country);
9 End city;
10 /
Warning: Procedure created with compilation errors.
SQL> show errors;
Errors for PROCEDURE CITY:
LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0 PL/SQL: Compilation unit analysis terminated
2/13 PLS-00225: subprogram or cursor 'CITY' reference is out of scope
SQL> create or replace procedure city
2 (city_namecities.city_name%TYPE,
3 Location_statecities.Location_State%TYPE,
17. 16
4 country cities.country%TYPE
5 )
6 AS BEGIN
7 INSERT INTO city(city_name, location_state, country)
8 values(city_name, location_state, country);
9 End city;
10 /
Warning: Procedure created with compilation errors.
SQL> show errors;
Errors for PROCEDURE CITY:
LINE/COL ERROR
-------- -----------------------------------------------------------------
7/7 PL/SQL: SQL Statement ignored
7/19 PL/SQL: ORA-04044: procedure, function, package, or type is not
allowed here
create or replace procedure city_insert
(city_namecities.city_name%TYPE,
Location_statecities.Location_State%TYPE,
countrycities.country%TYPE
)
AS BEGIN
INSERT INTO cities(city_name, location_state, country)
values(city_name, location_state, country);
End city_insert;
/
Procedure created.
SQL> Begin
2 city_insert('Washington','DC','USA');
18. 17
3 city_insert('Chicago','IL','USA');
4 End;
5 /
PL/SQL procedure successfully completed.
Select * from cities;
CITY_NAME LOCATION_STATE COUNTRY
Washington DC USA
Chicago IL USA
/
Begin
city_insert('Portland','Or','USA');
city_insert('Portland','ME','USA');
city_insert('New York','NY', 'USA');
End;
/
Select * from cities;
CITY_NAME LOCATION_STATE COUNTRY
Washington DC USA
Chicago IL USA
Portland Or USA
Portland ME USA
New York NY USA
20. 19
AS BEGIN
INSERT INTO cities(city_name,city_id, location_state, country)
values(city_name, city_id, location_state, country);
End city_insert;
/
SQL> Begin
2 city_insert('Portland',909,'Or','USA');
3 city_insert('Portland',890,'ME','USA');
4 city_insert('New York',788,'NY', 'USA');
5 city_insert('chicago',789,'IL', 'USA');
6 city_insert('Washington',787,'DC', 'USA');
7 End;
8 /
PL/SQL procedure successfully completed.
Select * from cities
CITY_NAME LOCATION_STATE COUNTRY CITY_ID
Portland Or USA 909
Portland ME USA 890
New York NY USA 788
chicago IL USA 789
Washington DC USA 787
21. 20
SQL> begin
2 city_update('IL','787');
3 end;
4 /
Select * from cities
CITY_NAME LOCATION_STATE COUNTRY CITY_ID
Portland Or USA 909
Portland ME USA 890
New York NY USA 788
chicago IL USA 789
Washington IL USA 787
SQL> create or replace procedure city_delete
2 (
3 c_idcities.city_id%TYPE
4 )AS Begin
5 delete cities
6 where city_id = c_id;
7 endcity_delete;
8 /
Procedure created.
22. 21
Select * from cities
CITY_NAME LOCATION_STATE COUNTRY CITY_ID
Portland Or USA 909
Portland ME USA 890
New York NY USA 788
chicago IL USA 789
SQL>
SQL> create or replace procedure studentselect
2 (stud_id in student.student_id%TYPE,
3 salut OUT student.salutation%TYPE,
4 F_name OUT student.first_name%TYPE,
5 L_Name OUT student.Last_name%TYPE,
6 S_Add OUTstudent.Street_Address%TYPE,
7 ZIP OUTstudent.ZIP%TYPE,
8 Phone OUT student.phone%TYPE,
9 EMP OUT student.Employer%Type,
10 RegdateOUT student.Registration_Date%TYPE,
11 Crdate OUT student.Created_date%TYPE,
12 CBY OUT student.created_by%TYPE,
13 MBY OUTstudent.Modified_by%TYPE,
14 MdateOUT student.Modified_date%Type)
15 IS
16 BEGIN
17 SELECT
18 initcap( SALUTATION)as SALUTATION, initcap (FIRST_NAME)as First_Name
nitcap (LAST_NAME)as Last_Name,
19 initcap(STREET_ADDRESS)as Street_Address,
30. 29
35 /
Name :Mr.Asian,Chirichella
STUDENT_ADDRESS : 134-25 Franklin Ave. #512
CITY : Flushing
R_STATE : NY
ZIP: 11355
PHONE: 718-555-5555
EMPLOYER :Peo Capital Corp.
REGISTRATION_DATE : February -23-2007
CREATED_DATE: February -23-2007
CREATED_BY: BROSENZWEIG
MODIFIED_BY: BROSENZW
MODIFIED_DATE: February -26-2007
PL/SQL procedure successfully completed.
SQL>
SELECT * FROM USER_PROCEDURES WHERE OBJECT_TYPE = 'PROCEDURE' ;
31. 30
SELECT * FROM USER_PROCEDURES WHERE OBJECT_TYPE ='PACKAGE';
OBJECT_NAME PROCEDURE_NAME OBJECT_ID SUBPROGRAM_ID OBJECT_TYPE AGGREGATE PIPELINED PARALLEL INTERFACE DETERMINISTIC AUTHID
STUDENTSELECT 72969 1 PROCEDURE NO NO NO NO NO DEFINER
CITY_DELETE 72966 1 PROCEDURE NO NO NO NO NO DEFINER
CITY_INSERT 72964 1 PROCEDURE NO NO NO NO NO DEFINER
CITY_UPDATE 72965 1 PROCEDURE NO NO NO NO NO DEFINER
CITY 72963 1 PROCEDURE NO NO NO NO NO DEFINER
STUDENT_SELECT 72968 1 PROCEDURE NO NO NO NO NO DEFINER
OBJECT_NAME PROCEDURE_NAME OBJECT_ID SUBPROGRAM_ID OBJECT_TYPE AGGREGATE PIPELINED PARALLEL INTERFACE DETERMINISTIC AUTHID
CITIESDB UPDATECITY 73066 1 PACKAGE NO NO NO NO NO DEFINER
CITIESDB DELETECITY 73066 2 PACKAGE NO NO NO NO NO DEFINER
CITIESDB 73066 0 PACKAGE NO NO NO NO NO DEFINER
32. 31
Packages
SQL> CREATE OR REPLACE PACKAGE CitiesDB as
2 PROCEDURE updatecity(CID IN CITIES.CITY_ID%TYPE,
3 CSTATE IN CITIES.LOCATION_STATE%TYPE,
4 CCITY IN CITIES.CITY_NAME%TYPE );
5 PROCEDURE DELETECITY(CID IN CITIES.CITY_ID%TYPE);
6
7 END CitiesDB;
8 /
Package created.
SQL> CREATE OR REPLACE PACKAGE BODY CitiesDB as
2 PROCEDURE updatecity(CID IN CITIES.CITY_ID%TYPE,
3 CSTATE IN CITIES.LOCATION_STATE%TYPE,
4 CCITY IN CITIES.CITY_NAME%TYPE ) IS
5 BEGIN
6 UPDATE CITIES
7 SET CITY_NAME = CCITY, LOCATION_STATE = CSTATE
8 WHERE CITY_ID = CID;
9 END updatecity;
10
11 PROCEDURE DELETECITY(CID IN CITIES.CITY_ID%TYPE) IS
12 BEGIN
13 DELETE FROM CITIES
14 WHERE CITY_ID = CID;
15 END DELETECITY;
16
17 END CitiesDB;
18 /
Package body created.
33. 32
SQL> BEGIN
2 CitiesDB.updatecity(789,'PA', 'CHICAGO');
3 END;
4 /
PL/SQL procedure successfully completed.
SELECT * FROM CITIES;
CITY_NAME LOCATION_STATE COUNTRY CITY_ID
Portland Or USA 909
Portland ME USA 890
New York NY USA 788
CHICAGO PA USA 789
SQL>
SQL> BEGIN
2 CitiesDB.updatecity(909, 'WA','PORTLAND' );
3 CitiesDB.DELETECITY(890);
4 END;
5 /
PL/SQL procedure successfully completed.
CITY_NAME LOCATION_STATE COUNTRY CITY_ID
PORTLAND WA USA 909
New York NY USA 788
CHICAGO PA USA 789
34. 33
Functions
SQL> CREATE OR REPLACE FUNCTION STUDENTNAME
2 (studid in student.student_id%TYPE
3 )
4 RETURN
5 VARCHAR2 IS
6
7 STUDENT_NAME VARCHAR2(50);
8 BEGIN
9
10 SELECT INITCAP (S.FIRST_NAME||','||S.LAST_NAME)AS NAME
11 INTO
12 STUDENT_NAME
13 FROM STUDENT S
14 WHERE S.STUDENT_ID = studid;
15
16 RETURN STUDENT_NAME;
17
18 END STUDENTNAME;
19 /
Function created.
SQL> COMMIT;
Commit complete.
35. 34
SQL>
SQL> SELECT STUDENTNAME(STUDENT_ID)STUDENT_NAME
2 FROM STUDENT
3 WHERE STUDENT_ID =120;
STUDENT_NAME
---------------------------------------------------
Ralph,Alexander
SQL>
SQL> SELECT STUDENTNAME(S.STUDENT_ID)STUDENT_NAME
2 ,S.STUDENT_ID, SUM(G.NUMERIC_GRADE)AS GRADE,
3 G.SECTION_ID
4 FROM STUDENT S
5 INNER JOIN GRADE G
6 ON
7 S.STUDENT_ID = G.STUDENT_ID
8 WHERE S.STUDENT_ID = 120
9 AND
10 G.SECTION_ID = 103
11 GROUP BY STUDENTNAME(S.STUDENT_ID), S.STUDENT_ID, G.SECTION_ID;
STUDENT_NAME
--------------------------------------------------------------------------
STUDENT_ID GRADE SECTION_ID
---------- ---------- ----------
Ralph,Alexander
120 751 103
36. 35
SQL> GROUP BY STUDENTNAME(S.STUDENT_ID), S.STUDENT_ID, G.SECTION_ID;
SP2-0734: unknown command beginning "GROUP BY S..." - rest of line ignored.
SQL>SELECT STUDENTNAME(S.STUDENT_ID)STUDENT_NAME
2 ,S.STUDENT_ID, SUM(G.NUMERIC_GRADE)AS GRADE,
3 G.SECTION_ID
4 FROM STUDENT S
5 INNER JOIN GRADE G
6 ON
7 S.STUDENT_ID = G.STUDENT_ID
8 WHERE S.STUDENT_ID IN( 120,211)
9 GROUP BY STUDENTNAME(S.STUDENT_ID), S.STUDENT_ID, G.SECTION_ID;
STUDENT_NAME
--------------------------------------------------------------------------------
STUDENT_ID GRADE SECTION_ID
---------- ---------- ----------
Jenny,Goldsmith
211 798 141
Jenny,Goldsmith
211 943 86
Ralph,Alexander
120 751 103
create OR REPLACE view S_GRADES
(STUDENT_NAME, STUDENT_ID, GRADE, INSTRUCTOR_NAME, SECTION_ID, DESCRIPTION)
as
SELECT S.salutation||''||S.first_name ||','||S.last_name as Student_Name
,S.STUDENT_ID, SUM(G.NUMERIC_GRADE)AS GRADE, I.salutation||''||I.first_name ||','||I.last_name as Instructor_Name,
G.SECTION_ID, C.DESCRIPTION as DESCRIPTION
37. 36
FROM STUDENT S
INNER JOIN GRADE G
ON
S.STUDENT_ID = G.STUDENT_ID
INNER JOIN SECTION T
ON
G.SECTION_ID = T.SECTION_ID
INNER JOIN COURSE C
ON
C.COURSE_NO = T.COURSE_NO
INNER JOIN INSTRUCTOR I
ON
T.INSTRUCTOR_ID = I.INSTRUCTOR_ID
group by S.salutation||''||S.first_name ||','||S.last_name, S.STUDENT_ID, I.salutation||''||I.first_name ||','||I.last_name,
G.SECTION_ID, C.DESCRIPTION;
38. 37
Cursors
Explicit Cursors
SQL> show user;
USER is "SUNNY"
SQL>
SQL> set serveroutput on
SQL> declare
2 salustudent.salutation%type;
3 fnamestudent.first_name%type;
4 lnamestudent.last_name%type;
5 saddressstudent.street_address%TyPE;
6 scityzipcode.city%Type;
7 sstatezipcode.state%Type;
8 szipzipcode.zip%type;
9 sphonestudent.phone%type;
10 sregdatevarchar2(80) :=0;
11 rowcount PLS_INTEGER :=0;
12
13 cursorstudrec
14 is
15 SELECT S.SALUTATION ,S.FIRST_NAME, S.LAST_NAME,
16 S.STREET_ADDRESS,Z.CITY,Z.STATE,Z.ZIP,S.PHONE, TO_CHAR(S.REGISTRATION_DATE,'MON-DD-RRRR') AS REGISTRATION_DATE
17 FROM STUDENT S INNER JOIN ZIPCODE Z
18 ON
19 S.ZIP = Z.ZIP;
20
21 begin
22
23 openstudrec;
24 loop
25 fetchstudrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate;
26 EXIT when studrec%NOTFOUND;
27 rowcount :=studrec%ROWCOUNT;
28
29 DBMS_OUTPUT.PUT_LINE(rowcount ||' ' ||'is the number of rows processed ');
30 DBMS_OUTPUT.PUT_LINE('Student Name:'||' '||salu||' '||fname||' '||lname);
31 DBMS_OUTPUT.PUT_LINE('Address:'||saddress);
32 DBMS_OUTPUT.PUT_LINE('City:'||scity);
39. 38
33 DBMS_OUTPUT.PUT_LINE('State:'||sstate);
34 DBMS_OUTPUT.PUT_LINE('Phone:'||sphone);
35 DBMS_OUTPUT.PUT_LINE('REGISTRATION DATE:'||sregdate);
36 END LOOP;
37 CLOSEstudrec;
38
39 IFstudrec%ISOPEN = FALSE
40 THEN
41 DBMS_OUTPUT.PUT_LINE('Cursor is closed');
42 ELSE
43 DBMS_OUTPUT.PUT_LINE('Cursor is open');
44 END IF;
45 EXCEPTION
46 WHEN OTHERS
47 THEN
48 DBMS_OUTPUT.PUT_LINE(SQLERRM);
49 END;
50 /
1 is the number of rows processed
Student Name: Mr. Jim Joas
Address:53-33 192nd St.
City:Fresh Meadows
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
2 is the number of rows processed
Student Name: Ms. Sally Naso
Address:812 79th St.
City:North Bergen
State:NJ
Phone:201-555-5555
REGISTRATION DATE:FEB-02-2007
3 is the number of rows processed
Student Name: Mr. Frantz McLean
Address:23-08 Newtown Ave.
City:Astoria
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
40. 39
4 is the number of rows processed
Student Name: Ms. P. Balterzar
Address:30 Carriage Rd.
City:Roslyn
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
Results Abridged
SQL> set serveroutput on
SQL> declare
2 studidstudent.student_id%type;
3 salustudent.salutation%type;
4 fnamestudent.first_name%type;
5 lnamestudent.last_name%type;
6 saddressstudent.street_address%TyPE;
7 scityzipcode.city%Type;
8 sstatezipcode.state%Type;
9 szipzipcode.zip%type;
10 sphonestudent.phone%type;
11 sregdatevarchar2(80) :=0;
12 rowcount PLS_INTEGER :=0;
13
14 cursorstudrec(studidint)
15 is
16 SELECT S.SALUTATION ,S.FIRST_NAME, S.LAST_NAME,
17 S.STREET_ADDRESS,Z.CITY,Z.STATE,Z.ZIP,S.PHONE, TO_CHAR(S.REGISTRATION_DATE,'MON-DD-RRRR') AS REGISTRATION_DATE,
18 S.STUDENT_ID
19 FROM STUDENT S INNER JOIN ZIPCODE Z
20 ON
21 S.ZIP = Z.ZIP
22 wherestudent_id<studid ;
23
24 begin
25
26 openstudrec(375);
27 loop
28 fetch studrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate,studid ;
29 EXIT when studrec%NOTFOUND;
41. 40
30 rowcount :=studrec%ROWCOUNT;
31
32 DBMS_OUTPUT.PUT_LINE(rowcount ||' ' ||'is the number of rows processed ');
33 DBMS_OUTPUT.PUT_LINE('Student ID:'||' '||studid);
34 DBMS_OUTPUT.PUT_LINE('Student Name:'||' '||salu||' '||fname||' '||lname);
35 DBMS_OUTPUT.PUT_LINE('Address:'||saddress);
36 DBMS_OUTPUT.PUT_LINE('City:'||scity);
37 DBMS_OUTPUT.PUT_LINE('State:'||sstate);
38 DBMS_OUTPUT.PUT_LINE('Phone:'||sphone);
39 DBMS_OUTPUT.PUT_LINE('REGISTRATION DATE:'||sregdate);
40 END LOOP;
41 CLOSEstudrec;
42
43 IFstudrec%ISOPEN = FALSE
44 THEN
45 DBMS_OUTPUT.PUT_LINE('Cursor is closed');
46 ELSE
47 DBMS_OUTPUT.PUT_LINE('Cursor is open');
48 END IF;
49 EXCEPTION
50 WHEN OTHERS
51 THEN
52 DBMS_OUTPUT.PUT_LINE(SQLERRM);
53 END;
54 /
1 is the number of rows processed
Student ID: 167
Student Name: Mr. Jim Joas
Address:53-33 192nd St.
City:Fresh Meadows
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
2 is the number of rows processed
Student ID: 168
Student Name: Ms. Sally Naso
Address:812 79th St.
City:North Bergen
State:NJ
42. 41
Phone:201-555-5555
REGISTRATION DATE:FEB-02-2007
3 is the number of rows processed
Student ID: 169
Student Name: Mr. Frantz McLean
Address:23-08 Newtown Ave.
City:Astoria
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
4 is the number of rows processed
Student ID: 170
Student Name: Ms. P. Balterzar
Address:30 Carriage Rd.
City:Roslyn
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
Results Abridged
SQL> set serveroutput on
SQL> declare
2 studidstudent.student_id%type;
3 salustudent.salutation%type;
4 fnamestudent.first_name%type;
5 lnamestudent.last_name%type;
6 saddressstudent.street_address%TyPE;
7 scityzipcode.city%Type;
8 sstatezipcode.state%Type;
9 szipzipcode.zip%type;
10 sphonestudent.phone%type;
11 sregdatevarchar2(80) :=0;
12 rowcount PLS_INTEGER :=0;
13
14 cursorstudrec(studidint)
15 is
16 SELECT S.SALUTATION ,S.FIRST_NAME, S.LAST_NAME,
17 S.STREET_ADDRESS,Z.CITY,Z.STATE,Z.ZIP,S.PHONE, TO_CHAR(S.REGISTRATION_DATE,'MON-DD-RRRR') AS REGISTRATION_DATE,
18 S.STUDENT_ID
43. 42
19 FROM STUDENT S INNER JOIN ZIPCODE Z
20 ON
21 S.ZIP = Z.ZIP
22 wherestudent_id != studid;
23
24 begin
25
26 openstudrec(375);
27 loop
28 fetch studrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate,studid ;
29 EXIT when studrec%NOTFOUND;
30 rowcount :=studrec%ROWCOUNT;
31
32 DBMS_OUTPUT.PUT_LINE(rowcount ||' ' ||'is the number of rows processed ');
33 DBMS_OUTPUT.PUT_LINE('Student ID:'||' '||studid);
34 DBMS_OUTPUT.PUT_LINE('Student Name:'||' '||salu||' '||fname||' '||lname);
35 DBMS_OUTPUT.PUT_LINE('Address:'||saddress);
36 DBMS_OUTPUT.PUT_LINE('City:'||scity);
37 DBMS_OUTPUT.PUT_LINE('State:'||sstate);
38 DBMS_OUTPUT.PUT_LINE('Phone:'||sphone);
39 DBMS_OUTPUT.PUT_LINE('REGISTRATION DATE:'||sregdate);
40 END LOOP;
41 CLOSEstudrec;
42
43 IFstudrec%ISOPEN = FALSE
44 THEN
45 DBMS_OUTPUT.PUT_LINE('Cursor is closed');
46 ELSE
47 DBMS_OUTPUT.PUT_LINE('Cursor is open');
48 END IF;
49 EXCEPTION
50 WHEN OTHERS
51 THEN
52 DBMS_OUTPUT.PUT_LINE(SQLERRM);
53 END;
54 /
1 is the number of rows processed
Student ID: 167
Student Name: Mr. Jim Joas
44. 43
Address:53-33 192nd St.
City:Fresh Meadows
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
2 is the number of rows processed
Student ID: 168
Student Name: Ms. Sally Naso
Address:812 79th St.
City:North Bergen
State:NJ
Phone:201-555-5555
REGISTRATION DATE:FEB-02-2007
3 is the number of rows processed
Student ID: 169
Student Name: Mr. Frantz McLean
Address:23-08 Newtown Ave.
City:Astoria
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
4 is the number of rows processed
Student ID: 170
Student Name: Ms. P. Balterzar
Address:30 Carriage Rd.
City:Roslyn
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
5 is the number of rows processed
Student ID: 171
Student Name: Ms. Denise Brownstein
Address:104-36 196th St.
City:Hollis
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
6 is the number of rows processed
Student ID: 172
45. 44
Student Name: Ms. Maria Arias
Address:Box 216
City:Bellrose
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
7 is the number of rows processed
Student ID: 173
Student Name: Mr. Oscar McGill
Address:578 E 40th ST.
City:Brooklyn
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
8 is the number of rows processed
Student ID: 174
Student Name: Mr. Michael Brown
Address:265 Hawthorne St #2D
City:Brooklyn
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
9 is the number of rows processed
Student ID: 175
Student Name: Ms. Debra Boyce
Address:294 East 98 St.
City:Brooklyn
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
10 is the number of rows processed
Student ID: 176
Student Name: Ms. Beth Satterfield
Address:140 Amity St
City:Brooklyn
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-02-2007
Results Abridged
46. 45
SQL>
SQL> set serveroutput on
SQL> declare
2 studidstudent.student_id%type;
3 salustudent.salutation%type;
4 fnamestudent.first_name%type;
5 lnamestudent.last_name%type;
6 saddressstudent.street_address%TyPE;
7 scityzipcode.city%Type;
8 sstatezipcode.state%Type;
9 szipzipcode.zip%type;
10 sphonestudent.phone%type;
11 sregdatevarchar2(80) :=0;
12 rowcount PLS_INTEGER :=0;
13
14 cursorstudrec(studidint)
15 is
16 SELECT S.SALUTATION ,S.FIRST_NAME, S.LAST_NAME,
17 S.STREET_ADDRESS,Z.CITY,Z.STATE,Z.ZIP,S.PHONE, TO_CHAR(S.REGISTRATION_DATE,'MON-DD-RRRR') AS REGISTRATION_DATE,
18 S.STUDENT_ID
19 FROM STUDENT S INNER JOIN ZIPCODE Z
20 ON
21 S.ZIP = Z.ZIP
22 wherestudent_id = studid;
23
24 begin
25
26 openstudrec(375);
27 loop
28 fetch studrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate,studid ;
29 EXIT when studrec%NOTFOUND;
30 rowcount :=studrec%ROWCOUNT;
31
32 DBMS_OUTPUT.PUT_LINE(rowcount ||' ' ||'is the number of rows processed ');
33 DBMS_OUTPUT.PUT_LINE('Student ID:'||' '||studid);
34 DBMS_OUTPUT.PUT_LINE('Student Name:'||' '||salu||' '||fname||' '||lname);
35 DBMS_OUTPUT.PUT_LINE('Address:'||saddress);
47. 46
36 DBMS_OUTPUT.PUT_LINE('City:'||scity);
37 DBMS_OUTPUT.PUT_LINE('State:'||sstate);
38 DBMS_OUTPUT.PUT_LINE('Phone:'||sphone);
39 DBMS_OUTPUT.PUT_LINE('REGISTRATION DATE:'||sregdate);
40 END LOOP;
41
42
43 IFstudrec%ISOPEN = FALSE
44 THEN
45 DBMS_OUTPUT.PUT_LINE('Cursor is closed');
46 ELSE
47 DBMS_OUTPUT.PUT_LINE('Cursor is open');
48 END IF;
49 EXCEPTION
50 WHEN OTHERS
51 THEN
52 DBMS_OUTPUT.PUT_LINE(SQLERRM);
53 END;
54 /
1 is the number of rows processed
Student ID: 375
Student Name: Mr. Jack Kasperovich
Address:98-17 162nd Ave.
City:Howard Bank
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-21-2007
Cursor is open
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL> set serveroutput on
SQL> declare
2 studidstudent.student_id%type;
3 salustudent.salutation%type;
4 fnamestudent.first_name%type;
5 lnamestudent.last_name%type;
48. 47
6 saddressstudent.street_address%TyPE;
7 scityzipcode.city%Type;
8 sstatezipcode.state%Type;
9 szipzipcode.zip%type;
10 sphonestudent.phone%type;
11 sregdatevarchar2(80) :=0;
12 rowcount PLS_INTEGER :=0;
13
14 cursorstudrec(studidint)
15 is
16 SELECT S.SALUTATION ,S.FIRST_NAME, S.LAST_NAME,
17 S.STREET_ADDRESS,Z.CITY,Z.STATE,Z.ZIP,S.PHONE, TO_CHAR(S.REGISTRATION_DATE,'MON-DD-RRRR') AS REGISTRATION_DATE,
18 S.STUDENT_ID
19 FROM STUDENT S INNER JOIN ZIPCODE Z
20 ON
21 S.ZIP = Z.ZIP
22 wherestudent_id = studid;
23
24 begin
25
26 openstudrec(375);
27 loop
28 fetch studrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate,studid ;
29 EXIT when studrec%NOTFOUND;
30 rowcount :=studrec%ROWCOUNT;
31
32 DBMS_OUTPUT.PUT_LINE(rowcount ||' ' ||'is the number of rows processed ');
33 DBMS_OUTPUT.PUT_LINE('Student ID:'||' '||studid);
34 DBMS_OUTPUT.PUT_LINE('Student Name:'||' '||salu||' '||fname||' '||lname);
35 DBMS_OUTPUT.PUT_LINE('Address:'||saddress);
36 DBMS_OUTPUT.PUT_LINE('City:'||scity);
37 DBMS_OUTPUT.PUT_LINE('State:'||sstate);
38 DBMS_OUTPUT.PUT_LINE('Phone:'||sphone);
39 DBMS_OUTPUT.PUT_LINE('REGISTRATION DATE:'||sregdate);
40 END LOOP;
41 CLOSEstudrec;
42
43 IFstudrec%ISOPEN = FALSE
44 THEN
49. 48
45 DBMS_OUTPUT.PUT_LINE('Cursor is closed');
46 ELSE
47 DBMS_OUTPUT.PUT_LINE('Cursor is open');
48 END IF;
49 EXCEPTION
50 WHEN OTHERS
51 THEN
52 DBMS_OUTPUT.PUT_LINE(SQLERRM);
53 END;
54 /
1 is the number of rows processed
Student ID: 375
Student Name: Mr. Jack Kasperovich
Address:98-17 162nd Ave.
City:Howard Bank
State:NY
Phone:718-555-5555
REGISTRATION DATE:FEB-21-2007
Cursor is closed
PL/SQL procedure successfully completed.
SQL>
SQL> SET ECHO OFF;
SQL> SPOOL OFF;
Implicit Cursors
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
UPDATE STUDENT
SET FIRST_NAME = 'JACKSON'
WHERE STUDENT_ID = 375;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT||'rows updated');
50. 49
IF SQL%NOTFOUND
THEN
DBMS_OUTPUT.PUT_LINE('CHECK THE STUDENT ID ENTERED BECAUSE IS NOT FOUND IN DATABASE');
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_lINE(SQLERRM);
END;
/
anonymous block completed
1rows updated
select first_name ,last_name, student_id from student where student_id = 375
FIRST_NAME LAST_NAME STUDENT_ID
JACKSON Kasperovich 375
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
UPDATE STUDENT
SET FIRST_NAME = 'JACKSON'
WHERE STUDENT_ID = 500;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT||'rows updated');
IF SQL%NOTFOUND
THEN
DBMS_OUTPUT.PUT_LINE('CHECK THE STUDENT ID ENTERED BECAUSE IS NOT FOUND IN DATABASE');
END IF;
COMMIT;
51. 50
EXCEPTION
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_lINE(SQLERRM);
END;
anonymous block completed
0rows updated
CHECK THE STUDENT ID ENTERED BECAUSE IS NOT FOUND IN DATABASE
Records
SQL> DECLARE
2
3 Studentsstudent%rowtype;
4
5 begin
6
7 Students.student_id :=400;
8 Students.first_name :='joe';
9 Students.last_name :='Joe';
10 Students.street_address := '5909 Wood Lane Drv';
11 Students.zip := '48104';
12 Students.phone := '709-908-9876';
13 Students.Employer :='ABC NEWS';
14 Students.Registration_date := '30-jan-09';
52. 51
15 Students.Created_date := '30-jan-09';
16 Students.modified_date := '30-jan-09';
17 Students.created_by := 'sunny';
18 Students.modified_by :='sunny';
19
20
21 insert into student(student_id, first_name, last_name,street_address,zip,phone,
22 employer, registration_date, created_date, modified_date, created_by,modified_by)
23 values(Students.student_id,Students.first_name ,Students.last_name , Students.street_address,
24 Students.zip,Students.phone,Students.Employer, Students.Registration_date ,
25 Students.Created_date ,Students.modified_date, Students.created_by,
26 Students.modified_by
27 );
28
29 end;
30 /
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
select first_name,last_name,student_id from student where student_id =400;
DECLARE
TYPE studentsR is record
( student_idstudent.student_id%TYPE,
first_namestudent.first_name%TYPE,
last_namestudent.last_name%TYPE,
street_addressstudent.street_address%TYPE,
phonestudent.phone%TYPE,
zip student.zip%TYPE,
FIRST_NAME LAST_NAME STUDENT_ID
joe Joe 400
54. 53
select first_name,last_name,student_id from student where student_id =600;
FIRST_NAME LAST_NAME STUDENT_ID
Michael Joesph 600
Oracle Label Security
DBA
SQL> create user secmanidentified by s;
User created.
SQL> create user bank identified by b;
User created.
SQL> grant dba to secman;
Grant succeeded.
SQL> grant create session , resources to bank;
grant create session , resources to bank
*
ERROR at line 1:
ORA-01919: role 'RESOURCES' does not exist
SQL> grant create session , resource to bank;
55. 54
Grant succeeded.
SQL> spool off
grant execute on dbms_rls to secman;
grant execute succeeded.
grant execute on dbms_rls to system;
grant execute succeeded.
SQL> create user lara identified by l;
User created.
SQL> create user scott identified by s;
User created.
SQL> grant create session to scott, lara;
Grant succeeded.
SQL> set echo off
SQL> spool off;
USER Secman
SQL> create table access_policy
2 (AM_NAME VARCHAR2(20) NOT NULL,
3 CUST_ID NUMBER NOT NULL,
4 ACCESS_TYPE CHAR(1) NOT NULL);
Table created.
SQL>
SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)
2 VALUES('SCOTT', 123, 'S');