Professional Documents
Culture Documents
UTTARAKHAND
Submitted To:
Submitted By:
Himanshu Panchpal
BT14CSE053
INDEX
S.No
EXPERIMENT
DATE
1.
16/08/16
2.
23/08/16
3.
03/09/16
4.
06/09/16
5.
13/09/16
6.
Use of Keyword as
20/09/16
7.
Date operations
04/10/16
8.
18/10/16
Experiment 1
1. Creating and using Databases:
to view all the databases present
show databases;
create database company;
use company;
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);
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)));
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))));