DBMS Unit 3
DBMS Unit 3
by
Relation:
Example:
Attributes:
Tuples:
Domain:
Relation name:
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Informal Formal
Table Relation
Rows Tuples
Values Domain
Candidate key
Primary key
Foreign key
1 1
Employee works Department
E1 A 22 E1 D1 D1 IT Delhi
D2 ETC Mumbai
E2 B 21 E2 D3
D3 MECH Shirdi
E3 C 24 E3 D2
Referenced (base table)
Referenced (base table) Referencing table
Primary key ?
Merging of table ?
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Reduced Table
Simple (EMP_ID)
Composite (EMP_NAME)
Multivalued (EMP_EMAILID)
4
PARTIAL DEPENDANCY
CONDITIONS TO BE CHECKED
ON FD
DB= ABCD
,D
BCNF
(Boyce Codd Normal Form)
• It must be in 3NF
• LHS of each FD must be CK or SK
CK={Stud_rn, VoterID}
FD {Stud_rn Sname,
Stud_rn Saddress,
VoterID Sname,
VoterID Stud_rn }
BCNF
Stud_rn Sname Saddress VoterID
1 A 22 A11
2 B 21 B22
3 C 24 C33
PL/SQL stands for Procedural Language/Structured Query Language. PL/SQL is
Procedural Language extension of SQL.
Advantages of PL/SQL
1) PL/SQL not only supports SQL data manipulation but also provides
conditional checking, branching and looping.
2) PL/SQL sends an entire block of statements to the Oracle engine at one time.
This in turn reduces network traffic.
3) Applications written in PL/SQL are portable to any computer hardware and
operating system where Oracle is present.
4) PL/SQL handles errors or exceptions effectively during the execution of a
PL/SQL program.
5) PL/SQL blocks can be stored in the database and reused.
6) PL/SQL provides features like Triggers, Functions, Cursors, Locks
PL/SQL
DECLARATION
(a int)
EXCEPTION HANDLING
Program for addition of two numbers
If Input accepted from user:
DECLARE
A number; B number; C number; (OR A int; B int; C int; )
DECLARE BEGIN
A:=&A; (for user defined values OR A:=:A)
A number:=5; B:=&A;
B number:=6; C:=A+B;
C number; dbms_output.put_line(‘The addition of two numbers is : ‘||C);
END;
BEGIN
C:=A+B;
dbms_output.put_line(‘The addition of two numbers is : ‘||C);
END;
To find out whether number is even or odd.
DECLARE
nm number ;
BEGIN
nm:=:nm ;
IF (mod(nm,2)=0) THEN
dbms_output.put_line(nm || ' is Even Number');
ELSE
dbms_output.put_line(nm || ' is Odd Number');
END IF;
END;
DECLARE
a number(2) := 10;
BEGIN
a:= 10;
IF( a < 20 ) THEN
dbms_output.put_line('a is less than 20 ' );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;
Program for comparison of two numbers :
DECLARE
no1 number;
no2 number;
BEGIN
no1:=:no1;
no2:=:no2;
IF (no1>no2) THEN
dbms_output.put_line(no1|| ' is a Greater Number');
ELSE IF (no2>no1) THEN
dbms_output.put_line(no2|| ' is a Greater Number');
ELSE
dbms_output.put_line(no1|| ' and ' ||no2 || ' are Equal');
END IF;
END IF;
END;
Program to print the numbers 1 to 5
DECLARE
i number:=0;
BEGIN
LOOP
i:=i+1;
dbms_output.put_line(i);
IF (i>=5) THEN
EXIT;
END IF;
END LOOP;
END;
Program to print 1 to 7 numbers using FOR LOOP.
DECLARE
i number;
BEGIN
FOR i IN 1..7 LOOP
dbms_output.put_line(i);
END LOOP;
END;
Program to print 1 to 10 numbers in Reverse order using FOR LOOP.
DECLARE
i number;
BEGIN
FOR i IN REVERSE 1..10 LOOP
dbms_output.put_line(i);
END LOOP;
END;
Program to print the sum of 1 to 50 numbers using EXIT WHEN condition.
DECLARE
i number(3) := 1;
ans number(5):=0;
BEGIN
LOOP
ans:=ans+i;
i := i + 1;
EXIT WHEN i >=50;
END LOOP;
dbms_output.put_line('Sum of 1 to 50 numbers is: ' || ans);
END;
DECLARE
row_del number;
BEGIN
delete from emp;
row_del:=SQL%ROWCOUNT;
dbms_output.put_line('Number of rows deleted are : ' || row_del);
END;
CURSOR emp_cur IS SELECT ename from emp; Statement submitted to Oracle server
OPEN emp_cur ;
DATA loaded into Context area of
FETCH emp-cur INTO e_no, e_name, e_job; Program Global Area(PGA)
CLOSE emp_cur ;
DECLARE
e_no emp.empno%type; (If the data type of the column or variable
changes, there is no need to modify the declaration code.)
e_name emp.ename%type;
CURSOR e_cur IS select empno,ename from emp;
BEGIN
OPEN e_cur;
LOOP
FETCH e_cur INTO e_no,e_name;
dbms_output.put_line(e_no || ' ' || e_name);
EXIT WHEN e_cur%NOTFOUND;
END LOOP;
CLOSE e_cur;
END;
• Procedures
A Subprogram that performs a specific action or task is called as Procedure. In
other words Procedure is a logically grouped set of SQL and PL/SQL statements
that perform a specific task.
• Procedures are stored in the Oracle database. They are invoked or called by any
PL/SQL block that appears within an application.
• Procedures consists of following three parts:
1) Declarative Part : It contains the declaration of cursors, constants, variables,
exceptions, subprograms. These objects are local to the procedure.
2) Executable Part : The executable part is a PL/SQL block consisting of SQL and
PL/SQL statements that assign values to control execution and manipulate
data. The action that the Procedure is expected to perform is coded here.
3) Exception handling : This part contains code that performs actions to deal
with exceptions that may be raised during the execution of code in the
executable part.
Creating a Procedure
• CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] data type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;
Executing a Standalone Procedure
• mysql> CREATE PROCEDURE disp()
• -> BEGIN
• -> select * from user;
• -> END;
• -> /
• mysql> call disp/
• | fname | lname | city |
• +--------+----------+-----------+
• | Mahesh | Patil | Pune |
• | Nitin | Patil | Pune |
• | Ramesh | Pawar | Pune |
• | Nilesh | Deshmukh | Pune |
• | Onkar | Gunjal | Kopargaon |
• | Kunal | Dhorde | Kopargaon |
• 8 rows in set (0.07 sec)
mysql> CREATE PROCEDURE search_name(IN inputname long)
-> BEGIN
-> select * from user where fname=inputname;
-> END;
-> /
Query OK, 0 rows affected (0.00 sec)
• Advantages of Functions
1. Code Reusability feature can be used.
2. It saves time and cost.
3. Increases the flexibility of the program.
4. Works better in client-server type of operations.
5. Memory space required is less.
6. It returns value to the calling program.
7. Arguments can be passed to Functions to get the desired result.
create or replace function adder(n1 in number, n2 in number)
return number
is DECLARE
n3 number(8); n3 number(2);
begin BEGIN
n3 := adder(11,22);
n3 :=n1+n2;
dbms_output.put_line('Addition is: ' || n3);
return n3; END;
end; /
/
Thank you !!!