pl/sql introduction database 1. practice. sample database the schema of the sample database is the...
TRANSCRIPT
PL/SQL Introduction
Database 1. Practice
Sample Database
• The schema of the sample database is the following:
Drinkers (name, occupation, birthday, salary)Wines (name, type, country)Bars (name, street_number, street, city, tel)Likes (drinker, wine)Frequents (drinker, wine)Sells (bar, wine, price)
Blokk• A PL/SQL program consists of blocks.• A block optionally contains a declaration part, the body of the
block must be given, which may be followed by an exception handling part.
[DECLARE declarations]BEGIN command [commands]...[EXCEPTION exceptions]END;
• Note: for the use of labels and GOTO commands consult the Oracle documentation.
Declaration of record type
• Records are heterogenous, complex data types which should be always declared before their use.
• TYPE name IS RECORD (field_name type [[NOT NULL] {:= | DEFAULT} expression][, field_name type [[NOT NULL] {:= | DEFAULT} expression]…);
Example for a record type declaration
DECLARE
TYPE film IS RECORD (title VARCHAR2(20) NOT NULL,year DATE,length NUMBER DEFAULT 90,studio VARCHAR2(15));
%TYPE, %ROWTYPE
• %TYPE: is used to refer to a previously declared type which is given to an attribute of a table, or a field of a record, or a variable.
• %ROWTYPE: returns the record type corresponding to the type of the rows of a table or a cursor.
• In both cases, whenever there is a change in the type of the referred object, in the corresponding declarations the types are automatically updated.
%TYPE, %ROWTYPE example
CREATE TABLE Books AS (title VARCHAR2(50) NOT NULL,writer VARCHAR2(40) NOT NULL);
DECLARE
books_row Books%ROWTYPE,title Books.title%TYPE,…
Displaying texts
• DMBMS_OUTPUT package• The most important procedures are
– NEW_LINE– PUT– PUT_LINE
• Before the code of a program using the DMBMS_OUTPUT package the SET SERVEROUTPUT ON command should be issued in order to enable the displaying of texts.
Value assignment
• variable := value
• Conditional value assignment
CASE selectorWHEN condition THEN value[WHEN condition THEN value]...[ELSE value]
END
Example for value assignments
SET SERVEROUTPUT ONDECLARE
v_date DATE := DATE '1983-05-29'; v_season VARCHAR2(6);
BEGIN v_season := CASE WHEN TO_CHAR(v_date, 'MM') IN ('01', '02', '12')
THEN 'winter' WHEN TO_CHAR(v_date, 'MM') IN ('03', '04', '05') THEN 'spring' WHEN TO_CHAR(v_date, 'MM') IN ('06', '07', '08') THEN 'summer' WHEN TO_CHAR(v_date, 'MM') IN ('09', '10', '11') THEN 'autumn' ELSE 'error' END; DBMS_OUTPUT.PUT_LINE (v_season);
END;
SELECT INTO statement I.
SET SERVEROUTPUT ONDECLARE v_occ Drinkers.occupation%TYPE; BEGIN SELECT occupation INTO v_occ FROM Drinkers WHERE name = 'Maria'; DBMS_OUTPUT.PUT_LINE('The occupation of Maria is: ' || v_occ);END;
• Note: if the SQL query returns more than one row or it does not return any row, then an exception is thrown.
SELECT INTO statement II.
SET SERVEROUTPUT ONDECLARE
v_occ Drinkers.occupation%TYPE;
v_birth Drinkers.birthday%TYPE;
BEGIN SELECT occupation, birthday INTO v_occ, v_birth FROM Drinkers WHERE name = 'Maria'; DBMS_OUTPUT.PUT_LINE('Occupation: ' || v_occ ||
' Birthday: ' || v_birth);END;
A SELECT INTO utasítás III.
SET SERVEROUTPUT ONDECLARE
v_row Drinkers%ROWTYPE;
BEGIN SELECT * INTO v_row FROM Drinkers WHERE name = 'Maria'; DBMS_OUTPUT.PUT_LINE('Occupation: ' || v_row.occupation || ' Birthday: ' || v_row.birthday);END;
IF-THEN-ELSE
IF condition THEN command [command]....[ELSIF condition THEN command [command]....]...[ELSE command [command]....]END IF;
Example for an if-then-else statement
SET SERVEROUTPUT ONDECLARE
v_number NUMBER := -5; v_abs_val NUMBER;
BEGINIF v_number >= 0 THEN v_abs_val := v_number;ELSE v_abs_val := -1 * v_number;END IF;DBMS_OUTPUT.PUT_LINE ('The absolute value of: ' || v_number || ' is ' || v_abs_val );
END;
CASE
CASE [selector]WHEN {condition | expression} THEN command [command ]...[WHEN {condition | expression} THEN command [command ]...]...[ELSE command [command ]...]
END CASE;
Example for a CASE statement
SET SERVEROUTPUT ONDECLARE
mark NUMBER(1) := 4;
BEGIN CASE mark WHEN 1 THEN DBMS_OUTPUT.PUT_LINE('failed'); WHEN 2 THEN DBMS_OUTPUT.PUT_LINE('bad'); WHEN 3 THEN DBMS_OUTPUT.PUT_LINE('not bad'); WHEN 4 THEN DBMS_OUTPUT.PUT_LINE('good'); WHEN 5 THEN DBMS_OUTPUT.PUT_LINE('excellent'); ELSE DBMS_OUTPUT.PUT_LINE('There is no such mark.'); END CASE;END;
Simple loop• Syntax:
LOOP command [command]... END LOOP;
• Example:DECLARE fakt NUMBER:=1; i PLS_INTEGER:=2;BEGIN LOOP fakt:=fakt*i; EXIT WHEN fakt > 1000; i:=i+1; END LOOP; DBMS_OUTPUT.PUT_LINE(' The factorial is: ' || fakt);END;
WHILE loop
• Syntax:WHILE conditionLOOP command [command]...END LOOP;
• Example: DECLARE
v_exp NUMBER :=1; BEGIN WHILE v_exp < 10000 LOOP v_exp := 2* v_exp; END LOOP; DBMS_OUTPUT.PUT_LINE (v_exp); END;
FOR loop
• Syntax:
FOR variable IN [REVERSE] lower_bound..upper_boundLOOP command [command]...END LOOP;
• The type of variable is automatically set to PLS_INTEGER.• By using the EXIT command one can terminate a loop
statement. The program continues with the next command after the loop.
• In other words, the effect of EXIT is the same as that of the break statement in C++.
Example for a FOR statement
SET SERVEROUTPUT ONDECLARE
v_sum NUMBER := 0;
BEGIN FOR i in 1..100 LOOP v_sum := v_sum + i; END LOOP; DBMS_OUTPUT.PUT_LINE('The sum of the first 100 numbers is: ' || v_sum);
END;
Exercises
• Write a program which displays the following data:– the I love the whole world string– the country, where Cave Geisse Nature is produced– the occupation and birthday of Hannah– the number of those who visit Bar do Mineiro– the smallest number which is greater than 10000 and is divisible by
7– those prime numbers that are less than 100.