Documente Academic
Documente Profesional
Documente Cultură
Lab Exercise 1
Exercise:
Answer the following questions:
Need OF A DBMS:
Reasons are as under:
Program-data independence
Planned data redundancy
Improved data consistency
Improved data sharing
Increased application development productivity
Enforcement of standards
Improved data quality
Improved data accessibility and responsiveness
Page 1
DATA BASE MANAGEMENT SYSTEM
Reduced program maintenance
Improved decision support
Advantages:
The file-oriented system is supported by a conventional operating system.
Permanent records are stored in various files and a number of different
application programs are written to extract records from and add records to
the appropriate files.
Disadvantages:
Data redundancy and Inconsistency
Difficulty in accessing data
Data isolation
Concurrent access anomalies
Security problems
Integrity problems
SQL is the standard language for Relation Database System. All relational
database management systems like MySQL, MS Access, Oracle, Sybase,
Informix, postgres and SQL Server use SQL as standard database language.
Page 2
DATA BASE MANAGEMENT SYSTEM
Q7. TYPES OF SQL COMMANDS
Types of SQL commands are as under
DDL - Data Definition Language:
Command Description
CREATE Creates a new table, a view of a table, or other object in database
ALTER Modifies an existing database object, such as a table.
DROP Deletes an entire table, a view of a table or other object in the database.
Command Description
INSERT Creates a record
Command Description
Command Description
SELECT Retrieves certain records from one or more tables
Page 3
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 2
Exercise:
Answer the following questions:
A table is the most common and simplest form of data storage in a relational
database.
Page 4
DATA BASE MANAGEMENT SYSTEM
Page 5
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 3
Exercise:
1. Create the following tables:
Query
create table client_master
(client_no varchar2(6),name varchar2(20),address1 varchar2(30),
address2 varchar2(30),city varchar2(15),state varchar2(15),
pincode number(6),bal_due number(10,2));
Page 6
DATA BASE MANAGEMENT SYSTEM
Query
create table product_master
(product_no varchar2(10),Description varchar2(30),
Profit_percent number(10),Unit_measure varchar2(10),
Qty_on_hand number(10),Reoder_lvl number(10),Sell_price number(15),
Cost_price number(15));
Query:
insert all
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0001,'Ivan','Bombay',400054,'Maharashtra',15000)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0002,'Vandana','Madras',780001,'Tamilnadu',0)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0003,'Pramada','Bombay',400057,'Maharashtra',5000)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0004,'Basu','Bombay',400056,'Maharashtra',0)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(00005,'Ravi','Delhi',100001,null,2000)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0006,'Rukmini','Bombay',400050,'Maharashtra',0)
select * from dual;
Page 7
DATA BASE MANAGEMENT SYSTEM
b) Data for Product Master Table:
Query
insert all
INTO product_master VALUES('P00001','1.44floppies',5,'piece',100,20,525,500)
INTO product_master VALUES('P03453','Monitors',6,'piece',10,3,12000,11200)
INTO product_master VALUES('P06743','Mouse',5,'piece',20,5,1050,500)
INTO product_master VALUES('P07865','1.22floppies',5,'piece',100,20,525,500)
INTO product_master VALUES('P07868','Keyboards',2,'piece',10,3,3150,3050)
INTO product_master VALUES('P07885','CDDrive',2.5,'piece',10,3,5250,5100)
INTO product_master VALUES('P07965','540HDD',4,'piece',10,3,8400,8000)
INTO product_master VALUES('P07975','1.44Drive',5,'piece',10,3,1050,1000)
INTO product_master VALUES('P08865','1.22Drive',5,'piece',2,3,1050,1000)
select * from dual;
Query:
select name from client_master;
ii. Retrieve the list names and citites of all the clients.
Query:
select name,city from client_master;
iii. List the various products available from the product_master table.
Page 8
DATA BASE MANAGEMENT SYSTEM
Query:
select Description from product_master;
vi. Find the products with the description as 1.44 drive and 1.22 Drive.
Query:
select product_no,description from product_master
where description in ('1.44Drive','1.22Drive');
vii. Find all the products whose sell price is greater than 5000.
Query:
select sell_price from product_master
where sell_price>5000;
viii. Find the list of all clients who stay in city Bombay or city Delhi or
Madras.
Query:
select name from client_master
where city='Bombay' OR city='Delhi' OR city='Madras';
ix. Find the product whose selling price is greater than 2000 and less
than or equal to 5000.
Query:
select product_no,description from product_master
where sell_price>2000 and sell_price<=5000;
x. List the name,city and state of clients no in the state of
Maharashtra.
Query:
select name,city,client_no from client_master
where state='Maharashtra';
Page 9
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 4
Q 1. Using the table client master and product master answer the following
questionnaires.
ii. Delete the record with client 0001 from the client Master table.
Query:
delete from client_master
where client_no=0001;
v. Find the Products whose selling price more than 1500 and also find
the new selling price as original selling price * 15.
Query:
select sell_price,sell_price*15 from product_master
where sell_price>1500;
vi. Find the clients who stay in a city whose second letter is a.
Query:
select name,city from client_master
where city like '_a%'
Page
10
DATA BASE MANAGEMENT SYSTEM
vii. Find out the name of all clients have a as the second letter in their in
names.
Query:
select name from client_master
where name like '_a%';
Query:
select description from product_master order by description;
xii. Determine the maximum and minimum prices. Rename the tittle as
max_price and min_price respectively.
Query:
select max(SELL_PRICE) "max_price",min(SELL_PRICE) "min_price" from
product_master;
xiii. Count the number of products having price greater than or equal to
1500.
Query:
select count(sell_price) from product_master
where sell_price>1500 OR sell_price=1500;
Page
11
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 5
Exercise:
i. Create the following table.
Query:
CREATE TABLE challan_header
(challan_no varchar2(6) not null, s_order_no VARCHAR2(6),
challan_date date not null,billed_yn char(1) DEFAULT 'n',
PRIMARY KEY (challan_no),
FOREIGN KEY (s_order_no) REFERENCES sales_order(s_order_no));
Query:
CREATE TABLE challan_details
(challan_no varchar2(6) not null,Qty_disp number(4,2) not null,
PRIMARY KEY (challan_no),
FOREIGN KEY (challan_no)
REFERENCES product_master(challan_no));
Page
12
DATA BASE MANAGEMENT SYSTEM
Query:
insert all
into
challan_header(challan_no,s_order_no,challan_date,billed_yn)
values('CH9001','19001','12-Dec-95','Y')
into
challan_header(challan_no,s_order_no,challan_date,billed_yn)
values('CH865','46865','12-Nov-96','Y')
into
challan_header(challan_no,s_order_no,challan_date,billed_yn)
values('CH3965','10008','12-Oct-95','Y')
select * from dual
Query:
insert all
into challan_details values('CH9001','P00001',4)
into challan_details values('CH9001','P07965',1)
into challan_details values('CH9001','P07885',1)
into challan_details values('CH6865','P07868',3)
into challan_details values('CH6865','P03453',4)
into challan_details values('CH6865','P00001',10)
Page
13
DATA BASE MANAGEMENT SYSTEM
into challan_details values('CH3965','P00001',5)
into challan_details values('CH3965','P07975',2)
select * from dual;
Page
14
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 6
Objective: The purpose of this exercise is to create Entity Relationship
Diagrams.
Exercise
a) A company has several departments. Each department has a supervisor and
at least one employee. Employees must be assigned to at least one, but
possibly more departments. At least one employee is assigned to a project,
but an employee may be on vacation and not assigned to any projects. The
important data fields are the names of the departments, projects, supervisors
and employees, as well as the supervisor and employee number and a unique
project number.
Is Assigned
works
Employee-Project Project
Page
15
DATA BASE MANAGEMENT SYSTEM
b) A University contains many Faculties. The Faculties in turn are divided into
several Schools. Each School offers numerous programs and each program
contains many courses. Lecturers can teach many different courses and even
the same course numerous times. Courses can also be taught by many
lecturers. A student is enrolled in only one program but a program can
contain many students. Students can been rolled in many courses at the
same time and the courses have many students enrolled
Faculty University
divided
enrolled
hire enrolled
course std-course
Lecturer
Course name Course name
Lecturer enrolled
Course ID Course ID
ID Student Name
Student ID
Lect-Course
teaches
teaches Lecturer ID
Course name,id
Page
16
DATA BASE MANAGEMENT SYSTEM
c) A doctor can be scheduled for many appointments, but may not have any scheduled at
all. Each appointment is scheduled with exactly 1 doctor. A patient can schedule 1 or more
appointments. One appointment is scheduled with exactly 1 patient. An appointment must
generate exactly 1 bill, a bill is generated by only 1 appointment. One payment is applied to
exactly 1 bill, and 1 bill can be paid off over time by several payments. A bill can be
outstanding, having nothing yet paid on it at all. One patient can make many payments, but
a single payment is made by only 1 patient. Some patients are insured by an insurance
company. If they are insured, they can only carry insurance with one company. An insurance
compnay can have many patients carry their policies. For patients that carry insurance, the
insurance company will make payments, each single payment is made by exactly 1 insurance
company
Scheduled Applied
Patitent Payment
Insured Insured
Insurance Company
Company name
Company Address
Page
17
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 7
Objective: The purpose of this exercise is to learn Normalization and
Normal Forms.
Exercise 1:
Normalize the following two problems up to 3Nnf
0NF
project (project_code, project_title, project_manager, project_budget
Employee_no, Employee_name, Department_no, Department_name,
Hourly_rate, totall staff, Average hourly rate)
1NF
project (project_code, project_title, project_manager, project_budget)
employee(Project_code, Employee_no, Employee_name,
Department_no, Department_name)
Page
18
DATA BASE MANAGEMENT SYSTEM
2NF
project (project_code, project_title, project_manager, project_budget)
project employee(project_code, employee_no)
employee(Employee_no, Employee_name, Department_no,
Department_name)
3NF
project (project_code, project_title, project_manager, project_budget)
project employee(project_code, employee_no)
employee(Employee_no, Employee_name, Department_No)
Department(Department_no, Department_name)
***********************************************************
Page
19
DATA BASE MANAGEMENT SYSTEM
0NF
CustomerNo, CustomerName, CustomerAddress, Sales order number, Sales
order date, ClerkNo, ClerkName, ItemNo, Description, Quantity, Unit Price,
total, Order Total
1NF
CustomerNo, CustomerName, CustomerAddress, Sales order number, Sales
order date, ClerkNo, ClerkName, ItemNo, Description, Quantity, Unit Price
2NF
ItemNo, Description
SalesOrderNo, ItemNo, Quantity, Unit Price
SalesOrderNo, Date, CustomerNo, CustomerName, CustomerAddress, ClerkNo,
ClerkName
3NF
CustomerNo, CustomerName, CustomerAddress
ClerkNo, ClerkName
SalesOrderNo, Date, CustomerNo, ClerkNo
ItemNo, Description
SalesOrderNo, ItemNo, Quantity, Unit Price
Page
20
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 8
Exercise I:
Problem Statement:
Create location, department, job_grade, and employee tables with the
following columns.
Location: (location_id: number, city: string)
Solution:
1. Understand create table syntax.
Page
21
DATA BASE MANAGEMENT SYSTEM
Table name: Job_grade
Page
22
DATA BASE MANAGEMENT SYSTEM
manager_id number
salary Number
Query:
Query:
Page
23
DATA BASE MANAGEMENT SYSTEM
Exercise 2:
Problem Statement:
Alter employee table to add job_grade column which is of string
data type.
Query:
ALTER TABLE employee
ADD job_grade varchar2(2);
Exercise 3:
Problem Statement:
Alter employee table to make job_grade a foreign key to job_grade
table, manager_id a foreign key to employee table, department_id a
foreign key to department table.
Page
24
DATA BASE MANAGEMENT SYSTEM
Exercise 4:
Problem Statement:
Create a dummy table called my_employee with the same definition as
employee table and then drop the table.
Page
25
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 9
Exercise 1:
Problem Statement:
Insert data into location, department, job_grade & employee tables.
Q8. Add to employee one row at a time using the insert into
syntax.
Query:
Insert into employee
(Employee no,employee name, salary)
Values(111,Mike,5000,);
Page
26
DATA BASE MANAGEMENT SYSTEM
Exercise 2:
Problem Statement:
Give a list of all employees (names as first_name, last_name) who
belong to one department_id.
Query:
Select first_name,last_name from emp
Where deptno=10;
Exercise 3:
Problem Statement:
Select employee last_names from employee table who belong to a
certain department_id and have a salary greater than 5000.
1. Use SELECT FROM WHERE syntax.
2. Use AND operator for 2 conditions in WHERE.
Query:
Select last_name from emp
Where department=10 and sal>5000;
Exercise 4:
Problem Statement:
Select employee last_name with first letter in capital, all smalls and all
capitals from employee table for all employees.
Solution:
1. Use select from where clause.
2. Use initcap, lower, and upper functions on last_name in select clause
to get the result.
Query:
Select initcap(last_name),
lower(last_name),
upper(last_name) from emp;
Page
27
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 10
Exercise 1:
Problem Statement:
Select the average salary of all employees in department with
department_id = 2.
1. Use the AVG aggregate function in select clause.
2. Use the department_id = 2 condition in where clause.
Query:
Select avg(sal) from emp
Where deptno=2;
Exercise 2:
Problem Statement:
Select AVG salary of each department which has employees in employee
table.
1. Use the AVG aggregate function in select clause.
2. Since we want average per department, have department_id in select
clause.
3. Include a grouping clause by department_id to indicate you want to
aggregate data by department_id
Query:
Select avg(sal) from emp
Where GROUP BY department_id;
Exercise 3:
Problem Statement:
Select minimum salary of all departments where the minimum salary is less
than 1000.
1. Use the MIN aggregate function in select clause.
2. Since we want average per department, have department_id in select
clause.
3. Include a grouping clause by department_id to indicate you want to
aggregate data by department_id.
4. Exclude groupings which have a minimum salary greater than 1000
using having clause.
Page
28
DATA BASE MANAGEMENT SYSTEM
Query:
select min(salary)from employee group by(dept_id) HAVING
min(salary)<1000;
Exercise 4:
Problem Statement:
Give a list of all employees who earn a salary greater than 10000 or work
in job grade MANAGER.
1. Write a select from where which gives all employees with salary
greater than 10000.
2. Write a select from where which gives all employees whose job grade
in manager.
3. Combine them using UNION clause.
Query:
select * from employee where salary>10000
union
select * from employee where job_grade='manager';
Page
29
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 11
Exercise 1:
Problem Statement:
Select the salary and additional HRA (7.5% of the salary) for each
employee in employee table rounded to a whole number.
1. Use select from where clause and arithmetic to compute HRA.
2. Use the round function to round off the computed HRA to the nearest
whole number.
Query:
select salary ,round(salary*.075) "HRA" from employee;
Exercise 2:
Problem Statement:
Select employee last_name of all employees whose salary is greater than
the salary of employee with id = 2.
Exercise 3:
Problem Statement:
Select all employees whose salary is greater than the salaries of both
employees with ids 2 & 3.
3. Use an inner query to first get the salary of employee_id in (2,3).
4. The result from the inner query is than given in the where clause of
the outer query using the all operator that selects all employees with
salary greater than that.
Query:
Select sal from emp
Where sal>(select sal from emp where emp_id in(2,3));
Page
30
DATA BASE MANAGEMENT SYSTEM
Exercise 4:
Problem Statement:
Select employee lastname and the corresponding department_name for
all employees in employees table.
1. Use select from where clause with the from coming from
employee and department tables and a condition joining these
two table using the foreign key department_id which connects
both the tables with an equality condition.
Query:
Select lastname,deptname from emp
join dept
on emp.dept_id=dept.dept_id;
Page
31
DATA BASE MANAGEMENT SYSTEM
Lab Exercise 12
Objective:
The purpose of this exercise is to learn SQL Aggregate function.
Instructions:
SQL aggregate functions return a single value, calculated from values in a
column.
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
Page
32