0% found this document useful (0 votes)
67 views60 pages

3139 Database Management System Lab

The document describes experiments involving data definition language (DDL) commands to create, alter, truncate, and drop tables in a database. It provides the syntax and examples for creating tables, adding/dropping/modifying columns, renaming tables and columns, truncating tables, and describing table structures. It also lists problems to create three tables - PRODUCT_MASTER, CLIENT_MASTER, and SALESMAN_MASTER - with specified column names and data types.
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)
67 views60 pages

3139 Database Management System Lab

The document describes experiments involving data definition language (DDL) commands to create, alter, truncate, and drop tables in a database. It provides the syntax and examples for creating tables, adding/dropping/modifying columns, renaming tables and columns, truncating tables, and describing table structures. It also lists problems to create three tables - PRODUCT_MASTER, CLIENT_MASTER, and SALESMAN_MASTER - with specified column names and data types.
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/ 60

REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

E
Lab Manual

EG
for

LL
CO
IC
DATABASE MANAGEMENT
HN
SYSTEM LAB - 3139
EC
LYT
PO

Diploma In Computer Engineering


N

3 rd Semester
DI
A'
M

1 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

INDEX

EX PAGE
LIST OF EXPERIMENTS
.NO NO

1 Implement Data Definition Language commands 3

E
2 Implement Constraints 9

EG
LL
3 Implement Data Manipulation Language (DML) 14

CO
Implement Data Control Language (DCL)
4 commands 20

5 Computations done on data


IC 23
HN
6 Nested Queries and Joins 25
EC

7 Views 32
YT

8 Functions and Procedures 36


L
PO

9 cursors 38
N

10 Triggers 39
DI

11 Database Connectivity 42
A'
M

2 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO: 1

DATA DEFINITION LANGUAGE (DDL) COMMANDS


AIM:

To execute and verify the Data Definition Language commands.

OBJECTIVES

To understand DDL commands.

E
EG
THEORY

The commands used are:

LL
● CREATE - It is used to create a table.

CO
● 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 IC
column definition and to include or drop integrity constraint.
HN
● DROP - It will delete the table structure provided the table should be empty.
EC

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

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


L
PO

PROCEDURE
N

CREATION OF TABLE:
DI

SYNTAX:
A'

create table<table name>(column1 datatype,column2 datatype...);


M

EXAMPLE:

SQL>CREATE TABLE Employee ( EmpNo number(5), EName VarChar(15),


Job Char(10) , DeptNo number(3));

ALTER TABLE

3 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
EG
(b)To Add Multiple columns to existing Table

LL
Syntax:

CO
alter table table-name add(column-name1 datatype1, column-name2
datatype2, column-name3 datatype3);

EXAMPLE: IC
HN
alter table Employee add(salary number(7), age(5));
EC

(c) Dropping a Column from a Table


YT

Syntax:
L

ALTER TABLE <Table Name>DROP COLUMN <CoumnName>;


PO

EXAMPLE:
N

ALTER TABLE Employee DROP COLUMN phone_no ;


DI

(d) Modifying Existing Columns


A'

Syntax:
M

ALTER TABLE <Table Name>MODIFY (<CoumnName><Newdata


type>(<size>));
EXAMPLE:

ALTER TABLE Employee MODIFY (EName VarChar(25));

4 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
alter table Employee rename address to Location;

EG
LL
RENAMING TABLES

CO
Syntax:

Rename <oldtable> to <new table>;

EXAMPLE:
IC
HN
rename Employee to Employee 1;

TRUNCATE TABLE
EC

.
Syntax:
YT

TRUNCATE TABLE <TABLE NAME>;


L
PO

Example:

Truncate table Employee;


N
DI

DESTROYING TABLES
A'

Syntax:
M

DROP TABLE <TABLE NAME>;

Example:

DROP TABLE Employee;

5 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

DESCRIBE TABLES

. Syntax:

DESC <TABLE NAME>;

Example:

desc employee;

E
RESULT:

EG
The DDL commands have been executed successfully

LL
Problems

CO
1. Create the tables described below

Table Name : PRODUCT_MASTER

Description
IC
: used to store product information
HN
Column name Data type size
EC

PRODUCTNO Varchar2 6
YT

DESCRIPTION Varchar2 15

PROFITPERCENT Varchar2 4,2


L
PO

UNITMEASURE Varchar2 10

QTYONHAND Number 8
N
DI

REORDERLVL Number 8

SELLPRICE Number 8,2


A'
M

COSTPRICE Number 8,2

Table Name : CLIENT_MASTER

6 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Description : used to store client information

Column name Data type size

CLIENTNO Varchar2 6

NAME Varchar2 20

ADDRESS1 Varchar2 30

ADDRESS2 Varchar2 30

E
CITY Varchar2 15

EG
PINCODE Number 8

LL
STATE Varchar2 15

CO
BALDUE Number 10,2

Table Name : SALESMAN_MASTER


IC
HN
Description : used to store salesman information working for the company
EC

Column name Data type size


YT

SALESMANNO Varchar2 6

SALESMANNAME Varchar2 20
L
PO

ADDRESS1 Varchar2 30

ADDRESS2 Varchar2 30
N
DI

CITY Varchar2 15

PINCODE Number 8
A'
M

STATE Varchar2 15

7 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Table Name : STUDENT

Description : used to store student information

Column name Data type size

SNO Number 5

SNAME Varchar2 20

AGE Number 5

E
SDOB Date

EG
SMARK1 Number 4,2

