Download presentation
Presentation is loading. Please wait.
Published byAnne Williams Modified over 9 years ago
1
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL
2
CIS4368: Advanced DatabaseSlide # 2 PL/SQL Dr. Peeter KirsSpring, 2003 Procedural Language for SQL (PL/SQL) is an extension of Oracle SQL The basic intent of PL/SQL is increase the expressiveness of SQL process query results in a tuple-oriented way develop modular database application programs reuse program code, and reduce the cost for maintaining and changing applications. constants and variables can be declared, and variables can be used to store query results. Statements in a PL/SQL block include: The basic construct of PL/SQL is a block SQL statements Control structures (loops) Condition statements (if-then-else) Exception handling Calls of other PL/SQL blocks.
3
CIS4368: Advanced DatabaseSlide # 3 PL/SQL Dr. Peeter KirsSpring, 2003 Each block builds a (named) program unit, and blocks can be nested. The structure of a PL/SQL looks as follows: (brackets [ ] enclose optional parts) [ ] [declare ] begin [exception ] end;
4
CIS4368: Advanced DatabaseSlide # 4 PL/SQL Dr. Peeter KirsSpring, 2003 Consider the following simple code: Needed to display output Our local variable of data type DATE Get today’s date and store it in our variable Display the contents of the variable Program Output
5
CIS4368: Advanced DatabaseSlide # 5 PL/SQL Dr. Peeter KirsSpring, 2003 Numeric operations in PL/SQL:
6
CIS4368: Advanced DatabaseSlide # 6 PL/SQL Dr. Peeter KirsSpring, 2003 Bind Variables: Host variables: Declared in the SQL “Host Environment” Remain active for the length of the SQL Session Can be displayed with the SQL Print Command Can only be accessed in the program when prefaced with a colon (:)
7
CIS4368: Advanced DatabaseSlide # 7 PL/SQL Dr. Peeter KirsSpring, 2003 Interactive Input:
8
CIS4368: Advanced DatabaseSlide # 8 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: IF-THEN:
9
CIS4368: Advanced DatabaseSlide # 9 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: IF-THEN-ELSE:
10
CIS4368: Advanced DatabaseSlide # 10 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: IF-THEN-ELSIF: (Note Spelling)
11
CIS4368: Advanced DatabaseSlide # 11 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: Basic Loop:
12
CIS4368: Advanced DatabaseSlide # 12 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: While:
13
CIS4368: Advanced DatabaseSlide # 13 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: For:
14
CIS4368: Advanced DatabaseSlide # 14 PL/SQL Dr. Peeter KirsSpring, 2003 SQL in PL: We must be careful about single record/field queries and multiple return queries
15
CIS4368: Advanced DatabaseSlide # 15 PL/SQL Dr. Peeter KirsSpring, 2003 Single Return Queries: The data type applied to field studentname is automatically applied
16
CIS4368: Advanced DatabaseSlide # 16 PL/SQL Dr. Peeter KirsSpring, 2003 Multiple Record Queries:
17
CIS4368: Advanced DatabaseSlide # 17 PL/SQL Dr. Peeter KirsSpring, 2003 Remember our problem about calculating a student grade? /* This program calculates a grade */ declare cursor studentgrade is select lastname, firstname, quiz1, quiz2, quiz3 from grades; studentlastname grades.lastname%type; studentfirstname grades.firstname%type; q1 grades.quiz1%type; q2 grades.quiz2%type; q3 grades.quiz3%type; average number; sgrade grades.grade%type; nblanks number; blanks char(5); Variable Declarations:
18
CIS4368: Advanced DatabaseSlide # 18 PL/SQL Dr. Peeter KirsSpring, 2003 Remember our problem about calculating a student grade? begin dbms_output.put_line('Student Name Grade'); dbms_output.put_line('------------------ -----'); open studentgrade; loop fetch studentgrade into studentlastname, studentfirstname, q1, q2, q3; exit when studentgrade%notfound; average := (q1 + q2 + q3)/3; nblanks := 20 - (length(trim(studentfirstname)) + length(trim(studentlastname))); if average >= 90 then sgrade := 'A'; elsif average >= 80 then sgrade := 'B'; elsif average >= 70 then sgrade := 'C'; elsif average >= 60 then sgrade := 'D'; else sgrade := 'F'; end if; dbms_output.put_line(trim(studentfirstname) || ' ' || trim(studentlastname) || lpad(' ',nblanks,' ') || sgrade); update grades set grade = sgrade; end loop; close studentgrade; end;
19
CIS4368: Advanced DatabaseSlide # 19 PL/SQL Dr. Peeter KirsSpring, 2003 Remember our problem about calculating a student grade?
20
CIS4368: Advanced DatabaseSlide # 20 PL/SQL Dr. Peeter KirsSpring, 2003 Exceptions: Act as error handling routines
21
CIS4368: Advanced DatabaseSlide # 21 PL/SQL Dr. Peeter KirsSpring, 2003 Types of Exceptions: Exception Name No_data_found Description Single row select returned no data Too_Many_rowsSingle row select returned multiple rows Zero_DivideAttempt to divide by zero Value_ErrorArithmetic, Conversion, Truncation error Storage_ErrorPL/SQL ran out of memory or memory corrupted Login_DeniedInvalid Username or password Program_ErrorRun Time error Access_Into_NullAttempt to assign values to uninitialized object Invalid_CursorIllegal cursor operation Rowtype_MismatchCursor variable involved in incompatible return types --- And Others ---
22
CIS4368: Advanced DatabaseSlide # 22 PL/SQL Dr. Peeter KirsSpring, 2003 Procedures: Remember our Grading program? We could have created it as stored Procedure:
23
CIS4368: Advanced DatabaseSlide # 23 PL/SQL Dr. Peeter KirsSpring, 2003 To run the program:
24
CIS4368: Advanced DatabaseSlide # 24 PL/SQL Dr. Peeter KirsSpring, 2003 Functions, which are called by procedures, can also be created and stored:
25
CIS4368: Advanced DatabaseSlide # 25 PL/SQL Dr. Peeter KirsSpring, 2003 To call the function:
26
CIS4368: Advanced DatabaseSlide # 26 PL/SQL Dr. Peeter KirsSpring, 2003 Triggers: A stored block which is implicitly called when an event occurs INSERT A triggering event is based on a Data Manipulation Language statement such as: UPDATE DELETE Execution of the trigger is known as firing the trigger
27
CIS4368: Advanced DatabaseSlide # 27 PL/SQL Dr. Peeter KirsSpring, 2003 Recall our problem about determining whether or not a student had enrolled for two classes at the same time Information about students in a class was found only in the table enrollment Information about when a class met was found only in the table class If the information were in one table we could apply a constraint which would not allow a student to enroll in both classes:
28
CIS4368: Advanced DatabaseSlide # 28 PL/SQL Dr. Peeter KirsSpring, 2003 However, even if we had created this table, it still would not stop a student from enrolling in two classes that meet at the same time Enrollment in a class is done by entering a record in table enrollment (not table temp_table) One way to stop dual enrollment is to set a trigger which tries to insert the record (from enrollment) into table temp_table (which contains the constraint) If the record can be inserted into temp_table, it will then be inserted into table enrollment If the record can NOT be inserted into temp_table, it will NOT be inserted into table enrollment
29
CIS4368: Advanced DatabaseSlide # 29 PL/SQL Dr. Peeter KirsSpring, 2003 The trigger might appear as: FOR EACH ROW is a row trigger which fires once for each row inserted: :NEW refers to the new record to be inserted
30
CIS4368: Advanced DatabaseSlide # 30 PL/SQL Dr. Peeter KirsSpring, 2003 Suppose we look up the Spring 2003 (semester = 102) schedule for Yao Ming (studentid = 21098765): There is one other class that meets at the same time
31
CIS4368: Advanced DatabaseSlide # 31 PL/SQL Dr. Peeter KirsSpring, 2003 If we now try and enroll Yao for the other class:
32
CIS4368: Advanced DatabaseSlide # 32 PL/SQL Dr. Peeter KirsSpring, 2003
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.