0% found this document useful (0 votes)
23 views22 pages

Dbms440 Final

Uploaded by

21amtics440
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)
23 views22 pages

Dbms440 Final

Uploaded by

21amtics440
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/ 22

202103103510440

Practical 8
Aim : Execute group by and having clause queries given in tutorial 7.
Query 1: List the number of different products supplied by each supplier_no.
select supplier_no,count(product_no) from product group by supplier_no;

Query 2: List the name of each supplier with the location of each depot and the number of
products supplied by that supplier and stocked at that depot.
select name,location,count(product_no) from supplier,depot,product where
supplier.supplier_no=product.supplier_no and product.supply_depot_no = depot.depot_no
group by name,location;

Database Management System(CE4012)


202103103510440

Query 3: List the depot_no’s of all depots where the average credit_limit for all the
customers receiving deliveries from the depot is > 5000.
select customer_no,count(depot.depot_no) from customer,depot where depot.depot_no =
customer.depot_no group by customer_no having avg(credit_limit)>5000;

Query 4: List total no of quantity and product number ordered by customer.


select count(quantity),product_no from online group by product_no;

Database Management System(CE4012)


202103103510440

Query 5: Give product number which has maximum quantity stock at any depot.
select product_no from stock where quantity in(select max(quantity) from stock) group by
product_no;

Query 6: Give customer address which has minimum credit limit.


select address from customer where credit_limit in (select min(credit_limit) from customer)
group by address;

Database Management System(CE4012)


202103103510440

Query 7: List supplier no who has supplied products whose total price is <1000.
select supplier_no from supplier where supplier_no in (select supplier_no from product where
price < 1000) group by supplier_no;

Query 8: Give total number of customers who has ordered product on same date.
select date_placed,count(customer_no) from orders group by date_placed;

Database Management System(CE4012)


202103103510440

Query 9: List sum of quantity stocked at each rack.


select rack,sum(quantity) from group by rack;

Query 10: Display total no of customers who has received product from same location.
select location,count(customer_no) from depot,customer where depot.depot_no =
customer.depot_no group by location;

Database Management System(CE4012)


202103103510440

Practical 9
Aim : I. Create a simple PL/SQL program which includes declaration
section, executable section and exception –Handling section (Ex. Student
marks can be selected from the table and printed for those who secured
second class and an exception can be raised if no records were found)
Theory :
An error or a warning event is called an exception. It is encountered during the run time
execution of the PL/SQL program. PL/SQL has mechanisms to handle these exceptional
conditions with the help of the EXCEPTION block of code where it is defined how to counter
the error condition.
There are two types of exceptions. They are listed below:
1. User-defined exceptions.

2.System defined exceptions.


User-defined exceptions are declared in a package, subprogram, or within the declaration
section of the PL/SQL block of code and should be assigned names.
Once an exception occurs, the natural flow of execution is halted, and then the execution
points to the exception section of the PL/SQL code.
Exception Handling Syntax:
DECLARE
<< declaration section >>
BEGIN
<<Block of executable code>>
EXCEPTION
<< exception handling >>
WHEN excp1 THEN
<< excp1 handling block >>
WHEN excp2 THEN
<< excp2 handling block >>
........
WHEN others THEN
<< excp2 handling block>>
END;

Database Management System(CE4012)


202103103510440

Query :
Ans. : SQL CODE:
create table student(sid int(10),sname varchar2(20),rank varchar(10));
insert into student values(501,'Ravi','second');
insert into student values(502,'Raju','third');
insert into student values(503,'Ramu','');
select *from student;

PL / SQl CODE :

Output:

Database Management System(CE4012)


202103103510440

Aim : ii. Insert data into student table and use COMMIT, ROLLBACK
and SAVEPOINT in MYSQL.
Theory:
COMMIT command :
COMMIT command is used to permanently save any transaction into the database.
When we use any DML command like INSERT, UPDATE or DELETE, the changes made
by these commands are not permanent, until the current session is closed, the changes made
by these commands can be rolled back.
To avoid that, we use the COMMIT command to mark the changes as permanent.

Following is commit command's syntax:


COMMIT;