LL
SMARK2 Number 4,2

CO
SMARK3 Number 4,4

IC
HN
EC

2. Exercise on altering the table structure


(a) Add a column called „telephone‟ of data type „number‟ and size =‟10‟ to the
YT

Client _Master table.


L

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


PO

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


N

(a)Destroy the table Client_Master along with its data


DI

4. Exercise on renaming the table


A'

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


M

8 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO:2
CONSTRAINTS

AIM:

To implement Data Constraints.

THEORY

E
Constraints are the business Rules which are enforced on the data being stored

EG
in a table are called Constraints

LL
TYPES OF CONSTRAINTS:
1) Primary key

CO
2) Foreign key/references
3) Check
4) Unique
5) Not null
6) Null
IC
HN
7) Default

PROCEDURE
EC

(a) The PRIMARY KEY


YT

The PRIMARY KEY defined at column level


L
PO

Syntax:

CREATE TABLE tablename (Columnname1 DATATYPE CONSTRAINT


N

<constraintname1> PRIMARY KEY,Columnname2 DATATYPE,


DI

columnname3 DATATYPE,..... );
A'

EXAMPLE
M

SQL>create table Employee(empno number(4) primary key,ename


varchar2(10),job varchar2(6),sal number(5),deptno number(7));

9 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

The PRIMARY KEY defined at table level

Syntax:

CREATE TABLE tablename (Columnname1 DATATYPE, columnname2


DATATYPE, columnname3 DATATYPE, PRIMARY KEY (columnname1,
columnname2));

EXAMPLE

E
(b) CHECK CONSTRAINT

EG
The CHECK Constraint defined at column level

LL
Syntax:

CO
CREATE TABLE tablename
(Columnname1 DATATYPE CHECK (logical expression), columnname2
IC
DATATYPE, columnname3 DATATYPE,...);
HN
EXAMPLE
EC

CREATE TABLE Employee(empno number(3),ename varchar2(20),design


varchar2(15),sal number(5) CHECK(sal>500 and sal<10001),deptno
YT

number(2));
L

The CHECK Constraint defined at table level


PO

Syntax:
N

CREATE TABLE tablename


DI

(Columnname1 DATATYPE, columnname2 DATATYPE, columnname3


DATATYPE, CHECK (logical expression1), CHECK (logical expression2));
A'

EXAMPLE
M

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

10 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
design varchar2(15),sal number(5), UNIQUE(design));

EG
LL
The UNIQUE Constraint defined at the the table level

CO
Syntax

CREATE TABLE tablename (Columnname1 DATATYPE, columnname2


IC
DATATYPE, columnname3 DATATYPE, UNIQUE (columnname1));
HN
EXAMPLE
EC

sql>create table Employee(empno number(3),ename varchar2(20),


design varchar2(15),sal number(5), UNIQUE(design));
YT

(d) Not Null


L
PO

Syntax

CREATE TABLE tablename(Columnname1 DATATYPE NOT NULL,


N

columnname2 DATATYPE NOT NULL,columnname3 DATATYPE,...);


DI

EXAMPLE
A'

sql>CREATE TABLE Employee(empno number(4),ename varchar2(20) NOT


M

NULL,design varchar2(20),sal number(3));

Problems

11 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
must start with

EG
„p‟

DESCRIPTION Varchar2 15 Not Null

LL
PROFITPERCENT Varchar2 4,2 Not Null

CO
UNITMEASURE Varchar2 10 Not Null

QTYONHAND Number IC 8 Not Null


HN
REORDERLVL Number 8 Not Null

SELLPRICE Number 8,2 Not


EC

Null,cannot be
0
YT

COSTPRICE Number 8,2 Not


L

Null,cannot be
PO

0
N

Table Name : CLIENT_MASTER


DI

Description : used to store client information


A'

Column name Data type size Attributes


M

CLIENTNO Varchar2 6 Primary


key/first letter
must start with
„C‟

12 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

NAME Varchar2 20 Not Null

ADDRESS1 Varchar2 30

ADDRESS2 Varchar2 30

CITY Varchar2 15

PINCODE Number 8

STATE Varchar2 15

E
BALDUE Number 10,2

EG
LL
Table Name : SALESMAN_MASTER

CO
Description : used to store salesman information working for the company

Column name Data type size Attributes

SALESMANNO Varchar2
IC 6 Primary
HN
key/first letter
must start with
EC

„S‟
YT

SALESMANNAME Varchar2 20 Not Null

ADDRESS1 Varchar2 30 Not Null


L
PO

ADDRESS2 Varchar2 30

CITY Varchar2 15
N
DI

PINCODE Number 8

STATE Varchar2 15
A'
M

13 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO 3
DATA MANIPULATION LANGUAGE

AIM:

To execute the Data Manipulation Language (DML) commands in RDBMS.

OBJECTIVES

To understand Data Manipulation Language (DML) commands

E
THEORY

EG
DML commands are the most frequently used SQL commands and is used to

LL
query and manipulate the existing database objects. Some of the commands are
1. INSERT

CO
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 IC
It is used to retrieve information from the table.it is generally referred to
HN
as querying the table. We can either display all columns in a table or only
specify column from the table.
EC

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

4. DELETE
After inserting row in a table we can also delete them if required. The
L

delete command consists of a from clause followed by an optional where


PO

clause
N

PROCEDURE
DI

INSERT COMMAND
A'

(a) Inserting a single row into a table:


M

Syntax:

insert into <table name> values (<expression1>,<expression2>)

