0% found this document useful (0 votes)
110 views80 pages

DBMS Lab Manual 6116

This document is a lab manual for a Database Management Systems course. It contains instructions for students to complete 12 experiments involving SQL commands like CREATE, ALTER, DROP, TRUNCATE, and DESCRIBE to define and manipulate database tables. The manual provides the syntax and examples for implementing data definition language commands, constraints, data manipulation language, data control language commands, computations on data, nested queries and joins, views, functions and procedures, cursors and triggers. It also covers concepts of database normalization.

Uploaded by

Rutvik Sakhavala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views80 pages

DBMS Lab Manual 6116

This document is a lab manual for a Database Management Systems course. It contains instructions for students to complete 12 experiments involving SQL commands like CREATE, ALTER, DROP, TRUNCATE, and DESCRIBE to define and manipulate database tables. The manual provides the syntax and examples for implementing data definition language commands, constraints, data manipulation language, data control language commands, computations on data, nested queries and joins, views, functions and procedures, cursors and triggers. It also covers concepts of database normalization.

Uploaded by

Rutvik Sakhavala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

Enrolment No : 200160116116 Batch No : B2

Lab Manual
For

Database Management System Lab

3130703

B.E. 3rd Semester

Department of Information Technology

DBMS LAB MANUAL Page 1


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 2


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No : 1

Faculty 1 : Manmitsinh Zala Branch : IT


Faculty 2 : Archana Gondaliya 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, and then the records alone can be deleted.

● DESC - This is used to view the structure of the table

DBMS LAB MANUAL Page 3


Enrolment No : 200160116116 Batch No : B2

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:

alter table table-name add(column-name datatype );

EXAMPLE:

ALTER TABLE Employee ADD (phone_no char (20));

(b)To Add Multiple columns to existing Table

Syntax:

alter table table-name add(column-name1 datatype1, column-name2


datatype2, column-name3 datatype3);

EXAMPLE:

alter table Employee add(salary number(7), age(5));

(c) Dropping a Column from a Table

Syntax:

DBMS LAB MANUAL Page 4


Enrolment No : 200160116116 Batch No : B2

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));


(e) To Rename a column
Using alter command we can rename an existing column

Syntax:

alter table table-name rename old-column-name to column-name;


EXAMPLE:

alter table Employee rename address to Location;

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;

DBMS LAB MANUAL Page 5


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 6


Enrolment No : 200160116116 Batch No : B2

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 Number 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

DBMS LAB MANUAL Page 7


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 8


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 9


Enrolment No : 200160116116 Batch No : B2

Answers :

Exercise 1 :

(a) : Product master

CREATE TABLE PRODUCT_MASTER


(
PRODUCTNO VARCHAR (6),
DESCRIPTION VARCHAR (15),
PROFITPERCENT NUMERIC (4,2),
UNITMEASURE VARCHAR (10),
QTYONHAND NUMERIC (8),
REORDERLVL NUMERIC (8),
SELLPRICE NUMERIC (8,2),
COSTPRICE NUMERIC (8,2)
);

DBMS LAB MANUAL Page 10


Enrolment No : 200160116116 Batch No : B2

(b) Client master

CREATE TABLE CLIENT_MASTER


(
CLIECTNO VARCHAR(6),
NAME VARCHAR(20),
ADDRESS1 VARCHAR(30),
ADDRESS2 VARCHAR(30),
CITY VARCHAR(15),
PINCODE NUMERIC(8),
STATE VARCHAR(15),
BALDUE NUMERIC(10,2)
);

DBMS LAB MANUAL Page 11


Enrolment No : 200160116116 Batch No : B2

(c) Salesman master

CREATE TABLE SALESMAN_MASTER


(
SALESMANNO VARCHAR(6),
SALESMANNAME VARCHAR(20),
ADDRESS1 VARCHAR(30),
ADDRESS2 VARCHAR(30),
CITY VARCHAR(15),
PINCODE NUMERIC(8),
STATE VARCHAR (15)
);

