DBMS Manual
DBMS Manual
Objectives:
The major objective of this lab is to provide a strong formal foundation in database concepts,
technology and practice to the participants to groom them into well-informed database
application developers.
The sub-objectives are:
to give a good formal foundation on the relational model of data
to present SQL and procedural interfaces to SQL comprehensively
to give an introduction to systematic database design approaches covering
conceptual design, logical design and an overview of physical design
to present the concepts and techniques relating to query processing by SQL engines
to introduce the concepts of transactions and transaction processing
Learning Outcomes:
After this lab, the student should be able to:
Understand, appreciate and effectively explain the underlying concepts of database
technologies
Design and implement a database schema for a given problem-domain
Create small scale database
Populate and query a database using SQL DML/DDL commands.
Declare and enforce integrity constraints on a database using a state-of-the-art RDBMS
Programming PL/SQL including stored procedures, stored functions, cursors, packages.
Create triggers
1 Creation, altering and droping of tables and inserting rows into a table (use
constraints while creating tables) examples using SELECT command.
2 Queries (along with sub Queries) using ANY, ALL, IN, EXISTS,
NOTEXISTS, UNION,
INTERSET, Constraints
3 Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN),
GROUP BY, HAVING
(Concatenation, lpad, rpad, ltrim, rtrim, lower, upper, initcap, length, substr and
instr), date
exception –Handling section (Ex. Student no can be selected from the table and
printed and an exception can be raised if no records were found)
ii)Insert data into student table and use COMMIT, ROLLBACK and
SAVEPOINT in PL/SQL
block.
9
Program using declaration, execution, exceptions. (when no_data_found)
10
Program using FUNCTIONS to check sid values of sailors and to find factorial
value.
Develop Programs using BEFORE and AFTER Triggers, Row and Statement
Triggers and
INSTEAD OF Triggers
12 Develop programs using REPORTS to display the sailor id, name, rating and
boat color, name in sailor and boat tables
INTRODUCTION
DATA:
Collection of facts or raw material is known as Data
DATABASE:
Collection of data that describes activities of one or more related organization from the
data is called ‘DATABASE’.
DATABASE MANAGEMENT SYSTEM(DBMS):
Collection of data and set of programs which are used to access the data in an efficient
manner is known as ‘DBMS’.
INTRODUCTION TO SQL:
SQL stands for Structured Query Language and is commonly pronounced as ‘SEQUEL’. It is
the interface between user and the oracle database. The American National Standard Institute has
accepted SQL as the standard access language for relational database management system and
all of ORACLE access tools are based on this standard.
SQL is the set of statements with which all the programs and users access data in an oracle
database. The purpose of SQL is to provide an interface to a relational database such as oracle
and all SQL statements. It has been a common language for communication with oracle server
from any tool or application.
COMPONENTS OF SQL
1. DATA DEFINITION LANGUAGE: These commands are used to define the database
structure or schema
Create: It is used to create oracle database objects.
2. DATA MANIPULATION LANGUAGE: These commands are used for managing data
within schema
objects.
Insert: It is used to insert new data into the existing table.
Syntax: insert into <table name> values(<exp1>,<exp2>,----------------<expn>);
Delete: It is used to delete records from the table.
Syntax: delete from <table name> [where <condition>];
Update: It is used to modify values in the table.
Syntax: update <table name> set <col name> =<exp> where <condition>;
Select: It is used to access records from the database.
Syntax: Select <selection list> from <table name> where <condition>;
3. DATA CONTROL LANGUAGE : DCL commands are used to configure and control
database objects.
GRANT : SQL GRANT is a command used to provide access or privileges on
the database objects to the users.
Syntax : GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
REVOKE : The REVOKE command removes user access rights or privileges to
the database objects.
E-R MODEL
ATTRIBUTES is the properties of entity i.e sno, sname. .etc. and it is represented by
EXERCISE-1
Creation, altering and dropping of tables and inserting rows into a table (use constraints
while creating tables) examples using SELECT command.
NOT NULL: this is to restrict null values in specified column. Null is not a zero or a space.
UNIQUE:restricts the duplicate values (rows)in the coplumn and allows the blanks in the
column.
PRIMARY KEY: It uniquely identifies each row in the coloumn but it does not allows the
blanks in the column.it is the combination of NOT NULL and UNIQUE.
CREATION: create command is used to create any oracle database objects like
tables,views,users etc.
Syntax
city char(15),
pincode number(6),
state char(15),
bal_due number(5));
Table created
desc <tablename>
desc client_master;
CLIENT_M CLIENT
Varchar2 25 - - 1 - - -
ASTER NO
NAME Varchar2 20 - - - - -
CITY Char 15 - - - - -
PINCOD
Number - 6 0 - - -
E
STATE Char 15 - - - - -
BAL_Du Number - 5 0 - - -
1 row inserted
c00003 kumar It
c00004 mahesh It
Altering of tables
Syntax:
add(newcolumn datatype(size));
Syntax
drop(column name);
Syntax
modify(columnname datatype(size));
Table dropped
1 row updated
1 row updated
NAME CITY
ivan bombay
vandana Tamilnadu
basu hyderabad
pramada bombay
ravi Delhi
rukhmini bombay
NAME
ivan
pramada
rukhmini
update client_master
update client_master
5)Delete from client master where the column name state holds the value tamilnadu
6)Add a column called telephone of data type number and size=10 to the client master table
add(phno number(10));
NUMBE
R NAME CITY PIN STATE BAL phno
EXERCISE-2
Queries (along with sub Queries) using ANY, ALL, IN, EXISTS,NOT EXISTS,
UNION,INTERSECT keywords,constrints.
Creation of sailors
name varchar(10),
rating number(10),
age number(6,3));
table created
22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
95 Bob 3 63.5
85 Art 3 25.5
71 Zorba 10 16
74 Horatio 9 35
bname varchar(10),
color varchar(10));
table created
Foreign key is used to relate the two or more than two tables.
sid number(10),
bid number(10),
day date,
22 101 10/10/98
74 103 9/8/98
64 102 9/8/98
64 101 9/5/98
31 104 11/12/98
31 103 11/6/98
31 102 11/10/98
22 104 10/7/98
22 103 10/8/98
22 102 10/10/98
Queries
1)Find the names of sailors who have reserved the boat number 103
NAME
Dustin
Lubber
select s.name from sailors s,reserves r,boatss b where s.sid=r.sid AND r.bid=b.bid AND
b.color='red';
NAME
Dustin
Lubber
Horatio
select b.color from sailors s,reserves r,boats b where s.sid=r.sid AND r.bid=B.bid AND
s.name='lubber';
COLOR
Green
Red
Red
22 Dustin 7 45
USING IN,NOT IN,EXISTS,ANY
29 Brutus 1 33
5) Find the names of sailors who have
31 Lubber 8 55.5
reserved boat number 103 using IN operator.
32 Andy 8 25.5
select s.sname from sailors s where s.sid
in(select r.sid from 58 Rusty 10 35 reserves r where
r.bid=103); 64 Horatio 7 35
95 Bob 3 63.5
85 Art 3
SNAME 25.5
Dustin
74 Horatio Lubber
9 35
Horatio
6) Find the names of sailors who have reserved boat number 103 using EXISTS operator.
select s.sname from sailors s where exists(select * from reserves r where r.bid=103 and
s.sid=r.sid);
SNAME
Dustin
Lubber
Horatio
7) Find the names of sailors who have not reserved boat number 103 using NOT
EXISTSoperator.
select s.name from sailors s where not exists(select * from reserves r where r.bid=103 AND s.sid=r.sid);
NAME
Zorba
Art
Horatio
Rusty
Andy
Horatio
Brutes
Bob
8)Find the names of sailors who have not reserved a red boat using NOT IN operator
select s.name from sailors s where s.sid NOT IN(select r.sid from reserves r where r.bid
IN(select b.bid from boatss b where b.color='red'));
NAME
Rusty
Brutes
Andy
Zorba
Horatio
Art
Bob
9)Find the names of sailors who have reserved a red or green boat.
select s.name from sailors s,reserves r,boatss b where s.sid=r.sid AND r.bid=b.bid
AND(b.color='red' OR b.color='green');
NAME
Dustin
Lubber
Horatio
10)Find the names of sailors who have reserved a red or a green boat using union operator.
select s.name from sailors s,reserves r,boatss b where s.sid=r.sid AND r.bid=b.bid AND
b.color='red'
UNION
select s1.name from sailors s1,reserves r1,boats b1 where s1.sid=r1.sid AND r1.bid=b1.bid AND
b1.color='green';
NAME
Dustin
Lubber
Horatio
11)Find the sailors whose rating is better than some sailor called horatio using ANY operator.
select s.sid from sailors s where rating>any(select s.rating from sailors s where
s.name='horatio');
SID
58
71
74
31
32
12)Find the sailors with the highest rating using all operator.
select s.sid from sailors s where rating>=all(select s.rating from sailors s);
SID
58
71
13)Find the names of sailors who have reserved a both red and green boat using intersect
operator.
select s.name from sailors s,reserves r,boatss b where s.sid=r.sid AND r.bid=b.bid AND
b.color='red'
intersect
select s1.name from sailors s1,reserves r1,boats b1 where s1.sid=r1.sid AND r1.bid=b1.bid AND
b1.color='green';
SNAME
Lubber
Lubber
Dustin
Dustin
EXERCISE-3
Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN), GROUP BY,
HAVING
AVG(AGE)
36.9
SUM(AGE)
51
MAX(AGE)
63.5
MIN(AGE)
16
COUNT(DISTINCTS.SNAME)
COUNT(DISTINCTS.SNAME)
10
7)find the age of the youngest sailor for each rating level.
RATING MIN(S.AGE)
1 33
8 25.5
7 35
3 25.5
10 16
9 35
8) find the age of youngest sailor who is eligible to vote(18 years age)for each rating level with
atleast 2 sailors.
RATING MIN(S.AGE)
3 25.5
7 35
8 25.5
View is a virtual table which is created from a base table. They allows user to see only the part of
data but not all data. These are dynamic in nature. They donot accupy any memoryAny
changes/modifications made to the views are reflected back to the original table and vice versa.
CREATION
Syntax
ex:
View created
view updated
Destroying of view:
syntax
View dropped.
Select * from viewclient
EXERCISE- 4
String functions(Concatenation, lpad, rpad, ltrim, rtrim, lower, upper, initcap, length,
substr and instr) Date functions (Sysdate, next_day, add_months,last_day,
months_between , to_char, to_date)
These are performed from a table called Dual easily. Because dual is a dummy table. We cannot
create it separately. We can also perform these functions from any user created table.
DUMMY
Date functions:
SYSDATE
07-MAR-10
Add_months: it returns date after adding a specified date with specified number of months
Syntax: add_months(d,n).
ADD_MONTHS(SYSDATE,3)
07-JUN-10
LAST_DAY(SYSDATE)
31-MAR-10
Syntax: months_between(d1,d2)
D1=date1,d2=date2
MONTHS_BETWEEN('01-APR-10','01-JAN-10')
Next_day: used to find the immediate next day of the specified date
Syntax: next_day(d,day)
NEXT_DAY(SYSDATE,'SUNDAY')
14-MAR-10
String Functions
CONCAT('CSE','IT')
cseit
LOWER('CSE')
Cse
UPPER('CSE')
CSE
Syntax: ltrim(‘string’,’substring’)
LTRIM('ABCDEFG','ABC')
Defg
Syntax: rtrim(‘string’,’substring’)
RTRIM('ABCDEFG','EFG')
Abcd
Syntax: translate(char,from,to)
TRANSLATE('MANA','M','G')
Viva
Syntax: substr(string,startingvalue,length)
SUBSTR('GANAS','1','4')
Gana
Numeric Functions:
they accept numeric input and return numeric values as the output.
Abs(n):
ABS(-17)
17
Ceil(n):
CEIL(44.78)
45
Exp(n):
EXP(0)
Power(m,n):
POWER(2,0)
Mod(m,n):
MOD(10,2)
Sqrt(n):
SQRT(4)
Round(m,n):
ROUND(99.99,1)
100
Trunc(m,n):
TRUNC(99.99,1)
99.9
Conversion Functions:
TO_CHAR(SYSDATE,'DDTH"OF"MONTHYYYY')
Syntax: to_date(char,[fmt])
TO_DATE('01-JANUARY-2010','DD-MON-YY')
01-JAN-10
What is PL/SQL?
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
Advantages of PL/SQL.
Block Structures: PL SQL consists of blocks of code, which can be nested within each
other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can be
stored in the database and reused.
Procedural Language Capability: PL SQL consists of procedural language constructs
such as conditional statements (if else statements) and loops like (FOR loops).
Error Handling: PL/SQL handles errors or exceptions effectively during the execution
of a PL/SQL program.
EXERCISE-5
1 Raju 2500
2 John 3200
3 Raghu 2500
4 Ganesh 4500
5 Siva 2500
a)program which includes declaration, execution and exception section (Ex:select salaery
from emp where name is raju and excepton can be raised if no_data_found)
declare
newsal number(10);
begin
dbms_output.put_line('salary' || newsal);
exception
end
b)update data in emp table and use ROLLBACK, SAVE POINT, COMMIT in pl/sql block
declare
total_sal number(10);
begin
savepoint emp_sal;
if(total_sal>90000) then
end if;
commit;
end;
before this program create a table called emp with columns empno, ename and salary
1 Raju 2500
2 John 3200
3 Raghu 2500
4 Ganesh 4500
5 siva 2500
Then output table can be seen after executing the above code
1 Raju 7500
2 John 10200
3 Raghu 2500
4 Ganesh 4500
5 Gana 2500
EXERCISE-6
declare
a number(3):=&a;
b number(3):=&b;
c number(3):=&c;
begin
if (a>b and a>c)then
dbms_output.put_line('A is Big'||a);
elsif (b>c)then
dbms_output.put_line('B is Big'||b);
else
dbms_output.put_line('C is Big'||c);
end if;
end;
out put
-------
old 2: a number(3):=&a;
new 2: a number(3):=3;
old 3: b number(3):=&b;
new 3: b number(3):=4;
old 4: c number(3):=&c;
new 4: c number(3):=5;
C is Big5
PL/SQL procedure successfully completed.
b)PL/SQL Block for calculating area of the Circle for given radius
declare
pi constant number(5,2):=3.14;
r number(5):=&r;
a number(7,2);
begin
a:=pi*r*r;
dbms_output.put_line('Area of Circle is'||a);
end;
old 3: r number(5):=&r;
new 3: r number(5):=4;
Area of Circle is50.24
PL/SQL procedure successfully completed.
EXERCISE-7
a)PL/SQL Program to check whether a given number is prime or not.
Output
------
old 3: a number(3):=&a;
new 3: a number(3):=7;
given number is prime
PL/SQL procedure successfully completed.
/*PL/SQL Block to interpret Pre-defined Exception*/
output:
-------
old 6: select accno,cname,balance into num,c_name,c_bal from banks where accno=&no;
new 6: select accno,cname,balance into num,c_name,c_bal from banks where accno=501;
output:
-------
old 6: select accno,cname,balance into num,c_name,c_bal from banks where accno=&no;
new 6: select accno,cname,balance into num,c_name,c_bal from banks where accno=444;
EXERCISE-8
A procedure is a module performing one or more actions. It does not need to return any values.
Parameters in Procedure:
1) IN type parameter: These types of parameters are used to send values to stored
procedures.
2) OUT type parameter: These types of parameters are used to get values from stored
procedures. This is similar to a return type in functions.
3) IN OUT parameter: These types of parameters are used to send values and get values
from stored procedures.
begin
i:=i+1;
j:=j-1;
end;
calling program
declare
a number:=&a;
b number:=&b;
begin
incr_dec(a,b);
end;
b)Write PL/SQL Block using procedures to display the bonus given to employee based on
designation and salary
calling program.
declare
id number(10);
begin
id:=&id;
bonus(id);
end;
output:
------
old 4: id:=&id;
new 4: id:=105;
Employee Bonus:2000
PL/SQL procedure successfully completed.
EXERCISE-9
FUNCTIONS
Functions are another type of stored code and are very similar to procedures. The
Significant difference between is that function is a pl/sql block which returns a single
Value. Functions accept one, many, or no parameters. But a function must have a return clause in
the executable section of the function.
f number:=1;
i number:=1;
begin
while(i<=a)
loop
f:=f*i;
i:=i+1;
end loop;
return f;
end;
OUTPUT:
Function created
FACTORIAL
24
x varchar2(10);
begin
return 'EXIST’;
exception
return 'NOTEXISTS';
end;
OUTPUT:
Function created.
STATUS
-------------
EXIST
STATUS
-------------
NOTEXISTS
EXERCISE-10
Package
Write a PL/SQL Block using packages to execute both function and procedure*
create or replace package pack as
function mul(a number,b number) return number ;
procedure incr_dec(i in out number,j in out number);
end pack;
calling program
declare
a number:=&a;
b number:=&b;
c number;
begin
c:=pack.mul(a,b);
dbms_output.put_line('Value after Multiplication'||c);
pack.incr_dec(a,b);
dbms_output.put_line('Increment Value'||a||'Decrement Value'||b);
end;
output:
Value after Multiplication72
Increment Value10Decrement Value7
PL/SQL procedure successfully completed.
EXERCISE-11
CURSORS
A cursor is a temporary work area created in the system memory when a SQL statement is
executed. A cursor contains information on a select statement and the rows of data accessed by
it. This temporary work area is used to store the data retrieved from the database, and manipulate
this data. A cursor can hold more than one row, The set of rows the cursor holds is called the
active set.
Write a PL/SQL Block using Cursor to display the Employee Names and Employee
Salaries from Employee table
end;
output:
Employee Name:raju
Employee Salary:24000
Employee Name:john
Employee Salary:21000
Employee Name:kumar
Employee Salary:12000
Employee Name:krishna
Employee Salary:10000
Employee Name:joe
Employee Salary:8000
PL/SQL procedure successfully completed.
EXERCISE-12
TRIGGERS
Specified changes to the database, and is typically specified by the DBA. A Trigger
Trigger created
OUTPUT:
Trigger created