Example:

14 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
( c) Skipping the fields while inserting:

EG
Insert into <tablename>(<column name1>,<column name3>)>values

LL
(<expression1>,<expression3>);
Other way is to give null while passing the values.

CO
SELECT COMMAND IC
HN
(a) view all rows and all columns

Syntax:
EC

Select * from tablename;


YT

Example:
L

Select * from Employee;


PO

(b) Selected Columns And All Rows

Syntax:
N
DI

Select <column1>,<column2> from tablename;

Example:
A'

Select empno, empname from Employee;


M

(c)Selected Columns And selected Rows

Syntax:
SELECt <column1>, <column2> FROM <tablename> WHERE <condition> ;

15 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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;

E
EG
UPDATE COMMAND

LL
(b)updating all rows

CO
Syntax:
update tablename set IC
columnname1>=<exprssion1>,<columnname2>=<exprssion2>;
HN
Example:
EC

Update Employee set Designation = „lecturer‟;


L YT

(b)updating records conditionally


PO

Syntax:
N

update tablename set field=values where condition;


DI

Example:
A'

Update Employeeemp set sal = 10000 where empno=135;


M

DELETE COMMAND
(b)Removal of all rows
Syntax:

16 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Delete from <table name> ;


Example:
Delete from emp;

(b)removal of specific rows


Syntax:

E
EG
Delete from <table name> where <condition>;
Example:

LL
delete from emp where empno=135;

CO
RESULT
IC
The DML commands are executed successfully.
HN
EC

Problems
YT

1. Insert the following data into their respective tables.


L
PO

Data for CLIENT_MASTER table


ClientNo Name City Pincode State BalDue
N
DI

C00001 Ivan Mumbai 400054 Maharashtra 15000


A'

C00002 Ashwini Chennai 780001 TamilNadu 0


M

C00003 Joshi Mangalore 560001 Karnataka 5000


C00004 Deepak Chennai 780001 TamilNadu 0
C00005 Sharma Mumbai 400054 Maharashtra 2000

17 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Product Descripti Profitpe unitmeas qtyonha sellpri Cost


No on rcent ure nd ce price
P00001 Tshirt 5 piece 200 350 250
P00065 Shirt 6 piece 150 500 350
P00032 Jeans 5 piece 100 600 450

E
P00324 Skirts 4 piece 120 750 500

EG
P02345 CottonJe 3 piece 80 850 550

LL
ans

CO
2. Data for PRODUCT_MASTER table

IC
HN
Sales Name Address1 Address2 city Pincode State
manN
EC

o
YT

S0000 Aman A/4 Worli Mumbai 400002 Maharashtra


1
L
PO

S0000 Omkar 65 Nariman Mumbai 400001 Maharashtra


2
N
DI

S0000 Raj P-7 Bandra Mumbai 400032 Maharashtra


3
A'

S0000 Ashish A/5 Juhu Mumbai 400044 Maharashtra


M

4
3. Data for SALESMAN_MASTER table

18 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

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

LL
5. Exercise on updating the records on a table

CO
a. Change the city of ClientNo‟C00005‟ to „Bangaluru‟.
b. Change the cBalDue of ClientNo‟C00001‟ to Rs.1000.
IC
c. Change the costprice of „Shirt „ to Rs.450.
HN
d. Change the city of salesman to Pune.
EC

6. Exercise on deleting the records in a table


YT

a. Delete all salesman from the Salesman_master whose salaries are


equal to Rs.3500.
L
PO

b. Delete all sproducts from the Product_master where quantity on


hand is equal to 100
N

c. Delete from the Client_master where the column state holds the
DI

value „Tamilnadu‟.
A'
M

19 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO:3
DATA CONTROL LANGUAGE

AIM:

To implement DCL statements.

OBJECTIVES

To understand DCL commands

E
EG
THEORY:

LL
Data Control Language (DCL) consists of various commands which are related

CO
to data sharing and security of data in database.
They are
GRANT
REVOKE
IC
HN
Granting Privileges:
EC

Objects that are created by a user are owned and controlled by that user. If user
wishes to access any of the objects belonging to another user, the owner of the
YT

object will have to give permissions for such access. This is called Granting of
L

Privileges.
PO

Granting privileges using the GRANT statements:


The GRANT statements provide various types of access to database objects
N

such as tables, views.


DI

Syntax:
A'

GRANT {object privileges}


M

ON object name
TO username;
Object Privileges:

20 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
DELETE command.

EG
• INDEX: allows the grantee to create an index on the table with the CREATE

LL
INDEX command.
• INSERT: allows the grantee to add records to the table with the INSERT

CO
command.
• SELECT: allows the grantee to query the table with SELECT command.
IC
• UPDATE: allows the grantee to modify the records in the table with the
HN
UPDATE command.
Revoking privileges given:
EC

Privileges once given can be denied to a user using the REVOKE command.
YT

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
L
PO

power to REVOKE the privileges from the grantee.


Revoking permission using the REVOKE statement:
N

The REVOKE statement is used to deny the grant given on an object.


DI

Syntax:
A'

REVOKE {object privileges}


M

ON object name
FROM username;
The REVOKE command is used to revoke object privileges that the user
previously granted to the Revoke.

21 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

The REVOKE command cannot be used to revoke the privileges granted


through operating system.
RESULT:
Familiarised DCL statements.

E
EG
LL
CO
IC
HN
EC
L YT
PO
N
DI
A'
M

