0% found this document useful (0 votes)
57 views77 pages

DBMS Unit 3

The document discusses a lecture on database modification and relational databases using SQL given by Mr. Nitin Bhopale of Sanjivani College of Engineering. The lecture covers inserting, updating, and deleting data from databases using SQL queries, as well as concepts related to stored procedures, functions, cursors, triggers, roles, privileges, embedded and dynamic SQL. It also discusses the relational model, including attributes, domains, and ensuring relational integrity through normalization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views77 pages

DBMS Unit 3

The document discusses a lecture on database modification and relational databases using SQL given by Mr. Nitin Bhopale of Sanjivani College of Engineering. The lecture covers inserting, updating, and deleting data from databases using SQL queries, as well as concepts related to stored procedures, functions, cursors, triggers, roles, privileges, embedded and dynamic SQL. It also discusses the relational model, including attributes, domains, and ensuring relational integrity through normalization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

Sanjivani College of Engineering, Kopargaon

Department of Electronics & Computer Engineering


(An Autonomous Institute)
Affiliated to Savitribai Phule Pune University
Accredited ‘A’ Grade by NAAC
_______________________________________________________________________________________

Subject: Database Management Systems(DBMS) & SQL

by

Mr. Nitin Bhopale


Unit-III: Database modification and Relational Database

Database Modification using SQL Insert, Update and


Delete Queries. PL/SQL: concept of Stored Procedures &
Functions. Cursors, Triggers, Assertions, roles and
privileges, Embedded SQL, Dynamic SQL. Relational
Model: Basic concepts, Attributes and Domains, Relational
Integrity: Domain, Referential Integrities, Database
Design: Normalization, Atomic Domains and Normal Form.

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Data-Manipulation Language for Relational Database

select instructor.name El Said Califieri


from instructor
where instructor.dept name = ’History’;
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Relational Model:

Relation:

Example:

Attributes:

Tuples:

Domain:

Relation name:
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Informal Formal

Table Relation

Column name/header/field Attributes

Rows Tuples

Values Domain

Table name Relation name

Table definition Schema of relation

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Degree of relation: No of attributes in relation

Cardinality of relation: No of tuples in relation

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Properties of relation

 Distinct relation name

 Each cell of relation have exact one value

 Distinct attribute name

 Distinct tuple name

 No order significance for attribute

 No order significance for tuple

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Referential Integrity

Candidate key

Primary key

Foreign key

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Relationship(Cardinality) (1:1)

1 1
Employee works Department

eid Ename Eage eid did did Dname dloc

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

eid Ename Eage did


E1 A 22 D1
E2 B 21 D3
E3 C 24 D2

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Relational Integrity

Self referential Integrity

eid Ename Eage Manager


E1 A 22 D1
E2 B 21 E1
E3 C 24 E2

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Referential Integrity
Activity(referencing=derived) Student(referenced=base)
Act_name Act_date Winner Stud_rn Sname Saddress
Drawing 22/11/21 1 1 A shirdi
Chess 21/10/22 1 2 B kopargaon
Badminton 24/11/22 2 3 C yeola

 Foreign key used to reduce data redundancy

 Referencing attribute must be subset of referenced /referred attribute

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Types of Attributes

 Simple (EMP_ID)

 Composite (EMP_NAME)

 Single valued (EMP_DOB)

 Multivalued (EMP_EMAILID)

 Derived (EMP_POSERVICE) OR (EMP_AGE)

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
X->Y OR X-> X

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Properties of FD

THIS IS ALWAYS VALID

Then Srn -> Scity


Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
AB+=(ABCD)
-> is not candidate
key BUT IS SUPER
KEY since A is
combined with B

Mr. Nitin Bhopale, Department of Electronics & Computer Engineering


Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
Mr. Nitin Bhopale, Department of Electronics & Computer Engineering
To get any data from the TABLE we can have PRI KEY
SCORE_ID CANNOT BE PRI KEY
PARTIAL DEPENDANCY
CONDITIONS TO BE
CHECKED ON FD
Whether given
Table satisfies
2NF ?? This table is not
satisfying 2NF
1

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)

PL/SQL CODE FORMAT


EXECUTABLE CODE (block structure)
(begin a:=10; ……. end;)

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;

Sum of 1 to 50 numbers is: 1225


Program to print Fibonacci series
DECLARE
fno number:=0;
sno number:=1;
tno number:=0;
i number:=0;
maxval number:=0;
BEGIN
maxval:=:maxval;
dbms_output.put_line(fno);
dbms_output.put_line(sno);
LOOP
tno:=fno+sno;
dbms_output.put_line(tno);
fno:=sno;
sno:=tno;
i:=i+1;
EXIT WHEN i>=maxval-2;
END LOOP;
END;
Cursor
• Oracle creates a memory area, known as context area, for processing an SQL
statement, which contains all information needed for processing the statement;
for example, number of rows processed, etc.
• A Cursor is a pointer to this context area. PL/SQL controls the context area
through a Cursor. A Cursor holds the rows (one or more) returned by a SQL
statement.
• Conceptually size of the Cursor in memory is the size required to hold the
number of rows in the active set. Oracle has a pre-defined area in memory kept
aside, in which Cursors are opened. Hence the Cursor’s size will be limited by the
size of this pre-defined area.
• Write SELECT statement while creating a Cursor. Cursors are memory areas that
allow access to the data present in the table without disturbing the actual
contents of the table.
1) Implicit cursors
• Implicit cursors are automatically created by Oracle whenever an SQL
statement is executed and when there is no explicit cursor for the
statement. Programmers cannot control the implicit cursors and the
information in it.
• SQL cursor attributes
Program to print number of rows deleted

DECLARE
row_del number;
BEGIN
delete from emp;
row_del:=SQL%ROWCOUNT;
dbms_output.put_line('Number of rows deleted are : ' || row_del);
END;

• Number of rows deleted are 14


Statement processed
DECLARE
total_rows number(2);
no number;
BEGIN
no:=:no;
UPDATE emp SET sal = sal + 500 where deptno=no;
IF SQL%NOTFOUND THEN
dbms_output.put_line('no customers selected');
ELSIF SQL%FOUND THEN
total_rows := SQL%ROWCOUNT;
dbms_output.put_line( total_rows || ' employees are updated');
END IF;
END;

Output : If the input no is 20


5 employees are updated
2) Explicit cursors
• Explicit Cursors are user defined cursors for gaining more control over
the context area. An explicit cursor should be defined in the
declaration section of the PL/SQL Block.

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)

mysql> call search_name("Nitin")/


+-------+-------+------+
| fname | lname | city |
+-------+-------+------+
| Nitin | Patil | Pune |
+-------+-------+------+
Functions
Function is a subprogram that is used for the purpose of performing a specific
task. The main use of Function is code reusability. Functions and Procedures are
almost same, except that Function have a RETURN clause i.e. a Function must
return a value back to the caller.

• 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 !!!

You might also like