RDBMS
RDBMS
RDBMS
RDBMS DAY 1
PL/SQL is a combination of SQL along with the procedural features of programming languages. It was
developed by Oracle Corporation in the early 90's to enhance the capabilities of SQL. PL/SQL is one
of three key programming languages embedded in the Oracle Database, along with SQL itself and
Java. This tutorial will give you great understanding on PL/SQL to proceed with Oracle database and
other advanced RDBMS concepts.
The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as
procedural extension language for SQL and the Oracle relational database. Following are certain
notable facts about PL/SQL −
PL/SQL can also directly be called from the command-line SQL*Plus interface.
Direct call can also be made from external programming language calls to database.
Features of PL/SQL
PL/SQL has the following features −
SQL is the standard database language and PL/SQL is strongly integrated with SQL.
PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations
and transaction control from PL/SQL block. In Dynamic SQL, SQL allows
embedding DDL statements in PL/SQL blocks.
PL/SQL allows sending an entire block of statements to the database at one time. This
reduces network traffic and provides high performance for the applications.
PL/SQL gives high productivity to programmers as it can query, transform, and
update data in a database.
PL/SQL saves time on design and debugging by strong features, such as exception
handling, encapsulation, data hiding, and object-oriented data types.
Applications written in PL/SQL are fully portable.
PL/SQL provides high security level.
PL/SQL provides access to predefined SQL packages.
PL/SQL provides support for Object-Oriented Programming.
PL/SQL provides support for developing Web Applications and Server Pages.
A block without a name is an anonymous block. An anonymous block is not saved in the
Oracle Database server, so it is just for one-time use. However, PL/SQL anonymous blocks
can be useful for testing purposes.
A PL/SQL block has a declaration section where you declare variables, allocate memory for
cursors, and define data types.
2) Executable section
A PL/SQL block has an executable section. An executable section starts with the keyword
BEGIN and ends with the keyword END. The executable section must have a least one
executable statement, even if it is the NULL statement which does nothing.
3) Exception-handling section
A PL/SQL block has an exception-handling section that starts with the keyword EXCEPTION.
The exception-handling section is where you catch and handle exceptions raised by the code
in the execution section.
Note a block itself is an executable statement, therefore you can nest a block within other blocks.
The PL/SQL supports single-line and multi-line comments. All characters available inside
any comment are ignored by the PL/SQL compiler. The PL/SQL single-line comments start
with the delimiter -- (double hyphen) and multi-line comments are enclosed by /* and */.
DECLARE
-- variable declaration
message varchar2(20):= 'Hello World!';
BEGIN
/*
* PL/SQL executable statement(s)
*/
dbms_output.put_line(message);
END;
/
BEGIN
/*
* PL/SQL executable statement(s)
*/
dbms_output.put_line('Hello');
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Hello
The following example shows a simple PL/SQL anonymous block with one executable
section.
1 BEGIN
2 DBMS_OUTPUT.put_line ('Hello World!');
3 END;
The executable section calls the DMBS_OUTPUT.PUT_LINE procedure to display the "Hello
World" message on the screen.
EXAMPLES
--ADDITION
declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=a+b;
dbms_output.put_line('Sum of ' || a || ' and ' || b || ' is ' || c);
End;
/
Declare
a number;
s1 number default 0;
Begin
a:=1;
loop
s1:=s1+a;
exit when (a=100);
a:=a+1;
end loop;
dbms_output.put_line('Sum between 1 to 100 is '||s1);
End;
Declare
a number;
mult number default 0;
counter number default 1;
Begin
a:=&a;
loop
mult:=a*counter;
dbms_output.put_line(a||'*’||counter||=||mult);
counter=counter+1;
exit when (counter=10);
end loop;
End;
Home work
1. Write a program to display area and circumference of a circle with given radius(user will
enter radius)
2. Write a program to display area and circumference of a circle with radius values 3 to 10
Day 2
1
Scalar
Single values with no internal components, such as a NUMBER, CHARACTER,
DATE, or BOOLEAN
Number : Numeric values on which arithmetic operations are performed.
NUMBER (prec, scale)
Fixed-point or floating-point number
INTEGER: Stores only integer values
Character:
Char: Fixed-length character string with maximum size of 32,767 bytes
Varchar: Variable-length character string with maximum size of 32,767 bytes
2
Long: Variable-length character string with maximum size of 32,760 bytes
Boolean: Data type stores logical values that are used in logical
operations. The logical values are the Boolean
4 values TRUE and FALSE and the value NULL.
Null
6 NULL values represent missing or unknown data and they are not an
integer, a character, or any other specific data type
Arithmetic operators
Relational operators
Comparison operators
Logical operators
String operators
Here, we will understand the arithmetic, relational, comparison and logical operators
one by one. The String operators will be discussed in a later chapter − PL/SQL -
Strings.
Arithmetic Operators
Following table shows all the arithmetic operators supported by PL/SQL. Let us
assume variable A holds 10 and variable B holds 5, then −
Show Examples
Relational Operators
Relational operators compare two expressions or values and return a Boolean
result. Following table shows all the relational operators supported by PL/SQL. Let
us assume variable A holds 10 and variable B holds 20, then −
Show Examples
Checks if the values of two operands are equal or not, if yes then condition (A = B) is not
=
becomes true. true.
!=
Checks if the values of two operands are equal or not, if values are not equal
<> (A != B) is true.
then condition becomes true.
~=
Checks if the value of left operand is greater than the value of right operand, if (A > B) is not
>
yes then condition becomes true. true.
Checks if the value of left operand is less than the value of right operand, if yes (A < B) is true.
<
then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value of right (A >= B) is not
operand, if yes then condition becomes true. true.
Checks if the value of left operand is less than or equal to the value of right (A <= B) is true
<=
operand, if yes then condition becomes true.
Comparison Operators
Comparison operators are used for comparing one expression to another. The
result is always either TRUE, FALSE or NULL.
Show Examples
If x = 10 then, x
between 5 and 20
The BETWEEN operator tests whether a value lies in a
returns true, x between
BETWEEN specified range. x BETWEEN a AND b means that x
5 and 10 returns true,
>= a and x <= b.
but x between 11 and
20 returns false.
Logical Operators
Following table shows the Logical operators supported by PL/SQL. All these
operators work on Boolean operands and produce Boolean results. Let us
assume variable A holds true and variable B holds false, then −
Show Examples
Called the logical AND operator. If both the operands are (A and B)
and
true then condition becomes true. is false.
Called the logical NOT Operator. Used to reverse the not (A and
not logical state of its operand. If a condition is true then B) is true.
Logical NOT operator will make it false.
Operator Operation
** exponentiation
+, - identity, negation
*, / multiplication, division
AND conjunction
OR inclusion
EG: dbms_output.put_line(x);
-- The comment line begins with a double hyphen (--). The entire line will
be treated as a comment.
IF and else.....
IF --Condition THEN
--Action
ELSEIF --Condition THEN
--Action
ELSE
--Action
END IF;
SIMPLE LOOP
loop
-- Sequence of statements;
end loop;
the loop ends when u use EXIT WHEN statement --condition
WHILE LOOP
While --condition
loop
--sequence of statements
end loop;
FOR LOOP
FOR i in 1..10
loop
--sequence of statements
end loop;
Declare
a number:= 100;
begin
loop
a := a+25;
exit when a=250;
end loop;
dbms_output.put_line (to_Char(a));
end;
Declare
i number:=0;
j number:= 0;
begin
while i <=100 loop
j := j+1;
i := i +2;
end loop;
dbms_output.put_line(to_char(i));
end;
Declare
begin
for i in 1..10
loop
dbms_output.put_line(to_char(i));
end loop;
end;
Declare
pi constant number(4,2) := 3.14;
radius number(5);
area number(14,2);
Begin
radius := 3;
While radius <=7
Loop
area := pi* power(radius,2);
Insert into areas values (radius, area);
radius:= radius+1;
end loop;
end;
Declare
given_number varchar(5) := '5639';
str_length number(2);
inverted_number varchar(5);
Begin
str_length := length(given_number);
For cntr in reverse 1..str_length
loop
inverted_number := inverted_number || substr(given_number, cntr, 1);
end loop;
dbms_output.put_line('The Given no is ' || given_number);
dbms_output.put_line('The inverted number is ' || inverted_number);
end;
Home Work
1. Write Data types in PL SQL
2. Write short note on
a. Initializing variable with example
b. Scope of variable Global and local
c. Operators used in PL SQL
i. Arithmetic operators
ii. Comparative operators
iii. Logical operators
3. Which command is used to display message on screen
4. Write a program to find factorial of a number
5. Write a program to find reverse number of a number
DAY 3
--QUERY EXAMPLE--IS SMITH EARNING ENOUGH
Programmable SQL
SQL is embedded in program.
Emp table
create table emp(empno number(3) primary key,ename
varchar(10),salary number(6));
declare
veno emp.empno%type;
vename emp.ename%type;
vesalary emp.salary%type;
begin
select empno,ename,salary into veno,vename,vesalary from emp where
empno=1;
dbms_output.put_line(‘empno:’||vempno||
’ename:’||vename||
’salary:’||vsalary’);
end;
/
Select empno from emp where empno=3;
In this program
DDL
DML
Select
Where
Group bu
Aggregate
1. Declare section 3 variables are declared veno,vename and
vsalary. V prefix is used to for variable.
%type automatically assigns data type column in the table to a
variable. So no need to define a datatype to a variable.
2. In execution section values stored in a table are transferred into
variables with empno=1
3. To display the contents of variables dbms_output.put_line
command is used.
Declare
erec emp%rowtype;
begin
select * into erec
from emp
where empno=1;
dbms_output.put_line( ' Emp No = ' || erec.empno ||
' Emp Name = ' || erec.ename ||
' Salary = ' || erec.sal ||
' Commission = ' || erec.comm ||
' Designation = ' || erec.job);
end details;
/