You are on page 1of 16

NATIONAL INSTITUTE OF TECHNOLOGY,

UTTARAKHAND

DATABASE MANAGEMENT SYSTEM CSL 303


LAB RECORD FILE

Submitted To:

Submitted By:

Mr. Maroti Deshmukh


Assistant Professor

Himanshu Panchpal
BT14CSE053

INDEX
S.No

EXPERIMENT

DATE

1.

Creating and using databases & tables

16/08/16

2.

Performing queries on the tables

23/08/16

3.

Keywords like, count, auto_increment

03/09/16

4.

Distinct, Order by, Delete

06/09/16

5.

Foreign key and delete cascade

13/09/16

6.

Use of Keyword as

20/09/16

7.

Date operations

04/10/16

8.

Trigger and few operations

18/10/16

Experiment 1
1. Creating and using Databases:
to view all the databases present
show databases;
create database company;
use company;

2. Create 3 tables inside this database:


Inside the database Company we will create 3 tables customer, sales and product using create table
command and insert values in it.

3. View these tables:


select * from customer;
select * from sales;
select * from product;

Experiment 2
Performing various queries on the tables we created in first experiment:
1. Find customer id of person who purchased product P4:
select c_id from sales where P_code=P4;
2. Find average of MRP and SP in the product table:
select avg(MRP) from product;
select avg(SP) from product;
3. Find the details of customers who purchased P6:
select * from customer where c_id in(select c_id from sales where P_code=P4);

4. Alter the tables we have created:


To make the Phone number of customer detail unique for each customer
alter table customer modify ph_no int(10) unique;
To make the name column of customer detail not null
alter table customer modify column c_name char(10) not null;
To add a new column to an existing table
alter table sales add sno int(5) first;

5. Use of keyword between:


To find the products whose MRP is between Rs.50 and Rs.50000

Experiment 3
Performing various queries on the tables present in our database Company
1. Use of keyword like :
select c_name from customer where c_name like %aj%;
select c_name from customer where c_name like J%;
select c_name from customer where c_name like __p__;

2. Find the details of the customer who purchased the costliest product:
select * from customer where c_id=(select c_id from sales where P_code=(select P_code from product
where MRP=(select max(MRP) from product)));

3. Use of keyword count(*)


It counts the number of rows present in a table
select count(*) as Count from product;
To find the number of products whos Selling Price is greater than or equal to the average MRP
select count(*) from product where SP>=(select avg(MRP) from product);

4. Use of keyword Auto_increment:


alter table sales modify sno int(5) not null primary key auto_increment;

Experiment 4
1. Use of keyword Distinct
Sometimes various columns of a table may contain duplicate values and or purpose is to project only distinct
values present in a particular column. Thus in such condition DISTINCT keyword can be used.
Command used: select distinct c_id from sales;

2. Use of Order By
In a given table they data might be arranged without any particular order. Thus we can use ORDER BY to
arrange the data of any particular column and then display the table with that particular column sorted as per
its data type.
Command used:
select * from customer order by c_name;
select * from customer order by c_name desc;
select * from customer order by c_address;

3. Delete Operation
To delete a row of a given table: delete from sales where qty=3;

Experiment 5
Delete cascade:
When two tables are linked with each other via a Foreign Key, and if an entry is deleted from the referencing
table then that particular table should also be deleted from the referred table.
Eg. If entry of Customer C1 is deleted from customer table then all the sales made by C1 should be deleted
automatically from sales table.
commands used:
create table sales(c_id varchar(20), p_code varchar(20), qty int(5), foreign key (c_id) references customer
(c_id) on delete cascade, foreign key (p_code) references product(P_code));

Experiment 6
Adding an Extra column and use of keyword as
As keyword is used to rename table or attributes. Also in the following way we can add a column which is
not attributed to the table.
Command used:
select c_id, product.pcode, sales.qty, product.sp*sales.qty from sales, product where
sales.p_code=product.p_code;
instead of this we can use:
select c_id, product.pcode, sales.qty, product.sp*sales.qty as amount from sales, product where
sales.p_code=product.p_code;
select c_id, p.pcode, s.qty, p.sp*s.qty as amount from sales as s, product as p where s.p_code=p.p_code;

Experiment 7
Date Operation in SQL

Experiment 8
1. Trigger
Trigger operation is used in SQL in the particular condition when we have to insert or update an
entry in a given table and due to this value a change is required in the values of other table.
Eg. Suppose shopkeeper has 45 cellphones in stock. We insert an entry in sales table indicating 5
cellphones are purchased. As this sale is done, the qty of cellphones in stock should be reduced to
40 automatically.
command used:
create trigger xyz before insert on sales for each row update set product.qty=product.qty-new.qty
where product.p_code=new.p_code;

2. Query used to implement which customer has purchased products from either
Electronics or Medicine category

3. Query used to implement which customer has purchased products from both
Electronics and Medicine category.
Command used:
select * from customer where customer.c_id in (select customer.c_id from customer where customer.c_id in
(select s1.c_id from sales as s1, sales as s2 where s1.c_id = s2.c_idand (s1.p_code in (select product.p_code
from product where product.category =electronics)) and (s2.p_code in (select product.p_code from product
where product.category=medicine))));

You might also like