SRM Institute of Science & Technology, Delhi NCR
Campus
Department of Computer Science & Engineering
Database Management Systems (18CSC303J)
Lab File
DBMS Lab (18CSC303J) (CSE, VI SEM) Page 1
SRM Institute of Science & Technology, Delhi NCR Campus
Department of Computer Science & Engineering
LABORATORY FILE
Faculty Name : Ms.Neetu Bansla Department : CSE
Course Name : DBMS Lab Course Code : 18CSC303J
Year/Sem : 3rd/6th Academic Year : 2022-23
DBMS Lab (18CSC303J) (CSE, VI SEM) Page 2
LIST OF EXPERIMENTS
Expt. Title of experiment
No.
1. Creating tables and writing Queries in SQL.
2. To implement various DML Operations on table
3. To Implement the restrictions/constraints on the table
4. To Alter the structure of the table
5. To implement the concept of Joins
6. To implement the concept of grouping of Data
7. To implement the concept of Sub Queries
8. To implement the concept of Indexes and views.
9. To implement the basics of PL/SQL.
10. To implement the concept of Cursor and Trigger
Content Beyond Syllabus
1. Write a program to implement REPORTS.
2. Write a program to implement FORMS.
DBMS Lab (18CSC303J) (CSE, VI SEM) Page 6
GUIDELINES FOR LABORTORY RECORD PREPARATION
While preparing the lab records, the student is required to adhere to the following guidelines:
Contents to be included in Lab Records:
1. Cover page
2. Index
3. Experiments
Aim
Source code
Input-Output
DBMS Lab (18CSC303J) (CSE, VI SEM) Page 11
DBMS Lab (18CSC303J)
Student Name ANANYA GUPTA
RA1911003030265
Registration No.
B.TECH CSE I – BATCH 1
Section- Batch
DBMS Lab (18CSC303J) (CSE, VI SEM) Page 12
SRM Institute of Science & Technology, Ghaziabad
Department of Computer Science & Engineering
INDEX
Exper
iment Experiment Date of Date of Faculty
No. Name Conduction Submission Signature
1 Creating tables and writing
Queries in SQL.
2 To implement various DML
Operations on table.
3 To Implement the
restrictions/constraints on the
table.
4 To Alter the structure of the table.
5 To implement the concept of
Joins.
6 To implement the concept of
grouping of Data.
7 To implement the concept of
procedures.
8 To implement the concept of
functions.
9 To implement the concept of
cursors.
10 To implement the concept of
triggers.
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page 13
Experiment No.1
Aim: Creating tables and writing Queries in SQL.
Q1. Create the following tables:
i) client_master
columnname datatype size
client_no varchar2 6
name varchar2 20
address1 varchar2 30
address2 varchar2 30
city varchar2 15
state varchar2 15
pincode number 6
bal_due number 10,2
ii) Product_master
Columnname datatype
Product_no varchar2
Description varchar
Profit_percent number
Unit_measure varchar
Qty_on_hand number
Reorder_lvlnumber
Sell_price number
Cost_price number
Q2- Insert the following data into their respective tables:
Clientno Name city pincode state bal.due
0001 Ivan Bombay 400054 Maharashtra 15000
0002 Vandana Madras 780001 Tamilnadu 0
0003 Pramada Bombay 400057 Maharashtra 5000
0004 Basu Bombay 400056 Maharashtra 0
0005 Ravi Delhi 100001 2000
0006 Rukmini Bombay 400050 Maharashtra 0
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page 14
Data for Product Master:
Product No. Desciption Profit % Unit Qty Reorder Sell Cost
Percent measured on hand lvl pric pric
e e
P00001 1.44floppies 5 piece 100 20 525 500
P03453 Monitors 6 piece 10 3 12000 11200
P06734 Mouse 5 piece 20 5 1050 500
P07865 1.22 5 piece 100 20 525 500
floppies
P07868 Keyboards 2 piece 10 3 3150 3050
P07885 CD Drive 2.5 piece 10 3 5250 5100
P07965 540 HDD 4 piece 10 3 8400 8000
P07975 1.44 Drive 5 piece 10 3 1050 1000
P08865 1.22 Drive 5 piece 2 3 1050 1000
ANSWER –
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page 15
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page 16
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page 17
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page 18
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page 19
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page
110
Q3:- On the basis of above two tables answer the following Queries:
i) Find out the names of all the clients.
ii) Retrieve the list of names and cities of all the clients.
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page
111
iii) List the various products available from the product_master table.
iv) List all the clients who are located in Bombay.
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page
112
v) Display the information for client no 0001 and 0002.
vi) Find the products with description as ‘1.44 drive’ and ‘1.22 Drive’.
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page
113
vii) Find all the products whose sell price is greater than 5000.
viii) Find the list of all clients who stay in city ‘Bombay’ or city ‘Delhi’ or ‘Madras’.
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page
114
ix) Find the product whose selling price is greater than 2000 and less than or equal to 5000.
x) List the name, city and state of clients not in the state of ‘Maharashtra’.
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page
115
DBMS Lab (KCS-501) Manual (CSE, V SEM) Page
116
Experiment No. 2
Aim: To implement various DML Operations on table
Question.1 Using the table client master and product master answer the following Queries.
i. Change the selling price of ‘1.44 floppy drive to Rs.1150.00
ii. Delete the record with client 0001 from the client master table.
iii. Change the city of client_no’0005’ to Bombay.
iv. Change the bal_due of client_no ‘0001, to 1000.
v. Find the products whose selling price is more than 1500 and also find the new selling price as
original selling price *15.
vi. Find out the clients who stay in a city whose second letter is a.
vii. Find out the name of all clients having ‘a’ as the second letter in their names.
viii. List the products in sorted order of their description.
ix. Count the total number of orders.
x. Calculate the average price of all the products.
xi. Calculate the minimum price of products.
xii. Determine the maximum and minimum prices . Rename the tittle as ‘max_price’ and
min_price respectively.
xiii. Count the number of products having price greater than or equal to 1500.
USE OF PRIMARY KEY -
Experiment No.3
Aim: To implement the restrictions/constraints on the table
Question.2 Create the following tables:
i. Sales_master
Columnname Datatype Size Attributes
Salesman_no varchar2 6 Primary key/first letter
must start with ‘s’
Sal_name varchar2 20 Not null
Address varchar2 Not null
City varchar2 20
State varchar2 20
Pincode Number 6
Sal_amt Number 8,2 Not null, cannot be 0
Tgt_to_get Number 6,2 Not null, cannot be 0
Ytd_sales Number 6,2 Not null, cannot be 0
Remarks Varchar2 30
ii. Sales_order
Columnname Datatype Size Attributes
S_order_no varchar2 6 Primary/first letter must be 0
S_order_date Date 6 Primary key reference clientno of
client_master table
Client_no Varchar2 25
Dely_add Varchar2 6
Salesman_no Varchar2 6 Foreign key references
salesman_no of salesman_master
table
Dely_type Char 1 Delivery part(p)/full(f),default f
Billed_yn Char 1
Dely_date Date Can not be lessthan s_order_date
Order_status Varchar2 10 Values (‘in
process’;’fulfilled’;back
order’;’canceled
I. Sales_order_details
Column Datatype Size Attributes
S_order_no Varchar2 6 Primary key/foreign key
references s_order_no
of sales_order
Product_no Varchar2 6 Primary key/foreign key
references product_no
of product_master
Qty_order Number 8
Qty_disp Number 8
Product_rate Number 10,2
Insert the following data into their respective tables using insert statement:
Data for sales_man master table
Salesman_no Salesman Address CityPin code State Salamt Tgt_ Ytd Remark
name to_g Sales
et
500001 Kiran A/14 worli Bomba 400002 Mah 3000 100 50 Good
y
500002 Manish 65,nariman Bomba 400001 Mah 3000 200 100 Good
y
500003 Ravi P-7 Bandra Bomba 400032 Mah 3000 200 100 Good
y
500004 Ashish A/5 Juhu Bomba 400044 Mah 3500 200 150 Good
y
(i) Data for salesorder table:
S_orderno S_orderdate Client no Dely Bill yn Salesman no Delay Orderstatus
type date
019001 12-jan-96 0001 F N 50001 20-jan-96 Ip
019002 25-jan-96 0002 P N 50002 27-jan-96 C
016865 18-feb-96 0003 F Y 500003 20-feb-96 F
019003 03-apr-96 0001 F Y 500001 07-apr-96 F
046866 20-may-96 0004 P N 500002 22-may- C
96
010008 24-may-96 0005 F N 500004 26-may- Ip
96
(ii) Data for sales_order_details table:
S_order no Product no Qty ordered Qty disp Product_rate
019001 P00001 4 4 525
019001 P07965 2 1 8400
019001 P07885 2 1 5250
019002 P00001 10 0 525
046865 P07868 3 3 3150
046865 P07885 10 10 5250
019003 P00001 4 4 1050
019003 P03453 2 2 1050
046866 P06734 1 1 12000
046866 P07965 1 0 8400
010008 P07975 1 0 1050
010008 P00001 10 5 525
SALES_MASTER TABLE:
INSERTION IN SALES_MASTER –
AFTER ALL ATTRIBUTES:-
SALES_ORDER TABLE:
INSERTION IN SALES_ORDER –
SALES_ORDER_DETAILS TABLE:
Experiment No.4
Aim: To Alter the structure of the table
Q1. Create the following tables:
Challan_Header
Column name data type size Attributes
Challan_no varchar2 6 Primary key
s_order_no varchar2 6 Foreign key references s_order_no of
sales_order table
challan_date date not null
billed_yn char 1 values (‘Y’,’N’). Default ‘N’
Table Name : Challan_Details
Column name data type size Attributes
Challan_no varchar2 6 Primary key
Product No Varchar2 10 Foreign Key references Product_no
of product_master
Qty_disp number 4,2 not null
Q2. Insert the following values into the challan header and challan_details tables:
(i) Challan No S_order No Challan Date Billed
CH9001 019001 12-DEC-95 Y
CH865 046866 12-NOV-95 Y
CH3965 010008 12-OCT-95 Y
Data for challan_details table
Challan No Product No Qty Disp
CH9001 P00001 4
CH9001 P07965 1
CH9001 P07885 1
CH6865 P07868 3
CH6865 P03453 4
CH6865 P00001 10
CH3965 P00001 5
CH3965 P07975 2
CHALLAN HEADER TABLE –
CHALLAN_DETAILS TABLE-
Objective – Answer the following Queries
Q1. Make the primary key to client_no in client_master.
Q2. Add a new column phone_no in the client_master table.
Q3. Add the not null constraint in the product_master table with the columns description, profit
percent , sell price and cost price.
Q4. Change the size of client_no field in the client_master table.
Q5. Select product_no, description where profit percent is between 20 and 30 both inclusive.
Experiment No.5
Aim: to implement the concept of Joins
A. Consider the following schema for a Library Database:
BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Branch_id, No-of_Copies)
CARD(Card_No)
BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_Name, Address)
1- PUBLISHER TABLE –
2- BOOK TABLE-
3- BOOK_AUTHORS TABLE –
4- LIBRARY_BRANCH TABLE –
5- BOOK_COPIES TABLE-
6- CARD TABLE-
7- BOOK_LENDING TABLE-
Write SQL queries to :
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies
in each branch, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun
2017.
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation
operation.
4. Create a view of all books and its number of copies that are currently available in the Library.
5. Partition the BOOK table based on year of publication. Demonstrate its working with a simple
query.
EXPERIMENT NO. 6
OBJECTIVE: Answer the following Queries:
Experiment No.7
Objective:- To implement the concept of procedure.
PL/SQL Concepts- Stored Procedure:-
The PL/SQL stored procedure or simply a procedure is a PL/SQL block which performs one or more specific tasks.
It is just like procedures in other programming languages.
The procedure contains a header and a body.
Header: The header contains the name of the procedure and the parameters or variables passed to the procedure.
Body: The body contains a declaration section, execution section and exception section similar to a general PL/SQL
block.
How to pass parameters in procedure:
When you want to create a procedure or function, you have to define parameters .There is three ways to pass
parameters in procedure:
1. IN parameters: The IN parameter can be referenced by the procedure or function. The value of the parameter
cannot be overwritten by the procedure or the function.
2. OUT parameters: The OUT parameter cannot be referenced by the procedure or function, but the value of the
parameter can be overwritten by the procedure or function.
3. INOUT parameters: The INOUT parameter can be referenced by the procedure or function and the value of the
parameter can be overwritten by the procedure or function.
NOTE: A procedure may or may not return any value.
Syntax for creating procedure:
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION exception_section]
END [procedure_name];
OBJECTIVE:
TO CREATE A PROCEDURE .
Experiment NO.8
Objective:- To implement the concept of functions.
PL/SQL Concepts- Functions-
The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between
procedure and a function is, a function must always return a value, and on the other hand a
procedure may or may not return a value. Except this, all the other things of PL/SQL
procedure are true for PL/SQL function too.
Syntax to create a function:
1. CREATE [OR REPLACE] FUNCTION function_name [parameters]
2. [(parameter_name [IN | OUT | IN OUT] type [, ...])]
3. RETURN return_datatype
4. {IS | AS}
5. BEGIN
6. < function_body >
7. END [function_name];
Here:
1. Function_name: specifies the name of the function.
2. [OR REPLACE] option allows modifying an existing function.
3. The optional parameter list contains name, mode and types of the parameters.
4. IN represents that value will be passed from outside and OUT represents that this parameter will be used to return
a value outside of the procedure.
The function must contain a return statement.
5. RETURN clause specifies that data type you are going to return from the function.
6. Function_body contains the executable part.
7.The AS keyword is used instead of the IS keyword for creating a standalone
function.
Objective:-
TO CREATE FUNCTION-
Experiment No.9
Objective:- To implement the concept of Cursor.
Cursor– We have seen how oracle executes an SQL statement. Oracle DBA uses a work area for its
internal processing. This work area is private to SQL’s operation and is called a cursor.
The data that is stored in the cursor is called the Active Data set. The size of the cursor in memory is
the size required to hold the number of rows in the Active Data Set.
Explicit Cursor- You can explicitly declare a cursor to process the rows individually. A cursor
declared by the user is called Explicit Cursor. For Queries that return more than one row, You must
declare a cursor explicitly.
The data that is stored in the cursor is called the Active Data set. The size of the cursor in memory is
the size required to hold the number of rows in the Active
Why use an Explicit Cursor- Cursor can be used when the user wants to process data one row
at a time.
Explicit Cursor Management- The steps involved in declaring a cursor and manipulating data
in the active data set are:-
Declare a cursor that specifies the SQL select statement that you want to process.
Open the Cursor.
Fetch the data from the cursor one row at a time.
Close the cursor.
Explicit Cursor Attributes- Oracle provides certain attributes/ cursor variables to control the
execution of the cursor. Whenever any cursor(explicit or implicit) is opened and used Oracle creates a
set of four system variables via which Oracle keeps track of the ‘Current’ status of the cursor. You
Declare a cursor that specifies the SQL select statement that you want to process.
Open the Cursor.
Fetch the data from the cursor one row at a time.
Close the cursor.
How to Declare the Cursor:-
The General Syntex to create any perticular cursor is as follows:-
Cursor <Cursorname> is Sql Statement;
How to Open the Cursor:-
The General Syntex to Open any perticular cursor is as follows:-
Open Cursorname;
Fetching a record From the Cursor:-
The fetch statement retrieves the rows from the active set to the variables one at a time. Each time a
fetch is executed. The focus of the DBA cursor advances to the next row in the Active set.
One can make use of any loop structute(Loop-End Loop along with While,For) to fetch the records
from the cursor into variable one row at a time.
The General Syntex to Fetch the records from the cursor is as follows:-
Fetch cursorname into variable1,variable2,_
Closing a Cursor:-
The General Syntex to Close the cursor is as follows:-
Close <cursorname>;
Experiment No.10
Objective:- To implement the concept of Trigger.
Database Triggers:-
Database triggers are procedures that are stored in the database and are implicitly executed(fired)
when the contents of a table are changed.
Use of Database Triggers:-
Database triggers support Oracle to provide a highly customized database management system. Some
of the uses to which the database triggers can be put to customize management information in Oracle
are as follows:-
A Trigger can permit DML statements against a table anly if they are issued, during regular
bussiness hours or on predetermined weekdays.
A trigger can also be used to keep an audit trail of a table along with the operation performed
and the time on which the operation was performed.
It can be used to prevent invalid transactions.
Enforce complex security authorizations.
How to apply DataBase Triggers:-
A trigger has three basic parts:-
1. A triggering event or ststement.
2. A triger restriction
3. A trigger action.
Types of Triggers:-
Using the various options , four types of triggers can be created:-
1. Before Statement Trigger:- Before executing the triggering statement, the trigger action is
executed.
2. Before Row Trigger:- Before modifying the each row affected by the triggering statement
and before appropriate integrity constraints, the trigger is executed if the trigger restriction
either evaluated to TRUE or was not included.’
3. After Ststement Trigger:- After executing the triggering statement and applying any
deferred integrity canstraints, the trigger action is executed.
4. After row Trigger:- After modifying each row affected by the triggering statement and
possibly applying appropriate integrity constraints, the trigger action is executed for the
current row if the trigger restriction either evaluates to TRUE or was not included.
Syntex For Creating Trigger:-
The syntex for Creating the Trigger is as follows:-
Create or replace Trigger<Triggername> {Before,After} {Delete, Insert, Update } On <Tablename>
For Each row when Condition
Declare
<Variable declarations>;
<Constant Declarations>;
Begin
<PL/SQL> Subprogram Body;
Exception
Exception Pl/SQL block;
End;
How to Delete a Trigger:-
The syntex for Deleting the Trigger is as follows:-
Drop Trigger <Triggername>;
PL/SQL Trigger Example
Let's take a simple example to demonstrate the trigger. In this example, we are using the following CUSTOMERS table:
Create table and have records:
Create trigger: Let's take a program to create a row level trigger for the CUSTOMERS table that would fire for INSERT or
UPDATE or DELETE operations performed on the CUSTOMERS table. This trigger will display the salary difference between
the old values and new values:
Check the salary difference by procedure: Use the following code to get the old salary, new salary and salary difference
after the trigger created. Note: As many times you executed this code, the old and new both salary is incremented by 5000
and hence the salary difference is always 5000.