22 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO:5
COMPUTATIONS ON TABLE DATA WITH
BUILT IN FUCTIONS
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

E
functions

EG
THEORY
Group Functions/Aggregate functions

LL
A group function returns a result based on group of rows.

CO
1. avg
Example: select avg (total) from student;
2.max
IC
Example: select max (percentagel) from student;
HN
2.min
Example: select min (marksl) from student;
4. sum
EC

Example: select sum(price) from product


Count Function
YT

In order to count the number of rows, count function is used.


L

1. count(*) – It counts all, inclusive of duplicates and nulls.


PO

Example: select count(*) from student;


2. count(col_name)– It avoids null value.
N

Example: select count(total) from order;


DI

2. count(distinct col_name) – It avoids the repeated and null values.


Example: select count(distinct ordid) from order;
A'
M

Special Clauses:

Group by clause
This allows us to use simultaneous column name and group functions.

23 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
deptname having count(*)>=50;

EG
In / not in – used to select a equi from a specific set of values
Any - used to compare with a specific set of values

LL
Between / not between – used to find between the ranges

CO
Like / not like – used to do the pattern matching
PROCEDURE
IC
HN
OUTPUT
RESULT
EC
YT

PROGRAMS
L

1. generate SQL statements to perform the following computations on


PO

table data.
N

a.list the names of all clients having „a‟ as the second letter in their
DI

names.
A'

b.listing of clients who stay in a city whose first letter is „M‟


M

c.list all clients who stay in „Bangaluru‟ or „Mangalore‟


d.list all clients whose BalDue is greater than 10000
e.display the order information of clientno „C00001‟ and‟C00002‟

24 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

f.list products whose selling price is greater than 500 and less than or
equal to 750
g.listing of names,city and state of clients who are not in the state of
„maharashtra‟.
h.count the total number of orders
i.calculating the average price of all products.

E
j.determining the maximum and minimum price for the product

EG
prices.

LL
k.count the number of products having the price greater than or equal
to 500

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

IC
a. printing the description and total quantity sold for each product.
HN
b. Finding the value of each product sold
c. find out the total of all the billed orders for the month of june.
EC
L YT
PO
N
DI
A'
M

25 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO:6
NESTED QUERIES/SUB QUERIES AND JOINS

AIM:

To implement e nested queries and joins on the given table

OBJECTIVES

E
To understand nested queries and joins.

EG
THEORY

LL
a) NESTED QUERIES:

CO
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
IC
clause, the FROM clause, or the SELECT clause.
HN
b) JOINS:

Join is a query in which data is returned from two or more tables.


EC

Natural join:
YT

It returns the matching rows from the table that are being joined
.
L

Syntax:
PO

>select <attribute> from TN where TN1.attribute=TN2.attribute.


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

Syntax:
>select <attribute> from TN1 innerjoin TN2 on TN1.attribute=TN2.attribute.
A'

Left outer join:


It returns all the rows from the table1 even when they are unmatched.
M

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.

26 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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:

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

EG
PROCEDURE

LL
CO
NESTED QUERIES -

SQL> desc emp_det;


Name Null? Type
IC
HN
----------------------------------------- -------- ----------------------------
ENO NOT NULL NUMBER(3)
ENAME VARCHAR2(25)
EC

ADDRESS VARCHAR2(30)
BASIC_SAL NUMBER(12,2)
YT

JOB_STATUS VARCHAR2(15)
DNO NUMBER(3)
L
PO

SQL> desc pro_det;


Name Null? Type
N

----------------------------------------- -------- ----------------------------


DI

PNO NOT NULL NUMBER(3)


PNAME VARCHAR2(30)
A'

NO_OF_STAFF NUMBER(3)
M

SQL> desc work_in;

Name Null? Type


----------------------------------------- -------- ----------------------------
PNO NUMBER(3)
ENO NUMBER(3)

27 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
2 Mahendran RainbowColon 5000 Supervisor 10

EG
y
3 RajKumar EastCoastRoad 10000 Professor 2
4 Shirley KKnagar 8000 AsstManager 3

LL
CO
SQL> select * from Pro_det;
IC
HN
PNO PNAME NO_OF_STAFF

1 DBMS 2
EC

2 COMPILER 3
3 C1 1
YT

SQL> select * from work_in;


L
PO

PNO ENO PJOB


N

1 1 Programmer
2 1 Analyst
DI

1 2 Analyst
A'

2 2 Programmer
M

NESTED QUERIES

(i) SQL> select ename from emp_det where dno not in(select dno from
emp_det where ename ='SaravanaKumar');

ENAME

28 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

---------------
RajKumar
Shirley

(ii)SQL> select ename, dno from emp_det where dno = (select dno from
emp_det where ename ='RajKumar');

ENAME DNO
--------------- ----------
RajKumar 2

E
(iii)SQL> select ename from emp_det where eno in(select eno from work_in

EG
where pno = (select pno from pro_det where pname = 'DBMS')) order by
ename;

LL
ENAME
---------------

CO
Mahendran
SaravanaKumar
(iv)SQL> select ename, basic_sal from emp_det where dno = 2 and
IC
basic_sal>(select max(basic_sal) from emp_det where dno = 10) order by
ename;
HN
ENAME BASIC_SAL
EC

--------------- ----------
RajKumar 10000
YT

(v)SQL> select pno,pname from pro_det where exists(select pno from work_in
where work_in.pno =pro_det.pno);
L
PO

PNO PNAME
------ ------------------------------
N

1 DBMS
2 COMPILER
DI

