Presentation is loading. Please wait.

Presentation is loading. Please wait.

Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.

Similar presentations


Presentation on theme: "Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data."— Presentation transcript:

1 Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data

2 Guide to Oracle 10g2 Lesson A Objectives After completing this lesson, you should be able to: Describe the fundamentals of the PL/SQL programming language Write and execute PL/SQL programs in SQL*Plus Execute PL/SQL data type conversion functions

3 Guide to Oracle 10g3 Display output through PL/SQL programs Manipulate character strings in PL/SQL programs Debug PL/SQL programs

4 Guide to Oracle 10g4 Fundamentals of PL/SQL Full-featured programming language Execute using Oracle 10g utilities SQL*Plus Forms Builder Interpreted language Semicolon ends each command Reserved words

5 Guide to Oracle 10g5 A procedural programming language is a programming language that uses detailed, sequential instructions to process data. A PL/SQL program combines SQL queries with procedural commands for task such as manipulating variable values, evaluating IF/THEN decision control structures, and creating loop structure that repeat instructions multiple times until the loop reaches an exit condition.

6 Guide to Oracle 10g6 PL/SQL interface directly with the Oracle 10g database and is available within Oracle development environments. Another advantage of using PL/SQL to create programs that interface with Oracle 10g database is that database developers can store PL/SQL programs directly in the database, which makes the programs valuable to all database users.

7 Guide to Oracle 10g7 PL/SQL Variables and Data Types Variable names must follow the Oracle naming standard Strongly typed language Explicitly declare each variable including data type before using variable Variable declaration syntax: variable_name data_type_declaration; Default value always NULL

8 Guide to Oracle 10g8 Scalar Variables Reference single value Data types correspond to Oracle 10g database data types VARCHAR2 CHAR DATE NUMBER

9 Guide to Oracle 10g9

10 10 General Scalar Data Types PL/SQL has general scalar data types that do not correspond to database data types.

11 Guide to Oracle 10g11 Composite Variables Data object made up of multiple individual data elements Data structure contains multiple scalar variables Types: RECORD, which specifies a structure that contains multiple scalar values, and is similar to a table record. TABLE, which specifies a tabular structure with multiple columns and rows. VARRAY, which specifies a variable-sized array. A variable-sized array is a tabular structure that can expand or contract based on the data values it contains.

12 Guide to Oracle 10g12 Reference Variables Directly reference specific database column or row and assume data type of associated column or row.

13 Guide to Oracle 10g13 LOB Data type PL/SQL LOB data types declare variables that reference binary data objects, such as image or sound. The LOB data type can store either binary or character data up to four gigabytes in size. The LOB values in PL/SQL programs must be manipulated using special set of programs, called the DBMS_LOB package.

14 Guide to Oracle 10g14 PL/SQL Program Blocks Declaration section Optional Execution section Required Exception section Optional Comment statements Enclosed within /* and */

15 Guide to Oracle 10g15 The structure of a PL/SQL program block is

16 Guide to Oracle 10g16 PL/SQL Arithmetic Operators in Describing Order of Precedence

17 Guide to Oracle 10g17 Assignment Statements Operator := Syntax variable_name := value; Ex: Current_s_first_name := ‘Tammy’;

18 Guide to Oracle 10g18 Recall that when you declare a new PL/SQL variable, the default value is NULL. You can place an assignment statement within a variable declaration to assign an initial value to a new variable. Ex: cuurent_student_Id NUMBER :=100; Or cuurent_student_Id NUMBER DEFAULT 100;

19 Guide to Oracle 10g19 If you use a NULL value in an assignment statement that perform an arithmetic operation, the resulting value is always NULL. DECLARE variable1 NUMBER; variable2 NUMBER :=0; BEGIN variable2:= variable1+1; END;

20 Guide to Oracle 10g20 Executing a PL/SQL Program in SQL*Plus Create and execute PL/SQL program blocks Within variety of Oracle 10g development environments Add PL/SQL programs to your Forms Builder Report Builder Execute PL/SQL programs in SQL*plus.