DBMS LAB MANUAL Page 12


Enrolment No : 200160116116 Batch No : B2

(d) Student

CREATE TABLE STUDENT


(
SNO NUMERIC(5),
SNAME VARCHAR(20),
AGE NUMERIC(5),
SDOB DATE,
SMARK1 NUMERIC(4,2),
SMARK2 NUMERIC(4,2),
SMARK3 NUMERIC(4,4)
);

DBMS LAB MANUAL Page 13


Enrolment No : 200160116116 Batch No : B2

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.

Ans.

Query = ALTER TABLE client_master

ADD COLUMN telephone numeric(10);

OUTPUT:

(b)Change the size of Sellprice column in Product_Master to 10,2

Ans.

Query = ALTER TABLE Product_master

MODIFY COLUMN SELLPRICE numeric(10,2);

OUTPUT:

DBMS LAB MANUAL Page 14


Enrolment No : 200160116116 Batch No : B2

3. Exercise on deleting the table structure along with the data

(a)Destroy the table Client_Master along with its data

Ans.

Query = DROP TABLE Client_Master;

After OUTPUT:

4. Exercise on renaming the table

DBMS LAB MANUAL Page 15


Enrolment No : 200160116116 Batch No : B2

(a)Change the name of the Salesman_Master table to sman_mast

Ans.

Query = RENAME TABLE Salesman_Master to sman_mast;

OUTPUT:

DBMS LAB MANUAL Page 16


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No : 2

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya 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,. .... );

DBMS LAB MANUAL Page 17


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 18


Enrolment No : 200160116116 Batch No : B2

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));

DBMS LAB MANUAL Page 19


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 20


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 21


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 22


Enrolment No : 200160116116 Batch No : B2

Answers :
(a)

CREATE TABLE PRODUCT_MASTER


(
PRODUCTNO VARCHAR (6) PRIMARY KEY CHECK (PRODUCTNO LIKE 'P%'),
DESCRIPTION VARCHAR (15) NOT NULL,
PROFITPERCENT NUMERIC (4,2) NOT NULL,
UNITMEASURE VARCHAR (10) NOT NULL,
QTYONHAND NUMERIC (8) NOT NULL,
REORDERLVL NUMERIC (8) NOT NULL,
SELLPRICE NUMERIC (8,2) NOT NULL CHECK (SELLPRICE > 0),
COSTPRICE NUMERIC (8,2) NOT NULL CHECK (COSTPRICE>0)
);

OUTPUT:

DBMS LAB MANUAL Page 23


Enrolment No : 200160116116 Batch No : B2

(b)

CREATE TABLE CLIENT_MASTER


(
CLIECTNO VARCHAR(6) PRIMARY KEY CHECK (CLIECTNO LIKE 'C%'),
NAME VARCHAR(20) NOT NULL,
ADDRESS1 VARCHAR(30),
ADDRESS2 VARCHAR(30),
CITY VARCHAR(15),
PINCODE NUMERIC(8),
STATE VARCHAR(15),
BALDUE NUMERIC(10,2)
);

OUTPUT:

(c)

DBMS LAB MANUAL Page 24


Enrolment No : 200160116116 Batch No : B2

CREATE TABLE SALESMAN_MASTER


(
SALESMANNO VARCHAR(6) PRIMARY KEY CHECK (SALESMANNO LIKE 'S%'),
SALESMANNAME VARCHAR(20) NOT NULL,
ADDRESS1 VARCHAR(30) NOT NULL,
ADDRESS2 VARCHAR(30),
CITY VARCHAR(15),
PINCODE NUMERIC(8),
STATE VARCHAR (15)
);

OUTPUT:

DBMS LAB MANUAL Page 25


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No :3

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya 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 is 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 onlyspecify
column from the table.
3. UPDATE
It is used to alter the column values in a table. A single column may beupdated
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 whereclause.

PROCEDURE

DBMS LAB MANUAL Page 26