ROLLBACK command:
This command restores the database to last commited state. It is also used with SAVEPOINT
command to jump to a savepoint in an ongoing transaction.
If we have used the UPDATE command to make some changes into the database, and realise
that those changes were not required, then we can use the ROLLBACK command to rollback
those changes, if they were not commited using the COMMIT command.
Following is rollback command's syntax:
ROLLBACK TO savepoint_name;

SAVEPOINT command:
SAVEPOINT command is used to temporarily save a transaction so that you can rollback to
that point whenever required.
Following is savepoint command's syntax:
SAVEPOINT savepoint_name;

Database Management System(CE4012)


202103103510440

Query :
Ans. : USE of COMMIT.

Ans. : USE of ROLLBACK

Database Management System(CE4012)


202103103510440

Ans. : USE of SAVEPOINT.

Conclusion:

we have learned about Package, its advantages, specifications, and structure.

Database Management System(CE4012)


202103103510440

Practical 10
Aim: Develop Programs using BEFORE and AFTER Triggers, Row and
Statement Triggers and INSTEAD OF Triggers.
Theory :
Triggers In PL/SQL:
A stored program that is fired by default or by some events is called a trigger.
Advantages Of Triggers:
 Ability to enforce referential integrity.
 The ability of monitoring.
 Ability to log and hold data on accessing tables.
 Ability to stop transactions that are not valid.
 Ability to enforce security features.
 Ability to produce derived column values by default.
The syntax for creating a trigger:
CREATE [OR REPLACE ] TRIGGER trigger_n
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF column_n]
ON table_n
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
<< Declaration statement >>
BEGIN
<< Block of executable code>>
EXCEPTION
<< Exception handling if any >>
END;
Here,
CREATE [OR REPLACE] TRIGGER trigger_n – This is for creating, replacing, or
updating a trigger having a name as trigger_n.

Database Management System(CE4012)


202103103510440

{BEFORE | AFTER | INSTEAD OF} – This is for determining the time when the trigger will
be fired. The INSTEAD OF is for creating a trigger that has a view.
{INSERT [OR] | UPDATE [OR] | DELETE} – This is for executing the DML actions.
[OF column_n] – This is for mentioning the column name that shall be modified.
[ON table_n] – This is for mentioning the table name that is attached to the trigger.
[REFERENCING OLD AS o NEW AS n] – This is for referring to the old and new values
by the DML statement like UPDATE, INSERT or DELETE.
[FOR EACH ROW] – This determines a row-level trigger, i.e., the trigger will be fired for
each row that is modified, else the trigger will fire just once when the SQL statement is
executed, which is known as a table-level trigger.
WHEN (condition) – This gives a condition for rows for which the trigger would be executed.
This is applicable only row-level triggers.

Query :

Output:

Output:

Database Management System(CE4012)


202103103510440

Output:

Output:

Conclusion:
In this tutorial, we have discussed some basic concepts of Triggers in PL SQL that are essential
to use them while programming. We have covered the following topics listed below:
Triggers.
Various operations on triggers.

Database Management System(CE4012)


20103103510440

Practical 11
Aim: Create a table and perform the search operation on table using
indexing and non- indexing techniques.
Theory:
The indexes are special objects which built on top of tables. The indexes can do an operation
like SELECT, DELETE and UPDATE statement faster to manipulate a large amount of data.
An INDEX can also be called a table and it has a data structure. An INDEX is created on
columns of a table. One table may contain one or more INDEX tables.
Syntax:
CREATE [UNIQUE] INDEX <index name> ON <table name> (<column(s)>);

Queries:
Ans.

Conclusion:
The article covers how to use SQL CREATE INDEX statement to create a clustered as well
as a non-clustered index.

Database Management System(CE4012)


202103103510440

Practical 12
Aim: Programs development using creation of procedures, passing
parameters IN and OUT of PROCEDURES.
Theory:
Procedures: − These subprograms do not return a value directly; mainly used to perform an
action.

Creating a Procedure:
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The
simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as follows –

CREATE [OR REPLACE] PROCEDURE procedure_name


