0% found this document useful (0 votes)
43 views

DB Exp 1-8

The document describes several experiments with SQL commands to create, alter, insert, select, update and delete from various tables. Experiment 1 creates a customer table, inserts sample data, adds a column, copies data to a new table, drops a column, renames a table and drops the table. Experiment 2 creates sales tables with constraints, inserts sample data, alters constraints, and re-adds constraints. Experiment 3 creates a hospital table, inserts and selects sample data, updates a row, and deletes a row. Experiment 4 creates banking tables, inserts sample data, and performs various joins and selects across the tables. Experiment 5 creates an employee table for testing aggregate functions.

Uploaded by

Ismail Musharaf
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

DB Exp 1-8

The document describes several experiments with SQL commands to create, alter, insert, select, update and delete from various tables. Experiment 1 creates a customer table, inserts sample data, adds a column, copies data to a new table, drops a column, renames a table and drops the table. Experiment 2 creates sales tables with constraints, inserts sample data, alters constraints, and re-adds constraints. Experiment 3 creates a hospital table, inserts and selects sample data, updates a row, and deletes a row. Experiment 4 creates banking tables, inserts sample data, and performs various joins and selects across the tables. Experiment 5 creates an employee table for testing aggregate functions.

Uploaded by

Ismail Musharaf
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 17

Exp 1 cutomer table

------------------------------------------------------------
create table customer(cust_no varchar(5),cust_name varchar(15),age numeric,phone
varchar(10));
a)
insert into customer values(1,'A',23,94567);

insert into customer values(2,'B',23,94567);

insert into customer values(3,'C',23,94567);

insert into customer values(4,'D',23,94567);

insert into customer values(5,'E',23,94567);

select *from customer;

cust_no | cust_name | age | phone


---------+-----------+-----+-------
1 |A | 23 | 94567
2 |B | 23 | 94567
3 |C | 23 | 94567
4 |D | 23 | 94567
5 |E | 23 | 94567
(5 rows)

b)alter table customer add d_birth date;


ALTER TABLE

\d customer;
Table "public.customer"
Column | Type | Modifiers
-----------+-----------------------+-----------
cust_no | character varying(5) |
cust_name | character varying(15) |
age | numeric |
phone | character varying(10) |
d_birth | date |

c)
create table cust_phone as select cust_name,phone from customer;
SELECT 5

select *from cust_phone;


cust_name | phone
-----------+-------
A | 94567
B | 94567
C | 94567
D | 94567
E | 94567
(5 rows)

d)

alter table customer drop age;


select *from customer;
cust_no | cust_name | phone | d_birth
---------+-----------+-------+---------
1 |A | 94567 |
2 |B | 94567 |
3 |C | 94567 |
4 |D | 94567 |
5 |E | 94567 |
(5 rows)

e)

ALTER TABLE customer ALTER COLUMN cust_name TYPE varchar(25);

\d customer;
Table "public.customer"
Column | Type | Modifiers
-----------+-----------------------+-----------
cust_no | character varying(5) |
cust_name | character varying(25) |
phone | character varying(10) |
d_birth | date
f)

TRUNCATE customer;

select *from customer;


cust_no | cust_name | phone | d_o_b
---------+-----------+-------+-------
(0 rows)

g)

ALTER TABLE customer RENAME TO cust;

select *from cust;


cust_no | cust_name | phone | d_o_b
---------+-----------+-------+-------
(0 rows)

h)drop table cust;

Exp 2 constraints
---------------------------------

create table sales_man(salesman_no int primary key,s_name varchar(10) not null,place


varchar(10),phone numeric(10) unique);

Column | Type | Modifiers


-------------+-----------------------+-----------
salesman_no | integer | not null
s_name | character varying(10) | not null
place | character varying(10) |
phone | numeric(10,0) |

create table sales_order(order_no int primary key,order_date date NOT NULL,order_status


char(10) NOT NULL check(order_status IN('inprocess','fullfiled','cancelled')),salesman_no int
references sales_man(salesman_no),del_type char(1) check (del_type='F' or del_type='P'));

Column | Type | Modifiers


--------------+---------------+-----------
order_no | integer | not null
order_date | date | not null
order_status | character(10) | not null
salesman_no | integer |
del_type | character(1) |

a)insert into
sales_man(salesman_no,s_name,place,phone)values(101,'ananthu','feroke',8137036211);

insert into
sales_man(salesman_no,s_name,place,phone)values(102,'fariz','chungam',8137036231);

insert into
sales_man(salesman_no,s_name,place,phone)values(103,'anees','chelari',9997036231);

insert into sales_man(salesman_no,s_name,place,phone)values(104,'aslah','chelari',9687036231);

insert into sales_man(salesman_no,s_name,place,phone)values(105,'jithin','tanur',9687035671);