Enrolment No : 200160116116 Batch No : B2

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:

DBMS LAB MANUAL Page 27


Enrolment No : 200160116116 Batch No : B2

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 conditionallySyntax:


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:

DBMS LAB MANUAL Page 28


Enrolment No : 200160116116 Batch No : B2

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

1.Data 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

DBMS LAB MANUAL Page 29


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 30


Enrolment No : 200160116116 Batch No : B2

Answers:
1.
INSERT INTO client_master
VALUES ('C00001','Ivan','Mumbai',400054,'Maharashtra',1500),
('C00002','Ashwini','Chennai',780001,'TamilNadu',0),
('C00003','Joshi','Mangalore',560001,'Karnataka',5000),
('C00004','Deepak','Chennai',780001,'TamilNadu',0),
('C00005','Sharma','Mumbai',400054,'Maharashtra',2000);

OUTPUT:

2.

INSERT INTO product_master


VALUES ('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', 'CottonJeans', 3, 'piece', 80, 850, 550);

OUTPUT:

DBMS LAB MANUAL Page 31


Enrolment No : 200160116116 Batch No : B2

3.
INSERT INTO salesman_master
VALUES ('S00001', 'Aman', 'A/4', 'Worli', 'Mumbai', 400002, 'Maharaashtra'),
('S00002', 'Omkar', '65', 'Nariman', 'Mumbai', 400001, 'Maharaashtra'),
('S00003', 'Raj', 'p-7', 'Bandra', 'Mumbai', 400032, 'Maharaashtra'),
('S00004', 'Ashish', 'A/5', 'Juhu', 'Mumbai', 400044, 'Maharaashtra');

OUTPUT:

DBMS LAB MANUAL Page 32


Enrolment No : 200160116116 Batch No : B2

4. Exercise on retrieving records from a table


a. Find out the names of all clients
Ans.

SELECT Name FROM Client_master;

OUTPUT:

b. Retrieve the entire contents of the Client _master table


Ans.

SELECT * FROM Client_master;

OUTPUT:

c. Retrieve the list of names,city and the state of all the clients
Ans.

SELECT Name,City,State FROM Client_master;

DBMS LAB MANUAL Page 33


Enrolment No : 200160116116 Batch No : B2

OUTPUT:

d. List the various products available from the Product _Master table
Ans.

SELECT DESCRIPTION FROM Product_master;

OUTPUT:

e. List all the clients who are located in Mumbai.


Ans.

SELECT * FROM Client_master WHERE city = ‘Mumbai’;

OUTPUT:

DBMS LAB MANUAL Page 34


Enrolment No : 200160116116 Batch No : B2

f. Find the names of salesmen who have a salary equal to Rs.3000


Ans.

OUTPUT:

5. Exercise on updating the records on a table


a. Change the city of ClientNo’C00005’ to ‘Bangaluru’.
Ans.

UPDATE client_master SET CITY = 'Bangaluru' WHERE CLIECTNO = 'C00005';

OUTPUT:

b. Change the BalDue of ClientNo’C00001’ to Rs.1000.

Ans.

UPDATE client_master SET BALDUE = 1000 WHERE CLIECTNO = 'C00001';

OUTPUT:

DBMS LAB MANUAL Page 35


Enrolment No : 200160116116 Batch No : B2

c. Change the costprice of ‘Shirt ‘ to Rs.450.

Ans.

UPDATE client_master SET COSTPRICE = 450 WHERE DESCRIPTION = 'Shirt';

OUTPUT:

d. Change the city of salesman to Pune.


Ans.

UPDATE Product_master SET city = ‘Pune’ WHERE 1;

OUTPUT:

6. Exercise on deleting the records in a table


a. Delete all salesman from the Salesman_master whose salaries areequal to
Rs.3500.

DBMS LAB MANUAL Page 36


Enrolment No : 200160116116 Batch No : B2

b. Delete all sproducts from the Product_master where quantity onhand is


