Professional Documents
Culture Documents
Sathyabama University: Department of Information Technology RDBMS LAB (612551) Lab Manual List of Excercises
Sathyabama University: Department of Information Technology RDBMS LAB (612551) Lab Manual List of Excercises
1
1. IN0TRODUCTION TO DBMS
DATA BASE:
Data base is a collection of related data. Data means known facts that can be
recorded and has implicit meaning.
DBMS:
A database management system (DBMS) is a collection of programs that enables
users to create and maintain a database. The DBMS is hence a general-purpose software
system that facilitates the process of defining, constructing and manipulating database for
various applications.
CHARACTERISTICS OF DBMS:
⮚ Self describing name of the data base systems.
⮚ Insulation between programs and data abstraction.
⮚ Support of multiple views of the data.
⮚ Sharing of data and multi user transaction processing.
ADVANTAGES OF DBMS:
⮚ Controlling redundancy.
⮚ Restricting unauthorized access.
⮚ Providing persistent storage for program objects and data structures.
⮚ Permitting inferencing and actions using rules
⮚ Providing multiple user interfaces.
⮚ Representing complex relationships among data.
⮚ Enforcing integrity constraints.
⮚ Providing backup and recovery.
TYPE OF ATTRIBUTES:
SINGLE- VALUE VERSUS MULTI-VALUED ATTRIBUTES:
2
Most attributes have a single value for a particular entity; such attributes are called
single valued. For example age is a single valued attribute of person.
An attribute can have many values for a single entity such attributes are called as
multi valued attributes. For example DEGREE of a person.
NULL VALUE:
The value if it is not specified is called as NULL value. The NULL value is not equal to
zero. It may be unknown, not available, and not applicable. For example TELEPHONE
NUMBER of a person.
COMPILERS:
1. DDL COMPILER:
DDL Compiler processes schema definition specific in DDL and stores the description
in the catalog.
2. QUERY COMPILER:
Query Compiler handles the interactive queries. It analyses a query and then
generates calls to the runtime processor for executing the request.
3. PRE COMPILER:
Pre Compiler extracts DML commands from an application program written in a host
programming language. These DML commands compiler for compilation and the rest of the
programs is sent to the host programming compilers. The object codes from both compilers
are linked, whose execution code calls to the run time database processors.
3
4
2. DATA DEFINITION LANGUAGE (DDL) COMMANDS
AIM:
To perform DDL Commands like Create, Alter and Drop on the database named employee
for storing employee details.
DDL COMMANDS:
1. CREATE:
Syntax: Create table <table name> (Fieldname1 datatype1 (width), ---------------
Fieldname n datatype n (width));
2. ALTER
Syntax:
a. ADD:
Alter table <table name> ADD (new fieldname datatype (width));
b. MODIFY:
Alter table <table name> MODIFY (old fieldname newdatatype (width));
3. DROP
Syntax: Drop table <table name>;
DDL OPERATIONS TO BE PERFORMED:
1. To create an employee table with ename, eid, doj, salary, dept,phoneno,address.
2. To include one more field address (varchar (40)).
3. Describe the table.
4. Modify the datatype of the Eid field.
5. Drop the table employee.
DATABASE DESCRIPTION:
Table Name: Employee
Field Name Data type Width
Ename Varchar 15
Eid Int 10
DOJ Date --
salary float --
email char 20
dept char 10
Phoneno int 10
Address varchar 40
5
3. DATA MANIPULATION LANGUAGE (DML) COMMANDS
AIM:
To perform DML Commands like Insert, Update and Delete.
DML COMMANDS:
1. INSERT:
To Insert One Record:
Insert into <table name> values (value1, value2, -----);
To Insert more than One Record:
Insert into <table name> values (&Fieldname1, &Fieldname2, -----------);
2. UPDATE:
Global Updation:
Update <table name> set Fieldname or Columnname = Expression;
Specific Records to be Updated:
Update <table name> set Fieldname=Expression where <condition>;
3. DELETE:
Deleting All Records:
Delete from <table name>;
Deleting Selected Records:
Delete from <table name> where <condition>;
DML OPERATIONS TO BE PERFORMED:
1. Insert five records to the employee database.
2. Increment the basic pay of all the employees by 10% of their basic pay.
3. Delete the third employee record from the table.
DATABASE DESCRIPTION
Table Name: Employee
Field Name Data type Width
Ename Varchar 15
Eid Int --
DOJ Date --
Basic pay Decimal (8,2)
Age Int --
Address Varchar 20
DATABASE DESCRIPTION
Table Name: student details
Field Name Data type Width
Rno Int --
Sname Varchar 15
DOB Date --
Course Varchar 6
Dept Varchar 4
7
Sem Varchar 3
Per decimal 10,2
DATABASE DESCRIPTION
Table Name: student details
8
Select COUNT (column name) from <table name>;
CHARACTER FUNCTIONS:
1. INITCAP(STR)
Select INITCAP (‘String’) from dual;
2. LOWER(STR)
Select LOWER (‘String’) from dual;
3. UPPER(STR)
Select UPPER (‘String’) from dual;
4. LTRIM (STR, SUBSTR)
Select LTRIM (‘String’, ‘Substr’) from dual;
5. RTRIM (STR, SUBSTR)
Select RTRIM (‘String’, ‘Substr’) from dual;
6. LPAD (STR, N, CHAR TO PAD)
Select LPAD (‘String’, n, ‘char to pad’) from dual;
7. RPAD (STR, N, CHAR TO PAD)
Select RPAD (‘String’, n, ‘char to pad’) from dual;
8. REPLACE (STR, SEARCHSTR, REPLACECHAR)
Select REPLACE (‘String’, ‘Searchstr’, ‘replacechar’) from dual;
9. TRANSLATE
Select TRANSLATE (‘String’, ‘char to be delete’, ‘replace char’) from dual;
10. SUBSTR
Select SUBSTR (‘String’, m, n) from dual;
NUMERIC FUNCTIONS:
1. ABS
Select ABS (Number) from dual;
2. CEIL
Select CEIL (decimal number) from dual;
3. FLOOR
Select FLOOR (decimal number) from dual;
4. SQRT
Select SQRT (number) from dual;
5. SIN
Select SIN (number) from dual;
6. COS
Select COS (number) from dual;
7. EXP
Select EXP (number) from dual;
8. POWER
Select POWER (n, m) from dual;
9. ROUND
Select ROUND (m, n) from dual;
10. TRUNCATE
Select TRUNCATE (m, n) from dual;
11. MOD
Select MOD (m, n) from dual;
DATE FUNCTIONS:
1. ADD – MONTHS
Select add-months (‘date’, n) from dual;
2. LAST – DAY
9
Select last-day (‘date’) from dual;
3. NEXT – DAY
Select next-day (‘date’, ‘day’) from dual;
4. MONTHS BETWEEN
Select months-between (‘date1’, ‘date2’) from dual;
5. GREATEST
Select greatest (‘date1’, ‘date2’) from dual;
6. ROUND
Select round (to_date (‘date’) dd/mm/yy) from dual;
7. SYS DATE
Select sysdate from dual;
OTER FUNCTIONS:
1. TO_CHAR
Select to_char (‘date’, statement) from dual;
2. TO_DATE
Select to_date (‘date’, statement) from dual;
3. TO_NUMBER
Select to_months (‘character’) from dual;
MISCELLANEOUS FUNCTION:
1. UID
Select UID;
2. USER
Select USER;
3. NVL
Select NVL (exp1, exp2) from dual;
SQL OPERATIONS TO BE PERFORMED:
I. Create a library database with book name, author name, access code, date of access,
publisher name, and price as the fields.
1. Select the average price of all books in the library database.
2. Select the name of the book with maximum price in the given database.
3. Select the book with minimum price.
4. Count the number of records in the library database.
5. Print the total (total cost of all the books) estimate of the library book.
II. Execute the following:
1. Convert the initial letters of given string to capital letter.
2. Convert the upper case letter to lower case.
3. Convert the lower case letter to upper case.
4. Ltrim (or) remove the leftmost substring.
5. Rtrim (or) remove the rightmost substring.
6. Allocate the total number of space and character to the filled.
7. Allocate the nspaces then add given character at rightmost position.
8. Replace the string searched by the replace character.
9. Delete a character in the string and replace it by another character.
10. Remove a substring from a string.
DATABASE DESCRIPTION:
Table Name: Library
7. CONSTRAINTS ON TABLE
AIM
To create databases to implement the constraints or conditions on tables.
CONSTRAINTS:
1. NOT NULL
i. Create table <table name> (field1 datatype(size) NOT NULL);
11
ii. Create table <table name> (field1 datatype(size) constraint <constraint
name> NOT NULL);
2. PRIMARY
i. Create table <table name> (field1 datatype(size) PRIMARY);
ii. Create table <table name> (field1 datatype(size) constraint <constraint
name> PRIMARY);
3. UNIQUE
i. Create table <table name> (field1 datatype(size) UNIQUE);
ii. Create table <table name> (field1 datatype(size) constraint <constraint
name> UNIQUE);
4. DEFAULT
Create table <table name> (field1 datatype(size) DEFAULT ‘default value’);
5. CHECK
Create table <table name> (field1 datatype (size) CHECK <condition>);
6. FOREIGN KEY CONSTRAINT
Create table <table name> (field1 datatype (size) REFERENCES fieldname of
table1 <table name>);
12
Table Name: ORDER
9. SUBQUERIES IN SQL
AIM
To develop databases to implement SubQueries.
COMMANDS
i. Select * from <table name> WHERE fieldname = (Select fieldname FROM
<table name>);
ii. Select * from <table name> WHERE fieldname IN (Select fieldname FROM
<table name>);
SYNTAX FOR OPERATORS IN SUBQUERIES
i. Select * from table1 WHERE t1.value > operator (Select * from table2 where
t2.value <condition>);
OPERATIONS TO BE PERFORMED:
1. Execute the following
Create 3 tables
i. Client with clno, cname, city
ii. Product with pno, pdesc, price, quantity on hand
iii. Sorder with sno, rno, clno and quantity required.
Perform the following operations
iv. Retrieve all clients who placed orders on ‘mouse’.
v. Retrieve all products ordered by ‘Karthik’;
vi. Retrieve all the products ordered by all clients except ‘Karthik’;
2. Execute the following
Create 3 database with the following columns
i. Book with codeno, bname, authorname, pname, price
ii. Student with sid, sname, semester, branch
iii. Library with codeno, sid, date of access, date of return.
Perform the following operations
iv. Retrieve all the books accessed by student ‘S’;
v. Retrieve the details of students who accessed books on DBMS.
vi. Retrieve the details of books ordered on any date.
vii. Retrieve the name and author of the book accessed by 3rd semester
MBA students.
viii. Retrieve the book accessed by students of all departments other than
‘MBA’.
ix. List the details of students who accessed books worth Rs.500 and
above.
x. List the students who accessed books on all publishers other than
‘PHI’.
xi. Retrieve the names of students who accessed books with author
starting with the letter ‘S’.
15
DATABASE DESCRIPTION:
Table Name: CLIENT
16
Field Name Data type Width
CODENO Number 5
SID Number 5
DOA Date --
DOR Date --
10. VIEWS
AIM
To develop databases and perform various view operations.
SYNTAX
VIEWS
1. ROW SUBSET VIEW
Create [OR OPTIONAL] view <view name> AS select * from <table name> where
<condition>;
2. COLUMN SUBSET VIEW
Create [OR OPTIONAL] view <view name> AS select field1, field2, …… from <table
name> where <condition>;
3. ROW COLUMN SUBSET VIEW
Create [OR REPLACE] view <view name> AS select field1, field2, …… from <table
name> where <condition>;
4. GROUPED VIEW
Create [OR REPLACE] view <view name> AS select aggregate fun (field) field alias
name from <table name> GroupBy fieldname;
5. PARTITIONED VIEW
Create [OR REPLACE] view <view name> AS select * from <table name> UNION
ALL select * from <table name>;
6. VIEWS WITH READ ONLY
Create [OR REPLACE] view <view name> AS select * from <table name> with read
only;
7. VIEWS WITH CHECK OPTIONS
Create [OR REPLACE] view <view name> AS select * from <table name> with
check option;
OPEATIONS TO BE PERFORMED
17
1. Create a table t1 with attributes iname, icode and cost then insert values to
it.
2. Create a table t2 with attributes icode, iname, cname, qty and qdelivery then
insert values.
3. Create a row subset view selecting the details of products that are not
delivered.
4. Create a column subset view selecting icode and quantity alone from t2.
5. Create a row column subset view selecting the itemcodes of delivered
products.
6. Create a grouped view showing the total number of orders placed for each
product.
7. Create a partitioned view selecting item name, cost from t1 and customer
name, and quantity from t2.
8. Create a read only view from t1 selecting icode and cost. Show whether the
values can be inserted or updated in the view.
9. Create a check option view from t2 showing only the delivered products list.
Show whether insertion of an undelivered product can be made on this view.
10. Drop the views created.
DATABASE DESCRIPTION:
Table Name: T1
Table Name: T2
SYNTAX:
CURSOR DECLARATION:
CURSOR <CURSOR NAME> IS SELECT colname1, colname2 from
<tablename> where <condition>;
OPEN:
OPEN <CURSOR NAME>;
FETCH:
FETCH <CURSOR NAME> into variable name1, variable name2…..;
CLOSE:
CLOSE <CURSOR NAME>;
OPERATIONS TO BE PERFORMED:
1. Simulate the banking transaction using ‘CURSORS’.
2. Update the salary of all employees in employee database by 500 using CURSOR FOR-
LOOP.
DATA BASE DESCRIPTION:
TABLE NAME: edetails
19
iv. goto
SYNTAX
i. SIMPLE IF
IF <Condition> THEN
Statements;
ENDIF;
ii. ELSE IF
IF <Condition> THEN
Statements;
ELSE
Statements;
ENDIF;
iii. ELSE IF LADDER
IF <Condition> THEN
Statements;
ELSE IF
Statements;
.
.
.
ELSE
Statements;
ENDIF;
ENDIF;
iv. GOTO
GOTO Label
<<Label>>
Statements;
OPERATIONS TO BE PERFORMED:
i. In inventory table, check using ‘SIMPLE-IF’ structure whether QOH is > 0
decrement the QOH by 1.
20
ii. Using employee table check using ‘ELSE – IF’ whether the salary is < 5000 or not
if so increment the basic pay by 5% else by 10% of basic pay.
iii. Given the service no of person, calculate the electricity bill to be paid that
person based on the conditions given below
Consumption Unit Charge per Unit
0 – 100 80 paise / unit
100 – 250 1 Re / unit
250 – 500 Rs. 230 + 120 paise / unit
> 500 Rs. 400 + 150 paise / unit
iv. Using ‘GOTO’ checks whether ‘QOH’ in Item table is < 25 if so add the reorder
level to QOH else perform No Updation.
DATABASE DESCRIPTION:
Table Name: T11
FORMAT:
DECLARE
Local variable declaration
BEGIN
Stmts;
Raise <exception name>;
When <exception> then
Stmts;
END;
OPERATIONS TO BE PERFORMED:
1. Create a user defined exception in simulation of banking system, that no
transactions to be performed on Saturday and Sunday.
2. Create a user defined exception in inventory database. Raise “stock is not available”
exception if QOH=0.
PRE-DEFINED EXCEPTION:
1. Too few row exception.
2. No-data found exception.
3. Value error exception.
4. Divide by zero error exception.
DATABASE DESCRIPTION:
TABLE NAME: inventory
TABLE NAME: t2
22
Field Name Data Type Width
Name Char 30
Code Number 5
QOH Number 5
Reorder Number 5
23