[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;

Executing a Standalone Procedure:


A standalone procedure can be called in two ways −
1. Using the EXECUTE keyword
2. Calling the name of the procedure from a PL/SQL block

Queries:

Ans. :

Database Management System(CE4012)


202103103510440

Ans. :

Ans.:

Ans. :

Conclusion:
Use the CREATE PROCEDURE statement to create a standalone stored procedure or a call
specification.

Database Management System(CE4012)


202103103510440

Practical 13
Aim: To implement B-trees/B+ trees and Indexing.
Theory:
B-Tree:
B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of order m can
have at most m-1 keys and m children. One of the main reason of using B tree is its capability to
store large number of keys in a single node and large key values by keeping the height of the tree
relatively small.
A B tree of order m contains all the properties of an M way tree. In addition, it contains the
following properties.

1. Every node in a B-Tree contains at most m children.


2. Every node in a B-Tree except the root node and the leaf node contain at least m/2 children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.

It is not necessary that, all the nodes contain the same number of children but, each node must have
m/2 number of nodes.
A B tree of order 4 is shown in the following image.

Operations:

Database Management System(CE4012)


202103103510440

Searching :
Searching in B Trees is similar to that in Binary search tree. For example, if we search for an item 49
in the following B Tree. The process will something like following :

1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.

Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log n) time
to search any element in a B tree.

Inserting:
Insertions are done at the leaf node level. The following algorithm needs to be followed in order to
insert an item into B Tree.

1. Traverse the B Tree in order to find the appropriate leaf node at which the node can be
inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o Insert the new element in the increasing order of elements. o Split the
node into the two nodes at the median. o Push the median
element upto its parent node.
o If the parent node also contain m-1 number of keys, then split it too by
following the same steps.
Deletion:

Database Management System(CE4012)


202103103510440

Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf
node or an internal node. Following algorithm needs to be followed in order to delete a node from a
B tree.

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node then delete the desired key from the node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from
eight or left sibling.
o If the left sibling contains more than m/2 elements then push its largest element up to its
parent and move the intervening element down to the node where the key is deleted. o If
the right sibling contains more than m/2 elements then push its smallest element up to the
parent and move intervening element down to the node where the key is deleted.
4. If neither of the sibling contain more than m/2 elements then create a new leaf node by
joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the parent too.

If the the node which is to be deleted is an internal node, then replace the node with its in-order
successor or predecessor. Since, successor or predecessor will always be on the leaf node hence, the
process will be similar as the node is being deleted from the leaf node.

Application of B tree:
B tree is used to index the data and provides fast access to the actual data stored on the disks since,
the access to value stored in a large database that is stored on a disk is a very time consuming
process.

Searching an un-indexed and unsorted database containing n key values needs O(n) running time in
worst case. However, if we use B Tree to index this database, it will be searched in O(log n) time in
worst case.

B+ Tree:
B+ Tree is an extension of B Tree which allows efficient insertion, deletion and search operations.

In B Tree, Keys and records both can be stored in the internal as well as leaf nodes. Whereas, in B+
tree, records (data) can only be stored on the leaf nodes while internal nodes can only store the key
values.

The leaf nodes of a B+ tree are linked together in the form of a singly linked lists to make the search
queries more efficient.

Database Management System(CE4012)


202103103510440

B+ Tree are used to store the large amount of data which can not be stored in the main memory. Due
to the fact that, size of main memory is always limited, the internal nodes (keys to access records) of
the B+ tree are stored in the main memory whereas, leaf nodes are stored in the secondary memory.

Insertion in B+ Tree:
Step 1: Insert the new node as a leaf node

Step 2: If the leaf doesn't have required space, split the node and copy the middle node to the next
index node.

Step 3: If the index node doesn't have required space, split the node and copy the middle element to
the next index page.

Deletion in B+ Tree:
Step 1: Delete the key and data from the leaves.

Step 2: if the leaf node contains less than minimum number of elements, merge down the node with
its sibling and delete the key in between them.

Step 3: if the index node contains less than minimum number of elements, merge the node with the
sibling and move down the key in between them.

Database Management System(CE4012)


202103103510440

Example :
Insert the value 195 into the B+ tree of order 5 shown in the following figure.

195 will be inserted in the right sub-tree of 120 after 190. Insert it at the desired position.

The node contains greater than the maximum number of elements i.e. 4, therefore split it and place
the median node up to the parent.

Now, the index node contains 6 children and 5 keys which violates the B+ tree properties, therefore
we need to split it, shown as follows.

Database Management System(CE4012)


202103103510440

Database Management System(CE4012)

You might also like