insert into sales_order values(1,'01-01-17','inprocess',101,'F'),(2,'03-02-17','fullfiled',102,'F'),


(3,'03-03-17','fullfiled',103,'P'),
(4,'03-03-17','cancelled',104,'F'), (5,'05-03-17','cancelled',105,'P');

b)ALTER TABLE sales_order DROP constraint sales_order_salesman_no_fkey;

ALTER TABLE sales_man DROP constraint sales_man_pkey;

c) ALTER TABLE sales_order DROP constraint sales_order_del_type_check;

ALTER TABLE sales_order DROP constraint sales_order_order_status_check;


d)ALTER TABLE sales_man ADD primary key(salesman_no);

e) ALTER TABLE sales_order ADD FOREIGN KEY (salesman_no) REFERENCES


sales_man(salesman_no);

ALTER TABLE sales_order ADD CHECK (del_type = 'F' OR del_type = 'P');


ALTER TABLE
ALTER TABLE sales_order ADD check(order_status IN('inprocess','fullfiled','cancelled')));
ALTER TABLE

Exp 3
Hospital table
---------------------------------------------------------

create table hospital(doctor_id char(4),doctor_name varchar(10),department


varchar(25),qualification varchar(25),experience int);

A)
insert into hospital values('D001','miya','cardiologist','mbbs',5);
insert into hospital values('D002','john','orthologist','md',4);

insert into hospital values('D003','ramesh','skin','mbbs',3);

insert into hospital values('D004','madona','dentist','bds',6);

insert into hospital values('D005','manoj','optometry','md',1);

B)
select * from hospital;
doctor_id | doctor_name | department | qualification | experience
-----------+-------------+--------------+---------------+------------
D001 | miya | cardiologist | mbbs | 5
D002 | john | orthologist | md | 4
D003 | ramesh | skin | mbbs | 3
D004 | madona | dentist | bds | 6
D005 | manoj | optometry | md | 1
(5 rows)

C)
select doctor_name from hospital where qualification='md';
doctor_name
-------------
john
manoj
(2 rows)

D)
select doctor_name from hospital where experience>5 and qualification!='md';
doctor_name
-------------
madona
(1 row)

E)
select doctor_name from hospital where department='skin';
doctor_name
-------------
ramesh
(1 row)

F) update hospital set experience=5 where doctor_id='D003';


select * from hospital;
doctor_id | doctor_name | department | qualification | experience
-----------+-------------+--------------+---------------+------------
D001 | miya | cardiologist | mbbs |5
D002 | john | orthologist | md |4
D004 | madona | dentist | bds |6
D005 | manoj | optometry | md |1
D003 | ramesh | skin | mbbs |5
(5 rows)

G)
delete from hospital where doctor_id='D005';
DELETE 1
select * from hospital;
doctor_id | doctor_name | department | qualification | experience
-----------+-------------+--------------+---------------+------------
D001 | miya | cardiologist | mbbs |5
D002 | john | orthologist | md |4
D004 | madona | dentist | bds |6
D003 | ramesh | skin | mbbs |5
(4 rows)
exp 4 banking database
----------------------------------------------------------------------
create table bank_customer(accno int primary key,customer_name varchar(10),place
varchar(25));

create table deposit(accno int references bank_customer(accno),deposit_no int, damount


numeric);

create table loan(accno int references bank_customer(accno), loan_no int,lamount numeric);

insert into bank_customer values(101,'ravi','clt');

insert into bank_customer values(102,'dasan','tvm');

insert into bank_customer values(103,'luthufi','mlprm');

insert into bank_customer values(104,'biju','knr');

insert into bank_customer values(105,'jose','klm');

insert into bank_customer values(106,'shibu','kch');

insert into bank_customer values(107,'shyam','tvm');

insert into bank_customer values(108,'mohan','knr');

insert into deposit values(101,15,400000);

insert into deposit values(102,13,75000);

insert into deposit values(105,12,55000);

insert into deposit values(108,16,750000);

insert into loan values(103,4,500000);

insert into loan values(104,2,200000);

insert into loan values(106,6,300000);


insert into loan values(108,8,600000);

a) select * from bank_customer;