(vi)SQL>select ename, job_status,basic_sal from emp_det where


A'

(dno,basic_sal) in (select dno,basic_sal from emp_det where ename


M

='RajKumar');

ENAME JOB_STATUS BASIC_SAL


--------------- --------------- ----------
RajKumar Professor 10000

29 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
8000

EG
(ix)SQL> select * from emp_det where basic_sal < (select avg(basic_sal) from

LL
emp_det);

CO
ENO ENAME ADDRESS BASIC_SAL JOB_STATUS DNO
---- --------------- --------------- ---------- --------------- ----------
2 Mahendran RainbowColony 5000 Supervisor 10

JOINS
IC
HN
SQL> create table emp(name varchar2(20),salary number(10));
Table created.
EC

SQL> select * from emp;


NAME SALARY
-------------------- ----------
YT

ashu 10000
asma 1200
L

asif 2000
PO

arif 1000
niyas 3000
SQL> create table emp1(name varchar2(20),empid number(10));
N

Table created.
DI

.
SQL> select * from emp1;
A'

NAME EMPID
M

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

30 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
-------------------- ----------

EG
asma 1200
asif 2000

LL
arif 1000
niyas 3000

CO
ashu 10000
RIGHT OUTER JOIN

IC
SQL>select emp1.name,empid from emp right outer join emp1 on
emp.name=emp1.name
HN
NAME EMPID
-------------------- ----------
asma 1200
EC

sweety 9
sumi 32
YT

wahab 10
fathi 12
L

priya 11
PO

6 rows selected.
FULL JOIN
SQL>select emp1.name,emp.name,emp1.empid,salary from emp full join emp1
N

on
DI

emp.name=emp1.name
NAME NAME EMPID SALARY
A'

-------------------- -------------------- ---------- ----------


asma asma 1200 1200
M

asif 2000
arif 1000
niyas 3000
ashu 10000
sweety 9
sumi 32

31 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

wahab 10
fathi 12
priya 11
10 rows selected.

RESULT:
Thus the nested queries and join operations are executed and verifiedin DBMS.

E
Programs

EG
1. Exercises on sub-queries

LL
a) find the non moving products.ie products not being sold.

CO
b) Find the name and complete address for the customer who has placed order
number „o19001‟

IC
c) find the clients who have placed orders before the month of may „02
HN
d) find the names of clients who have placed orders worth Rs.10000 or more.
EC
L YT
PO
N
DI
A'
M

32 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO:7
VIEWS
AIM:

To create and drop View on the given table.

OBJECTIVES

To implement views

E
EG
THEORY

LL
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

CO
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
IC
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
HN
stored as a SELECT statement in the data dictionary .
Advantages of a view:
EC

a. Additional level of table security.


b. Hides data complexity.
YT

c. Simplifies the usage by combining multiple tables into a single table

Creating and dropping view:


L
PO

Syntax:

Create or replace view view_name AS


N

SELECT column_name(s)
DI

FROM table_name
WHERE condition
A'
M

Drop view <view name>;

Example
Create or replace view empview as select * from emp;

Drop view empview;

33 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
Bb 23 2001 12 23435

EG
Cc 55 342 76 687478
dd 2 1233 123 53616578

LL
ee 21 1111 111 12435798

CO
3) create table qq
SQL> create table qq(name varchar2(20),book number(10),author
IC
varchar(20),publisher varchar2(20),ISBN number(20));
HN
4) describe table qq
EC

SQL> select * from qq;


NAME BOOK AUTHOR PUBLISHER ISBN
-------------------- ---------- -------------------- ------------------------------
YT

bb 21 23 dfd 573568
cc 43 55 fg 65839
L

ee 44 21 dfd 1235798
PO

oo 87 34 gfh 6358379

5) create a view on qq
N
DI

SQL>create view ww as select book,name,publisher from qq where


ISBN=573568
A'

View created.
M

6) display the view

SQL> select * from ww;


BOOK NAME PUBLISHER
- --------- -------------------- --------------------

34 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
SQL> create view wq as select name,ISBN,publisher from qq where book>21

EG
View created.
SQL> select * from wq;

LL
NAME ISBN PUBLISHER

CO
-------------------- ---------- --------------------
cc 65839 fg
ee 1235798 dfd
oo 6358379 gfh IC
HN
SQL> create view ss as select name,book from aa union select name,book from
qq;
EC

View created.
SQL> select * from ss;
NAME BOOK
YT

-------------------- ----------
bb 21
L

bb 23
PO

cc 43
cc 55
dd 2
N

ee 21
DI

ee 44
oo 87
A'

8 rows selected.
M

Result
Thus the view creation commands are executed successfully

35 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Problems

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)

E
Specialization Varchar2(4)

EG
Department Varchar2(4)

LL
Date_of_join Date

CO
Exper Number(2)

IC
1) create a view vw_doctor on Hosp_doc table
HN
2) create another view that contains doctor codes and doctor names of
„orthology‟ department
EC

3) delete the view vw_doctor


L YT
PO
N
DI
A'
M

36 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO:8
FUNCTIONS AND PROCEDURE
AIM:
To find factorial of a number using function

OBJECTIVES

E
To write PL/SQL(Functions)and to understand stored procedures in SQL.

EG
THEORY

LL
FUNCTION:

CO
A function is a subprogram that computes a value.

syntax
IC
Create or replace function<function_name>[argument]
Return datatype is
HN
(local declaration)
begin
EC

(executable statements)
[Exception]
(exception handlers)
YT

