Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 442 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
442
Dung lượng
6,43 MB
Nội dung
An Introduction to Oracle DBMS Architecture and Server-Side Programming Victor Matos Department of Computer and Information Science Cleveland State University CONTENTS Introduction Oracle Architecture PL/SQL Programming Supplied Packages Collections and Records Objects Cursors Batch Processing Exceptions Packages Triggers Mutating Tables Example Package (OLD) 18 127 213 222 272 309 333 344 357 367 397 423 CREDITS This document is a collage of notes taken from Oracle Documentation Files (www.oracle.com) Fundamentals of Database Systems by Elmasri & Navathe Ed Addison Wesley Oracle – The Complete Reference by Koch & Loney Ed Oracle Press Microsoft On-Line Help files (www.microsoft.com) V Matos lecture notes What is ORACLE ? • Very robust implementation of a Relational Database Management System (RDBMS) • The relational model of data is simple to understand and has been extensively scrutinized since 1970’s What else is good about ORACLE ? • The company is solid and successful, since 1994 revenues in the order of $2+ billion/year (~ $3BD in 2005) • Very popular software, worldwide distribution (≈ 41% world market in 2007) • Runs in different type of computers Reasons for the Oracle Success • • • • • users are given specific rights to operate on the data Backup and Recovery minimizes data loss and idle time in the presence of failures Space Management flexible allocation Open Connectivity uninterrupted services reachable from heterogeneous clients Security Mechanisms Development Tools many to choose from Software Support on the Oracle Server • • • built-in programming capabilities based on the PL/SQL language Stored Procedures: programs stored in the server-side could be invoked using an ADA-like interface Triggers: dormant server-side routines which are ‘fired’ when specific maintenance conditions are met Procedural Option: Parts of the Oracle Server cont • Packages: libraries of server-side routines including proc, functions, triggers, user defined data types, constants, variables, etc ORACLE PACKAGES Various Clients ORACLE SERVER Same set of Proc, functions, datatype, const, etc Parts of the Oracle Server cont Distributed Option: • Data could be stored in different machines and placed in different cities • Oracle Distributed option provides location transparency, the user does not need to be aware of where the data is kept Oracle Distributed Option Cleveland, Ohio Ohio Inventory Workstation SELECT PartNumber, Price FROM Ohio_inventory WHERE inv_sup = Sup_Numb; Indians95 Oraccle Server , suppliers NETWORK New York, NY Suppliers Yankees96 ORACLE Server Oracle Distributed Option Homogenous Distributed Database Systems A homogenous distributed database system is a network of two or more Oracle Databases that reside on one or more machines Heterogeneous Distributed Database Systems - In a heterogeneous distributed database system, at least one of the databases is a non-Oracle Database system - To the application, the heterogeneous distributed database system appears as a single, local, Oracle Database - The local Oracle Database server hides the distribution and heterogeneity of the data 10 OLD NOTES Example of a COMPANY personnel PACKAGE 428 PL/SQL Programming Invoking a Stored Function from a SQL Query The numbers in the BaseBall table are hard to read A function BigBucks() is called to facilitated the reading of the big numbers in the table SQL> select * from baseball; PNAME PSALARY -A Belle 987654321 K Griffey 1987654322 B Bonds 1234567890 D Cone 123456 P Butterfingers 900 429 PL/SQL Programming Invoking a Stored Function from a SQL Query Playball.sql This is a small SCRIPT (not an anonymous proc.) The pretty function is called from the SQL comnd column psalary format 99999999999 column bigbucks(psalary) format a30 select pname, psalary, BigBucks(psalary) from baseball; SQL> @Playball PNAME PSALARY BIGBUCKS(PSALARY) A Belle 987654321 987.65 Millions K Griffey 1987654322 1.99 Billions B Bonds 1234567890 1.23 Billions D Cone 123456 123.46 Thousands P Butterfingers 900 900.00 Peanuts 430 PL/SQL Programming A Stored Function for Beautifying Big Numbers Create or Replace Function BIGBUCKS ( salary number ) Return varchar IS Billion number := 1000000000; Million number := 1000000; LPAD( ) adds n spaces on Thousand number := 1000; Message varchar(30); on left side of the string Dollars number(6,2); begin if (salary >= Billion) then Dollars:= salary / billion; message:= LPAD(to_char(dollars),8) || ' Billions'; return (message); elsif (salary >= Million) then Dollars:= salary / Million; message:= LPAD(to_char(dollars),8) || ' Millions'; return (message); elsif (salary >= Thousand) then Dollars:= salary / Thousand; message:= LPAD(to_char(dollars),8) || ' Thousands'; return (message); Numeric format else message:= to_char(Salary,'999.00'); message:= LPAD(message,8)||' Peanuts'; return (message); end if; end; 431 PL/SQL Programming Writing a Procedure to Securely Insert Works_On records The procedure Insert_WorksOn() is invoked to reliably add new records to the table Works_On Each call to the function provides the programmers authentication code and data (SSN, ProjNumb, Hour) For example: Result:= Insert_WorkOn(‘JB007’,123456789,10,40) • If the programmer ‘JB007’ is not in the list of people authorized to make insertions, the operation is rejected and MYLOG file is update with a rejection message • If the SSN and PNO provided are already in the Works_On table the current insertion is rejected and the log file is updated • The function returns a numeric code 1, -1, -2, etc to indicate the status of the execution This number is stored into the variable Result (1: success, negative values: problems) 432 PL/SQL Programming create or replace FUNCTION Insert_WorksOn ( Programmer_Id EmployeeSsn ProjectNumber WeeklyHours number, number, number, number ) RETURN number IS Programmer_Name varchar(20); Message varchar(70); BEGIN First block: Is the programmer allowed to insert records? Note: All code goes into one BEGIN END block BEGIN select Operator_Name into Programmer_Name from Valid_Operators where Operator_Id = Programmer_Id; EXCEPTION when No_Data_Found then Insert into Mylog values (Programmer_Id, 'ERROR** unauthorized used of function'); RETURN -1; END; first block 433 PL/SQL Programming INSERTWORKSON cont Second block: Here other conditions could be checked before accepting record into the works_on table BEGIN insert into Works_On values (EmployeeSsn, ProjectNumber, WeeklyHours); RETURN 1; EXCEPTION When Dup_Val_on_Index then Message:= ' Emp ' || to_char(EmployeeSsn) || ' already working in project ' || to_char(ProjectNumber); insert into Mylog values (NULL, Message); RETURN -2; When Others then Message:= ' Emp ' || to_char(EmployeeSsn) || ' can not be placed on project ' || to_char(ProjectNumber); insert into Mylog values (NULL, Message); RETURN -3; END; second block END; 434 PL/SQL Programming Writing a Secure Procedure to Delete a Works_On record The procedure Delete_WorksOn() is invoked to remove records from the Works_On table In the function-call the programmers authentication code and the Works_On key field(s) are supplied For example: in the following call programmer ‘JB007’ asks to remove employee 123456789 from proj Result:= Delete_WorkOn(‘JB007’,123456789,1) • If the programmer ‘JB007’ is not authorized to make such a deletion, the operation is rejected and MYLOG file is update with a rejection message • If employee 123456789 is not in the Works_On table the current deletion is rejected and the log file is updated otherwise, the record is eliminated • The function returns a numeric code 1, -1, -2, etc to indicate the status of the execution (1: success, negative values: problems) 435 PL/SQL Programming DELETE_WORKSON script create or replace FUNCTION Delete_WorksOn ( Programmer_Id EmployeeSsn number, number ) RETURN number IS Programmer_Name varchar(20); Message varchar(70); BEGIN First block: Verify that programmer is allowed to delete Note: all code goes in one BEGIN END block BEGIN select Operator_Name into Programmer_Name from Valid_Operators where Operator_Id = Programmer_Id; EXCEPTION when No_Data_Found then Message:= 'Programmer not allowed to ' || 'delete Works_On rec.'; Insert into Mylog values(Programmer_Id, Message ); RETURN -1; END; first block 436 PL/SQL Programming DELETE_WORKSON script cont Second block: Other conditions could be checked here before deleting the record BEGIN delete from Works_On where Essn= EmployeeSsn; RETURN 1; EXCEPTION When No_Data_Found then Message:= ' Emp ' || to_char(EmployeeSsn) || ' does not exist in Works_On table'; insert into Mylog values (NULL, Message); RETURN -2; When Others then Message:= ' Emp ' || to_char(EmployeeSsn) || ' can not be deleted from Works_On'; insert into Mylog values (NULL, Message); RETURN -3; END; second block END; / 437 PL/SQL Programming Writing a Secure Procedure to Update Project records The procedure Update_Project( ) is used to modify existing records in the Project table Individual records are located using the Pnumber values as search-key The updating of projects occurs in the context of a function call such as the example bellow The arguments passed to the function are: Programmer_Id, project number, name, location; and number of responsible department Result:= Update_Project(‘JB007’,1,‘Cerveza XXX’,‘Margarita’,NULL); • • • • • • • Each project record to be updated must include the project number More than one field could be updated in a single function call Project numbers are not changeable (instead delete whole rec and re-insert with a new number) If the programmer ‘JB007’ is authorized to make deletion, the operation is rejected If there is no record for project number 1, the current update is rejected If a field is not going to be updated, the word NULL must be written in the proper column The function returns a numeric code 1, -1, -2, etc to indicate the status of the execution 438 PL/SQL Programming Writing a Secure Procedure to Update Project records create or replace FUNCTION Update_Project( Programmer_Id ProjectNumber ProjectName ProjectLocation DepartmentNumber RETURN number IS Programmer_Name varchar(20); Message varchar(70); VarPnumber number; number, number, varchar, varchar, number ) 439 PL/SQL Programming Writing a Secure Procedure to Update Project records cont BEGIN First block: Is programmer allowed to update projects? NOTE: all code goes in one BEGIN END block BEGIN select Operator_Name into Programmer_Name from Valid_Operators where Operator_Id = Programmer_Id; EXCEPTION when No_Data_Found then Message:= 'Programmer not allowed to ' || 'update Projects'; Insert into Mylog values (Programmer_Id, Message); RETURN -1; END; first block 440 PL/SQL Programming Writing a Secure Procedure to Update Project records cont Second block: Verify there is a project record matching Pnumber key If the field is not NULL, modify using given parameter BEGIN select Pnumber into VarPnumber from Project where Pnumber= ProjectNumber; if (ProjectName is not NULL) then update Project set Pname= ProjectName where Pnumber = ProjectNumber; end if; if (ProjectLocation is not NULL) then update Project set Plocation= ProjectLocation where Pnumber = ProjectNumber; End if; if (DepartmentNumber is not NULL) then update Project set Dnum = DepartmentNumber where Pnumber = ProjectNumber; End if; 441 RETURN ; success ! PL/SQL Programming Writing a Secure Procedure to Update Project records cont Deal with Update problems here EXCEPTION When No_Data_Found then Message:= ' Project ' || to_char(ProjectNumber) || ' does not exist '; insert into Mylog values (NULL, Message); RETURN -2; When Others then Message:= ' Project ' || to_char(ProjectNumber) || ' can not be updated '; insert into Mylog values (NULL, Message); RETURN -3; END; second block END; 442