21 Guide to Oracle 10g21 Displaying PL/SQL Program Output in SQL*Plus PL/SQL output buffer Memory area on database server Stores program’s output values before they are displayed to user Should increase size SET SERVEROUTPUT ON SIZE buffer_size Default buffer size 2000 bytes. SET SERVEROUTPUT ON SIZE 4000

22 Guide to Oracle 10g22 Displaying PL/SQL Program Output in SQL*Plus (continued) Display program output DBMS_OUTPUT.PUT_LINE('display_text'); Display maximum of 255 characters of text data

23 Guide to Oracle 10g23 Writing a PL/SQL Program Write PL/SQL program in Notepad or another text editor Copy and paste program commands into SQL*Plus Press Enter after last program command Type front slash ( / ) Then press Enter again

24 Guide to Oracle 10g24

25 Guide to Oracle 10g25 Writing a PL/SQL Program (continued) Good programming practice Place DECLARE, BEGIN, and END commands flush with left edge of text editor window Indent commands within each section

26 Guide to Oracle 10g26 PL/SQL Program Commands

27 Guide to Oracle 10g27 PL/SQL Data Conversion Functions Implicit data conversions Interpreter automatically converts value from one data type to another If PL/SQL interpreter unable to implicitly convert value error occurs Explicit data conversions Convert variables to different data types Using data conversion functions

28 Guide to Oracle 10g28 PL/SQL Data Conversion Functions of PL/SQL

29 Guide to Oracle 10g29 Manipulating Character Strings with PL/SQL String Character data value Consists of one or more characters Concatenating Joining two separate strings Parse Separate single string consisting of two data items separated by commas or spaces

30 Guide to Oracle 10g30 Concatenating Character Strings Operator || Syntax: new_string := string1 || string2;

31 Guide to Oracle 10g31 Removing Blank Leading and Trailing Spaces from Strings LTRIM function Remove blank leading spaces string := LTRIM(string_variable_name); RTRIM function Remove blank trailing spaces string := RTRIM(string_variable_name);

32 Guide to Oracle 10g32 Finding the Length of Character Strings LENGTH function syntax string_length := LENGTH(string_variable_name); Ex: You use the following command to declare an integer variable named code_length, and assign to it the string length: Code-length as NUMBER(3) := LENGTH(bldg_code);

33 Guide to Oracle 10g33 Character String Case Functions Modify case of character strings Functions and syntax: string := UPPER(string_variable_name); string := LOWER(string_variable_name); string := INITCAP(string_variable_name);

34 Guide to Oracle 10g34

35 Guide to Oracle 10g35 Parsing Character Strings INSTR function Searches string for specific substring Syntax: start_position := INSTR(original_string, substring); SUBSTR function Extracts specific number of characters from character string Starting at given point

36 Guide to Oracle 10g36 Parsing Character Strings (continued) SUBSTR function (continued) Syntax: extracted_string := SUBSTR(string_variable, starting_point, number_of_characters); Use INSTR to find delimiter

37 Guide to Oracle 10g37

38 Guide to Oracle 10g38 Debugging PL/SQL Programs Syntax error Occurs when command does not follow guidelines of programming language Generate compiler or interpreter error messages Logic error Does not stop program from running Results in incorrect result

39 Guide to Oracle 10g39 Program with a Syntax Error

40 Guide to Oracle 10g40 Program with a Logic Error

41 Guide to Oracle 10g41 Finding Syntax Errors Often involve: Misspelling reserved word Omitting required character in command Using built-in function improperly Interpreter Flags line number and character location of syntax errors May actually be on preceding line Displays error code and message

42 Guide to Oracle 10g42 Finding Syntax Errors (continued) Comment out program lines To find error Cascading errors One syntax error can generate many more errors

43 Guide to Oracle 10g43 Finding Logic Errors Caused by: Not using proper order of operations in arithmetic functions Passing incorrect parameter values to built-in functions Creating loops that do not terminate properly Using data values that are out of range or not of right data type

44 Guide to Oracle 10g44 Finding Logic Errors (continued) Debugger Program that enables software developers to pause program execution and examine current variable values SQL*Plus environment does not provide PL/SQL debugger Use DBMS_OUTPUT to print variable values

45 Guide to Oracle 10g45


Download ppt "Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data."

Similar presentations


Ads by Google