End
L

PROCEDURE:
PO

create [or replace] procedure procedurename


[parameter[in/out/in/in out] datatype
[:=/default expression]
N

[(parameter)]
DI

is/as
declaration
A'

begin
M

pl/sql codes
[exception]
end

PROGRAM

37 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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;

E
i:=i+1;

EG
end loop;
return f;

LL
end fact; /Function created.
SQL>begin

CO
2 dbms_output.put_line('the factorial='||fact(&a));
3* end;

OUTPUT IC
HN
SQL> /
Enter value for a: 4
old 2: dbms_output.put_line('the factorial='||fact(&a))
EC

new 2: dbms_output.put_line('the factorial='||fact(4));


the factorial=24
YT

PL/SQL procedure successfully completed.


L

RESULT:
PO

Thus the functions and stored procedures are executed in SQL.


N
DI

Problems;
1) procedure to find whether a given number is odd or even
A'

2) procedure to display 1-10 using while


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

38 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO: 9
CURSOR

AIM

To retrieve all students who have registered for Diploma and store their details
into another table called diploma (id,name) using cursors.

E
EG
OBJECTIVES

LL
To implement cursor

CO
PROCEDURE

1) TABLE CREATION
IC
SQL>create table student(id number,name varchar2(25),programme
varchar2(25));
HN
SQL>create table diploma(id number,name varchar2(25));
EC

SQL>insert into students values(1,‟rohan‟,‟diploma‟);


SQL>insert into students values(2,‟anu‟,‟MA‟);
YT

SQL>insert into students values(3,‟robert‟,‟diploma‟);


L

SQL>insert into students values(4,‟tom‟,‟btech‟);


PO

SQL>insert into students values(5,‟sunny‟,‟diploma‟);


N

SQL>select * from students;


DI

Id name programme
A'

1 rohan diploma
M

2 anu MA
3 robert diploma
4 tom btech
5 sunny diploma

SQL>declare
2 cursor stud is select * from students where programme =”diploma‟;

39 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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;

E
13 end;

EG
14 /

LL
OUTPUT

CO
RESULT

PROGRAMS IC
1.A HRD manager has decided to raise the salary of all employees in
HN
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
EC

cursors to update the salary of each employee of dept no 20 and insert a record
in the emp_raise table as well
L YT
PO
N
DI
A'
M

40 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO: 10
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

E
operations on a table

EG
THEORY.

LL
PROCEDURE

CO
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. IC
step 4: execute the trigger procedure for both before and after sequences
HN
step 5: carryout the operation on the table to check for trigger execution.
step 6: stop
EC

PROGRAM
sql> create table emp(iname varchar2(10),iid number(5),salary number(10));
YT

table created.
L
PO

sql> create table sal(iname varchar2(10),totalemp number(5),totalsal


number(10));
N

table created.
DI

sql> create or replace trigger emptrigr after insert on emp


A'

for each row


declare
M

a varchar2(10);
begin
a:=:new.iname;
update sal set
totalsal=totalsal+:new.salary,totalemp=totalemp+1 where
iname=a;

41 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
1 row created.

EG
sql> select * from sal;
iname totalemp totalsal

LL
---------- ---------- ----------

CO
vec 1 1000
srm 0 0

sql> insert into emp values('srm',200,3000);


1 row created.
IC
HN
sql> select * from sal;
iname totalemp totalsal
EC

---------- ---------- ----------


Vec 1 1000
YT

srm 1 3000
sql> insert into emp values('vec',100,5000);
L

1 row created.
PO

sql> select * from sal;


iname totalemp totalsal
N

---------- ---------- ----------


DI

vec 2 6000
srm 1 3000
A'

sql> insert into emp values('vec',100,2000);


1 row created.
M

sql> select * from sal;


iname totalemp totalsal
---------- ---------- ----------
vec 3 8000
srm 1 3000
sql> insert into emp values('srm',200,8000);

42 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
sequences.

EG
Problems

LL
1. Write a trigger that stores the details of students changing their program from

CO
CT to CHM.
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

Column name Data type


IC
should be added in the AUDIT_TRAIL table
size
HN
Client_no Varchar2 6
name Varchar2 20
EC

Bal_due Number 10,2


Operation Varchar2 8
userid Varchar2 20
YT

olddate date
L
PO
N
DI
A'
M

43 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Experiment No:11
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.

E
Objective:

EG
To study the concept of various levels of normalization and understand how to
convert into normalized forms.

LL
Requirements: Mysql database software

CO
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
IC
to the rules and regulations of each normal forms.
Procedure:
HN
Consider a student table as given below
Social Security Number FirstName LastName Major
EC

123-45-6789 Jack Jones Library and


YT

Information
Science
L
PO

222-33-4444 Lynn Lee Library and


Information
Science
N
DI

987-65-4321 Mary Ruiz Pre-Medicine


A'

123-54-3210 Lynn Smith Pre-Law


M

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

44 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Experiment No:12

Checking Normalization of a database table(Third normal form).


Problem Statement:

An excercise to check whether the given database table is normalized or not. If


yes find out the status of normalization and reasoning.

Objective:

E
EG
To study the concept of various levels of normalization and understand how to
convert into normalized forms.

LL
Requirements: Mysql database software

CO
Design/Theory
Create a database table in SQL with a few no of rows and
IC
columns.Analyze the table and determine to which normal form it beongs to
according to the rules and regulations of each normal forms.
HN
Procedure:
Consider a book database table as given below.
EC

