Subject Notes Front Page -DBMS-Lab
Subject Notes Front Page -DBMS-Lab
Prepared By,
Sandhya M
Faculty of Computer Applications
Part A
1. The Book detail databases has a table with the following attributes. books(book_title: varchar2,
book_author: varchar2, book_price: number) Execute a Single line query and group functions.
i) Create the above table.
ii) Enter five tuples into the table.
iii) Display all the tuples in books table.
iv) Use group by functions (MAX,MIN)
v) Use group by functions SUM
book_title max(book_price)
------------------ ---------------
DBMS 500
Java 400
C 300
DBMS 200
Java lab 100
select max(book_price) from books group by book_price ;
max(book_price)
-----------------------
500
400
300
200
100
select book_title, min(book_price) from books group by book_title;
book_title min(book_price)
--------------- ---------------
Java lab 100
DBMS 200
C 300
Java 400
DBMS 500
book_price
----------
100
book_price
----------
1500
----------------------------------------------------------------------------------------------------------------------------- ----------
--------------------------------------------------------------------------------------------------
2. The STUDENT detail databases has a table with the following attributes. The primary keys are
underlined. STUDENT (regno: int, name: string, dob: date, marks: int) Using DDL Commands.
desc student;
desc student;
desc student;
Table dropped.
----------------------------------------------------------------------------------------------------------------------------- -------------
Increase the salary of those employees working for the computer department by Rs.5000
----------------------------------------------------------------------------------------------------------------------------- ----------
---------------------------------------------------------------------------------------------------------------------------------------
vii) List the details of all the books whose price ranges between Rs. 100 and Rs. 300
PUBLISHERS
--------------------
MES
Subhas
Himalaya
vi) commit;
savepoint mysave;
Rollback to mysave;
viii) Select * from Library where Price between 250 and 275;
-----------------------------------------------------------------------------------------------------------------------
5) The Products database has a table with the following attributes. Products(Product_id :number,
Product_name: varchar2, Item_price :number) Implement the nested queries.
----------------------------------------------------------------------------------------------------------------------------- ----------
6. The Student detail databases has a table with the following attributes. The primary keys are underlined.
Student (id:number, name: varchar2, dob: date).
The Marks detail databases has a table with the following attributes. The primary keys are underlined.
Marks(id:number,DBMS:number,C#:number,English:number,CCN:number).Implement join operations
in SQL.
11 Ram 19-DEC-19 11 50 65 70 55
12 Arun 19-DEC-95 12 80 65 90 55
13 Vinu 22-OCT-94 13 70 65 40 75
14 Rose 16-DEC-95 14 50 65 70 55
15 Sharely 08-JUL-94 15 60 85 70 45
----------------------------------------------------------------------------------------------------------------------------- ----------
7. The Emp detail databases has a table with the following attributes. The primary keys are underlined.
Emp (Empid:number, Empname: varchar2, Deptno:number,salary:number).
The Dept detail databases has a table with the following attributes. The primary keys are underlined.Dept
(Deptno:number,Deptname:varchar2,).create foreign key for Deptno.Create views for particular table.
--------------------------------------------------------------------------------------------------------------------------------------
Create user:
Table(s) Locked.
----------------------------------------------------------------------------------------------------------------------------- ------
9. The SALARY database of an organization has a table with the following attributes.
v)Find the sum and average of the salaries of employees of a particular department.
SUM(SALARY)
-----------
124000
SUM(SALARY) AVG(SALARY)
----------- -----------
52000 26000
MIN(SALARY)
-----------
20000
MAX(SALARY)
-----------
40000
----------------------------------------------------------------------------------------------------------------------------- ------
10. Consider the insurance database given below. The primary keys are underline and the data types are
specified.
i) Create the above tables by properly specifying the primary keys and the foreign keys
a) Update the damage amount for the car with a specific regno in the accident with report no
12 to 25000.
iv) Find total number of people who owned cars that were involved in accidents in 2002
v) Find the number of accidents in which cars belonging to a specific model were involved
Queries
1. Create table persondb(driver_id varchar2(20) primary key,name varchar2(20) not null, address
varchar2(30));
2. Create table cardb(regno varchar2(20) primary key, model varchar2(20) not null,year
number(5));
3. Create table accidentdb(report_no number(10) primary key,accident_date date,location
varchar2(20));
4. Create table ownsdb (driver_id varchar2(20) references persondb,,regno varchar2(20)
references cardb);
5. Create table participateddb(driver_id varchar2(20) references persondb,regno varchar2(20)
references cardb,report_no number(10) references accidentdb,damage_amount number(10));
6. Insert into persondb values(&driver_id,’&name’,’&address’);
7. Insert into cardb values(®no,’&model’,&year);
8. Insert into accidentdb values(&report_no,&accident_date,’&location’);
9. Insert into ownsdb values(&driver_id,®_no);
10. Insert into participateddb values(&driver_id,®_no,&report_no,&damage_amount);
11. Update participateddb set damage_amount = 25000 where reg_no = 1001 and report_no = 12;
12. Insert into participateddb values(1004,2004,10,3000);
13. Select count(*) from accidentdb where accident_date LIKE ‘%-%-02’;
14. Select count(*) from cardb c ,participateddb p where c.regno=p.regno and c.model = ‘scoda’;
Part B
DECLARE
N number;
BEGIN
N:=10/0;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE ('zero divide error');
END;
/
Output:
Zero Divide Error
iv)PL/SQL
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET c_salary = c_salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/
--------------------------------------------------------------------------------------------------------------------------------------
13. Write PL/SQL procedure for an application using Find the total strength of students in a class using
functions.
// create a table
i)Create table student
(
Stud_id number,
Stud_name varchar(20),
Stud_address varchar(20)
);
// Function
iii)create or replace function totalStrength
return number as
strength number;
begin
select count(Stud_id) into strength from student;
return strength;
end;
/
// PL/SQL Block
iv)declare
strength number;
begin
strength:=totalstrength();
dbms_output.put_line('Total strength of students is ' || strength);
end;
/
Output:
----------------------------------------------------------------------------------------------------------------------------- ------
iv)Exec deptpack.addrec(10,’HRM’,’LONDON’);
Exec deptpack.addrec(10,’HRM’,’BOSTON’);
vi)Exec deptpack.delrec(20);
---------------------------------------------------------------------------------------------------------------------------
// PL/SQL Block
Declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=&c;
if (a>b and a>c) then
dbms_output.put_line(‘a is largest’ || a); elsif (b>a and
b>c) then
dbms_output.put_line(‘b is largest’ || b);
else
dbms_output.put_line(‘c is the largest’||c);
end if;
end;
/
Output:
// PL/SQL Block
declare
n number;
begin
n:= &number;
if mod(n,2) = 0 then
else
dbms_output.put_line ('the number '||n||' is odd number.');
end if;
end;
Output:
new 4: n:= 5;
----------------------------------------------------------------------------------------------------------------------------- ---------
// PL/SQL Block
Declare
grd char(1);
begin
grd := '&grade’;
end if;
end;
Output:
18. Write a PL/SQL program to check whether a given character is alphabet or digit or Special character.
// PL/SQL Block
declare
ch char(1);
begin
ch := '&character';
if ( ch >= 'a' and ch <= 'z' )
or ( ch >= 'a' and ch <= 'z' ) then
dbms_output.put_line ('the given character is an alphabet');
elsif ch>= '0' and ch<='9' then
dbms_output.put_line ('the given character is a number');
else
dbms_output.put_line ('the given character is a special character’);
end if;
end;
/
Output
DECLARE
area NUMBER(6, 2) ;
perimeter NUMBER(6, 2) ;
radius NUMBER(1) := 3;
BEGIN
END;
Area = 28.26
Perimeter = 18.84
// PL/SQL Block
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
/
Output:
Addition is: 33
************************************************************************************
END