RDBMS Lab Programs
RDBMS Lab Programs
RDBMS Lab Programs
Create table customer(cust_no varchar(5), cust_name varchar(15), age int, phone varchar(10) );
2. Create a table sales_man ( salesman_no primary key, s_name not null, place, phone unique)
Create table sales_order(order_no primary key
order_date not null
salesman_no foreign key references salesman_no in sales_man
del_type values should be either P or F (check constraints)
order_status values should be 'Inprocess','Fullfilled','Backorder', 'Cancelled' (check constraints) )
a) Insert few records in both tables
b) Delete primary key from sales_man table
c) Delete Foreign key and Check constraints from sales_order table
d) Add primary key in sales_man using ALTER TABLE
e) Add foreign key and CHECK constraints in sales_order table using ALTER TABLE
create table salesman(salesman_no varchar(5)primary key,s_name varchar(10) not null,place
varchar(15),phone varchar(15) unique);
Create table employee(EmpID varchar(5) primary key, EName varchar(10), Salary integer, Department
varchar(15),Age int);
insert into employee values('E001','Fasal',50000,'HR',33);
insert into employee values('E002','Julie',12000,'purchase',25);
insert into employee values('E003','Thanu',33450,'Administrative',29);
insert into employee values('E004','Jez',12345,'Office',28);
insert into employee values('E005','Aryan',51342,'Sales',40);
insert into employee values('E006','Meera',48000,'HR',40);
select * from employee
a) select count(*) from employee
b) select ename,age from employee where age in (select max(age) from employee group by department)
c) select department,avg(age) from employee group by department;
d) select department, avg(salary) from employee group by department
e) select min(salary) from employee
f) select count(*) from employee where department ='purchase'
g) select max(salary) from employee where department='Sales'
h) select max(salary)-min(salary) from employee
6. Create a table product with the fields (Product_code primary key, Product_Name, Category, Quantity,
Price).
Insert some records Write the queries to perform the following.
a. Display the records in the descending order of Product_Name
b. Display Product_Code, Product_Name with price between 20 and 50
c. Display the details of products which belongs to the categories of ‘bath soap’, ‘paste’, or
‘washing powder’
d. Display the products whose Quantity less than 100 or greater than 500
e. Display the products whose names starts with 's'
f. Display the products which not belongs to the category 'paste'
g. Display the products whose second letter is 'u' and belongs to the Category 'washing
powder'
Create table product(Product_code varchar(4) primary key, Product_Name varchar(15),
Category varchar(15), Quantity int, Price numeric(6,2))
insert into product values('p001','sunlight','washing powder',300,50.75);
insert into product values('p002','surf excel','washing powder',50,48.50);
insert into product values('p003','colgate','paste',120,20);
insert into product values('p004','lux','bath soap',25,50);
insert into product values('p005','enchanteur','perfume',530,120);
select * from product
a) select * from product order by Product_Name desc
b) select Product_Code, Product_Name from product where price between 20 and 50
c) select * from product where category in('bath soap','paste','washing powder')
d) select * from product where quantity<100 or quantity >500
e) select * from product where Product_Name like 's%';
f) select * from product where category!='paste'
g) select * from product where Product_Name like '_u%' and category='washing powder'
7. Consider the employee database given below. Give an expression in SQL for each of the following
queries:
EMPLOYEE (Employee-Name, City)
WORKS (Employee-Name, Company-Name, Salary)
COMPANY (Company-Name, City)
MANAGES (Employee-Name, Manager-Name)
A) Find the names of all employees who work in Infosys
B) Find the names and cities of residence of all employees who works in Wipro
C) Find the names, and cities of all employees who work in Infosys and earn more than Rs. 10,000.
D) Find the employees who live in the same cities as the companies for which they work.
E) Find all employees who do not work in Wipro Corporation.
F) Find the company that has the most employees.
8. Write a program code to calculate the area of a circle for a value of radius varying from 3 to 7. Store
the radius and the corresponding value of calculated area in an empty table named areas with field’s
radius and area.
r int;
a numeric;
begin
for r in 3..7
loop
a:=3.14*r*r;
end loop;
select radius();
9. Write a program block to calculate the electricity bill by accepting cust_no and units_consumed.
declare
BA numeric;
custno int;
unit int;
begin
open cur3;
loop
BA:= unit*3;
end loop;
close cur3;
select billcalc();
10. Create a procedure toprint Fibonacci number up to a limit, limit is passed as an argument
Create or replace function fib(N int)returns void as $$
declare
t1 int:=0;
t2 int:=1;
i int;
t3 int:=0;
begin
raise notice '%',t1;
raise notice '%',t2;
for i in 2..N loop
t3:=t1+t2;
t1:=t2;
t2:=t3;
raise notice '%',t3;
end loop;
end $$ language plpgsql;
select fib(10);
11. Create a function to check whether a given number is prime or not
returns void as $$
declare
i int ;
f int:=0;
begin
If n=2 then
f:=0;
else
for i in 2..(n-1)
loop
if (mod(n,i)=0)
then
f:=1;
exit;
end if;
end loop;
end if;
if(f=0)
then
else
select salcalc('IT');