Author Author Book Title Subject Collection or Building


Last First Library
YT

Name Name
L

Berdahl Robert The Politics of the History PCL General Perry-


PO

Prussian Nobility Stacks Castañeda


Library
N

Yudof Mark Child Abuse and Legal Law Library Townes Hall
Neglect Procedure
DI

s
A'

Harmon Glynn Human Memory and Cognitive PCL General Perry-


Knowledge Psycholog Stacks Castañeda
M

y Library
Graves Robert The Golden Fleece Greek Classics Library Waggener Hall
Literature
Miksa Francis Charles Ammi Cutter Library Library and Perry-

45 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Biograph Information Castañeda


y Science Library
Collection
Hunter David Music Publishing and Music Fine Arts Fine Arts
Collecting Literature Library Building
Graves Robert English and Scottish Folksong PCL General Perry-
Ballads Stacks Castañeda
Library

E
By examining the table, we can infer that books dealing with history,

EG
cognitive psychology, and folksong are assigned to the PCL General Stacks
collection; that books dealing with legal procedures are assigned to the Law

LL
Library; that books dealing with Greek literature are assigned to the Classics
Library; that books dealing with library biography are assigned to the Library

CO
and Information Science Collection (LISC);and that books dealing with music
literature are assigned to the Fine Arts Library.
IC
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
HN
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
EC

Building.
YT

Thus we can see that a transitive dependency exists in the above table :
any book that deals with history, cognitive psychology, or library biography
L