accno | customer_name | place
-------+---------------+-------
101 | ravi | clt
102 | dasan | tvm
103 | luthufi | mlprm
104 | biju | knr
105 | jose | klm
106 | shibu | kch
107 | shyam | tvm
108 | mohan | knr
(8 rows)
b) select b.accno,customer_name,damount from bank_customer b join deposit d on b.accno=d.accno
where b.accno not in(select accno from loan);
accno | customer_name | damount
-------+---------------+---------
101 | ravi | 400000
102 | dasan | 75000
105 | jose | 55000
(3 rows)
c) select b.accno,customer_name,lamount from bank_customer b join loan l on b.accno=l.accno
where b.accno not in(select accno from deposit);
accno | customer_name | lamount
-------+---------------+---------
103 | luthufi | 500000
104 | biju | 200000
106 | shibu | 300000
(3 rows) ^
d) select customer_name from bank_customer where accno in((select accno from
loan)intersect(select accno from deposit));
customer_name
---------------
mohan
(1 row)
e) select customer_name from bank_customer where accno not in((select accno from
loan)union(select accno from deposit));
customer_name
---------------
shyam
(1 row)
---------------------------------------------------------------------------
exp 5 Aggregate functions
create table employee(empid int PRIMARY KEY,Ename varchar(10),salary numeric,department
varchar(20),age int);
insert into employee values(001,'vishnus',15000,'marketting',49);
insert into employee values(002,'basith ',150000,'sales',25);
insert into employee values(003,'sreerag',30000,'purchase',35);
insert into employee values(004,'infan ',2000,'sales',35);
insert into employee values(005,'safwan ',2000,'sales',25);

Column | Type | Modifiers


------------+-----------------------+-----------
empid | integer | not null
ename | character varying(10) |
salary | numeric |
department | character varying(20) |
age | integer |

select * from employee;


empid | ename | salary | department | age
-------+---------+--------+-------------------+-----
1 | vishnus | 15000 | marketting | 49
2 | basith | 150000 | sales | 25
3 | sreerag | 30000 | purchase | 35
4 | infan | 200000 | sales | 35
(4 rows)

a)select count(empid)from employee;


count
-------
4
(1 row)

b) select ename,department from employee where age in(select max(age) from employee group
by department);
ename | department
---------+-------------------
vishnus | marketting
basith | sales
sreerag | purchase
infan |sales

c)select department,avg(age)from employee group by department;


department | avg
------------+---------------------
sales | 28.3333333333333333
marketting | 49.0000000000000000
purchase | 35.0000000000000000
(3 rows)
d)select department,avg (salary)from employee group by department;

department | avg
------------+------------------------
sales | 51333.333333333333
marketting | 15000.0000000000000000
purchase | 30000.000000000000
(3 rows)
e) select min(salary) as min_salary from employee;
select min(salary) as min_salary from employee;
min_salary
------------
2000
(1 row)
f)select count(ename)from employee where department='purchase';
count
-------
1
(1 row)
g)select max(salary)from employee where department='sales';
max
--------
150000
(1 row)
h) select max(salary) - min(salary) as sal_difference from employee;
sal_difference
----------------
148000
(1 row)

Exp 6 product table & select queries


create table product(Product_code int primary key, Product_Name varchar(20),Category
varchar(20), quantity int,price numeric);

insert into product values(1,'colgate','paste',10,100);

insert into product values(2,'close up','paste',9,90);


insert into product values(3,'nirma','bath soap',10,600);

insert into product values(4,'nirma','washing powder',10,700);

insert into product values(5,'toy','car',1,200);

insert into product values(6,'toy','bike',3,300);

insert into product values(7,'lux','bath soap',1,20);


insert into product values(8,'lux','bath liquid',600,2000);

insert into product values(9,'nirma','bath liquid',300,1000);

select * from product;


product_code | product_name | category | quantity | price
--------------+--------------+----------------+----------+-------
1 | colgate | paste | 10 | 100
2 | close up | paste | 9 | 90
3 | nirma | bath soap | 10 | 600
4 | nirma | washing powder | 10 | 700
5 | toy | car | 1 | 200
6 | toy | bike | 3 | 300
7 | lux | bath soap | 1 | 20
8 | lux | bath liquid | 600 | 2000
9 | nirma | bath liquid | 300 | 1000
(9 rows)

a)select * from product order by product_name desc;


product_code | product_name | category | quantity | price
--------------+--------------+----------------+----------+-------
5 | toy | car | 1 | 200
6 | toy | bike | 3 | 300
4 | nirma | washing powder | 10 | 700
9 | nirma | bath liquid | 300 | 1000
3 | nirma | bath soap | 10 | 600
8 | lux | bath liquid | 600 | 2000
7 | lux | bath soap | 1 | 20
1 | colgate | paste | 10 | 100
2 | close up | paste | 9 | 90
(9 rows)

b)select product_code,product_name from product where price between 20 and 50;


product_code | product_name
--------------+--------------
7 | lux
(1 row)

C)select product_name,price from product where category in ('bath soap','paste','washing


powder');
product_name | price
--------------+-------
colgate | 100
close up | 90
nirma | 600
nirma | 700
lux | 20
(5 rows)

d) select * from product where quantity<100 or quantity>500;


product_code | product_name | category | quantity | price
--------------+--------------+----------------+----------+-------
2 | close up | paste | 9 | 90
3 | nirma | bath soap | 10 | 600
4 | nirma | washing powder | 10 | 700
5 | toy | car | 1 | 200
6 | toy | bike | 3 | 300
7 | lux | bath soap | 1 | 20
8 | lux | bath liquid | 600 | 2000
1 | colgate | paste | 10 | 100
(8 rows)

