it6312_dbms_manual
it6312_dbms_manual
com
IT6312
Data Base
Management
System Lab
Manual
www.rejinpaul.com
www.rejinpaul.com
1. Creation of a database and writing SQL queries to retrieve information from the database.
2. Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records based on
conditions.
3. Creation of Views, Synonyms, Sequence, Indexes, Save point.
4. Creating an Employee database to set various constraints.
5. Creating relationship between the databases.
6. Study of PL/SQL block.
7. Write a PL/SQL block to satisfy some conditions by accepting input from the user.
8. Write a PL/SQL block that handles all types of exceptions.
9. Creation of Procedures.
10. Creation of database triggers and functions
11. Mini project (Application Development using Oracle/ Mysql )
a) Inventory Control System.
b) Material Requirement Processing.
c) Hospital Management System.
d) Railway Reservation System.
e) Personal Information System.
f) Web Based User Identification System.
g) Timetable Management System.
h) Hotel Management System
www.rejinpaul.com
www.rejinpaul.com
AIM
To create a database and to retrieve the information from the database using SQL queries.
COMMANDS
SQL> create table stud (sname varchar2(30), sid varchar2(10), sage number(2), sarea varchar2(20));
Table created.
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(2)
SAREA VARCHAR2(20)
Table altered.
Table altered.
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
www.rejinpaul.com
ww.rejinpaul.com
SDEPT VARCHAR2(20)
Table altered.
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
Table truncated.
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
SDEPT VARCHAR2(20)
Table dropped.
RESULT
Thus the creation of database and the SQL queries to retrieve information from the database has
been implemented and the output was verified.
www.rejinpaul.com
www.rejinpaul.com
A. AIM
To study the various categories of DML commands such as logical operations, aggregate functions,
string functions, numeric functions, date functions, conversion functions, group functions and set
operations.
DESCRIPTION
THE ORACLE TABLE – DUAL - Dual is a small oracle table which consists of only one row
and one column and contains the value X in that column.
INSERT - This command is used to insert values into the table.
SELECT - This command is used to display the contents of the table or those of a
particular column.
RENAME - This command renames the name of the table.
ARITHMETIC OPERATIONS - Various operations such as addition, multiplication,
subtraction and division can be performed using the numbers available in the table.
DISTINCT - This keyword is used along with select keyword to display unique values from
the specified column. It avoids duplicates during display.
ORDER BY CLAUSE - The order by clause arranges the contents of the table in
ascending order (by default) or in descending order (if specified explicitly) according to the
specified column.
CONCATENATION OPERATOR - This combines information from two or more columns in a
sentence according to the format specified.
LOGICAL OPERATORS
AND : The oracle engine will process all rows in a table and displays the result only when all of
the conditions specified using the AND operator are specified.
OR : The oracle engine will process all rows in a table and displays the result only when any of
the conditions specified using the OR operators are satisfied.
NOT : The oracle engine will process all rows in a table and displays the result only when none
of the conditions specified using the NOT operator are specified.
BETWEEN : In order to select data that is within a range of values, the between operator is used.
(AND should be included)
PATTERN MATCH
LIKE PREDICATE : The use of like predicate is that it allows the comparison of one string value
with another string value, which is not identical. This is achieved by using wildcard characters
which are % and _. The purpose of % is that it matches any string and _ matches any single
character.
IN AND NOT IN PREDICATE : The arithmetic operator = compares a single value to another
single value. In case a value needs to be compared to a list of values then the in predicate is used.
The not in predicate is the opposite of the in predicate. This will select all the rows whose values
do not match all of the values in the list.
www.rejinpaul.com
www.rejinpaul.com
NUMERIC FUNCTIONS
STRING FUNCTIONS
AGGREGATE FUNCTIONS
CONVERSION FUCTIONS
TO_NUMBER(CHAR): It converts the char value containing a number to a value of number data
type.
TO_CHAR(N,FMT): It converts a value of number data type to a value of char data type, using
the optional format string. It accepts a number n and a numeric format fmt in which the number
has to appear. If fmt is omitted, n is converted to a char value exactly long enough to hold
significant digits.
TO_CHAR(DATE, FMT): It converts a value of data type to char value. It accepts a date as well
as the format in which the date has to appear. Fmt must be a date format. If fmt is omitted, date is
the default date format.
www.rejinpaul.com
www.rejinpaul.com
DATE FUNCTIONS
SYSDATE : The sysdate is a pseudo column that contains the current date and time. It requires
no arguments when selected from the table dual and returns the current date.
ADD_MONTHS(D,N): It returns date after adding the number of months specified with the
function.
LAST_DAY(D): It returns the last date of the month specified with the function
MONTHS_BETWEEN(D1,D2): It returns number of months between D1 and D2.
NEXT_DAY(DATE, CHAR): It returns the date of the first week day named by char . char must
be a day of the week.
GROUP BY CLAUSE
The group by clause is another section of the select statement. This optional class tells oracle to group
rows based on distinct values that exists for specified columns.
HAVING CLAUSE
The having clause can be used in conjunction with the group by clause. Having imposes a condition
on the group by clause, which further filters the groups created by the group by clause.
SET OPERATIONS
UNION CLAUSE: Multiple queries can be put together and their output combined using the
union clause. The union clause merges the output of two or more queries into a single set of rows
and columns.
INTERSECT CLAUSE: Multiple queries can be put together and their output can be combined
using the intersect clause. The intersect clause outputs only rows produced by both the queries
intersected. The output in an intersect clause will include only those rows that are retrieved by
both the queries.
COMMANDS
CREATION OF TABLE
SQL>create table stud (sname varchar2(30), sid varchar2(10), sage number(10), sarea varchar2(20), sdept
varchar2(20));
Table created.
1 row created.
1 row created.
www.rejinpaul.com
www.rejinpaul.com
1 row created.
1 row created.
Table renamed.
ARITHMETIC OPERATION
SNAME stid
------------------------------ ---------
ashwin 201
bhavesh 202
pruthvik 203
charith 204
CONCATENATION OPERATOR
SQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs;
PROFESSION
-------------------------------------------------------------------
ashwin is a aeronautical engineer.
bhavesh is a marine engineer.
www.rejinpaul.com
www.rejinpaul.com
charith is a mechanical engineer.
SAREA
--------------------
anna nagar
kilpauk
nungambakkam
SNAME SAGE
------------------------------ ---------
ashwin 19
bhavesh 18
BETWEEN OPERATOR
SQL> select sname,sarea, sid from studs where sid between 102 and 104;
IN PREDICATE
PATTERN MATCHING
SQL> select sname, sarea from studs where sarea like '%g%';
www.rejinpaul.com
www.rejinpaul.com
SNAME SAREA
------------------------------ --------------------
bhavesh nungambakkam
SQL> select sname ,sid from studs where sid>102 and sarea='anna nagar';
SNAME SID
------------------------------ ----------
pruthvik 103
LOGICAL OR OPERATOR
SQL> select sname ,sid from studs where sid>102 or sarea='anna nagar';
SNAME SID
------------------------------ ----------
ashwin 101
pruthvik 103
charith 104
NOT IN PREDICATE
SQL> select sname, sid from studs where sid not in(102,104);
SNAME SID
------------------------------ ----------
ashwin 101
pruthvik 103
Table altered.
www.rejinpaul.com
www.rejinpaul.com
1 row updated.
1 row updated.
1 row updated.
1 row updated.
SPOCKET
--------------------
750
500
250
100
AGGREGATE FUNCTIONS
RESULT
---------
400
RESULT
www.rejinpaul.com
www.rejinpaul.com
--------------------
100
RESULT
---------
RESULT
---------
RESULT
---------
RESULT
--------------------
750
RESULT
---------
1600
NUMERIC FUNCTIONS
RESULT
---------
20
RESULT
www.rejinpaul.com
www.rejinpaul.com
---------
1024
RESULT
---------
15.36
RESULT
---------
STRING FUNCTIONS
RESULT
------
oracle
RESULT
------
ORACLE
RESULT
------
Oracle
RESULT
-----
racle
RESULT
www.rejinpaul.com
www.rejinpaul.com
----------
####oracle
RESULT
----------
oracle^^^^
CONVERSION FUNCTIONS
4 rows updated.
RESULT
--------
017,145
RESULT
-----------
16-jul-2008
DATE FUNCTIONS
SYSDATE
---------
16-JUL-08
www.rejinpaul.com
www.rejinpaul.com
SQL> select sysdate,add_months(sysdate,4) result from dual;
SYSDATE RESULT
--------- ---------
16-JUL-08 16-NOV-08
SYSDATE RESULT
--------- ---------
16-JUL-08 31-JUL-08
SYSDATE RESULT
--------- ---------
16-JUL-08 20-JUL-08
RESULT
---------
16.935484
GROUP BY CLAUSE
SAREA RESULT
-------------------- ------------
nungambakkam 500
kilpauk 100
HAVING CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea having spocket<600;
www.rejinpaul.com
www.rejinpaul.com
SAREA RESULT
-------------------- ------------
nungambakkam 500
kilpauk 100
DELETION
1 row deleted.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL>
www.rejinpaul.com
www.rejinpaul.com
SQL> insert into product values('tvstand',11010);
1 row created.
PRODNAME PRODNO
------------------------------ ----------
table 10001
chair 10010
desk 10110
cot 11110
sofa 10010
tvstand 11010
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
www.rejinpaul.com
www.rejinpaul.com
desk 809 10110
SET OPERATIONS
SQL> select prodname from product where prodno=10010 union select prodname from sale where
prodno=10010;
PRODNAME
------------------------------
chair
sofa
SQL> select prodname from product where prodno=11110 intersect select prodname from sale where
prodno=11110;
PRODNAME
------------------------------
cot
RESULT
The DML commands were executed and the output was verified.
B. AIM
Table created.
1 row created.
1 row created.
www.rejinpaul.com
www.rejinpaul.com
1 row created.
1 row created.
SNAME PLACE
-------------------- --------------------
prajan chennai
anand chennai
kumar chennai
ravi Chennai
SQL> create table sstud2 ( sname varchar2(20), dept varchar2(10), marks number(10));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
SNAME
www.rejinpaul.com
www.rejinpaul.com
--------------------
anand
prajan
ravi
SQL> select sname from sstud1 where sstud1.sname not in ( select sstud2.sname from sstud2 );
SNAME
--------------------
kumar
SQL> select sname from sstud2 where marks > some(select marks from sstud2 where dept='cse');
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= some (select marks from sstud2 where dept='cse' );
SNAME
--------------------
prajan
vasu
SQL> select sname from sstud2 where marks > any ( select marks from sstud2 where dept='cse' );
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= any ( select marks from sstud2 where dept='cse' );
SNAME
--------------------
prajan
vasu
SQL> select sname from sstud2 where marks > all ( select marks from sstud2 where dept='cse' );
no rows selected
SQL> select sname from sstud2 where marks < all ( select marks from sstud2 where dept='cse' );
www.rejinpaul.com
www.rejinpaul.com
SNAME
--------------------
anand
ravi
SQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2 where
sstud1.sname=sstud2.sname );
SNAME
--------------------
prajan
anand
ravi
SQL> select sname from sstud1 where not exists ( select sstud2.sname from sstud2 where
sstud1.sname=sstud2.sname );
SNAME
--------------------
kumar
RESULT
The Nested Queries using DML commands were executed and the output was verified.
To create views, synonyms, sequences, indexes and save points using DDL, DML and DCL
statements
DESCRIPTION:
Views
TYPES OF VIEWS
www.rejinpaul.com
www.rejinpaul.com
SQL> create table fviews( name varchar2(20),no number(5), sal number(5), dno number(5));
Table created.
1 row created.
1 row created.
1 row created.
xxx 1 19000 11
aaa 2 19000 12
yyy 3 40000 13
Table created.
1 row created.
1 row created.
DNO DNAME
--------- --------------------
11 x
12 y
SQL> create view sview as select name,no,sal,dno from fviews where dno=11;
View created.
www.rejinpaul.com
www.rejinpaul.com
SQL> select * from sview;
xxx 1 19000 11
Updates made on the view are reflected only on the table when the structure of the table and the view are
not similar -- proof
1 row created.
xxx 1 19000 11
xxx 1 19000 11
aaa 2 19000 12
yyy 3 40000 13
zzz 4 20000 14
Updates made on the view are reflected on both the view and the table when the structure of the table and
the view are similar – proof
View created.
xxx 1 19000 11
aaa 2 19000 12
www.rejinpaul.com
www.rejinpaul.com
yyy 3 40000 13
zzz 4 20000 14
1 row created.
xxx 1 19000 11
bbb 5 30000 15
xxx 1 19000 11
aaa 2 19000 12
yyy 3 40000 13
zzz 4 20000 14
bbb 5 30000 15
SQL> create view ssview( cusname,id) as select name, no from fviews where dno=12;
View created.
CUSNAME ID
-------------------- ---------
aaa 2
View dropped.
TO CREATE A VIEW ‘COMBO’ USING BOTH THE TABLES ‘FVIEWS’ AND ‘DVIEWS’
www.rejinpaul.com
www.rejinpaul.com
SQL> create view combo as select name,no,sal,dviews.dno,dname from fviews,dviews where
fviews.dno=dviews.dno;
View created.
xxx 1 19000 11 x
aaa 2 19000 12 y
ERROR at line 1:
This shows that when a view is created from two different tables no manipulations can be performed
using that view and the above error is displayed.
Synonyms
A synonym is an alias, that is, a form of shorthand used to simplify the task of
referencing a database object.
There are two categories of synonyms, public and private.
A public synonym can be accessed by any system user.
The individual creating a public synonym does not own the synonym – rather, it will
belong to the PUBLIC user group that exists within Oracle.
Private synonyms, on the other hand, belong to the system user that creates them and
reside in that user's schema.
A system user can grant the privilege to use private synonyms that they own to other
system users.
In order to create synonyms, we will need to have the CREATE SYNONYM privilege.
This privilege will be granted to us by the DBA.
We must have the CREATE PUBLIC SYNONYM privilege in order to create public
synonyms.
If we own a synonym, we have the right to drop (delete) the synonym. The DROP
SYNONYM command is quite simple.
DROP SYNONYM synonym_name;
www.rejinpaul.com
www.rejinpaul.com
In order to drop a public synonym we must include the PUBLIC keyword in the DROP
SYNONYM command.
In order to drop a public synonym, we must have the DROP PUBLIC SYNONYM
privilege.
DROP PUBLIC SYNONYM synonym_name;
Examples:
SQL> select * from class;
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
7 rows selected.
Create synonym:
SQL> create synonym c1 for class;
Synonym created.
SQL> insert into c1 values('kalai',20);
1 row created.
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
8 rows selected.
www.rejinpaul.com
www.rejinpaul.com
SQL> select * from c1;
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
8 rows selected.
SQL> insert into class values('Manu',21);
1 row created.
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
Manu 21
9 rows selected.
Drop Synonym:
SQL> drop synonym c1;
Synonym dropped.
www.rejinpaul.com
www.rejinpaul.com
Sequences
Oracle provides the capability to generate sequences of unique numbers, and they are
called sequences.
Just like tables, views, indexes, and synonyms, a sequence is a type of database object.
Sequences are used to generate unique, sequential integer values that are used as primary
key values in database tables.
The sequence of numbers can be generated in either ascending or descending order.
Creation of table:
SQL> create table class(name varchar(10),id number(10));
Table created.
1 row created.
SQL> /
Enter value for name: brindha
Enter value for id: 02
old 1: insert into class values('&name',&id)
new 1: insert into class values('brindha',02)
1 row created.
SQL> /
Enter value for name: chinthiya
Enter value for id: 03
old 1: insert into class values('&name',&id)
new 1: insert into class values('chinthiya',03)
1 row created.
NAME ID
---------- ----------
anu 1
brindha 2
www.rejinpaul.com
www.rejinpaul.com
chinthiya 3
Create Sequence:
Sequence created.
1 row created.
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
Alter Sequence:
SQL> alter sequence s_1
2 increment by 2;
Sequence altered.
1 row created.
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
www.rejinpaul.com
www.rejinpaul.com
fairoz 7
Drop Sequence:
SQL> drop sequence s_1;
Sequence dropped.
Indexes
An index can be created in a table to find data more quickly and efficiently.
The users cannot see the indexes; they are just used to speed up searches/queries.
Updating a table with indexes takes more time than updating a table without; because the
indexes also need an update. So we should only create indexes on columns (and tables)
that will be frequently searched against.
Syntax:
Create Index:
CREATE INDEX index_name ON table_name (column_name)
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
www.rejinpaul.com
www.rejinpaul.com
HP 2 kovai
Lenovo 3 trichy
Index created.
Index created.
Drop Index:
SQL> drop index sp1;
Index dropped.
Index dropped.
DCL statements
DESCRIPTION
The DCL language is used for controlling the access to the table and hence securing the database.
DCL is used to provide certain privileges to a particular user. Privileges are rights to be allocated. The
privilege commands are namely,
Grant
Revoke
Commit
Savepoint
Rollback
GRANT COMMAND: It is used to create users and grant access to the database. It requires database
administrator (DBA) privilege, except that a user can change their password. A user can grant access
to their database objects to other users.
REVOKE COMMAND: Using this command , the DBA can revoke the granted database privileges
from the user.
SAVEPOINT: It is used to temporarily save a transaction so that you can rollback to that point
whenever necessary
ROLLBACK: It restores the database to last commited state. It is also use with savepoint command to
jump to a savepoint in a transaction
www.rejinpaul.com
www.rejinpaul.com
SYNTAX
GRANT COMMAND
REVOKE COMMAND
<database_priv> -- Specifies the system level priveleges to be granted to the users or roles. This
includes create / alter / delete any object of the system.
<object_priv> -- Specifies the actions such as alter / delete / insert / references / execute / select /
update for tables.
[ With Grant Option ] – Allows the recipient user to give further grants on the objects.
The priveleges can be granted to different users by specifying their names or to all users by using
the “Public” option.
COMMIT:
Commit;
SAVEPOINT:
Savepoint savapoint_name;
ROLLBACK:
Rollback to savepoint_name;
EXAMPLES
Grant succeeded.
SQL> Grant select , update , insert on departments to abcde with grant option;
www.rejinpaul.com
www.rejinpaul.com
Grant succeeded.
Revoke succeeded.
Revoke succeeded.
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
1 row created.
SQL> commit;
Commit complete.
1 row updated.
SQL> savepoint A;
Savepoint created.
1 row created.
SQL> savepoint B;
www.rejinpaul.com
www.rejinpaul.com
Savepoint created.
1 row created.
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
indu 11
janani 13
9 rows selected.
SQL> rollback to B;
Rollback complete.
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
indu 11
8 rows selected.
SQL> rollback to A;
Rollback complete.
www.rejinpaul.com
www.rejinpaul.com
NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
RESULT:
Thus the Views, Synonyms, and Sequences, indexes and save points has been executed
using DDL, DML and DCL statements.
SQL> create table empl (ename varchar2(30) not null, eid varchar2(20) not null);
Table created.
1 row created.
1 row created.
ERROR at line 1:
ENAME EID
------------------------------ --------------------
www.rejinpaul.com
www.rejinpaul.com
abcde 11
fghij 12
SQL> create table depts ( dname varchar2(30) not null, did number(20) not null check (did<10000));
Table created.
1 row created.
1 row created.
ERROR at line 1:
DNAME DID
------------------------------ ---------
sales 9876
marketing 5432
SQL> create table airports (aname varchar2(30) not null , aid number(20) not null, acity varchar2(30)
check( acity in ('chennai','hyderabad','bangalore')));
Table created.
1 row created.
1 row created.
www.rejinpaul.com
www.rejinpaul.com
1 row created.
ERROR at line 1:
SQL> create table book (bname varchar2(30) not null, bid number(20) not null unique);
Table created.
1 row created.
1 row created.
ERROR at line 1:
BNAME BID
------------------------------ ---------
www.rejinpaul.com
www.rejinpaul.com
fairy tales 1000
SQL> create table orders( oname varchar2(30) not null , oid number(20) not null , unique(oname,oid));
Table created.
1 row created.
1 row created.
1 row created.
ERROR at line 1:
ONAME OID
------------------------------ ---------
chair 2005
table 2006
chair 2007
SQL> create table custo ( cname varchar2(30) not null , cid number(20) not null primary key);
Table created.
1 row created.
1 row created.
www.rejinpaul.com
www.rejinpaul.com
SQL> insert into custo values ('ricky',506);
ERROR at line 1:
CNAME CID
------------------------------ ---------
jones 506
hayden 508
SQL> create table branches( bname varchar2(30) not null , bid number(20) not null , primary key(bnam
e,bid));
Table created.
1 row created.
1 row created.
1 row created.
ERROR at line 1:
BNAME BID
------------------------------ ---------
www.rejinpaul.com
www.rejinpaul.com
anna nagar 1005
adyar 1006
Table created.
1 row created.
1 row created.
Table created.
1 row created.
1 row created.
ENAME DNO
-------------------- ---------
x 11
y 22
ALTER TABLE
Table altered.
1 row updated.
www.rejinpaul.com
www.rejinpaul.com
1 row updated.
x 11 10 gandhi road
y 22 12 m.g. road
SQL> select city, ename from depts, s2emp where depts.dno = s2emp.dno;
CITY ENAME
-------------------- --------------------
chennai x
hyderabad y
RESULT
The various constraints were implemented and the tables were created using the respective
constraints.
AIM
To create databases and implement the relationship between databases using joinoperation.
DESCRIPTION:
JOIN OPERATIONS
INNER JOIN/ NATURAL JOIN/ JOIN: It is a binary operation that allows us to combine certain
selections and a Cartesian product into one operation.
OUTER JOIN: It is an extension of join operation to deal with missing information.
Left Outer Join: It takes tuples in the left relation that did not match with any tuple in the
right relation, pads the tuples with null values for all other attributes from the right
relation and adds them to the result of the natural join.
www.rejinpaul.com
www.rejinpaul.com
Right Outer Join: It takes tuples in the right relation that did not match with any tuple in
the left relation, pads the tuples with null values for all other attributes from the left
relation and adds them to the result of the natural join.
Full Outer Join: It combines tuples from both the left and the right relation and pads the
tuples with null values for the missing attributes and hem to the result of the natural join.
Table created.
1 row created.
1 row created.
1 row created.
10 inventory hyd
20 finance bglr
30 HR mumbai
er(10),dno number(10));
Table created.
1 row created.
1 row created.
www.rejinpaul.com
www.rejinpaul.com
1 row created.
1 row created.
1. Equijoin:
A join which contains an equal to ‘=’ operator in this joins condition
Using Clause:
SQL> select eno,ename,job,dname,loc from emp2 e join dept d using(dno);
On Clause:
SQL> select eno,ename,job,dname,loc from emp2 e join dept d on(e.dno=d.dno);
www.rejinpaul.com
www.rejinpaul.com
2. Non-Equijoin:
A join which contains an operator other than equal to ‘=’ in the join condition.
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno>d.dno;
3. Self Join:
Joining the table itself is called self join.
4. Natural Join:
It compares all the common columns.
5. Cross Join:
This will give the cross product.
www.rejinpaul.com
www.rejinpaul.com
333 jagan manager finance bglr
444 madhu engineer finance bglr
111 saketh analyst HR mumbai
222 sandeep clerk HR mumbai
333 jagan manager HR mumbai
12 rows selected.
6. Outer Join:
It gives the non matching records along with matching records.
6.1 Left Outer Join:
This will display the all matching records and the records which are in left hand side table those
that are in right hand side table.
SQL> select eno,ename,job,dname,loc from emp2 e left outer join dept d on(e.dno=
d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno(+);
SQL> select eno,ename,job,dname,loc from emp2 e right outer join dept d on(e.dno =d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno(+)=d.dno;
www.rejinpaul.com
www.rejinpaul.com
ENO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ----------
333 jagan manager inventory hyd
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
444 madhu engineer
HR Mumbai
RESULT
Thus the relationship between databases has been implemented using join operation.
AIM
DESCRIPTION
PL/SQL PROGRAMMING
As we want output of PL/SQL Program on screen, before Starting writing anything type (Only
Once per session)
SQL> SET SERVEROUTPUT ON
To write program, use Notepad through Oracle using ED command.
SQL> ED ProName
Type the program Save & Exit.
To Run the program
SQL> @ProName
www.rejinpaul.com
www.rejinpaul.com
LOOPING STATEMENTS:-
For executing the set of statements repeatedly we can use loops. The oracle supports number of looping
statements like GOTO, FOR, WHILE & LOOP.
Here is the syntax of these all the types of looping statements.
GOTO STATEMENTS
<<LABEL>>
SET OF STATEMENTS
GOTO LABEL;
FOR LOOP
FOR <VAR> IN [REVERSE] <INI_VALUE>..<END_VALUE>
SET OF STATEMENTS
END LOOP;
WHILE LOOP
WHILE (CONDITION) LOOP
SET OF STATEMENTS
END LOOP;
LOOP STATEMENT
LOOP
SET OF STATEMENTS
IF (CONDITION) THEN
EXIT
SET OF STATEMENTS
END LOOP;
While using LOOP statement, we have take care of EXIT condition, otherwise it may go into
infinite loop.
SQL> declare
2 a varchar2(20);
3 begin
4 a:='Hello';
5 dbms_output.put_line(a);
6 end;
www.rejinpaul.com
www.rejinpaul.com
7 /
Hello
2. Insert the record into Sailors table by reading the values from the Keyboard.
SQL> create table sailors(sid number(10),sname varchar(10),rating number(10),age
number(10));
Table created.
SQL> /
Enter value for sid: 03
old 2: sid number(10):=&sid;
new 2: sid number(10):=03;
Enter value for sname: vani
old 3: sname varchar(10):='&sname';
new 3: sname varchar(10):='vani';
Enter value for rating: 02
old 4: rating number(10):=&rating;
new 4: rating number(10):=02;
Enter value for age: 25
old 5: age number(10):=&age;
new 5: age number(10):=25;
www.rejinpaul.com
www.rejinpaul.com
PL/SQL procedure successfully completed.
RESULT
PROGRAMS
SQL> declare
2 a varchar2(20);
3 begin
4 a:=&a;
5 dbms_output.put_line(a);
6 end;
7 /
old 4: a:=&a;
new 4: a:=5;
www.rejinpaul.com
www.rejinpaul.com
GREATEST OF THREE NUMBERS
SQL> declare
2 a number(7);
3 b number(7);
4 c number(7);
5 begin
6 a:=&a;
7 b:=&b;
8 c:=&c;
13 else
15 end if;
16 end if;
17 end;
18 /
old 6: a:=&a;
new 6: a:=5;
old 7: b:=&b;
new 7: b:=7;
old 8: c:=&c;
new 8: c:=1;
www.rejinpaul.com
www.rejinpaul.com
The greatest of the three is 7
SQL> declare
2 a number:=1;
3 begin
4 loop
5 dbms_output.put_line (a);
6 a:=a+1;
8 end loop;
9 end;
10 /
SQL> declare
2 a number:=1;
3 begin
4 while(a<5)
5 loop
6 dbms_output.put_line (a);
7 a:=a+1;
www.rejinpaul.com
www.rejinpaul.com
8 end loop;
9 end;
10 /
SQL> declare
2 a number:=1;
3 begin
4 for a in 1..5
5 loop
6 dbms_output.put_line (a);
7 end loop;
8 end;
9 /
SQL> create table saccount ( accno number(5), name varchar2(20), bal number(10));
Table created.
www.rejinpaul.com
www.rejinpaul.com
1 row created.
1 row created.
1 mala 20000
2 kala 30000
SQL> declare
2 a_bal number(7);
3 a_no varchar2(20);
4 debit number(7):=2000;
5 minamt number(7):=500;
6 begin
7 a_no:=&a_no;
9 a_bal:= a_bal-debit;
12 end if;
13 end;
14
15 /
old 7: a_no:=&a_no;
new 7: a_no:=1;
www.rejinpaul.com
www.rejinpaul.com
ACCNO NAME BAL
1 mala 18000
2 kala 30000
SQL> create table sroutes ( rno number(5), origin varchar2(20), destination varchar2(20), fare numbe
Table created.
1 row created.
1 row created.
1 row created.
SQL> declare
5 begin
6 route:=&route;
7 select fare, distance into fares , dist from sroutes where rno=route;
www.rejinpaul.com
www.rejinpaul.com
8 if (dist < 250) then
13 dbms_output.put_line('Sorry');
14 end if;
15 end if;
16 end if;
17 end;
18 /
old 6: route:=&route;
new 6: route:=3;
RESULT
The various programs in PL/SQL were implemented and their output was verified.
www.rejinpaul.com
www.rejinpaul.com
Ex. No: 8 Write a PL/SQL block that handles all types of exceptions.
AIM:
DESCRIPTION:
PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL Block known as
exception Handling. Using Exception Handling we can test the code and avoid it from exiting
abruptly.
DECLARE
Declaration section
BEGIN
Exception section
EXCEPTION
WHEN ex_name1 THEN
-Error handling statements
WHEN ex_name2 THEN
-Error handling statements
WHEN Others THEN
-Error handling statements
END;
SQL> DECLARE
2 N INTEGER:=&N;
3 A EXCEPTION;
4 B EXCEPTION;
5 BEGIN
6 IF MOD(N,2)=0 THEN
7 RAISE A;
8 ELSE
9 RAISE B;
10 END IF;
11 EXCEPTION
12 WHEN A THEN
13 DBMS_OUTPUT.PUT_LINE('THE INPUT IS EVEN.....')
14 WHEN B THEN
15 DBMS_OUTPUT.PUT_LINE('THE INPUT IS ODD.....');
16 END;
www.rejinpaul.com
www.rejinpaul.com
17 /
Enter value for n: 20
old 2: N INTEGER:=&N;
new 2: N INTEGER:=20;
THE INPUT IS EVEN.....
SQL> /
Enter value for n: 21
old 2: N INTEGER:=&N;
new 2: N INTEGER:=21;
THE INPUT IS ODD.....
SQL> DECLARE
2 L_NUM1 NUMBER;
3 L_NUM2 NUMBER;
5 BEGIN
6 L_NUM1 := 10;
7 L_NUM2 := 0;
8 DBMS_OUTPUT.PUT_LINE('RESULT:'||L_NUM1/L_NUM2);
10 EXCEPTION
12 DBMS_OUTPUT.PUT_LINE(SQLCODE);
13 DBMS_OUTPUT.PUT_LINE(SQLERRM);
14
15 END;
16 /
-1476
www.rejinpaul.com
www.rejinpaul.com
ORA-01476: divisor is equal to zero
2 id number,
3 employee_type_id number,
4 external_id varchar2(30),
5 first_name varchar2(30),
6 middle_name varchar2(30),
7 last_name varchar2(30),
8 name varchar2(100),
9 birth_date date ,
10 gender_id number );
Table created.
SQL>
2 id number,
3 code varchar2(30),
4 description varchar2(80),
6 inactive_date date );
Table created.
SQL> insert into gender ( id, code, description ) values ( 1, 'F', 'Female' );
1 row created.
SQL> insert into gender ( id, code, description ) values ( 2, 'M', 'Male' );
1 row created.
www.rejinpaul.com
www.rejinpaul.com
SQL> insert into gender ( id, code, description ) values ( 3, 'U', 'Unknown' );
1 row created.
SQL> declare
3 d_birth_date employee1.birth_date%TYPE;
4 n_gender_id employee1.gender_id%TYPE;
6 n_id employee1.id%TYPE;
7 v_first_name employee1.first_name%TYPE;
8 v_last_name employee1.last_name%TYPE;
9 v_middle_name employee1.middle_name%TYPE;
10 v_name employee1.name%TYPE;
11
12 begin
13 v_first_name := 'JOHN';
14 v_middle_name := 'J.';
15 v_last_name := 'DOUGH';
18
19 begin
21 exception
24 end;
www.rejinpaul.com
www.rejinpaul.com
25
26 begin
27 select id
28 into n_id
29 from employee1
33
34 n_selected := sql%rowcount;
35 exception
37 n_selected := sql%rowcount;
41 end;
42
44 end;
45 /
0 row(s) selected.
RESULT
Thus the PL/SQL program that handles exception has been implemented and output was verified.
www.rejinpaul.com
www.rejinpaul.com
Ex. No: 9 Creation of Procedures.
AIM
DEFINITION
A procedure or function is a logically grouped set of SQL and PL/SQL statements that perform a
specific task. They are essentially sub-programs. Procedures and functions are made up of,
• Declarative part
• Executable part
ARGUMENT: It is the name of the argument to the procedure. Paranthesis can be omitted if no
arguments are present.
IN: Specifies that a value for the argument must be specified when calling the procedure ie. used to pass
values to a sub-program. This is the default parameter.
OUT: Specifies that the procedure passes a value for this argument back to it’s calling environment after
execution ie. used to return values to a caller of the sub-program.
INOUT: Specifies that a value for the argument must be specified when calling the procedure and that
procedure passes a value for this argument back to it’s calling environment after execution.
RETURN: It is the datatype of the function’s return value because every function must return a value, this
clause is required.
PROCEDURES – SYNTAX
variable declaration;
constant declaration;
begin
exception
www.rejinpaul.com
www.rejinpaul.com
CREATING THE TABLE ‘ITITEMS’ AND DISPLAYING THE CONTENTS
SQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4), prodid number(4));
Table created.
1 row created.
1 row created.
1 row created.
2 null_price exception;
3 begin
6 raise null_price;
7 else
9 end if;
10 exception
www.rejinpaul.com
www.rejinpaul.com
12 dbms_output.put_line('price is null');
13 end;
14 /
Procedure created.
2 begin
6 dbms_output.put_line('price is null');
7 end if;
8 end;
9 /
Procedure created.
www.rejinpaul.com
www.rejinpaul.com
PROCEDURE FOR ‘OUT’ PARAMETER – CREATION, EXECUTION
2 begin
4 if identity<1000 then
5 b:=100;
6 end if;
7 end;
8 /
Procedure created.
SQL> declare
2 a number;
3 b number;
4 begin
5 zzz(101,b);
7 end;
8 /
2 begin
3 a:=a+1;
4 end;
5 /
Procedure created.
www.rejinpaul.com
www.rejinpaul.com
SQL> declare
2 a number:=7;
3 begin
4 itit(a);
6 end;
7 /
RESULT
The PL/SQL programs were executed and their respective outputs were verified.
DEFINITION
TYPES OF TRIGGERS
www.rejinpaul.com
www.rejinpaul.com
• For each row: It specifies that the trigger fires once per row.
• For each statement: This is the default trigger that is invoked. It specifies that the trigger fires once
per statement.
• :new
• :old
These two variables retain the new and old values of the column updated in the database. The
values in these variables can be used in the database triggers for data manipulation
TRIGGERS - SYNTAX
begin
-------------------------
-------------------------
-------------------------
exception
end;
The package “raise_application_error” is used to issue the user defined error messages
TO CREATE A SIMPLE TRIGGER THAT DOES NOT ALLOW INSERT UPDATE AND
DELETE OPERATIONS ON THE TABLE
SQL> create trigger ittrigg before insert or update or delete on itempls for each row
2 begin
4 end;
6 /
www.rejinpaul.com
www.rejinpaul.com
Trigger created.
ERROR at line 1:
ERROR at line 1:
ERROR at line 1:
Trigger dropped.
TO CREATE A TRIGGER THAT RAISES AN USER DEFINED ERROR MESSAGE AND DOES
NOT ALLOW UPDATION AND INSERTION
SQL> create trigger ittriggs before insert or update of salary on itempls for each row
2 declare
www.rejinpaul.com
www.rejinpaul.com
3 triggsal itempls.salary%type;
4 begin
8 end if;
9 end;
10 /
Trigger created.
ERROR at line 1:
ERROR at line 1:
FUNCTIONS – SYNTAX
create or replace function <function name> (argument in datatype,……) return datatype {is,as}
variable declaration;
constant declaration;
begin
exception
end;
www.rejinpaul.com
www.rejinpaul.com
CREATE THE TABLE ‘ITTRAIN’ TO BE USED FOR FUNCTIONS
Table created.
1 row created.
1 row created.
TNO TFARE
--------- ------------
1001 550
1002 600
SQL> create table itempls (ename varchar2(10), eid number(5), salary number(10));
Table created.
1 row created.
1 row created.
1 row created.
xxx 11 10000
yyy 12 10500
zzz 13 15500
www.rejinpaul.com
www.rejinpaul.com
2 trainfunction ittrain.tfare % type;
3 begin
5 return(trainfunction);
6 end;
7 /
Function created.
SQL> declare
2 total number;
3 begin
4 total:=aaa (1001);
6 end;
7 /
2 fact number:=1;
3 b number;
4 begin
5 b:=a;
6 while b>0
7 loop
8 fact:=fact*b;
9 b:=b-1;
10 end loop;
11 return(fact);
www.rejinpaul.com
www.rejinpaul.com
12 end;
13 /
Function created.
SQL> declare
2 a number:=7;
3 f number(10);
4 begin
5 f:=itfact(a);
7 end;
8 /
RESULT
The triggers and functions were created, executed and their respective outputs were verified.
AIM
PROCEDURE:
Step1: create a new project in visual basic using the option file---> new project.
Step2: In the form use the front end tools in the toolbox like textbox, label,command button and create a
front end Design for the simple calculator.
Step3: Open the properties window for the tool sand select properties. Now the properties window is
opened.
Step4: Set properties for each tool in the form like caption, name, etc.
Step5: Double click each and every tool to open the project code window.
www.rejinpaul.com
www.rejinpaul.com
Step7: write the code for the simple operations in the calculator like
Step7: The code is Automatically compiled at the end of each line while pressing the Enter key.
Step7: now execute the code by click the F5 button in the keyboard or select
Run--->start.
Step8: after successfully executing the project create the executable file by
CODING:
Dim a, b, c, d As Integer
End Sub
End Sub
End Sub
End Sub
End Sub
www.rejinpaul.com
www.rejinpaul.com
Private Sub button5_Click()
End Sub
End Sub
End Sub
End Sub
End Sub
a = Val(display.Text)
display.Text = ""
d=1
End Sub
a = Val(display.Text)
display.Text = ""
d=2
www.rejinpaul.com
www.rejinpaul.com
End Sub
a = Val(display.Text)
display.Text = ""
d=3
End Sub
a = Val(display.Text)
display.Text = ""
d=4
End Sub
b = Val(display.Text)
If d = 1 Then
c=a+b
display.Text = c
ElseIf d = 2 Then
c=a-b
display.Text = c
ElseIf d = 3 Then
c=a*b
display.Text = c
ElseIf d = 4 Then
c=a/b
display.Text = c
End If
www.rejinpaul.com
www.rejinpaul.com
End Sub
a=0
b=0
c=0
display.Text = ""
End Sub
MSG = MsgBox("THANKS FOR USING FX990ES FROM NASA COPY RIGHTS RESERVED",
vbOKOnly, "BYE")
End
End Sub
End Sub
RESULT:
Thus the simple calculator created by using the front end tools was executed successfully.
AIM:
To create a simple menu driven calculator using Visual Basic menu design.
PROCEDURE:
Step 2: In the form window place two text boxes to get the values and their corresponding labels.
Step 3: Go to Tools Menu Editor or right click on the form and select Menu Editor.
Step 4: In the Menu Editor dialog box enter the values forcaption, name and shortcut.
Step 5: Select next to add a new menu item and delete to remove a menu item.
www.rejinpaul.com
www.rejinpaul.com
step 6: To add a sub menu select the right button in the Menu Editor dialog box and then add the sub
menus caption, name and shortcut.
Step 7: Use “-“ in the name field to add a separator line inside a menu.
Step 9: Double click the menu items in the form window to add the corresponding codes.
Step 10: Similarly the codes are given to the text boxes and labels.
Step 12: After the debugging and successful execution of the project save the project and make an
executable file(*.exe) of it using the File menu.
CODING:
Dim a, b, c As Single
Text1.Text = ""
Text2.Text = ""
Label5.Caption = ""
Label6.Caption = ""
a=0
b=0
c=0
End Sub
a = Val(Text1.Text)
b = Val(Text2.Text)
c=a/b
Label6.Caption = "DIVISION"
End Sub
www.rejinpaul.com
www.rejinpaul.com
a = Val(Text1.Text)
b = Val(Text2.Text)
c=a+b
Label6.Caption = "ADDITION"
End Sub
a = Val(Text1.Text)
b = Val(Text2.Text)
c=a*b
Label6.Caption = "MULTIPLICATION"
End Sub
a = Val(Text1.Text)
b = Val(Text2.Text)
c=a-b
Label6.Caption = "SUBTRACTION"
End Sub
msg = MsgBox("enter the values in the text boxes and select the necessary operation from the menu",
vbOKOnly, "Instructions")
End Sub
End
End Sub
www.rejinpaul.com
www.rejinpaul.com
RESULT:
Thus the simple calculator is created using MENUS was executed successfully.
AIM:
To create the folowing Form using Database Grid tool in Visual Basic.
DESCRIPTION:
• The connection of database with the visual basic form window is made possible using Database
Grid.
• The database Table to be connected is specified in the record source field in the dbgrid
properties window.
• Text boxes or labels associated with the data fields are connected to the data grid using the
“Data source” and the filed in the data table is connected using “Data Field” from the
properties window of the respective textboxes or labels
The following commands are used to perform the data grid operations
CODING:
Data1.Recordset.MoveFirst
End Sub
Data1.Recordset.MoveLast
End Sub
Data1.Recordset.MovePrevious
End Sub
www.rejinpaul.com
www.rejinpaul.com
Private Sub Command4_Click()
Data1.Recordset.MoveNext
End Sub
Data1.Recordset.MoveLast
Data1.Recordset.AddNew
End Sub
Data1.Recordset.Delete
Data1.Recordset.MoveLast
End Sub
Data1.Recordset.Edit
Data1.Recordset.Update
End Sub
End
End Sub
RESULT:
Thus the employee information was created using DBGrid tool in Visual Basic.
AIM:
PROCEDURE:
Step2: right click connection one of the data environment and set the property as Microsoft jet 4.00
OLEDB provider and database name as empdb
Step3: check weather the connection is established using test connection button.
Step4: add command for the connection1 and set database object as table and object name as emptable.
www.rejinpaul.com
www.rejinpaul.com
Step5: drag command,objects and drop it in the data reports detail section. Two items will appear for each
object.the first label for heading information. Move this label into page header section. The label is used
to store the actual value of the object.
Step6: set data source and data report1 as data environment1 and the data member as command1
Step8: place a button in the form and connect it to the data report to view the report.
PROGRAM:
Dbase.recordset.addnew
End Sub
Dbase.recordset.delete
Dbase.recordset.movefirst
End Sub
Datareport1.show
End Sub
Unload me
End Sub
RESULT:
www.rejinpaul.com