will be physically housed in the PCL building (unless it is temporarily checked


PO

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,
N

we also know not only what library or collection it will be assigned to but also
what building it is physically housed in.
DI

A problem with transitive dependency is that, there is duplicated


A'

information: from three different rows we can see that the PCL General Stacks
M

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

46 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
Last First

EG
Name Name

LL
Berdahl Robert The Politics of the Prussian
Nobility

CO
Yudof Mark Child Abuse and Neglect

Harmon Glynn Human


IC
Memory and
HN
Knowledge
EC

Graves Robert The Golden Fleece


YT

Miksa Francis Charles Ammi Cutter


L

Hunter David Music Publishing and


PO

Collecting
N

Graves Robert English and Scottish Ballads


DI
A'

Table B
M

Book Title Subject

The Politics of the Prussian History


Nobility

47 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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

E
Collecting

EG
English and Scottish Ballads Folksong

LL
Table C

CO
Subject Collection or Library

History
IC
PCL General Stacks
HN
Legal Procedures Law Library
EC

Cognitive PCL General Stacks


Psychology
YT

Greek Literature Classics Library


L

Library Biography Library and Information Science


PO

Collection
Music Literature Fine Arts Library
N
DI

Folksong PCL General Stacks


A'
M

Table D
Collection or Library Building

PCL General Stacks Perry-Castañeda


Library

48 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Law Library Townes Hall

Classics Library Waggener Hall

Library and Information Science Perry-Castañeda


Collection Library
Fine Arts Library Fine Arts Building

E
EG
LL
CO
IC
HN
EC
L YT
PO
N
DI
A'
M

49 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Experiment No: 13
Database Connectivity
Java Database Connectivity
Aim: To understand the java database connevtivity

Problem Statement:

A program which connects to an online book database table and select the tuple
and display it.

E
Objective:

EG
To understand the connectivity to a database table using java. In java database is

LL
accessed through java database connectivity(JDBC).

CO
Requirements: Mysql database software, Java software.

Design/Theory:
IC
The four main components required for implementation of JDBC are
application,driver manager, data source specific drivers and corresponding data
HN
sources. The application program establishes/terminates connection with data.
Driver manager will load JDBC drivers and pass JDBC function calls from the
EC

application to the driver.The data source processes commands from the driver
and return the results.
1. Drivers for the database are loaded by using Class.for Name.
YT

2. The getConnection() function of DriverManager class of JDBC is used to


create the connection object. The URL specifies the machine
L

name(db.onlinebook.edu)
PO

public static void Sample(String DB_id, String U-id, String Pword)


N

{
String URL =”jdbc:oracle:oci8:@db.onlinebook.edu:100:onbook_db”;
DI

Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection cn=DriverManager.getConnection(URL,U_id, Pword);
A'

Statement st = Cn.createStatement();
M

try
{
st.executeUpdate(“INSERT INTO AUTHOR
VALUES('A010','SMITH',JONES','TEXAS')”);
}
catch(SQLException se)
{

50 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

System.out.println(“Tuple cannot be inserted.”+se);

E
EG
LL
CO
IC
HN
EC
L YT
PO
N
DI
A'
M

51 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

}
ResultSet rs = st.execute Query(“SELECT Aname,State from AUTHOR
WHERE City = 'Seatle'”);
while(rs.next())
{
System.out.println(rs.getString(1) + “ “+rs.getString(2));
}
st.close();
Cn.close();
}

E
EG
LL
CO
IC
HN
EC
LYT
PO
N
DI
A'
M

52 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Experiment No:14
Database Connectivity
AIM:
To create a PHP-Mysql Program to enter data into Mysql

Problem Statement:

A php program that connects to a database table and insert values into the table.

Objective:

E
EG
To understand the connectivity to a database table using php and how to insert
new values into that table.

LL
Requirements:

CO
Mysql database software, LAMP Server and Html

Design/Theory: IC
HN
In an existing or a newly created database an employee table is formed with
attributes firstname, lastname and email. A php-html program is used to insert
EC

values into the table.


Php code:
<?php
YT

$host="localhost"; // Host name


L

$username=""; // Mysql username


PO

$password=""; // Mysql password


$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
N
DI

// Connect to server and select database.


mysql_connect("$host", "$username", "$password")or die("cannot connect");
A'

mysql_select_db("$db_name")or die("cannot select DB");


M

// Get values from form


$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];

// Insert data into mysql

53 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

$sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name',


'$lastname', '$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".


if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {

E
echo "ERROR";

EG
}
?>

LL
<?php
// close connection

CO
mysql_close();
?>
<html>
<body> IC
HN
<form action= "<?php $_php_self ?>" method = "GET">
Empid: Empname: Empage: Dept: City
EC

<input type="text" name="Empid" />


<input type="text" name="Empname" />
YT

<input type="text" name="Empage" />


L

<input type="text" name="Dept" />


PO

<input type="text" name="City" />


N

<input type="submit" value="Show Result" />


DI

</form>
</body></html>
A'
M

54 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

EXPERIMENT NO: 15
DATABASE CONNECTIVITY

AIM

Create a php program that allows to enter the employee details into a database.

To create an application program that process a query which returns the grade
result of a student after processing the marks table.

E
OBJECTIVES

EG
To understand PHP mysql database connectivity.

LL
To study the concept of connecting database using an application program
(PHP) and process results after manipulating the database table

CO
Requirements:

IC
Mysql database software, LAMP Server and html.
HN
PROCEDURE

Create a database in Mysql. Create a 'marks' table with fields regno, name,
EC

batch, mark1, mark2 and mark3. Write a function which finds the total marks
and if the total marks is greater than 225 then the result is categorized as
YT

“Distinction”, if the total marks is less than 225 , the result is “firstclass”, it the
total marks is less than 180, the result is “second class”, if the total marks is less
L

than 150 the result is “passed”. If the marks in any one of the subject is less than
PO

40 the result is “Failed”.


N
DI

1. create a form in PHP/JAVA

2. create a database in mysql


A'
M

3. provide the database connectivity

4. retrieve the details of employee through forms

php code:

55 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

<?php

$user_name = "root";

$password = "";

$server = "localhost";

$database = "mysql";

$regno = $_POST['regno'];

E
//$regno = 1;

EG
$db_handle = mysql_connect($server, $user_name, $password);

LL
if (!$db_handle)

CO
{
die('Could not connect: ' . mysql_error());
}
IC
HN
//echo 'Connected successfully';
$db_found = mysql_select_db($database);
EC

if ($db_found)
YT

{
L

//echo "Database found";


PO

$SQL = "SELECT * FROM marks WHERE regno = '".$regno."'";


$result_set = mysql_query($SQL);
N
DI

$record = mysql_fetch_array($result_set);
echo "<BR>MARKLIST";
A'

echo "<BR>Reg No..:".$record['regno'];


M

echo "<BR>Name. ....".$record['name'];


echo "<BR>Group. .. ".$record['batch'];
echo "<BR>Mark1. ...".$record['mark1'];
echo "<BR>Mark2. ...".$record['mark2'];

56 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

echo "<BR>Mark3...:".$record['mark3'];
echo "<BR>Result..:".
compute_result($record['mark1'],$record['mark2'],$record['mark3']);

}
else
{

E
echo "<BR>Database not Found";

EG
}

LL
mysql_close($db_handle);

CO
function compute_result($m1, $m2, $m3)
{
$tmarks = $m1 + $m2 + $m3; IC
HN
if (($m1<40) || ($m2<40) || ($m3<40))
$result = "Failed";
EC

elseif ($tmarks < 150)


YT

$result = "Passed";
elseif ($tmarks<180)
L
PO

$result = "Second Class";


elseif ($tmarks<225)
N

$result = "First Class";


DI

else
A'

$result = "Distinction";
M

return $result;
}
?>

57 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

Html code:

<html>
<body>
<FORM Method = "post" Action="connect1.php">
Reg No:

E
<INPUT Type = "text" name = "regno"> <BR>

EG
<INPUT type = "submit" value = "Show Result">

LL
</FORM>

CO
</body>
</html>

OUTPUT IC
HN
RESULT
EC
L YT
PO
N
DI
A'
M

58 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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?

E
11. What is a super key?

EG
12. What is a composite key?
13. What is a relation?
14. What is a table?

LL
15. What is an attribute?
16. What is a domain?

CO
17. What is a tuple?
18. What is a selection?
19. what is a join operation?
IC
20. What are base operations in relational algebra?
21. What are different DBMS facilities? How many types of facilities are provided by a
HN
DBMS?
22. What is Data Definition Language?
23. What is Data Dictionary?
EC

24. What is a DML?


25. What is a query?
26. What is a query language?
YT

27. What are the advantages of DBMS?


28. What is a SQL?
29. What are the features of SQL?
L

30. How SQL organizes the data?


PO

31. What is data definition?


32. What is data retrieval?
33. What is data sharing?
N

34. What is a view?


35. What is normalization?
DI

36. What is a first normal form?


37. What is a second normal form?
A'

38. What is a third normal form?


39. What is BCNF?
M

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?

59 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM


REVISION-2015 DATABASE MANAGEMENT SYSTEM LAB - 3139

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?

E
59. What is Relational Calculus?

EG
LL
CO
IC
HN
EC
L YT
PO
N
DI
A'
M

60 MA‟DIN POLYTECHNIC COLLEGE, MALAPPURAM

You might also like