equal to 100

Ans.

DELETE FROM product_master WHERE QTYONHAND = 100;

OUTPUT:

c. Delete from the Client_master where the column state holds thevalue
‘Tamilnadu’.

Ans.

DELETE FROM client_master WHERE State = ‘TamilNadu’;

OUTPUT:

DBMS LAB MANUAL Page 37


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No :4

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya 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 relatedto 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 userwishes
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 objectssuch as
tables, views.
Syntax:
GRANT {object privileges}

DBMS LAB MANUAL Page 38


Enrolment No : 200160116116 Batch No : B2

ON object name
TO username;
Object Privileges:
Each object privilege that is granted authorizes the grantee to perform someoperation
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 ALTERtable
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 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 userpreviously
granted to the Revoke.

DBMS LAB MANUAL Page 39


Enrolment No : 200160116116 Batch No : B2

The REVOKE command cannot be used to revoke the privileges grantedthrough


operating system.
RESULT:
Familiarized DCL statements.

DBMS LAB MANUAL Page 40


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No :5

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya Semester : 3

Title : “Computations on table data with Built in


sFunctions”

AIM
To implement computations done on data of the given table

OBJECTIVES

To understand computations done on data of the given table with built infunctions

THEORY
Group Functions/Aggregate functions
A group function returns a result based on group of rows.
1. avg
Example: select avg (total) from student;
2. max
Example: select max (percentagel) from student;
2.min
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;

DBMS LAB MANUAL Page 41


Enrolment No : 200160116116 Batch No : B2

Special Clauses:

Group by clause
This allows us to use simultaneous column name and group functions.
Example: Select max (percentage), deptname from student group bydeptname;

Having clause
This is used to specify conditions on rows retrieved by using group byclause.
Example: Select max(percentage), deptname from student group bydeptname 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 rangesLike / not like – used to do
the pattern matching
PROCEDURE

OUTPUT
RESULT

PROGRAMS

1. Generate SQL statements to perform the following computations ontable data.

• list the names of all clients having ‘a’ as the second letter in theirnames.

Ans.

SELECT NAME FROM CLIENT_MASTER WHERE Name LIKE '_a%';

DBMS LAB MANUAL Page 42


Enrolment No : 200160116116 Batch No : B2

• listing of clients who stay in a city whose first letter is ‘M’

Ans.

SELECT * FROM client_master WHERE City LIKE 'M%';

• list all clients who stay in ‘Bangaluru’ or ‘Mangalore’

Ans.

SELECT * FROM client_master WHERE city = 'Bangaluru' OR city =


'Mangalore';

• list all clients whose BalDue is greater than 10000

Ans.

SELECT * FROM client_master WHERE baldue>10000;

• display the order information of clientno ‘C00001’ and’C00003’,

Ans.

SELECT * FROM client_master WHERE CLIECTNO = 'C00001' OR

DBMS LAB MANUAL Page 43


Enrolment No : 200160116116 Batch No : B2

CLIECTNO = 'C00003';

• list products whose selling price is greater than 500 and less than orequal to 750

Ans.

SELECT * FROM PRODUCT_MASTER WHERE SELLPRICE > 500


AND SELLPRICE <= 750;

• Listing of names,city and state of clients who are not in the state of
‘maharashtra’.

Ans.

SELECT Name,city,state FROM client_master WHERE state !=


'Maharashtra';

• count the total number of orders

Ans.

SELECT COUNT(*) FROM product_master;

DBMS LAB MANUAL Page 44


Enrolment No : 200160116116 Batch No : B2

• Calculating the average price of all products.

Ans.

SELECT AVG(SELLPRICE) FROM product_master;

• Determining the maximum and minimum price for the productprices.

Ans.

SELECT MAX(SELLPRICE), MIN(SELLPRICE) FROM


PRODUCT_MASTER;

• count the number of products having the price greater than or equalto 500

Ans.

SELECT COUNT(*) FROM product_master WHERE


