Dbms Complete Record
Dbms Complete Record
Sc CS
Part B: RDBMS LAB PROGRAMS
1. Create a table customer (cust_no varchar (5), cust_name varchar (15), age
number, phone varchar (10))
a) insert 5 records and display it
b) add new field d_birth with date datatype
c) create another table cust_phone with fields cust_name and phone from customer
table
d) remove the field age
e) change the size of the cust_name to 25
f) delete all the records from the table
g) rename the table cutomer to cust
h) drop the table
2. Create a table sale_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
3. Create a table Hospital with the fields
(doctorid,doctorname,department,qualification,experience).
Write the queries to perform the following.
a) Insert 5 records
b) Display the details of Doctors
c) Display the details of doctors who have the qualification ‘MD’
d) Display all doctors who have more than 5 years experience but do not have the
qualification ‘MD’
e) Display the doctors in ‘Skin’ department
f) update the experience of doctor with doctored=’D003’ to 5
g) Delete the doctor with DoctorID=’D005’
4. Create the following tables
Bank_customer (accno primary key, cust_name, place)
Deposit (accno foreign key, deposit_no, damount)
Loan (accno foreign key loan_no, Lamount)
Write the following queries
a) Display the details of the customers
b) Display the customers along with deposit amount who have only deposit with
the bank
c) Display the customers along with loan amount who have only loan with the
bank
d) Display the customers they have both loan and deposit with the bank
e) Display the customer who have neither a loan nor a deposit with the bank
5. Create a table employee with fields (EmpID, EName, Salary, Department, and
Age).
Insert some records. Write SQL queries using aggregate functions and group by
clause
A. Display the total number of employees.
B. Display the name and age of the oldest employee of each department.
C. Display the average age of employees of each department
D. Display departments and the average salaries
E. Display the lowest salary in employee table
F. Display the number of employees working in purchase department
G. Display the highest salary in sales department;
H. Display the difference between highest and lowest salary
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'
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.
9. Write a program block to calculate the electricity bill by accepting cust_no and
units_consumed
10. Create a procedure to print Fibonacci number up to a limit, limit is passed as an
argument
11. Create a function to check whether a given number is prime or not
12. create a table emp_salary(empno,enamedept,salary)
Write a function to return the average salary of a particular department by
accepting departmentname as argument.
Program 1: CUSTOMER DETAILS
Create a table customer (cust_no varchar (5), cust_name varchar (15), age
number, phone varchar (10))
A) insert 5 records and display it
B) add new field d_birth with date datatype
C) create another table cust_phone with fields cust_name and phone from
customer table
D) remove the field age
E) change the size of the cust_name to 25
F) delete all the records from the table
G) rename the table cutomer to cust
H) drop the table
desc customer;
Lengt Precisi Scal Primary Nullab Defau Comme
Table Column Data Type h on e Key le lt nt
CUSTOME
CUST_NO NUMBER - 5 0 - - -
R
CUST_NA VARCHA
10 - - - - -
ME R2
AGE NUMBER - 3 0 - - -
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - - - -
1-5
1 devi 25 9874561230 -
2 devi 25 9874561230 -
3 devi 25 9874561230 -
4 devi 25 9874561230 -
5 devi 25 9874561230 -
CUSTOM
CUST_NO NUMBER - 5 0 - - -
ER
CUST_NA VARCHA
10 - - - - -
ME R2
AGE NUMBER - 3 0 - - -
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - -
CUST_NA VARCHA
CUST_PHONE 10 - - - - -
ME R2
PHONE NUMBER - 10 0 - - -
1-2
CUSTOM
CUST_NO NUMBER - 5 0 - - -
ER
CUST_NA VARCHA
10 - - - - -
ME R2
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - -
CUSTOM
CUST_NO NUMBER - 5 0 - - -
ER
CUST_NA VARCHA
25 - - - - -
ME R2
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - -
CUS
CUST_NO NUMBER - 5 0 - - -
T
CUST_NAM VARCHAR
25 - - - - -
E 2
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - - - -
13. Create a table sale_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 sales_man(salesman_no number(10) ,s_name varchar(20) not null,place varchar(10), phone
number(10) unique, constraint pk_saleman primary key(salesman_no));
desc sales_man;
Data Leng Precisi Sca Primary Nulla Defa Comm
Table Column Type th on le Key ble ult ent
SALESMAN NUMBE
SALES_MAN - 10 0 1 - - -
_NO R
VARCHA
S_NAME 20 - - - - - -
R2
VARCHA
PLACE 10 - - - - -
R2
NUMBE
PHONE - 10 0 - - -
R
1-4
SALES_ORD NUMBE
ORDER_NO - 10 0 1 - - -
ER R
ORDER_DAT
DATE 7 - - - - - -
E
SALESMAN_ NUMBE
- 20 0 - - -
NO R
VARCH
DEL_TYPE 10 - - - - -
AR2
ORDER_STA VARCH
20 - - - - -
TUS AR2
11 04/08/2021 1 p backorder
12 06/08/2021 2 y fullfilled
13 06/09/2021 3 p cancelled
14 06/10/2021 4 p inprocess
5 10/10/2021 5 p backorder
desc sales_man;
Data Leng Precisi Scal Primary Nullab Defa Comme
Table Column Type th on e Key le ult nt
SALES_M SALESMAN_
NUMBER - 10 0 - - -
AN NO
VARCHA
S_NAME 20 - - - - - -
R2
VARCHA
PLACE 10 - - - - -
R2
PHONE NUMBER - 10 0 -
desc sales_man;
Data Lengt Precisi Scal Primary Nullab Defau Comme
Table Column Type h on e Key le lt nt
SALES_M SALESMAN_
NUMBER - 10 0 1 - - -
AN NO
VARCHA
S_NAME 20 - - - - - -
R2
PLACE VARCHA 10 - - - - -
R2
PHONE NUMBER - 10 0 - -
5. alter table sales_man add constraint pk_sales_man foreign key(salesman_no) references sales_man
(salesman_no);
desc hospital;
HOSPIT VARCHA
DOCTORID 10 - - - - -
AL R2
DOCTORNAM VARCHA
10 - - - - -
E R2
DEPARTMEN VARCHA
10 - - - - -
T R2
QUALIFICATI VARCHA
10 - - - - -
ON R2
EXPERIENCE NUMBER - 5 0
desc bank_customer;
Data Leng Precisi Scal Primary Nullab Defa Comme
ble Column Type th on e Key le ult nt
BANK_CUSTO
ACC_NO NUMBER 22 - 0 1 - - -
MER
CUST_NA VARCHA
10 - - - - -
ME R2
VARCHA
PLACE 15 - - - - -
R2
desc deposit;
Data Lengt Precisio Scal Primary Nullabl Defaul Comme
Table Column Type h n e Key e t nt
DEPOSI NUMBE
ACC_NO 22 - 0 - - -
T R
DEPOSIT_N NUMBE
22 - 0 - - -
O R
NUMBE
DAMOUNT 22 - 0 - - -
R
desc loan;
Data Lengt Precisio Scal Primary Nullabl Defaul Commen
Table Column Type h n e Key e t t
LOA
ACC_NO NUMBER 22 - 0 - - -
N
LOAN_NO NUMBER 22 - 0 - - -
L_AMOUN
NUMBER 22 - 0 - -
T
Athira 30000
Samuel 300000
CUST_NAME L_AMOUNT
Kiran 35000
Aparna 65000
Arun
CUST_NAME
Abin
5. eomployee details
Create a table employee with fields (EmpID, EName, Salary, Department, and
Age).
Insert some records. Write SQL queries using aggregate functions and group by
clause
I. Display the total number of employees.
J. Display the name and age of the oldest employee of each department.
K. Display the average age of employees of each department
L. Display departments and the average salaries
M. Display the lowest salary in employee table
N. Display the number of employees working in purchase department
O. Display the highest salary in sales department;
P. Display the difference between highest and lowest salary
EMPLOY
EMPID NUMBER - 5 0 - - -
EE
VARCHA
EMPNAME 10 - - - - -
R2
SALARY NUMBER - 10 0 - - -
DEPARTME VARCHA
10 - - - - -
NT R2
AGE NUMBER - 5 0 -
COUNT(*)
2 select empname,age, department from employee where age in(select max(age) from employee
group by department);
Ajith 35 purchase
Rohan 42 Sales
Anitha 28 packing
DEPARTMENT AVG(AGE)
Purchase 29
Sales 34
Packing 28
DEPARTMENT AVG(SALARY)
Purchase 23000
Sales 24500
Packing 22000
5. select min(salary) from employee;
MIN(SALARY)
17000
COUNT(EMPID)
32000
MAX(SALARY)-MIN(SALARY)
15000
desc product;
PRODU PRODUCTCO
NUMBER - 5 0 1 - - -
CT DE
PRODUCTNA VARCHA
10 - - - - -
ME R2
VARCHA
CATEGORY 10 - - - - -
R2
QUANTITY NUMBER - 10 0 - - -
PRINCE NUMBER - 10 0 - - -
101 Lux
102 Colgate
103 Arieal
104 Sunlight
105 Santhoor
7. select * from product where productname like '_u%' and category ='washpowder';
WORK VARCHA
EMP_NAME 20 - - - - -
S R2
COMPANY_NA VARCHA
20 - - - - -
ME R2
SALARY NUMBER 22 - 0 - - -
desc manages;
MANAG VARCHA
EMP_NAME 20 - - - - -
ES R2
MANAGER_NA VARCHA
20 - - -
ME R2
insert into employee values('Ajith','Tirur');
insert into employee values('Alex','Tanur');
insert into employee values('Shahina','Manjeri');
insert into employee values('Sindhu','Calicut');
insert into employee values('Balan','Vadakara');
Shahina Manjeri
Ajith Tirur
Alex Tanur
Sindhu Calicut
Balan Vadakara
Infosys Calicut
Wipro Trivandrum
TCS Koc
insert into manages values('Ajith','Sachin');
insert into manages values('Alex','Sayooj');
insert into manages values('Shahina','Akhila');
insert into manages values('Sindhu','Kiran');
insert into manages values('Balan','Salman');
Ajith Sachin
Alex Sayooj
Shahina Akhila
Sindhu Kiran
Balan Salman
EMP_NAME
Ajith
Sindhu
Balan
EMP_NAME CITY
Shahina Manjeri
EMP_NAME CITY
Ajith Tirur
Balan Vadakara
Sindhu
Ajith
Alex
Sindhu
Balan
COMPANY_NAME NO_OF_EMPLOYEES
Infosys 3
Query:-
create table areas ( r number(2), area number(5));
declare
r number(5);
area number(14,2);
pi constant number (4,2):=3.14;
begin
r:=3;
while r<=7
loop
area:=pi*power(r,2);
insert into areas values(r,area );
r:=r+1;
end loop;
end;
Output:-
R AREA
3 28
4 50
5 79
6 113
7 154
Output:-
CNO 11
:UNIT 50
billamount 75
Statement processed.
PROGRAM 10 Fibonacci series
Create a procedure to print Fibonacci number up to a limit, limit is passed as
an argument
Query:-
declare
a number(3):=0;
b number(3):=1;
c number(3):=0;
n number(3);
procedure fibo(n number)
as begin
dbms_output.put_line('the Fibonacci series');
while(c<=n)
loop
dbms_output.put_line(c);
a:=b;
b:=c;
c:=a+b;
end loop;
end;
begin
n:=:n;
fibo(n);
end;
Output:-
5
N
the Fibonacci series
0
1
1
2
3
5
declare
get_value number;
function checkprime(no in number)
return varchar is
ans varchar(25);
n number;
begin
if no = 2 then
ans:='given number is prime';
else
n:= no/2;
for i in 2..n
loop
if(mod(no,i)=0) then
ans:='given number is not prime';
exit;
else
ans:='given number is prime';
end if;
end loop;
end if;
return(ans);
end;
begin
get_value:=:get_value;
dbms_output.put_line(checkprime(get_value));
end;
7
GET_VALUE
Statement processed