Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Microsoft Access wikipedia , lookup
Tandem Computers wikipedia , lookup
Relational algebra wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Oracle Database wikipedia , lookup
Database model wikipedia , lookup
Clusterpoint wikipedia , lookup
Relational model wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna Outline • Purpose of PL/SQL. • PL/SQL block, its structure and types. • PL/SQL Syntax. Purpose of PL/SQL • Procedural programming language used to access an Oracle database. • Designed to overcome SQL's inability to handle control aspects of database interaction. • Extends SQL by adding procedural language constructs, such as: - Variables and types. - Control structures (IF-THEN-ELSE statements and loops). - Procedures and functions. • Procedural constructs are integrated seamlessly with Oracle SQL, resulting in a structured, powerful language. • Well-suited for designing complex applications. PL/SQL Block • Block is a basic unit in PL/SQL. • All PL/SQL programs consist of blocks and each block performs a logical function in the program. • Blocks can be nested within each other or can occur sequentially. PL/SQL Block Structure DECLARE (Declarative section) BEGIN (Executable section) EXCEPTION (Exception handling section) END; / (/ at the end of a block tells Oracle to run the block) Types of PL/SQL Blocks • as Anonymous blocks which are compiled and run when loaded. • as Triggers to maintain integrity. • as Subprograms (Procedures and Functions) stored within the database that can be executed many times. • as Packages, a named declarative section for storing related objects that can include procedures, functions and variables. PL/SQL Syntax Declaration • Variables are declared in the declarative section of the block. • Variable declaration examples: v_student_id CHAR(8); v_lastname VARCHAR2(25); v_capacity NUMBER(3) := 200; ( := is used to initialize variables ) Types Basic PL/SQL types are: • NUMBER(P,S) holds numeric value (where P is the precision and S is the scale) • VARCHAR2(L) holds strings or character data (where L is the maximum length of the variable) • CHAR(L) for fixed-length character strings • DATE for storing date and time information • BOOLEAN can hold TRUE, FALSE or NULL only Declaring variables with %TYPE attribute • %TYPE attribute is used when a PL/SQL variable is going to be used to manipulate the data stored in the database. • The variable should have the same type as the table column (field). • Specifying %TYPE attribute takes care. • For example, if the variable is used in the PL/SQL program to store the phone number of a student, following declaration can be used v_phone STUDENT.phone%TYPE; Instead of, say v_phone CHAR(10); PL/SQL Syntax Control Structures IF-THEN-ELSE IF boolean_expression1 THEN sequence_of_statements1; [ELSIF THEN sequence_of_statements2;] boolean_expression2 … [ELSE sequence_of_statements3;] END IF; LOOPS Simple Loops LOOP sequence_of_statements; END LOOP; EXIT [WHEN exit the loop condition]; is used to WHILE Loops WHILE condition LOOP sequence_of_statements; END LOOP: EXIT or EXIT WHEN used to exit the loop will also be FOR Loops FOR loop_counter IN [REVERSE] LOOP sequence_of_statements; END LOOP: low_bound .. high_bound Comments • Single-line comments DECLARE v_title CHAR(2) -- title can be Mr or Ms … • Multi-line comments DECLARE /* I am a multi-line comment */ • • -- Command to display the output at the SQL prompt SET SERVEROUTPUT ON • • • -- This PL/SQL block accepts a number from the terminal, and prints out its square. -- You can try this from sql+ -- It has no exception handling code. • • • DECLARE num1 NUMBER(3); num1_sq NUMBER(6); • • • • • BEGIN -- prompts user for a value for 'anytemp' num1 := &anytemp; num1_sq := num1 * num1 ; DBMS_OUTPUT.PUT_LINE('THE SQUARE OF ' || num1 || ' IS ' || num1_sq); END; / • •