Lab Manual
For
Database Management System Lab
3130703
B.E. 3rd Semester
Department of Computer Engineering
INDEX
EX .NO LIST OF EXPERIMENTS PAGE NO
1 Implement Data Definition Language commands 3
2 Implement Constraints 9
Implement Data Manipulation Language
3 14
(DML)
Implement Data Control Language (DCL)
4 20
commands
5 Computations done on data 23
6 Nested Queries and Joins 25
7 Views 32
8 Functions and Procedures 36
9 cursors 38
10 Triggers 39
11 Concepts of Normalization
12 Conversion in 3 NF
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No : 1
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “Data Definition language(DDL) ”
AIM:
To execute and verify the Data Definition Language commands.
OBJECTIVES
To understand DDL commands.
THEORY
The commands used are:
● CREATE - It is used to create a table.
● ALTER – The structure of a table can be modified by using the ALTER TABLE command.
This command is used to add a new column, modify the existing column definition and to
include or drop integrity constraint.
● DROP - It will delete the table structure provided the table should be empty.
● TRUNCATE - If there is no further use of records stored in a table and the structure has to be
retained, then the records alone can be deleted.
● DESC - This is used to view the structure of the table
PROCEDURE
CREATION OF TABLE :
SYNTAX:
Create table<table name>(column1 datatype,column2 datatype...);
EXAMPLE:
SQL>CREATE TABLE Employee ( EmpNo number(5), EName VarChar(15),
Job Char(10) , DeptNo number(3));
ALTER TABLE
(a) To Add column to existing Table
Syntax:
EXAMPLE:
ALTER TABLE Employee ADD (phone_no char (20));
(b)To Add Multiple columns to existing Table
Syntax:
EXAMPLE:
(c) Dropping a Column from a Table
Syntax:
ALTER TABLE <Table Name>DROP COLUMN <CoumnName>;
EXAMPLE:
ALTER TABLE Employee DROP COLUMN phone_no ;
(d) Modifying Existing Columns
Syntax:
ALTER TABLE <Table Name>MODIFY (<CoumnName><Newdata
type>(<size>));
EXAMPLE:
ALTER TABLE Employee MODIFY (EName VarChar(25));
Using alter command we can rename an existing column
Syntax:
EXAMPLE:
RENAMING TABLES
Syntax:
Rename <oldtable> to <new table>;
EXAMPLE:
Rename Employee to Employee 1;
TRUNCATE TABLE
Syntax:
TRUNCATE TABLE <TABLE NAME>;
Example:
Truncate table Employee;
DESTROYING TABLES
Syntax:
DROP TABLE <TABLE NAME>;
Example:
DROP TABLE Employee;
DESCRIBE TABLES
. Syntax:
DESC <TABLE NAME>;
Example:
desc employee;
RESULT:
The DDL commands have been executed successfully
Exercise:
1. Create the tables described
below Table Name :
PRODUCT_MASTER
Description : used to store product information
Column name Data type size
PRODUCTNO Varchar2 6
DESCRIPTION Varchar2 15
PROFITPERCENT Varchar2 4,2
UNITMEASURE Varchar2 10
QTYONHAND Number 8
REORDERLVL Number 8
SELLPRICE Number 8,2
COSTPRICE Number 8,2
Table Name : CLIENT_MASTER
Description : used to store client information
Column name Data type size
CLIENTNO Varchar2 6
NAME Varchar2 20
ADDRESS1 Varchar2 30
ADDRESS2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
BALDUE Number 10,2
Table Name : SALESMAN_MASTER
Description : used to store salesman information working for the company
Column name Data type size
SALESMANNO Varchar2 6
SALESMANNAME Varchar2 20
ADDRESS1 Varchar2 30
ADDRESS2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
Table Name : STUDENT
Description : used to store student information
Column name Data type size
SNO Number 5
SNAME Varchar2 20
AGE Number 5
SDOB Date
SMARK1 Number 4,2
SMARK2 Number 4,2
SMARK3 Number 4,4
2. Exercise on altering the table structure
(a) Add a column called ‘telephone’ of data type ‘number’ and size =’10’ to
the Client _Master table.
(b)Change the size of Sellprice column in Product_Master to 10,2
3. Exercise on deleting the table structure along with the data
(a)Destroy the table Client_Master along with its data
4. Exercise on renaming the table
(a)Change the name of the Salesman_Master table to sman_mast
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No : 2
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “Constraints ”
AIM:
To implement Data Constraints.
THEORY
Constraints are the business Rules which are enforced on the data being stored
in a table are called Constraints
TYPES OF CONSTRAINTS:
1) Primary key
2) Foreign key/references
3) Check
4) Unique
5) Not null
6) Null
7) Default
PROCEDURE
(a) The PRIMARY KEY
The PRIMARY KEY defined at column level
Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE CONSTRAINT
<constraintname1> PRIMARY KEY, Columnname2 DATATYPE,
columnname3 DATATYPE,. );
EXAMPLE
SQL>create table Employee(empno number(4) primary key,ename
varchar2(10),job varchar2(6),sal number(5),deptno number(7));
The PRIMARY KEY defined at table level
Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE, columnname2
DATATYPE, columnname3 DATATYPE, PRIMARY KEY (columnname1,
columnname2));
EXAMPLE
(b) CHECK CONSTRAINT
The CHECK Constraint defined at column level
Syntax:
CREATE TABLE tablename
(Columnname1 DATATYPE CHECK (logical expression), columnname2
DATATYPE, columnname3 DATATYPE,...);
EXAMPLE
CREATE TABLE Employee(empno number(3),ename varchar2(20),design
varchar2(15),sal number(5) CHECK(sal>500 and sal<10001),deptno
number(2));
The CHECK Constraint defined at table level
Syntax:
CREATE TABLE tablename
(Columnname1 DATATYPE, columnname2 DATATYPE, columnname3
DATATYPE, CHECK (logical expression1), CHECK (logical expression2));
EXAMPLE
CREATE TABLE Employee(empno number(3),ename varchar2(20), design
varchar2(15),sal number(5),deptno number(2), CHECK(sal>500 and
sal<1000));
(c) UNIQUE CONSTRAINT
The UNIQUE Constraint defined at the column level
Syntax
CREATE TABLE tablename (Columnname1 DATATYPE UNIQUE,
columnname2 DATATYPE UNIQUE, columnname3 DATATYPE ...);
EXAMPLE
sql>CREATE TABLE Employee(empno number(3),ename varchar2(20),
design varchar2(15),sal number(5), UNIQUE(design));
The UNIQUE Constraint defined at the the table level
Syntax
CREATE TABLE tablename (Columnname1 DATATYPE, columnname2
DATATYPE, columnname3 DATATYPE, UNIQUE (columnname1));
EXAMPLE
sql>create table Employee(empno number(3),ename varchar2(20),
design varchar2(15),sal number(5), UNIQUE(design));
(d) Not Null
Syntax
CREATE TABLE tablename(Columnname1 DATATYPE NOT NULL,
columnname2 DATATYPE NOT NULL,columnname3 DATATYPE,...);
EXAMPLE
sql>CREATE TABLE Employee(empno number(4),ename varchar2(20) NOT
NULL,design varchar2(20),sal number(3));
Exercise:
1. Create the tables described
below Table Name :
PRODUCT_MASTER
Description : used to store product information
Column name Data type size Attributes
PRODUCTNO Varchar2 6 Primary
key/first
letter
must start
with ‘p’
DESCRIPTION Varchar2 15 Not Null
PROFITPERCENT Varchar2 4,2 Not Null
UNITMEASURE Varchar2 10 Not Null
QTYONHAND Number 8 Not Null
REORDERLVL Number 8 Not Null
SELLPRICE Number 8,2 Not
Null,canno
t be 0
COSTPRICE Number 8,2 Not
Null,canno
t be 0
Table Name: CLIENT_MASTER
Description : used to store client information
Column name Data type size Attributes
CLIENTNO Varchar2 6 Primary
key/first
letter
must start
with ‘C’
NAME Varchar2 20 Not Null
ADDRESS1 Varchar2 30
ADDRESS2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
BALDUE Number 10,2
Table Name: SALESMAN_MASTER
Description : used to store salesman information working for the company
Column name Data type size Attributes
SALESMANNO Varchar2 6 Primary
key/first
letter must
start with
‘S’
SALESMANNAME Varchar2 20 Not Null
ADDRESS1 Varchar2 30 Not Null
ADDRESS2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :3
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “Data Manipulation Language ”
AIM:
To execute the Data Manipulation Language (DML) commands in RDBMS.
OBJECTIVES
To understand Data Manipulation Language (DML) commands
THEORY
DML commands are the most frequently used SQL commands and are used to query
and manipulate the existing database objects. Some of the commands are
1. INSERT
This is used to add one or more rows to a table. The values are separated by
commas and the data types char and date are enclosed in apostrophes. The
values must be entered in the same order as they are defined.
2. SELECT
It is used to retrieve information from the table. It is generally referred to as
querying the table. We can either display all columns in a table or only specify
columns from the table.
3. UPDATE
It is used to alter the column values in a table. A single column may be updated
or more than one column could be updated.
4. DELETE
After inserting row in a table we can also delete them if required. The delete
command consists of a from clause followed by an optional where clause.
PROCEDURE
INSERT COMMAND
(a) Inserting a single row into a table:
Syntax:
insert into <table name> values (<expression1>,<expression2>)
Example:
SQL>INSERT INTO EMPLOYEE VALUES(101,'MANU','LECTURER',15000);
(b) Inserting more than one record using a single insert commands:
Syntax:
insert into <table name> values (&col1, &col2, ….)
Example:
SQL> INSERT INTO EMPLOYEE
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY');
( c) Skipping the fields while inserting:
Insert into <tablename>(<column name1>,<column name3>)>values
(<expression1>,<expression3>);
Other way is to give null while passing the values.
SELECT COMMAND
(a) view all rows and all columns
Syntax:
Select * from
tablename;
Example:
Select * from Employee;
(b) Selected Columns And All Rows
Syntax:
Select <column1>,<column2> from tablename;
Example:
Select empno, empname from Employee;
(c)Selected Columns And selected Rows
Syntax:
SELECt <column1>, <column2> FROM <tablename> WHERE <condition> ;
Example:
Select empno, empname from Employee where
designation=’lecturer’; (c) Eliminating duplicate rows
Syntax:
SELECT DISTINCT <column1>, <column2> FROM
<tablename> Example:
Select distinct empname from Employee;
UPDATE COMMAND
(b)updating all rows
Syntax:
update tablename set columnname1>=<exprssion1>,<columnname2>=<exprssion2>;
Example:
Update Employee set Designation = ‘lecturer’;
(b)updating records conditionally Syntax:
update tablename set field=values where condition; Example:
Update Employeeemp set sal = 10000 where empno=135;
DELETE COMMAND
(b)Removal of all rows
Syntax:
Delete from <table name> ;
Example:
Delete from emp;
(b)removal of specific rows
Syntax:
Delete from <table name> where <condition>; Example:
delete from emp where empno=135;
RESULT
The DML commands are executed successfully.
Problems
1. Insert the following data into their respective tables. Data for
CLIENT_MASTER table
ClientNo Name City Pincode State BalDue
C00001 Ivan Mumbai 400054 Maharashtra 15000
C00002 Ashwini Chennai 780001 TamilNadu 0
C00003 Joshi Mangalore 560001 Karnataka 5000
C00004 Deepak Chennai 780001 TamilNadu 0
C00005 Sharma Mumbai 400054 Maharashtra 2000
[Link] For Client_master Table
Produc Descrip Profitp unitme qtyonh sellp Cos
t No ti on e rcent as ure a nd ri ce t
pric
e
P00001 Tshirt 5 piece 200 350 250
P00065 Shirt 6 piece 150 500 350
P00032 Jeans 5 piece 100 600 450
P00324 Skirts 4 piece 120 750 500
P02345 CottonJ 3 piece 80 850 550
e ans
2. Data for PRODUCT_MASTER table
SalesmanNo Name Addre Address2 city Pincode State
ss1
S0000 1 Aman A/4 Worli Mumbai 400002 Maharashtra
S0000 2 Omkar 65 Nariman Mumbai 400001 Maharashtra
S0000 3 Raj P-7 Bandra Mumbai 400032 Maharashtra
S0000 4 Ashish A/5 Juhu Mumbai 400044 Maharashtra
3. Data for SALESMAN_MASTER table
4. Exercise on retrieving records from a table
a. Find out the names of all clients
b. Retrieve the entire contents of the Client _master table
c. Retrieve the list of names,city and the state of all the clients
d. List the various products available from the Product _Master table
e. List all the clients who are located in Mumbai
f. Find the names of salesmen who have a salary equal to Rs.3000
5. Exercise on updating the records on a table
a. Change the city of ClientNo’C00005’ to ‘Bangaluru’.
b. Change the cBalDue of ClientNo’C00001’ to Rs.1000.
c. Change the costprice of ‘Shirt ‘ to Rs.450.
d. Change the city of salesman to Pune.
6. Exercise on deleting the records in a table
a. Delete all salesman from the Salesman_master whose salaries are equal to
Rs.3500.
b. Delete all sproducts from the Product_master where quantity on hand is
equal to 100
c. Delete from the Client_master where the column state holds the value
‘Tamilnadu’.
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :4
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “Data Control language ”
AIM:
To implement DCL statements.
OBJECTIVES
To understand DCL commands
THEORY:
Data Control Language (DCL) consists of various commands which are related to data
sharing and security of data in database.
They are
GRANT
REVOKE
Granting Privileges:
Objects that are created by a user are owned and controlled by that user. If a user wishes
to access any of the objects belonging to another user, the owner of the object will have
to give permissions for such access. This is called Granting of Privileges.
Granting privileges using the GRANT statements:
The GRANT statements provide various types of access to database objects such as
tables, views.
Syntax:
GRANT {object privileges}
ON object name
TO username;
Object Privileges:
Each object privilege that is granted authorizes the grantee to perform some operation
on the object. The user can grant all the privileges or grant only specific object
privileges.
The list of object privileges is as follows:
• ALTER: allows the grantee to change the table definitions with the ALTER
table command.
• DELETE: allows the grantee to remove the records from the table with the
DELETE command.
• INDEX: allows the grantee to create an index on the table with the CREATE
INDEX command.
• INSERT: allows the grantee to add records to the table with the INSERT
command.
• SELECT: allows the grantee to query the table with SELECT command.
• UPDATE: allows the grantee to modify the records in the table with the
UPDATE command.
Revoking privileges given:
Privileges once given can be denied to a user using the REVOKE command. The
object owner can revoke privileges granted to another user. A user of an object who
is not the owner, but has been granted the GRANT privilege, has the power to
REVOKE the privileges from the grantee.
Revoking permission using the REVOKE statement:
The REVOKE statement is used to deny the grant given on an object.
Syntax:
REVOKE {object privileges} ON
object name
FROM username;
The REVOKE command is used to revoke object privileges that the user previously
granted to the Revoke
The REVOKE command cannot be used to revoke the privileges granted through
operating system.
RESULT:
Familiarized DCL statements.
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :5
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “Computations on table data with Built in
Functions”
AIM
To implement computations done on data of the given table
OBJECTIVES
To understand computations done on data of the given table with built in functions
THEORY
Group Functions/Aggregate functions
A group function returns a result based on group of rows.
1. avg
Example: select avg (total) from student;
[Link]
Example: select max (percentagel) from student;
[Link]
Example: select min (marksl) from student;
4. sum
Example: select sum(price) from product
Count Function
In order to count the number of rows, count function is used.
1. count(*) – It counts all, inclusive of duplicates and nulls.
Example: select count(*) from student;
2. count(col_name)– It avoids null value.
Example: select count(total) from order;
2. count(distinct col_name) – It avoids the repeated and null values.
Example: select count(distinct ordid) from order;
Special Clauses:
Group by clause
This allows us to use simultaneous column name and group functions.
Example: Select max (percentage), deptname from student group by deptname;
Having clause
This is used to specify conditions on rows retrieved by using group by clause.
Example: Select max(percentage), deptname from student group by deptname having
count(*)>=50;
In / not in – used to select a equi from a specific set of values
Any - used to compare with a specific set of values Between / not
between – used to find between the ranges Like / not like – used to do
the pattern matching
PROCEDURE
OUTPUT
RESULT
PROGRAMS
1. Generate SQL statements to perform the following computations on table data.
● list the names of all clients having ‘a’ as the second letter in their names.
● listing of clients who stay in a city whose first letter is ‘M’
● list all clients who stay in ‘Bangaluru’ or ‘Mangalore’
● list all clients [Link] BalDue is greater than 10000
● display the order information of clientno ‘C00001’ and’C00002’,list products
whose selling price is greater than 500 and less than or equal to 750
● Listing of names,city and state of clients who are not in the state of
‘maharashtra’.
● count the total number of orders
● Calculating the average price of all products.
● Determining the maximum and minimum price for the product prices.
● count the number of products having the price greater than or equal to 500
2. SQL statements for using having and group by clauses.
a. printing the description and total quantity sold for each product.
b. Finding the value of each product sold
c. find out the total of all the billed orders for the month of june.
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No : 6
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “NESTED QUERIES/SUB QUERIES AND
JOINS”
AIM:
To implement e nested queries and joins on the given table
OBJECTIVES
To understand nested queries and joins.
THEORY
a)NESTED QUERIES:
A sub query is a query within a query. In Oracle, we can create sub queries
within your SQL statements. These sub queries can reside in the WHERE clause,
the FROM clause, or the SELECT clause.
b) JOINS:
Join is a query in which data is returned from two or more tables.
Natural join:
It returns the matching rows from the table that are being joined
.
Syntax:
>select <attribute> from TN where [Link]=[Link].
Inner join:
It returns the matching rows from the table that are being joined.
Syntax:
>select <attribute> from TN1 innerjoin TN2 on [Link]=[Link].
Left outer join:
It returns all the rows from the table1 even when they are unmatched.
Syntax:
5. select <attribute> from TN1 left outer join TN2 on
[Link]=[Link].
2. select <attribute> from TN where [Link](+)=[Link].
Right outer join:
It returns all the rows from the table2 even when they are unmatched.
Syntax:
4. select <attribute> from TN1 right outer join TN2 on
[Link]=[Link].
2. select <attribute> from TN where [Link]=(+)[Link].
Full join:
It is the combination of both left outer and right outer join.
Syntax:
>select <attribute> from TN1 full join TN2 on [Link]=[Link].
PROCEDURE
NESTED QUERIES -
SQL> desc emp_det;
Name Null? Type
ENO NOT NULL NUMBER(3)
ENAME VARCHAR2(25)
ADDRESS VARCHAR2(30)
BASIC_SAL NUMBER(12,2)
JOB_STATUS VARCHAR2(15)
DNO NUMBER(3)
SQL> desc pro_det;
Name Null? Type
PNO sNOT NULL NUMBER(3)
PNAME VARCHAR2(30)
NO_OF_STAFF NUMBER(3)
SQL> desc work_in;
Name Null? Type
PNO NUMBER(3)
ENO NUMBER(3)
PJOB CHAR (12)
SQL> select * from emp_det;
EN ENAME ADDRESS BASIC_SA JOB_STATU DNO
O L S
1 SaravanaKuma GandhiNagar 8000 Manager 10
r
2 Mahendran RainbowColon 5000 Supervisor 10
y
3 RajKumar EastCoastRoad 10000 Professor 2
4 Shirley KKnagar 8000 AsstManager 3
SQL> select * from Pro_det;
PNO PNAME NO_OF_STAFF
1 DBMS 2
2 COMPILER 3
3 C1 1
SQL> select * from work_in;
PNO ENO PJOB
1 1 Programmer
2 1 Analyst
1 2 Analyst
2 2 Programmer
NESTED QUERIES
(i) SQL> select ename from emp_det where dno not in(select dno
from emp_det where ename ='SaravanaKumar');
ENAME
RajKumar
Shirley
(ii) SQL> select ename, dno from emp_det where dno = (select dno
from emp_det where ename ='RajKumar');
ENAME DNO
RajKumar 2
(iii) SQL> select ename from emp_det where eno in(select eno from
work_in where pno = (select pno from pro_det where pname = 'DBMS'))
order by ename;
ENAME
Mahendran
SaravanaKumar
(iv) SQL> select ename, basic_sal from emp_det where dno = 2 and
basic_sal>(select max(basic_sal) from emp_det where dno = 10) order by
ename;
ENAME BASIC_SAL
RajKumar 10000
(v) SQL> select pno,pname from pro_det where exists(select pno from
work_in where work_in.pno =pro_det.pno);
PNO PNAME
1 DBMS
2 COMPILER
(vi) SQL>select ename, job_status,basic_sal from emp_det where
(dno,basic_sal) in (select dno,basic_sal from emp_det where ename
='RajKumar');
ENAME JOB_STATUS BASIC_SAL
RajKumar Professor 10000
(vii) SQL>select * from emp_det where basic_sal=(select max(basic_sal)
from emp_det);
ENO ENAME ADDRESS BASIC_SAL JOB_STATUS DNO
3 RajKumar EastCoastRoad 10000 Professor 2
(viii) SQL>select max(basic_sal) from emp_det where basic_sal<
(select max(basic_sal) from emp_det);
MAX(BASIC_SAL)
8000
(ix) SQL> select * from emp_det where basic_sal < (select avg(basic_sal)
from emp_det);
ENO ENAME ADDRESS BASIC_SAL JOB_STATUS DNO
2 Mahendran RainbowColony 5000 Supervisor 10
JOINS
SQL> create table emp(name varchar2(20),salary number(10));
Table created.
SQL> select * from emp;
NAME SALARY
ashu 10000
asma 1200
asif 2000
arif 1000
niyas 3000
SQL> create table emp1(name varchar2(20),empid number(10));
Table created.
.
SQL> select * from emp1;
NAME EMPID
fathi 12
sumi 32
priya 11
wahab 10
sweety 9
asma 1200
6 rows selected.
NATURAL JOIN
SQL>select [Link],salary from emp,emp1 where [Link]=[Link]
NAME SALARY
asma 1200
LEFT OUTER JOIN
SQL>select [Link],salary from emp left outer join emp1 on
[Link]=[Link]
NAME SALARY
asma 1200
asif 2000
arif 1000
niyas 3000
ashu 10000
RIGHT OUTER JOIN
SQL>select [Link],empid from emp right outer join emp1 on
[Link]=[Link]
NAME EMPID
asma 1200
sweety 9
sumi 32
wahab 10
fathi 12
priya 11,
6 rows selected.
FULL JOIN
SQL>select [Link],[Link],[Link],salary from emp full join emp1
on
[Link]=[Link]
NAME NAME EMPID SALARY
asma asma 1200 1200
asif 2000
arif 1000
niyas 3000
ashu 10000
sweety 9
sumi 32
wahab 10
fathi 12
priya 11
10 rows selected.
RESULT:
Thus the nested queries and join operations are executed and verifiedin DBMS.
Exercise :
1. Exercises on sub-queries
a) find the non moving [Link] products not being sold.
b) Find the name and complete address for the customer who has placed
order number ‘o19001’
c) find the clients who have placed orders before the month of may ‘02
d) find the names of clients who have placed orders worth Rs.10000 or more.
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No : 7
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “VIEWS”
AIM:
To create and drop View on the given table.
OBJECTIVES
To implement views
THEORY
A view is the tailored presentation of data contained in one or more table and can
also be said as restricted view to the data‟s in the tables. A view is a “virtual
table” or a “stored query” which takes the output of a query and treats it as a
table. The table upon which a view is created is called as base table . A view is a
logical table based on a table or another view. A view contains no data of its own
but is like a window through which data from tables can be viewed or changed.
The tables on which a view is based are called base tables. The view is stored as
a SELECT statement in the data dictionary .
Advantages of a view:
a. Additional level of table security.
b. Hides data complexity.
c. Simplifies the usage by combining multiple tables into a single table
Creating and dropping view:
Syntax:
Create or replace view view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Drop view <view name>;
Example
Create or replace view empview as select * from emp;
Drop view empview;
PROCEDURE
1)create a table aa
`SQL> create table aa(name varchar2(20),book number(10),edition
number(20),price number(20), ISBN number(20));
2) describe the
table aa SQL> select *
from aa;
3)create table qq
SQL> create table qq(name varchar2(20),book number(10),author
varchar(20),publisher varchar2(20),ISBN number(20));
4) describe table qq
SQL> select * from qq;
NAME BOOK AUTHOR PUBLISHER ISBN
bb 21 23 dfd 573568
cc 43 55 fg 65839
ee 44 21 dfd 1235798
oo 87 34 gfh 6358379
5)create a view on qq
SQL>create view ww as select book,name,publisher from qq where
ISBN=573568
View created.
6)display the view
SQL> select * from ww;
BOOK NAME PUBLISHER
…………………………………………..
21 bb dfd
7) Update View Statement
SQL> update ww set publisher='qwa'where book=21;
1 row updated.
SQL> select * from ww;
BOOK NAME PUBLISHER
21 bb qwa
SQL> create view wq as select name,ISBN,publisher from qq where book>21
View created.
SQL> select * from wq;
NAME ISBN PUBLISHER
cc 65839 fg
ee 1235798 dfd
oo 6358379 gfh
SQL> create view ss as select name,book from aa union select name,book from
qq;
View created.
SQL> select * from ss;
NAME BOOK
bb 21
bb 23
cc 43
cc 55
dd 2
ee 21
ee 44
oo 87
8 rows selected.
Result
Thus the view creation commands are executed successfully.
EXERCISE:
1)Create the following table and insert rows
Table:Hosp_doc
Column name Data type and size
Doc_code Varchar2(4)
Doc_name Varchar2(4)
Specialization Varchar2(4)
Department Varchar2(4)
Date_of_join Date
Exper Number(2)
1)create a view vw_doctor on Hosp_doc table
2) create another view that contains doctor codes and doctor
names of ‘orthologue’ department
3)delete the view vw_doctor
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No : 8
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “Functions and Procedures”
AIM:
To find factorial of a number using function
OBJECTIVES
To write PL/SQL(Functions)and to understand stored procedures in SQL.
THEORY
FUNCTION:
A function is a subprogram that computes a value.
Syntax:
Create or replace function<function_name>[argument]
Return datatype is
(local declaration)
begin
(executable statements)
[Exception]
(exception handlers)
End
PROCEDURE:
create [or replace] procedure procedurename
[parameter[in/out/in/in out] datatype [:=/default
expression]
[(parameter)]
is/as declaration
begin
pl/sql codes
[exception]
end
PROGRAM
SQL> create or replace function fact(a number) return number as i
number;
f number;
begin f:=1;
i:=1;
while (i<=a)
loop
f:=f*i;
i:=i+1;
end loop;
return f;
end fact; /Function created. SQL>begin
2 dbms_output.put_line('the factorial='||fact(&a)); 3*
end;
OUTPUT
SQL> /
Enter value for a: 4
old 2: dbms_output.put_line('the factorial='||fact(&a)) new
2: dbms_output.put_line('the factorial='||fact(4)); the
factorial=24
PL/SQL procedure successfully completed.
RESULT:
Thus the functions and stored procedures are executed in SQL.
Problems;
1) procedure to find whether a given number is odd or even
2) procedure to display 1-10 using while
3) Procedure to display some numbers lesser than given number
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :9
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “CURSORS”
AIM
To retrieve all students who have registered for Diploma and store their details into
another table called diploma (id,name) using cursors.
OBJECTIVES
To implement cursor
PROCEDURE
1) TABLE CREATION
SQL>create table student(id number,name varchar2(25),programme varchar2(25));
SQL>create table diploma(id number,name varchar2(25));
SQL>insert into students values(1,’rohan’,’diploma’); SQL>insert
into students values(2,’anu’,’MA’);
SQL>insert into students values(3,’robert’,’diploma’);
SQL>insert into students values(4,’tom’,’btech’); SQL>insert
into students values(5,’sunny’,’diploma’); SQL>select * from
students;
Id name programme
1 rohan diploma
2 anu MA
3 robert diploma
4 tom btech
5 sunny diploma
SQL>declare
2 cursor stud is select * from students where programme =”diploma’;
3 id [Link]%type;
4 name [Link]%type
5 prog [Link]%type
6 begin
7 open stud;
8 loop
9 fetch stud into id,name,prog;
10 exit when stud%notfound;
11 insert into diploma values(id,name);
12 endloop;
13 end;
14 /
OUTPUT
RESULT
PROGRAMS
1.A HRD manager has decided to raise the salary of all employees in department
number 20 by [Link] such raise is given to the employees,the employee
number ,the date when raise was given and raise amount are maintained in the
emp_raise [Link] a PL/SQL block using cursors to update the salary of each
employee of dept no 20 and insert a record in the emp_raise table as well
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :10
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “TRIGGER”
AIM
Create a Trigger for EMP table it will update another table SALARY while inserting
values
OBJECTIVES
To develop and execute a Trigger for Before and After update/Delete/Insert operations
on a table
THEORY.
PROCEDURE
step 1: start
step 2: initialize the trigger with specific table id.
step 3:specify the operations (update, delete, insert) for which the trigger has to be
executed.
step 4: execute the trigger procedure for both before and after sequences step
5: carryout the operation on the table to check for trigger execution. step 6:
stop
PROGRAM
sql> create table emp(iname varchar2(10),iid number(5),salary number(10)); table
created.
sql> create table sal(iname varchar2(10),totalemp number(5),totalsal number(10));
table created.
sql> create or replace trigger emptrigr after insert on emp for
each row
declare
a varchar2(10);
begin
a:=:[Link];
update sal set
totalsal=totalsal+:[Link],totalemp=totalemp+1 where iname=a;
end;
/
trigger created.
sql> insert into emp values('vec',100,1000);
1 row created.
sql> insert into sal values('vec',0,0);
1 row created.
sql> insert into sal values('srm',0,0);
1 row created.
sql> select * from sal;
iname totalemp totalsal
vec 1 1000
srm 0 0
sql> insert into emp values('srm',200,3000);
1 row created.
sql> select * from sal;
iname totalemp totalsal
Vec 1 1000
srm 1 3000
sql> insert into emp values('vec',100,5000);
1 row created.
sql> select * from sal;
iname totalemp totalsal
vec 2 6000
srm 1 3000
sql> insert into emp values('vec',100,2000);
1 row created.
sql> select * from sal;
iname totalemp totalsal
vec 3 8000
srm 1 3000
sql> insert into emp values('srm',200,8000);
1 row created.
sql> select * from sal;
iname totalemp totalsal
Vec 3 8000
Srm 2 11000
RESULT:
The trigger procedure has been executed successfully for both before and after
sequences.
Problems
1. Write a trigger that stores the details of students changing their program
from CT to CHM.
2. Write an update trigger on CLIENT_MASTER [Link] system should
keep track of the records that are being [Link] old values of the updated
record should be added in the AUDIT_TRAIL table
Column name Data type size
Client_no Varchar2 6
name Varchar2 20
Bal_due Number 10,2
Operation Varchar2 8
userid Varchar2 20
olddate date
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :11
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “Concepts of Normalization”
AIM: Checking Normalization of a database table (First Normal form)
Problem Statement:
An exercise to check whether the given database table is normalized or not. If
yes find out the status of normalization and reasoning.
Objective:
To study the concept of various levels of normalization and understand how to
convert into normalized forms.
Requirements: Mysql database software
Design/Theory
Create a database table in SQL with a few rows and columns.
Analyze the table and determine to which normal form it belongs to according to
the rules and regulations of each normal form.
Procedure:
Consider a student table as given below
Social Security FirstNa LastName Major
Number me
123-45-6789 Jack Jones Library and
Information Science
Library and
222-33-4444 Lynn Lee
Information Science
987-65-4321 Mary Ruiz Pre-Medicine
123-54-3210 Lynn Smith Pre-Law
We can easily verify that this table satisfies the definition of 1NF: viz., it has no
duplicated rows; each cell is single-valued (i.e., there are no repeating groups or
arrays); and all the entries in a given column are of the same kind. In this table
we can see that the key, SSN, functionally determines the other attributes;
i.e.,FirstName, LastName, and Major. .
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :11
Faculty 1 : Avinash Chaudhari Branch : CE
Faculty 2 : Pradeep Gamit Semester : 3
Title : “Checking Normalization of a database table (Third normal
form).”
AIM
An exercise to check whether the given database table is normalized or not. If yes,
find out the status of normalization and reasoning.
Objective:
To study the concept of various levels of normalization and understand how to
convert into normalized forms.
Requirements: Mysql database software
Design/Theory
Create a database table in SQL with a few rows and [Link] the table
and determine to which normal form it belongs to according to the rules and
regulations of each normal form.
Procedure:
Consider a book database table as given below.
Author Author Book Title Subject Collection Building
Last First or Library
Name Name
Berdahl Robert The Politics of History PCL Perry-
the Prussian General Castañe
Nobility Stacks da
Library
Yudof Mark Child Abuse and Legal Law Library Townes Hall
Neglect Procedu
re s
Harmon Glynn Human Memory Cogniti PCL Perry-
and Knowledge ve General Castañe
Psychol Stacks da
og y Library
Graves Robert The Golden Greek Classics Waggener
Fleece Literat Library Hall
ure
Miksa Francis Charles Ammi Library Library and Perry-
Cutter Castañeda
Information
Library
Science
Collection
Hunter David Music Fine Arts
Music Publishing Literat Fine Arts
and Collecting Library Building
ure
Graves Robert English and Folksong Folk Library Perry-
Scottish Ballads Cassata
Ballads Library
By examining the table, we can infer that books dealing with history, cognitive
psychology, and folksong are assigned to the PCL General Stacks collection; that books
dealing with legal procedures are assigned to the Law Library; that books dealing with Greek
literature are assigned to the Classics Library; that books dealing with library biography are
assigned to the Library and Information Science Collection (LISC);and that books dealing with
music literature are assigned to the Fine Arts Library.
Moreover, we can infer that the PCL General Stacks collection and the
LISC are both housed in the Perry-Castañeda Library (PCL) building; that the
Classics Library is housed in Waggener Hall; and that the Law Library and Fine
Arts Library are housed, respectively, in Townes Hall and the Fine Arts Building.
Thus we can see that a transitive dependency exists in the above table :
any book that deals with history, cognitive psychology, or library biography will
be physically housed in the PCL building (unless it is temporarily checked out to
a borrower); any book dealing with legal procedures will be housed in Townes
Hall; and so on. In short, if we know what subject a book deals with, we also
know not only what library or collection it will be assigned to but also what
building it is physically housed in.
A problem with transitive dependency is that, there is duplicated information:
from three different rows we can see that the PCL General Stacks are in the PCL
building. For another thing, we have possible deletion anomalies: if the Yudof
book were lost and its row removed from table, we would lose the information
that books on legal procedures are assigned to the Law Library and also the
information the Law Library is in Townes Hall. As a third problem, we have
possible insertion anomalies: if we wanted to add a chemistry book to the table,
we would find that the above table nowhere contains the fact that the Chemistry
Library is in Robert [Link] Hall. As a fourth problem, we have the chance of
making errors in updating: a careless data-entry clerk might add a book to the
LISC but mistakenly enter Townes Hall in the building column.
To solve this problem decompose the above table into three different tables as
follows
Table A
Author Author Book Title
Last First Name
Name
Berdahl Robert The Politics of the Prussian
Nobility
Yudof Mark Child Abuse and Neglect
Harmon Glynn Human Memory and Knowledge
Graves Robert The Golden Fleece
Miksa Francis Charles Ammi Cutter
Hunter David Music Publishing and Collecting
Graves Robert English and Scottish Ballads
Ballads
Table B
Book Title Subject
The Politics of the Prussian History
Nobility
Child Abuse and Neglect Legal Procedure s
Human Memory and Knowledge Cognitive
Psychology
The Golden Fleece Greek Literature
Charles Ammi Cutter Library
Music Literature
Music Publishing and Collecting
English and Scottish Ballads Ballads Folksong
Table C
Subject Collection or Library
History PCL General Stacks
Legal Procedure s Law Library
Cognitive Psychology PCL General Stacks
Greek Literature Classics Library
Library Library and
Information Science Collection
Music Literature
Fine Arts Library
Folksong Folk Library
Table D
Collection or Library Building
PCL General Stacks Perry- Castañeda
Library
Law Library Townes Hall
PCL General Stacks Perry- Castañeda
Library
Classics Library Waggener Hall
Library and Perry- Castañeda Library
Information Science
Collection
Fine Arts Library Fine Arts Building
Folk Library Perry- Cassata Library
VIVA VOCE QUESTIONS (DBMS)
1. What is a database?
2. What is DBMS?
3. Give an example for an RDBMS.
4. List the benefits of DBMS.
5. Disadvantage in File Processing System
6. What is a key? what are different keys in database?
7. What is a primary key?
8. What is a secondary key?
9. What is a candidate key?
10. What is an alternate key?
11. What is a super key?
12. What is a composite key?
13. What is a relation?
14. What is a table?
15. What is an attribute?
16. What is a domain?
17. What is a tuple?
18. What is a selection?
19. what is a join operation?
20. What are base operations in relational algebra?
21. What are different DBMS facilities? How many types of facilities are provided by a DBMS?
22. What is Data Definition Language?
23. What is Data Dictionary?
24. What is a DML?
25. What is a query?
26. What is a query language?
27. What are the advantages of DBMS?
28. What is a SQL?
29. What are the features of SQL?
30. How SQL organizes the data?
31. What is data definition?
32. What is data retrieval?
33. What is data sharing?
34. What is a view?
35. What is normalization?
36. What is a first normal form?
37. What is a second normal form?
38. What is a third normal form?
39. What is BCNF?
40. What is fifth normal form?
41. What is Functional Dependency?
42. What is Lossless join property?
43. What are the commands to delete, modify and insert a record in the table?
44. What is time stamping?
45. What is data base schema?
46. What is a self join?
47. What are the different aggregate functions in SQL?
48. What is data integrity?
49. What is data independence?
50. What is dead locking?
51. What is decryption?
52. What is a distributed database?
53. What is an entity?
54. What is a conceptual data model?
55. What is two phase locking?
56. What is projection?
57. What are the different phases of transaction?
58. What is Relational Algebra?
59. What is Relational Calculus?