SELLPRICE>=500;

DBMS LAB MANUAL Page 45


Enrolment No : 200160116116 Batch No : B2

2. SQL statements for using having and group by clauses.

a. printing the description and total quantity sold for each product.
Ans.

SELECT DESCRIPTION, QTYONHAND FROM PRODUCT_MASTER GROUP BY


DESCRIPTION;

b. Finding the value of each product sold


Ans.

SELECT DESCRIPTION,SELLPRICE FROM product_master GROUP BY QTYONHAND;

c. find out the total of all the billed orders for the month of june.
Ans.

SELECT SUM(Sellprice) FROM Product_master WHERE MONTH(ORDERDATE) = 6;

DBMS LAB MANUAL Page 46


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No : 6

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya 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 TN1.attribute=TN2.attribute.

DBMS LAB MANUAL Page 47


Enrolment No : 200160116116 Batch No : B2

Inner join:
It returns the matching rows from the table that are being joined.

Syntax:
>select <attribute> from TN1 innerjoin TN2 on TN1.attribute=TN2.attribute.
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
TN1.attribute=TN2.attribute.
2. select <attribute> from TN where TN1.attribute(+)=TN2.attribute.

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
TN1.attribute=TN2.attribute.
2. select <attribute> from TN where TN1.attribute=(+)TN2.attribute.
Full join:

It is the combination of both left outer and right outer join.


Syntax:
>select <attribute> from TN1 full join TN2 on TN1.attribute=TN2.attribute.

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;

DBMS LAB MANUAL Page 48


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 49


Enrolment No : 200160116116 Batch No : B2

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);

DBMS LAB MANUAL Page 50


Enrolment No : 200160116116 Batch No : B2

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));

DBMS LAB MANUAL Page 51


Enrolment No : 200160116116 Batch No : B2

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 emp.name,salary from emp,emp1 where emp.name=emp1.name


NAME SALARY

asma 1200

LEFT OUTER JOIN

SQL>select emp.name,salary from emp left outer join emp1 on


emp.name=emp1.name
NAME SALARY

asma 1200
asif 2000
arif 1000
niyas 3000
ashu 10000

RIGHT OUTER JOIN

SQL>select emp1.name,empid from emp right outer join emp1 on


emp.name=emp1.name
NAME EMPID

asma 1200
sweety 9
sumi 32
wahab 10
fathi 12
priya 11,

DBMS LAB MANUAL Page 52


Enrolment No : 200160116116 Batch No : B2

6 rows selected.

FULL JOIN
SQL>select emp1.name,emp.name,emp1.empid,salary from emp full join emp1
on
emp.name=emp1.name
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 products.ie products not being sold.


Query:

select DESCRIPTION from product_master WHERE product_master.PRODUCTNO NOT IN ( SELECT


productno from order_master );

b) Find the name and complete address for the customer who has placed order
number ‘o19001’.

Query:

DBMS LAB MANUAL Page 53


Enrolment No : 200160116116 Batch No : B2

SELECT CITY,PINCODE,STATE FROM client_master LEFT OUTER JOIN order_master


on client_master.CLIENTNO = order_master.clientno where
order_master.orderNo='o19001';

c) find the clients who have placed orders before the month of may ‘02
Query:

select NAME from client_master INNER JOIN order_master on


client_master.CLIENTNO=order_master.clientno where month(date_of_order)<= month(20-05-02)

d) find the names of clients who have placed orders worth Rs.10000 or more.

Query:

SELECT NAME FROM client_master NATURAL JOIN order_master WHERE


order_master.total_amount>=10000

DBMS LAB MANUAL Page 54


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No : 7

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya 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

DBMS LAB MANUAL Page 55


Enrolment No : 200160116116 Batch No : B2

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;


NAME BOOK EDITION PRICE ISBN

Bb 23 2001 12 23435
Cc 55 342 76 687478
dd 2 1233 123 53616578
ee 21 1111 111 12435798

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

