Oracle 11G- PLSQL

2,254 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,254
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
366
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oracle 11G- PLSQL

  1. 1. ORACLLE PLSQLPROGRAMMINGBySunny Okoro
  2. 2. 1ContentsIntroduction ..................................................................................................................................................2School Database Diagram.............................................................................................................................4Triggers..........................................................................................................................................................6Stored Procedures ......................................................................................................................................15Packages......................................................................................................................................................31Functions.....................................................................................................................................................33Cursors ........................................................................................................................................................37Records .......................................................................................................................................................50Oracle Label Security ..................................................................................................................................53
  3. 3. 2IntroductionDatabase PlatformORACLE 11GApplicationsOracle JDeveloper 11gOracle SQL Plus
  4. 4. 3Microsoft Power PivotOracle SQL Developer Data Modeler
  5. 5. 4School Database Diagram
  6. 6. 5
  7. 7. 6TriggersConnected to:Personal Oracle Database 11g Release 11.1.0.6.0 - 64bit ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> create table student_aduit2 (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_seq2 start with 090813 increment by 454 nocycle5 nocache;Sequence created.
  8. 8. 7SQL> create or replace trigger student_aduit_RD2 before insert on student_aduit3 for each row4 begin5 /* fill in the record_id with the primary key values created by sequence*/6 select studentrecord_aduit_seq.nextval7 into :NEW.record_id8 from dual;9 end student_aduit_RD;10 /Trigger created.SQL> Alter table student_aduit2 add modified_date date;Table altered.SQL> Alter table student_aduit2 add EMPLOYER varchar2(50);Table altered.SQL> Alter table student_aduit2 add REGISTRATION_DATE date;Table altered.SQL> Alter table student_aduit2 add created_by varchar2(50);Table altered.SQL>SQL> Alter table student_aduit2 add CREATED_DATE date;Table altered.
  9. 9. 8SQL>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 NUMBERMODIFIED_BY VARCHAR2(30)MODIFIED_DATE DATEEMPLOYER VARCHAR2(50)REGISTRATION_DATE DATECREATED_BY VARCHAR2(50)CREATED_DATE DATESQL> Alter table student_aduit2 add action_typechar(12);Table altered.SQL> Create or replace trigger studentrecd_inst_trgg2 after insert on student3 for each row4 begin5 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. 10. 9Warning: Trigger created with compilation errors.SQL> SHOW ERRORS;Errors for TRIGGER STUDENTRECD_INST_TRGG:LINE/COL ERROR-------- -----------------------------------------------------------------2/4 PL/SQL: SQL Statement ignored4/60 PL/SQL: ORA-00919: invalid functionSQL> Create or replace trigger studentrecd_42 after insert on STUDENT3 for each row4 begin5 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_trg2 after update on student3 for each row
  11. 11. 104 begin5 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_trgafter delete on studentfor each rowbegininsert 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. 12. 11STUDENT_ID SALUTATION FIRST_NAME LAST_NAME STREET_ADDRESS ZIP PHONE EMPLOYERREGISTRATION_DATE CREATED_BY CREATED_DATE MODIFIED_BY MODIFIED_DATE---------------------- ---------- ------------------------- ------------------------- -------------------------------------------------- ----- --------------- -------------------------------------------------- ------------------------- ------------------------------ ------------------------- ------------------------------ -------------------------805678 Mr Jackson Philp 1900 E Bellview Apt 341D 07055 7089809812 County Foods15-MAR-13 Jackson 15-MAR-13 NA 15-MAR-13update studentset last_name =Mark,modified_by=Jackson,Modified_date = (select sysdate from dual)wherestudent_id = 805678;select * from student_aduit;STUDENT_IDSALUTATIONFIRST_NAMELAST_NAMESTREET_ADDRESSZIP PHONERECORD_IDMODIFIED_BYMODIFIED_DATEEMPLOYERREGISTRATION_DATECREATED_BYCREATED_DATEACTION_TYPE805678Mr JacksonPhilp 1900 EBellview Apt341D705570898098129171 CountyFoods3/15/2013 Jackson3/15/2013805678Mr JacksonPhilp 1900 EBellview Apt341D705570898098129216 CountyFoods3/15/2013 Jackson3/15/2013805678Mr JacksonMark 1900 EBellview Apt341D705570898098129261 Jackson3/15/2013CountyFoods3/15/2013
  13. 13. 12SQL> delete student where student_id = 805678;1 row deleted.SQL> commit;select * from student_aduit;STUDENT_IDSALUTATIONFIRST_NAMELAST_NAMESTREET_ADDRESSZIPPHONERECORD_IDMODIFIED_BYMODIFIED_DATEEMPLOYERREGISTRATION_DATECREATED_BYCREATED_DATEACTION_TYPE805678 Mr Jackson Philp 1900 EBellview Apt341D705570898098129171 CountyFoods3/15/2013 Jackson 3/15/2013805678 Mr Jackson Philp 1900 EBellview Apt341D705570898098129216 CountyFoods3/15/2013 Jackson 3/15/2013805678 Mr Jackson Mark 1900 EBellview Apt341D705570898098129261 Jackson 3/15/2013CountyFoods3/15/2013805678 Mr Jackson Mark 1900 EBellview Apt341D705570898098129306 Jackson 3/15/2013CountyFoods3/15/2013
  14. 14. 13selecttrigger_name, trigger_type, triggering_event, table_name,column_name, action_type, trigger_body,REFERENCING_NAMES ,CROSSEDITIONfromuser_triggersTRIGGER_NAMETRIGGER_TYPETRIGGERING_EVENTTABLE_NAMECOLUMN_NAMEACTION_TYPETRIGGER_BODY REFERENCING_NAMESCROSSEDITIONSTUDENTRECD_INST_TRGGAFTEREACHROWINSERT STUDENT PL/SQL begininsert intostudent_aduit(student_id,salutation,first_name,last_name,street_address,REFERENCING NEW ASNEW OLD ASOLDNOSTUDENTRECD_UPDT_TRGAFTEREACHROWUPDATE STUDENT PL/SQL begininsert intostudent_aduit(student_id,salutation,first_name,last_name, street_address,zip,REFERENCING NEW ASNEW OLD ASOLDNOSTUDENT_ADUIT_RDBEFOREEACHROWINSERT STUDENT_ADUITPL/SQL begin/* fill in the record_idwith the primary keyvalues created bysequence*/select studentrecREFERENCING NEW ASNEW OLD ASOLDNOSTUDENTRECD_4AFTEREACHROWINSERT STUDENT PL/SQL begininsert intostudent_aduit(student_id,salutation,first_name,last_name, street_address,zip,REFERENCING NEW ASNEW OLD ASOLDNOSTUDENTRECD_DELT_TRGAFTEREACHROWDELETE STUDENT PL/SQL begininsert intostudent_aduit(student_id,salutation,first_name,REFERENCING NEW ASNEW OLD ASOLDNO
  15. 15. 14last_name, street_address,zip,
  16. 16. 15Stored ProceduresSQL> Create table cities2 (city_namechar(45),3 Location_Statechar(45),4 country char(45)5 );Table created.SQL>SQL> create or replace procedure city2 (city_namecity.city_name%TYPE,3 Location_statecity.Location_State%TYPE,4 country city.country%TYPE5 )6 AS BEGIN7 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 terminated2/13 PLS-00225: subprogram or cursor CITY reference is out of scopeSQL> create or replace procedure city2 (city_namecities.city_name%TYPE,3 Location_statecities.Location_State%TYPE,
  17. 17. 164 country cities.country%TYPE5 )6 AS BEGIN7 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 ignored7/19 PL/SQL: ORA-04044: procedure, function, package, or type is notallowed herecreate or replace procedure city_insert(city_namecities.city_name%TYPE,Location_statecities.Location_State%TYPE,countrycities.country%TYPE)AS BEGININSERT INTO cities(city_name, location_state, country)values(city_name, location_state, country);End city_insert;/Procedure created.SQL> Begin2 city_insert(Washington,DC,USA);
  18. 18. 173 city_insert(Chicago,IL,USA);4 End;5 /PL/SQL procedure successfully completed.Select * from cities;CITY_NAME LOCATION_STATE COUNTRYWashington DC USAChicago IL USA/Begincity_insert(Portland,Or,USA);city_insert(Portland,ME,USA);city_insert(New York,NY, USA);End;/Select * from cities;CITY_NAME LOCATION_STATE COUNTRYWashington DC USAChicago IL USAPortland Or USAPortland ME USANew York NY USA
  19. 19. 18create or replace procedure city_update(L_statecities.location_state%TYPE,c_idcities.city_id%TYPE)AS Beginupdate citiessetlocation_state = L_statewherecity_id = c_id;endcity_update;/Procedure created.SQL> begin2 city_update(New York,PA);3 end;4 /PL/SQL procedure successfully completed.SQL> ALTER TABLE CITIES ADD CITY_ID NUMBER;Table altered.SQL> DELETE CITIES;5 rows deleted.create or replace procedure city_insert(city_namecities.city_name%TYPE,city_idcities.city_id%TYPE,Location_statecities.Location_State%TYPE,countrycities.country%TYPE)
  20. 20. 19AS BEGININSERT INTO cities(city_name,city_id, location_state, country)values(city_name, city_id, location_state, country);End city_insert;/SQL> Begin2 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 citiesCITY_NAME LOCATION_STATE COUNTRY CITY_IDPortland Or USA 909Portland ME USA 890New York NY USA 788chicago IL USA 789Washington DC USA 787
  21. 21. 20SQL> begin2 city_update(IL,787);3 end;4 /Select * from citiesCITY_NAME LOCATION_STATE COUNTRY CITY_IDPortland Or USA 909Portland ME USA 890New York NY USA 788chicago IL USA 789Washington IL USA 787SQL> create or replace procedure city_delete2 (3 c_idcities.city_id%TYPE4 )AS Begin5 delete cities6 where city_id = c_id;7 endcity_delete;8 /Procedure created.
  22. 22. 21Select * from citiesCITY_NAME LOCATION_STATE COUNTRY CITY_IDPortland Or USA 909Portland ME USA 890New York NY USA 788chicago IL USA 789SQL>SQL> create or replace procedure studentselect2 (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 IS16 BEGIN17 SELECT18 initcap( SALUTATION)as SALUTATION, initcap (FIRST_NAME)as First_Namenitcap (LAST_NAME)as Last_Name,19 initcap(STREET_ADDRESS)as Street_Address,
  23. 23. 2220 ZIP, PHONE, initcap (EMPLOYER)as Employer, REGISTRATION_DATE,21 CREATED_BY, CREATED_DATE,22 MODIFIED_BY, MODIFIED_DATE23 INTO24 salut,F_name,L_Name,S_Add,ZIP,Phone,EMP,Regdate,CBY,Crdate,MBY,Mdate25 FROM STUDENT26 WHERE STUDENT_ID = stud_id;27 ENDstudentselect;28 /Procedure created.SQL>SQL> DECLARE2 salutstudent.salutation%TYPE;3 F_namestudent.first_name%TYPE;4 L_Namestudent.Last_name%TYPE;5 S_Addstudent.Street_Address%TYPE;6 ZIP student.ZIP%TYPE;7 Phone student.phone%TYPE;8 EMP student.Employer%Type;9 Regdatestudent.Registration_Date%TYPE;10 Crdatestudent.Created_date%TYPE;11 CBY student.created_by%TYPE;12 MBY student.Modified_by%TYPE;13 Mdatestudent.Modified_date%Type;14 BEGIN15 studentselect(384,salut,F_name,L_Name,S_Add,ZIP,16 Phone,EMP,Regdate,Crdate,CBY,MBY,Mdate);171819 DBMS_OUTPUT.PUT(SALUTATION : || salut);20 DBMS_OUTPUT.PUT(FIRST_NAME: || F_name);21 DBMS_OUTPUT.PUT(LAST_NAME : || L_Name);
  24. 24. 2322 DBMS_OUTPUT.PUT(STUDENT_ADDRESS : || S_Add);23 DBMS_OUTPUT.PUT(ZIP: || ZIP);24 DBMS_OUTPUT.PUT(PHONE: || Phone);25 DBMS_OUTPUT.PUT(EMPLOYER : || EMP);26 DBMS_OUTPUT.PUT(REGISTRATION_DATE : || Regdate);27 DBMS_OUTPUT.PUT(CREATED_DATE: || Crdate);28 DBMS_OUTPUT.PUT(CREATED_BY: || CBY);29 DBMS_OUTPUT.PUT(MODIFIED_BY: || MBY);30 DBMS_OUTPUT.PUT(MODIFIED_DATE: || Mdate);3132 end;33 /PL/SQL procedure successfully completed.SQL>SQL> DECLARE2 salutstudent.salutation%TYPE;3 F_namestudent.first_name%TYPE;4 L_Namestudent.Last_name%TYPE;5 S_Addstudent.Street_Address%TYPE;6 ZIP student.ZIP%TYPE;7 Phone student.phone%TYPE;8 EMP student.Employer%Type;9 Regdatestudent.Registration_Date%TYPE;10 Crdatestudent.Created_date%TYPE;11 CBY student.created_by%TYPE;12 MBY student.Modified_by%TYPE;13 Mdatestudent.Modified_date%Type;14 BEGIN15 studentselect(384,salut,F_name,L_Name,S_Add,ZIP,16 Phone,EMP,Regdate,Crdate,CBY,MBY,Mdate);1718
  25. 25. 2419 DBMS_OUTPUT.PUT_LINE(SALUTATION : || salut);20 DBMS_OUTPUT.PUT_LINE(FIRST_NAME: || F_name);21 DBMS_OUTPUT.PUT_LINE(LAST_NAME : || L_Name);22 DBMS_OUTPUT.PUT_LINE(STUDENT_ADDRESS : || S_Add);23 DBMS_OUTPUT.PUT_LINE(ZIP: || ZIP);24 DBMS_OUTPUT.PUT_LINE(PHONE: || Phone);25 DBMS_OUTPUT.PUT_LINE(EMPLOYER : || EMP);26 DBMS_OUTPUT.PUT_LINE(REGISTRATION_DATE : || Regdate);27 DBMS_OUTPUT.PUT_LINE(CREATED_DATE: || Crdate);28 DBMS_OUTPUT.PUT_LINE(CREATED_BY: || CBY);29 DBMS_OUTPUT.PUT_LINE(MODIFIED_BY: || MBY);30 DBMS_OUTPUT.PUT_lINE(MODIFIED_DATE: || Mdate);3132 end;33 /SALUTATION : Mr.FIRST_NAME: AsianLAST_NAME :ChirichellaSTUDENT_ADDRESS : 134-25 Franklin Ave. #512ZIP: 11355PHONE: 718-555-5555EMPLOYER :Peo Capital Corp.REGISTRATION_DATE : 23-FEB-07CREATED_DATE: 23-FEB-07CREATED_BY: BROSENZWEIGMODIFIED_BY: BROSENZWMODIFIED_DATE: 26-FEB-07PL/SQL procedure successfully completed.
  26. 26. 25SQL> DECLARE2 salutstudent.salutation%TYPE;3 F_namestudent.first_name%TYPE;4 L_Namestudent.Last_name%TYPE;5 S_Addstudent.Street_Address%TYPE;6 ZIP student.ZIP%TYPE;7 Phone student.phone%TYPE;8 EMP student.Employer%Type;9 Regdatestudent.Registration_Date%TYPE;10 Crdatestudent.Created_date%TYPE;11 CBY student.created_by%TYPE;12 MBY student.Modified_by%TYPE;13 Mdatestudent.Modified_date%Type;14 BEGIN15 studentselect(384,salut,F_name,L_Name,S_Add,ZIP,16 Phone,EMP,Regdate,Crdate,CBY,MBY,Mdate);171819 DBMS_OUTPUT.PUT_LINE(Name : || salut ||F_name||,||L_Name);20 DBMS_OUTPUT.PUT_LINE(STUDENT_ADDRESS : || S_Add);21 DBMS_OUTPUT.PUT_LINE(ZIP: || ZIP);22 DBMS_OUTPUT.PUT_LINE(PHONE: || Phone);23 DBMS_OUTPUT.PUT_LINE(EMPLOYER : || EMP);24 DBMS_OUTPUT.PUT_LINE(REGISTRATION_DATE : || TO_CHAR(Regdate,Month-dd-YYYY));25 DBMS_OUTPUT.PUT_LINE(CREATED_DATE: || TO_CHAR( Crdate,Month-dd-YYYY));26 DBMS_OUTPUT.PUT_LINE(CREATED_BY: || CBY);27 DBMS_OUTPUT.PUT_LINE(MODIFIED_BY: || MBY);28 DBMS_OUTPUT.PUT_lINE(MODIFIED_DATE: || TO_CHAR(Mdate, Month-dd-YYYY));29
  27. 27. 2630 end;31 /Name :Mr.Asian,ChirichellaSTUDENT_ADDRESS : 134-25 Franklin Ave. #512ZIP: 11355PHONE: 718-555-5555EMPLOYER :Peo Capital Corp.REGISTRATION_DATE : February -23-2007CREATED_DATE: February -23-2007CREATED_BY: BROSENZWEIGMODIFIED_BY: BROSENZWMODIFIED_DATE: February -26-2007PL/SQL procedure successfully completed.SQL>SQL> create or replace procedure studentselect32 (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 Phone OUT student.phone%TYPE,8 EMP OUT student.Employer%Type,9 RegdateOUT student.Registration_Date%TYPE,10 Crdate OUT student.Created_date%TYPE,11 CBY OUT student.created_by%TYPE,12 MBY OUTstudent.Modified_by%TYPE,13 MdateOUT student.Modified_date%Type,14 CTY OUT ZIPCODE.CITY%TYPE,15 STA OUT ZIPCODE.STATE%TYPE,
  28. 28. 2716 ZIP OUTstudent.ZIP%TYPE17 )18 IS19 BEGIN2021 SELECT22 initcap(S.SALUTATION)as SALUTATION, initcap (S.FIRST_NAME)as First_Name, Initcap (S.LAST_NAME)asLast_Name,23 initcap(S.STREET_ADDRESS)as Street_Address,24 PHONE, initcap (S.EMPLOYER)as Employer, S.REGISTRATION_DATE,25 S.CREATED_BY,S.CREATED_DATE,26 S.MODIFIED_BY, S.MODIFIED_DATE, Z.CITY, Z.STATE, Z.ZIP27 INTO28 salut,F_name,L_Name,S_Add,Phone,EMP,Regdate,CBY,Crdate,MBY,Mdate, CTY,STA,ZIP29 FROM STUDENT S30 INNER JOIN31 ZIPCODE Z32 ON33 S.ZIP = Z.ZIP34 WHERE STUDENT_ID = stud_id;35 END studentselect3;36 /Procedure created.SQL>SQL> DECLARE2 salutstudent.salutation%TYPE;3 F_namestudent.first_name%TYPE;
  29. 29. 284 L_Namestudent.Last_name%TYPE;5 S_Addstudent.Street_Address%TYPE;6 Phone student.phone%TYPE;7 EMP student.Employer%Type;8 Regdatestudent.Registration_Date%TYPE;9 Crdatestudent.Created_date%TYPE;10 CBY student.created_by%TYPE;11 MBY student.Modified_by%TYPE;12 Mdatestudent.Modified_date%Type;13 CTY ZIPCODE.CITY%TYPE;14 STA ZIPCODE.STATE%TYPE;15 ZIP student.ZIP%TYPE;16 BEGIN17 studentselect3(384,salut,F_name,L_Name,S_Add,18 Phone,EMP,Regdate,Crdate,CBY,MBY,Mdate, CTY,STA,ZIP);192021 DBMS_OUTPUT.PUT_LINE(Name : || salut ||F_name||,||L_Name);22 DBMS_OUTPUT.PUT_LINE(STUDENT_ADDRESS : || S_Add);23 DBMS_OUTPUT.PUT_LINE(CITY : || CTY);24 DBMS_OUTPUT.PUT_LINE(R_STATE : || STA);25 DBMS_OUTPUT.PUT_LINE(ZIP: || ZIP);26 DBMS_OUTPUT.PUT_LINE(PHONE: || Phone);27 DBMS_OUTPUT.PUT_LINE(EMPLOYER : || EMP);28 DBMS_OUTPUT.PUT_LINE(REGISTRATION_DATE : || TO_CHAR(Regdate,Month-dd-YYYY));29 DBMS_OUTPUT.PUT_LINE(CREATED_DATE: || TO_CHAR( Crdate,Month-dd-YYYY));30 DBMS_OUTPUT.PUT_LINE(CREATED_BY: || CBY);31 DBMS_OUTPUT.PUT_LINE(MODIFIED_BY: || MBY);32 DBMS_OUTPUT.PUT_lINE(MODIFIED_DATE: || TO_CHAR(Mdate, Month-dd-YYYY));3334 end;
  30. 30. 2935 /Name :Mr.Asian,ChirichellaSTUDENT_ADDRESS : 134-25 Franklin Ave. #512CITY : FlushingR_STATE : NYZIP: 11355PHONE: 718-555-5555EMPLOYER :Peo Capital Corp.REGISTRATION_DATE : February -23-2007CREATED_DATE: February -23-2007CREATED_BY: BROSENZWEIGMODIFIED_BY: BROSENZWMODIFIED_DATE: February -26-2007PL/SQL procedure successfully completed.SQL>SELECT * FROM USER_PROCEDURES WHERE OBJECT_TYPE = PROCEDURE ;
  31. 31. 30SELECT * FROM USER_PROCEDURES WHERE OBJECT_TYPE =PACKAGE;OBJECT_NAME PROCEDURE_NAME OBJECT_ID SUBPROGRAM_ID OBJECT_TYPE AGGREGATE PIPELINED PARALLEL INTERFACE DETERMINISTIC AUTHIDSTUDENTSELECT 72969 1 PROCEDURE NO NO NO NO NO DEFINERCITY_DELETE 72966 1 PROCEDURE NO NO NO NO NO DEFINERCITY_INSERT 72964 1 PROCEDURE NO NO NO NO NO DEFINERCITY_UPDATE 72965 1 PROCEDURE NO NO NO NO NO DEFINERCITY 72963 1 PROCEDURE NO NO NO NO NO DEFINERSTUDENT_SELECT 72968 1 PROCEDURE NO NO NO NO NO DEFINEROBJECT_NAME PROCEDURE_NAME OBJECT_ID SUBPROGRAM_ID OBJECT_TYPE AGGREGATE PIPELINED PARALLEL INTERFACE DETERMINISTIC AUTHIDCITIESDB UPDATECITY 73066 1 PACKAGE NO NO NO NO NO DEFINERCITIESDB DELETECITY 73066 2 PACKAGE NO NO NO NO NO DEFINERCITIESDB 73066 0 PACKAGE NO NO NO NO NO DEFINER
  32. 32. 31PackagesSQL> CREATE OR REPLACE PACKAGE CitiesDB as2 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);67 END CitiesDB;8 /Package created.SQL> CREATE OR REPLACE PACKAGE BODY CitiesDB as2 PROCEDURE updatecity(CID IN CITIES.CITY_ID%TYPE,3 CSTATE IN CITIES.LOCATION_STATE%TYPE,4 CCITY IN CITIES.CITY_NAME%TYPE ) IS5 BEGIN6 UPDATE CITIES7 SET CITY_NAME = CCITY, LOCATION_STATE = CSTATE8 WHERE CITY_ID = CID;9 END updatecity;1011 PROCEDURE DELETECITY(CID IN CITIES.CITY_ID%TYPE) IS12 BEGIN13 DELETE FROM CITIES14 WHERE CITY_ID = CID;15 END DELETECITY;1617 END CitiesDB;18 /Package body created.
  33. 33. 32SQL> BEGIN2 CitiesDB.updatecity(789,PA, CHICAGO);3 END;4 /PL/SQL procedure successfully completed.SELECT * FROM CITIES;CITY_NAME LOCATION_STATE COUNTRY CITY_IDPortland Or USA 909Portland ME USA 890New York NY USA 788CHICAGO PA USA 789SQL>SQL> BEGIN2 CitiesDB.updatecity(909, WA,PORTLAND );3 CitiesDB.DELETECITY(890);4 END;5 /PL/SQL procedure successfully completed.CITY_NAME LOCATION_STATE COUNTRY CITY_IDPORTLAND WA USA 909New York NY USA 788CHICAGO PA USA 789
  34. 34. 33FunctionsSQL> CREATE OR REPLACE FUNCTION STUDENTNAME2 (studid in student.student_id%TYPE3 )4 RETURN5 VARCHAR2 IS67 STUDENT_NAME VARCHAR2(50);8 BEGIN910 SELECT INITCAP (S.FIRST_NAME||,||S.LAST_NAME)AS NAME11 INTO12 STUDENT_NAME13 FROM STUDENT S14 WHERE S.STUDENT_ID = studid;1516 RETURN STUDENT_NAME;1718 END STUDENTNAME;19 /Function created.SQL> COMMIT;Commit complete.
  35. 35. 34SQL>SQL> SELECT STUDENTNAME(STUDENT_ID)STUDENT_NAME2 FROM STUDENT3 WHERE STUDENT_ID =120;STUDENT_NAME---------------------------------------------------Ralph,AlexanderSQL>SQL> SELECT STUDENTNAME(S.STUDENT_ID)STUDENT_NAME2 ,S.STUDENT_ID, SUM(G.NUMERIC_GRADE)AS GRADE,3 G.SECTION_ID4 FROM STUDENT S5 INNER JOIN GRADE G6 ON7 S.STUDENT_ID = G.STUDENT_ID8 WHERE S.STUDENT_ID = 1209 AND10 G.SECTION_ID = 10311 GROUP BY STUDENTNAME(S.STUDENT_ID), S.STUDENT_ID, G.SECTION_ID;STUDENT_NAME--------------------------------------------------------------------------STUDENT_ID GRADE SECTION_ID---------- ---------- ----------Ralph,Alexander120 751 103
  36. 36. 35SQL> 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_NAME2 ,S.STUDENT_ID, SUM(G.NUMERIC_GRADE)AS GRADE,3 G.SECTION_ID4 FROM STUDENT S5 INNER JOIN GRADE G6 ON7 S.STUDENT_ID = G.STUDENT_ID8 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,Goldsmith211 798 141Jenny,Goldsmith211 943 86Ralph,Alexander120 751 103create OR REPLACE view S_GRADES(STUDENT_NAME, STUDENT_ID, GRADE, INSTRUCTOR_NAME, SECTION_ID, DESCRIPTION)asSELECT 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. 37. 36FROM STUDENT SINNER JOIN GRADE GONS.STUDENT_ID = G.STUDENT_IDINNER JOIN SECTION TONG.SECTION_ID = T.SECTION_IDINNER JOIN COURSE CONC.COURSE_NO = T.COURSE_NOINNER JOIN INSTRUCTOR IONT.INSTRUCTOR_ID = I.INSTRUCTOR_IDgroup 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. 38. 37CursorsExplicit CursorsSQL> show user;USER is "SUNNY"SQL>SQL> set serveroutput onSQL> declare2 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;1213 cursorstudrec14 is15 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_DATE17 FROM STUDENT S INNER JOIN ZIPCODE Z18 ON19 S.ZIP = Z.ZIP;2021 begin2223 openstudrec;24 loop25 fetchstudrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate;26 EXIT when studrec%NOTFOUND;27 rowcount :=studrec%ROWCOUNT;2829 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. 39. 3833 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;3839 IFstudrec%ISOPEN = FALSE40 THEN41 DBMS_OUTPUT.PUT_LINE(Cursor is closed);42 ELSE43 DBMS_OUTPUT.PUT_LINE(Cursor is open);44 END IF;45 EXCEPTION46 WHEN OTHERS47 THEN48 DBMS_OUTPUT.PUT_LINE(SQLERRM);49 END;50 /1 is the number of rows processedStudent Name: Mr. Jim JoasAddress:53-33 192nd St.City:Fresh MeadowsState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20072 is the number of rows processedStudent Name: Ms. Sally NasoAddress:812 79th St.City:North BergenState:NJPhone:201-555-5555REGISTRATION DATE:FEB-02-20073 is the number of rows processedStudent Name: Mr. Frantz McLeanAddress:23-08 Newtown Ave.City:AstoriaState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-2007
  40. 40. 394 is the number of rows processedStudent Name: Ms. P. BalterzarAddress:30 Carriage Rd.City:RoslynState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-2007Results AbridgedSQL> set serveroutput onSQL> declare2 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;1314 cursorstudrec(studidint)15 is16 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_ID19 FROM STUDENT S INNER JOIN ZIPCODE Z20 ON21 S.ZIP = Z.ZIP22 wherestudent_id<studid ;2324 begin2526 openstudrec(375);27 loop28 fetch studrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate,studid ;29 EXIT when studrec%NOTFOUND;
  41. 41. 4030 rowcount :=studrec%ROWCOUNT;3132 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;4243 IFstudrec%ISOPEN = FALSE44 THEN45 DBMS_OUTPUT.PUT_LINE(Cursor is closed);46 ELSE47 DBMS_OUTPUT.PUT_LINE(Cursor is open);48 END IF;49 EXCEPTION50 WHEN OTHERS51 THEN52 DBMS_OUTPUT.PUT_LINE(SQLERRM);53 END;54 /1 is the number of rows processedStudent ID: 167Student Name: Mr. Jim JoasAddress:53-33 192nd St.City:Fresh MeadowsState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20072 is the number of rows processedStudent ID: 168Student Name: Ms. Sally NasoAddress:812 79th St.City:North BergenState:NJ
  42. 42. 41Phone:201-555-5555REGISTRATION DATE:FEB-02-20073 is the number of rows processedStudent ID: 169Student Name: Mr. Frantz McLeanAddress:23-08 Newtown Ave.City:AstoriaState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20074 is the number of rows processedStudent ID: 170Student Name: Ms. P. BalterzarAddress:30 Carriage Rd.City:RoslynState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-2007Results AbridgedSQL> set serveroutput onSQL> declare2 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;1314 cursorstudrec(studidint)15 is16 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. 43. 4219 FROM STUDENT S INNER JOIN ZIPCODE Z20 ON21 S.ZIP = Z.ZIP22 wherestudent_id != studid;2324 begin2526 openstudrec(375);27 loop28 fetch studrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate,studid ;29 EXIT when studrec%NOTFOUND;30 rowcount :=studrec%ROWCOUNT;3132 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;4243 IFstudrec%ISOPEN = FALSE44 THEN45 DBMS_OUTPUT.PUT_LINE(Cursor is closed);46 ELSE47 DBMS_OUTPUT.PUT_LINE(Cursor is open);48 END IF;49 EXCEPTION50 WHEN OTHERS51 THEN52 DBMS_OUTPUT.PUT_LINE(SQLERRM);53 END;54 /1 is the number of rows processedStudent ID: 167Student Name: Mr. Jim Joas
  44. 44. 43Address:53-33 192nd St.City:Fresh MeadowsState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20072 is the number of rows processedStudent ID: 168Student Name: Ms. Sally NasoAddress:812 79th St.City:North BergenState:NJPhone:201-555-5555REGISTRATION DATE:FEB-02-20073 is the number of rows processedStudent ID: 169Student Name: Mr. Frantz McLeanAddress:23-08 Newtown Ave.City:AstoriaState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20074 is the number of rows processedStudent ID: 170Student Name: Ms. P. BalterzarAddress:30 Carriage Rd.City:RoslynState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20075 is the number of rows processedStudent ID: 171Student Name: Ms. Denise BrownsteinAddress:104-36 196th St.City:HollisState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20076 is the number of rows processedStudent ID: 172
  45. 45. 44Student Name: Ms. Maria AriasAddress:Box 216City:BellroseState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20077 is the number of rows processedStudent ID: 173Student Name: Mr. Oscar McGillAddress:578 E 40th ST.City:BrooklynState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20078 is the number of rows processedStudent ID: 174Student Name: Mr. Michael BrownAddress:265 Hawthorne St #2DCity:BrooklynState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-20079 is the number of rows processedStudent ID: 175Student Name: Ms. Debra BoyceAddress:294 East 98 St.City:BrooklynState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-200710 is the number of rows processedStudent ID: 176Student Name: Ms. Beth SatterfieldAddress:140 Amity StCity:BrooklynState:NYPhone:718-555-5555REGISTRATION DATE:FEB-02-2007Results Abridged
  46. 46. 45SQL>SQL> set serveroutput onSQL> declare2 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;1314 cursorstudrec(studidint)15 is16 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_ID19 FROM STUDENT S INNER JOIN ZIPCODE Z20 ON21 S.ZIP = Z.ZIP22 wherestudent_id = studid;2324 begin2526 openstudrec(375);27 loop28 fetch studrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate,studid ;29 EXIT when studrec%NOTFOUND;30 rowcount :=studrec%ROWCOUNT;3132 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. 47. 4636 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;414243 IFstudrec%ISOPEN = FALSE44 THEN45 DBMS_OUTPUT.PUT_LINE(Cursor is closed);46 ELSE47 DBMS_OUTPUT.PUT_LINE(Cursor is open);48 END IF;49 EXCEPTION50 WHEN OTHERS51 THEN52 DBMS_OUTPUT.PUT_LINE(SQLERRM);53 END;54 /1 is the number of rows processedStudent ID: 375Student Name: Mr. Jack KasperovichAddress:98-17 162nd Ave.City:Howard BankState:NYPhone:718-555-5555REGISTRATION DATE:FEB-21-2007Cursor is openPL/SQL procedure successfully completed.SQL>SQL>SQL> set serveroutput onSQL> declare2 studidstudent.student_id%type;3 salustudent.salutation%type;4 fnamestudent.first_name%type;5 lnamestudent.last_name%type;
  48. 48. 476 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;1314 cursorstudrec(studidint)15 is16 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_ID19 FROM STUDENT S INNER JOIN ZIPCODE Z20 ON21 S.ZIP = Z.ZIP22 wherestudent_id = studid;2324 begin2526 openstudrec(375);27 loop28 fetch studrec into salu,fname, lname, saddress,scity,sstate,szip, sphone,sregdate,studid ;29 EXIT when studrec%NOTFOUND;30 rowcount :=studrec%ROWCOUNT;3132 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;4243 IFstudrec%ISOPEN = FALSE44 THEN
  49. 49. 4845 DBMS_OUTPUT.PUT_LINE(Cursor is closed);46 ELSE47 DBMS_OUTPUT.PUT_LINE(Cursor is open);48 END IF;49 EXCEPTION50 WHEN OTHERS51 THEN52 DBMS_OUTPUT.PUT_LINE(SQLERRM);53 END;54 /1 is the number of rows processedStudent ID: 375Student Name: Mr. Jack KasperovichAddress:98-17 162nd Ave.City:Howard BankState:NYPhone:718-555-5555REGISTRATION DATE:FEB-21-2007Cursor is closedPL/SQL procedure successfully completed.SQL>SQL> SET ECHO OFF;SQL> SPOOL OFF;Implicit CursorsSET SERVEROUTPUT ONBEGINDBMS_OUTPUT.ENABLE(1000000);UPDATE STUDENTSET FIRST_NAME = JACKSONWHERE STUDENT_ID = 375;DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT||rows updated);
  50. 50. 49IF SQL%NOTFOUNDTHENDBMS_OUTPUT.PUT_LINE(CHECK THE STUDENT ID ENTERED BECAUSE IS NOT FOUND IN DATABASE);END IF;COMMIT;EXCEPTIONWHEN OTHERSTHEN DBMS_OUTPUT.PUT_lINE(SQLERRM);END;/anonymous block completed1rows updatedselect first_name ,last_name, student_id from student where student_id = 375FIRST_NAME LAST_NAME STUDENT_IDJACKSON Kasperovich 375SET SERVEROUTPUT ONBEGINDBMS_OUTPUT.ENABLE(1000000);UPDATE STUDENTSET FIRST_NAME = JACKSONWHERE STUDENT_ID = 500;DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT||rows updated);IF SQL%NOTFOUNDTHENDBMS_OUTPUT.PUT_LINE(CHECK THE STUDENT ID ENTERED BECAUSE IS NOT FOUND IN DATABASE);END IF;COMMIT;
  51. 51. 50EXCEPTIONWHEN OTHERSTHEN DBMS_OUTPUT.PUT_lINE(SQLERRM);END;anonymous block completed0rows updatedCHECK THE STUDENT ID ENTERED BECAUSE IS NOT FOUND IN DATABASERecordsSQL> DECLARE23 Studentsstudent%rowtype;45 begin67 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. 52. 5115 Students.Created_date := 30-jan-09;16 Students.modified_date := 30-jan-09;17 Students.created_by := sunny;18 Students.modified_by :=sunny;192021 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_by27 );2829 end;30 /PL/SQL procedure successfully completed.SQL> commit;Commit complete.select first_name,last_name,student_id from student where student_id =400;DECLARETYPE 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_IDjoe Joe 400
  53. 53. 52employerstudent.employer%type,registration_datestudent.registration_date%type,created_datestudent.created_date%type,modified_datestudent.modified_date%type,created_bystudent.created_by%type,modified_bystudent.modified_by%type);Students studentsR;beginStudents.student_id :=600;Students.first_name :=Michael;Students.last_name :=Joesph;Students.street_address := 5909 Wood Lane Drv;Students.zip := 48104;Students.phone := 709-908-9876;Students.Employer :=ABC NEWS;Students.Registration_date := 30-jan-09;Students.Created_date := 30-jan-09;Students.modified_date := 30-jan-09;Students.created_by := sunny;Students.modified_by :=sunny;insert into student(student_id, first_name, last_name,street_address,zip,phone,employer, registration_date, created_date, modified_date, created_by,modified_by)values(Students.student_id,Students.first_name ,Students.last_name , Students.street_address,Students.zip,Students.phone,Students.Employer, Students.Registration_date ,Students.Created_date ,Students.modified_date, Students.created_by,Students.modified_by);commit;end;/
  54. 54. 53select first_name,last_name,student_id from student where student_id =600;FIRST_NAME LAST_NAME STUDENT_IDMichael Joesph 600Oracle Label SecurityDBASQL> 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 existSQL> grant create session , resource to bank;
  55. 55. 54Grant succeeded.SQL> spool offgrant 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 offSQL> spool off;USER SecmanSQL> create table access_policy2 (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);
  56. 56. 551 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES(SCOTT, 123, I);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES(SCOTT, 123, D);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES (SCOTT, 123, U);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES (SCOTT, 456, S);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES(SCOTT, 789, S);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES(LARA, 456, S);1 row created.SQL>
  57. 57. 56SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES (LARA, 456, I);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES (LARA, 456, D);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES (LARA, 456, U);1 row created.SQL>SQL> create or replace function get_sel_cust_id2 (p_schema in varchar2,3 p_table in varchar24 )5 return varchar26 as7 l_retstrvarchar2(2000);8 begin9 if (p_schema = user) then10 l_retstr :=null;11 else12 for cust_rec in13 (select cust_id from access_policy where am_name = USER14 and access_type = S15 )loop16 l_retstr :=l_retstr||,||cust_rec.cust_id;17 end loop;18 l_retstr :=ltrim(l_retstr, ,);19 l_retstr :=CUST_ID IN (||l_retstr||);20 end if;21 return l_retstr;
  58. 58. 5722 end;23 /Function created.SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES (SECMAN, 123, S);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES(SECMAN,456,S);1 row created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES(SECMAN, 789,S);1 row created.SQL> CREATE OR REPLACE FUNCTION get_iud_cust_id2 (p_schema in varchar2,3 p_table in varchar24 ) return varchar25 as6 l_retstrvarchar2(2000);7 begin8 if(p_schema = user) then9 l_retstr := null;10 else11 forcust_rec in12 (13 select cust_id from access_policy where am_name = USER14 and access_type in (I, U, D)15 ) loop16 l_retstr := l_retstr||,||cust_rec.cust_id;17 end loop;
  59. 59. 5818 l_retstr := ltrim(l_retstr, ,);19 l_retstr := CUST_ID IN (||l_retstr||);20 end if;21 return l_retstr;22 end;23 /Function created.SQL>SQL> INSERT INTO ACCESS_POLICY(AM_NAME, CUST_ID, ACCESS_TYPE)2 VALUES(SECMAN,123, U);1 row created.SQL> SELECT GET_IUD_CUST_ID(BANK, CUSTOMERS) FROM DUAL2SQL> /GET_IUD_CUST_ID(BANK,CUSTOMERS)--------------------------------------------------------------------------------CUST_ID IN (123)SQL> SET ECHO OFFSQL> SPOOL OFFUserScottSQL> select * from BANK.CUSTOMERS;CUST_ID CUST_NAME---------- --------------------123 Jay Kulkarni456 Wim PatelSQL> select * from BANK.ACCOUNTS;ACC_ID CUST_ID BALANCE
  60. 60. 59---------- ---------- ----------101 123 10000000102 123 15000000201 456 10000000202 456 20000000SQL> SET ECHO OFF;SQL> SPOOL OFF;USER BankSQL> select * from bank.accounts;ACC_ID CUST_ID BALANCE---------- ---------- ----------101 123 10000000102 123 15000000201 456 10000000202 456 20000000SQL> select * from bank.customers;CUST_ID CUST_NAME---------- --------------------456 Wim PatelSQL> update bank.customers set cust_name = Smith Joe;1 row updated.SQL> select * from bank.customers;CUST_ID CUST_NAME---------- --------------------456 Smith JoeSQL> commit;
  61. 61. 60Commit complete.SQL> insert into bank.customers (cust_id, cust_name) values(999, Monica joe);insert into bank.customers (cust_id, cust_name) values(999, Monica joe)*ERROR at line 1:ORA-28115: policy with check option violationSQL> spool offUSER Secman2SQL>SQL> begin2 dbms_rls.add_policy (3 object_schema => BANK,4 object_name => CUSTOMERS,5 policy_name => CUST_SEL_POLICY,6 function_schema => SECMAN,7 policy_function => GET_SEL_CUST_ID,8 statement_types => SELECT,9 update_check => TRUE10 );11 end;12 /PL/SQL procedure successfully completed.SQL>SQL> begin2 dbms_rls.add_policy (3 object_schema => BANK,4 object_name => CUSTOMERS,5 policy_name => CUST_IUD_POLICY,6 function_schema => SECMAN,7 policy_function => GET_IUD_CUST_ID,
  62. 62. 618 statement_types => INSERT, UPDATE, DELETE,9 update_check => TRUE10 );11 end;12 /PL/SQL procedure successfully completed.SQL> spool off
  63. 63. 62

×