E)select product_name from product where product_name like 's%';


product_name
--------------
(0 rows)

f)select product_name from product where category !='paste';


product_name
--------------
colgate
close up
nirma
nirma
toy
toy
lux
lux
nirma
radhas
(10 rows)

g)insert into product values(10,'super wash','washing powder',600,2000);


INSERT 0 1

select product_name from product where product_name like '_u%' and category='washing
powder';
product_name
--------------
super wash
(1 row)

exp 7 employee database


------------------------------------------------

create table employee(empname varchar(10) primary key,city varchar(10));

create table company(company_name varchar(10) primary key ,city varchar(10));

create table works(empname varchar(10) primary key references employee(empname),cname


varchar(10) references company(company_name),salary int);
create table manages(empname varchar(10) references employee(empname),manager_name
varchar(10) references employee(empname),primary key(empname,manager_name));

insert into employee values('swathi','kzkd');


insert into employee values('vishnu','tvm');
insert into employee values('shreya','usa');
insert into employee values('choilly','dubai');
insert into employee values('sajid','malappuram');

insert into company values('infosys','tvm');


insert into company values('chandrika','kolkatha');
insert into company values('wipro','kochi');
insert into company values('tata','mumbai');
insert into company values('vibro','delhi');

insert into works values('swathi','infosys',10000);


insert into works values('vishnu','infosys',15000);
insert into works values('shreya','wipro',71502);
insert into works values('choilly','infosys',8000);
insert into works values('sajid','vibro',18000);

insert into manages values('swathi','shreya');


insert into manages values('vishnu','vishnu');
insert into manages values('sajid','choilly');
insert into manages values('sajid','swathi');
insert into manages values('shreya','vishnu');

a) select empname from works where cname='infosys';

empname
---------
swathi
vishnu
choilly
(3 rows)

b) select employee.empname,employee.city from employee,works where


employee.empname=works.empname and company_name='wipro';

empname | city
---------+------
shreya | usa
(1 row)

c)select employee.empname,city from employee,works where employee.empname=


works.empname and company_name='wipro' and salary>10000;
empname | city
---------+------
shreya | usa
(1 row)

d)select employee.empname from employee,works,company where


employee.empname=works.empname and employee.city=company.city and
works.cname=company.company_name;
empname
---------
vishnu
(1 row)

e)select empname from works where cname!='wipro';


empname
---------
sajid
swathi
vishnu
choilly
(4 rows)

f) select cname from works group by cname order by count(*) desc limit 1;
cname
---------
infosys
(1 row)

--------------------------------------------------------
exp 8 supplier-product database
create table supplier(supcode char(4) primary key,sname varchar(20),city varchar(15));

create table product(pcode char(4) primary key,pname varchar(20));


create table splproduct(supcode varchar(4) references supplier(supcode),pcode varchar(4)
references product(pcode),qty int,primary key(supcode,pcode));

insert into supplier values('s1','musthafa','pgdi');


insert into supplier values('s2','kavya','ktmchi');
insert into supplier values('s3','pranav','kdlndi');
insert into supplier values('s4','manuppa','pgdi');
insert into supplier values('s5','mdmah','chtpdi');
insert into supplier values('s6','sreya','kdlndi');

insert into product values('p1','pr1');


insert into product values('p2','pr2');
insert into product values('p3','pr3');
insert into product values('p4','pr4');
insert into product values('p5','pr5');

insert into splproduct values('s1','p1',150);


insert into splproduct values('s2','p2',270);
insert into splproduct values('s3','p3',120);
insert into splproduct values('s4','p4',320);
insert into splproduct values('s5','p4',320);

a)select first.supcode,second.supcode from supplier first,supplier second where


first.city=second.city and first.supcode<second.supcode;

supcode supcode
s1 s4
s3 s6

b)select distinct sname from supplier where supcode in (select supcode from splproduct where
pcode='p2');

sname
-------
kavya
(1 row)

c) select pcode from splproduct group by pcode having count(pcode) > 1;


pcode
-------
p4
(1 row)

d) select supcode from supplier where city =(select city from supplier where supcode='s1');
supcode
---------
s1
s4
(2 rows)
e)select sname from supplier where supcode in (select supcode from splproduct where
pcode='p1');
sname
----------
musthafa
(1 row)
f) select count (distinct supcode )from splproduct;
count
-------
5
(1 row)
g)select pname,p.pcode,qty from product p join splproduct sp on p.pcode=sp.pcode ;
pname | pcode | qty
-------+-------+-----
pr1 | p1 | 150
pr2 | p2 | 270
pr3 | p3 | 120
pr4 | p4 | 320
pr4 | p4 | 320
(5 rows)

You might also like