DBMS LAB MANUAL Page 56


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 57


Enrolment No : 200160116116 Batch No : B2

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.


Query:

create view vw_doctor as SELECT * from hosp_doc WHERE 1

2) create another view that contains doctor codes and doctor names of
‘orthologue’ department.

DBMS LAB MANUAL Page 58


Enrolment No : 200160116116 Batch No : B2

Query:

create view vw2_doctor as select doc_code,doc_name FROM hosp_doc WHERE


department='orth'

3) delete the view vw_doctor.

Query :
drop view vw_doctor;

DBMS LAB MANUAL Page 59


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No : 8

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya 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

DBMS LAB MANUAL Page 60


Enrolment No : 200160116116 Batch No : B2

expression]
[(parameter)]
is/as declaration
begin
pl/sql codes
[exception]
end

PROGRAM

SQL> create or replace function fact(a number) return number asi


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.

Ans :

DECLARE -- Declare variable n, s, r, len


-- and m of datatype number

DBMS LAB MANUAL Page 61


Enrolment No : 200160116116 Batch No : B2

n NUMBER := 1634;
r NUMBER;
BEGIN
-- Calculating modulo
r := MOD(n, 2);
IF r = 0 THEN
dbms_output.Put_line('Even');
ELSE
dbms_output.Put_line('Odd');
END IF;
END;
--End program
2) procedure to display 1-10 using while.

Ans :

DECLARE @Counter INT


SET @Counter=1
WHILE ( @Counter <= 10)
BEGIN
PRINT 'The counter value is = ' + CONVERT(VARCHAR,@Counter)
SET @Counter = @Counter + 1
END

3) Procedure to display some numbers lesser than given number.

DECLARE
DECLARE @counter int,
n int
SET @counter=1
WHILE(@counter<=n)
BEGIN PRINT ‘The values are :’+CONVERT(VARCHAR,@counter)
SET @counter=@counter+1
END

DBMS LAB MANUAL Page 62


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No :9

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya Semester : 3

Title : “CURSORS”

AIM

To retrieve all students who have registered for Diploma and store their detailsinto
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),programmevarchar2(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

DBMS LAB MANUAL Page 63


Enrolment No : 200160116116 Batch No : B2

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 students.id%type;
4 name students.name%type
5 prog students.programme%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 0.05.Whenever such raise is given to the
employees,the employee number ,the date when raise was given and raise
amunt are maintained in the emp_raise table.Write 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.

Solution :

1. Table creation:
create table employee(
emp_name varchar(20),
emp_number int(5),
dep_name varchar(10),
dep_number int(5),
salary decimal(10,2)
);

DBMS LAB MANUAL Page 64


Enrolment No : 200160116116 Batch No : B2

2. Data insertion

3. Cursor creation :

BEGIN
DECLARE dep_number;
declare salary;
declare raise_date;
declare salary_raise
CURSOR FOR SELECT dep_number,salary,raise_date from employee;
OPEN salary_raise;
FETCH FROM salary_raise INTO salary,raise_date;
if dep_number==20 THEN
UPDATE TABLE employee
SET salary=salary+(salary*0.05);
SET raise_date=CURRENT_DATE;
END IF;
CLOSE salary_raise;
END

Output :

DBMS LAB MANUAL Page 65


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No :10

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya 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 tobe
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),totalsalnumber(10));

table created.

sql> create or replace trigger emptrigr after insert on empfor

DBMS LAB MANUAL Page 66


Enrolment No : 200160116116 Batch No : B2

each row
declare
a varchar2(10);
begin
a:=:new.iname;
update sal set
totalsal=totalsal+:new.salary,totalemp=totalemp+1 whereiname=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.

DBMS LAB MANUAL Page 67


Enrolment No : 200160116116 Batch No : B2

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.
1) Creating table
Query:
Create table student(name varchar(20),enroll varchar(20),branch varchar(20),program
varchar(20));

2) Inserted data:

3) Creation of trigger:
Query:
CREATE TRIGGER `stored_program` AFTER UPDATE ON `student`
FOR EACH ROW INSERT into student_stored
values(name,enroll,branch,program)

DBMS LAB MANUAL Page 68


Enrolment No : 200160116116 Batch No : B2

Output of Table :

2. Write an update trigger on CLIENT_MASTER table.The system should keep


track of the records that are being updated.The 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

1) Creation of table
Query:
Create table AUDIT_TRAIL(client_no varchar(6),name varchar(20),baldue
decimal(8,2),Operation varchar(8),userid varchar(20),olddate date);

2) Creation of trigger
Query:
Create trigger `store_data` before update on `client_master`
For each row
Begin
insert into AUDIT_TRAIL
Set action=’update’
Client_no=old.client_no
Name=old.name
Baldue=old.baldue
Olddate=currentdate()
End

DBMS LAB MANUAL Page 69


Enrolment No : 200160116116 Batch No : B2

Output of table after updating table client_master:

AUDIT_TRAIL table:

DBMS LAB MANUAL Page 70


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No :11

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya 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 no of rows and columns.
Analyze the table and determine to which normal form it belongs to according
to the rules and regulations of each normal forms.
Procedure:
Consider a student table as given below
Social Security FirstNa LastNam Major
Number me e

123-45-6789 Jack Jones Library and


Information Science

222-33-4444 Lynn Lee Library and


Information Science

987-65-4321 Mary Ruiz Pre-Medicine

123-54-3210 Lynn Smith Pre-Law

DBMS LAB MANUAL Page 71


Enrolment No : 200160116116 Batch No : B2

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. .

DBMS LAB MANUAL Page 72


Enrolment No : 200160116116 Batch No : B2

Government Engineering College Modasa


LAB MANUAL
Course Name: DBMS(3130703) Practical No :12

Faculty 1 : Prof. Manmitsinh Zala Branch : IT


Faculty 2 : Prof. Archana Gondaliya Semester : 3

Title : “Checking Normalization of a database table(Third normal


form).”

AIM
An excercise 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 no of rows and
columns.Analyze the table and determine to which normal form it beongs to
according to the rules and regulations of each normal forms.

Procedure:
Consider a book database table as given below.

DBMS LAB MANUAL Page 73


Enrolment No : 200160116116 Batch No : B2

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 Information Castañeda
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

DBMS LAB MANUAL Page 74


Enrolment No : 200160116116 Batch No : B2

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

DBMS LAB MANUAL Page 75


Enrolment No : 200160116116 Batch No : B2

Chemistry Library is in Robert A.Welch 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

Table B

Book Title Subject

The Politics of the Prussian History


Nobility

DBMS LAB MANUAL Page 76


Enrolment No : 200160116116 Batch No : B2

Child Abuse and Neglect Legal Procedures

Human Memory and Cognitive


Knowledge Psychology
The Golden Fleece Greek Literature

Charles Ammi Cutter Library Biography

Music Publishing and Music Literature


Collecting
English and Scottish Ballads Folksong

Table C
Subject Collection or Library

History PCL General Stacks

Legal Procedures Law Library

Cognitive PCL General Stacks


Psychology
Greek Literature Classics Library

Library Biography Library and Information Science


Collection
Music Literature Fine Arts Library

Folksong PCL General Stacks

Table D
Collection or Library Building

PCL General Stacks Perry-Castañeda


Library

DBMS LAB MANUAL Page 77


Enrolment No : 200160116116 Batch No : B2

Law Library Townes Hall

Classics Library Waggener Hall

Library and Information Science Perry-Castañeda


Collection Library
Fine Arts Library Fine Arts Building

DBMS LAB MANUAL Page 78


Enrolment No : 200160116116 Batch No : B2

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?

DBMS LAB MANUAL Page 79


Enrolment No : 200160116116 Batch No : B2

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?

DBMS LAB MANUAL Page 80

You might also like