You are on page 1of 81

EX NO:1 CREATE A STUDENT INFORMATION AND MARK

DATE: DETAILS USING DDL,DML,TCL AND DCL

OBJECTIVE:
To create a student information and mark details database using DDL,DML,TCL and
DCL.
DESCRIPTION:

DDL (Data Definition Language), it is used to create and modify the structure of database

objects in database

DML (Data Manipulation Language), it is used to manipulate the data present in the database.

DCL (Data Control Language), it deals with permissions, rights and other controls of the
database.

TCL (Transaction Control Language),it deals with transaction within the database
ALGORITHM:
DDL COMMANDS:
• Create

• Alter

• Drop

• View

• Truncate

CREATE:
Create command is used to create a new table and also use constraints.

ALTER:
Alter command is used to alter a table or make some changes in a table. It has 3
methods such as ADD, MODIFY, DROP

ADD:
Add command is used to insert a new attribute/column with data type.

MODIFY:
Modify command is used to modify or change the data type and to increase the data
size in the table not the attribute/column.

DROP:
Drop command is used to delete the attribute/column in a table.
RENAME:
Rename command is used to re-change the table name.
VIEW:
View command is used to imaginary.
TRUNCATE:
Truncate command is used to delete a value in the table.
DML COMMANDS:
• Insert

• Delete

• Update

• Select

INSERT:
Insert command is used to insert a table. It has 2 ways
1. All the column
2. Specific
column
DELETE:
Delete command is used to delete a value from the table. Always use WHERE clause,
unless it deletes all the values.
UPDATE:
Update command is used to change the existing value. It has 2 ways
1. Calculations type the particular columns to change for all.

2. Specific record to change use WHERE clause.

SELECT:
Select command is used to select the table.
TCL COMMANDS:
• Commit

• Rollback

• Savepoint

COMMIT:
Commit command is used to permanently save any transaction into the database.
ROLLBACK:
Rollback command restore the database to last commited state. It is also used with
savepoint comment that jump to a savepoint to a transaction.
SAVEPOINT:
Savepoint command is used to save a transaction temporarily so that you can rollback
to that point whenever necessary.
DCL COMMANDS:
• Grant

• Revoke

GRANT:
Grant command is used to give user access privileges to the database.
REVOKE:
Revoke command is used to take back permission from the user.
SOURCE CODE

Create:
SQL> create table prabha_12(regno number(6),name varchar(15),DBMS
number(3),OOAD number(3),DCN number(3));
Table created.
View:
SQL> desc prabha_12;
Name Null? Type
----------------------------------------- -------- --------------------------------------
REGNO NUMBER(6)
NAME VARCHAR2(15)
DBMS NUMBER(3)
OOAD NUMBER(3)
DCN NUMBER(3)
Alter:
SQL> alter table prabha_12 add phonenumber number(10);
Table altered.
Insert:
SQL>insert into prabha_12 values ('&regno','&name','&DBMS','&OOAD','&DCN
','&phone number');
Enter value for regno: 00101
Enter value for name: Sumi
Enter value for dbms: 95
Enter value for ooad: 96
Enter value for dcn: 97
Enter value for phonenumber: 8903090281
old 1: insert into prabha_12 values('&regno','&name','&DBMS','&OOAD',

'&DCN','&phone number')

new 1: insert into prabha_12 values('00101','Sumi','95','96','97','8903090281')

1 row created.
SQL> /
Enter value for regno: 00102
Enter value for name: Agalya
Enter value for dbms: 92
Enter value for ooad: 94
Enter value for dcn: 91
Enter value for phonenumber: 948615226
old 1: insert into prabha_12 values
('&regno','&name','&DBMS','&OOAD','&DCN','&phone number')
new 1: insert into prabha_12 values('00102','Agalya','92','94','91','9486152261')
1 row created.
SQL> /
Enter value for regno: 00103
Enter value for name: Preetipal
Enter value for dbms: 86
Enter value for ooad: 92
Enter value for dcn: 99
Enter value for phonenumber: 9985642585
old 1: insert into prabha_12 values
('&regno','&name','&DBMS','&OOAD','&DCN','&phone number')
new 1: insert into prabha_12 values
('00103','Preetipal','86','92','99','9985642585')
1 row created
Select:
SQL> select * from prabha_12;
REGNO NAME DBMS OOAD DCN PHONENUMBER
---------- --------------- ---------- ---------- ---------- -----------
------------------------------------------
101 Sumi 95 96 97 8903090281
102 Agalya 92 94 91 9486152260
103 Preetipal 86 92 99 9985642585
Update:
SQL> update prabha_12 set OOAD='100' where name='Preetipal';
1 row updated.
Select:
SQL> select * from prabha_12;
REGNO NAME DBMS OOAD DCN PHONENUMBER
---------- --------------- ---------- ---------- ---------- -----------------------------------------
101 Sumi 95 96 97 8903090281
102 Agalya 92 94 91 9486152261
103 Preetipal 86 92 99 9985642585
Delete:
SQL> delete from prabha_12 where regno=’103’;
1 row deleted.
Select:
SQL> select * from prabha_12;
REGNO NAME DBMS OOAD DCN PHONENUMBER
------------ ---------- --------------- ---------- ---------- --------------------------------------
101 Sumi 95 96 97 8903090281
102 Agalya 92 94 91 9486152261
Rename:
SQL> rename prabha_12 to gayu_12;
Table renamed.
Select:
SQL> select * from gayu_12;
REGNO NAME DBMS OOAD DCN PHONENUMBER
---------- --------------- ---------- -------------------- -----------
--------------------------------------
101 Sumi 95 96 97 8903090281
102 Agalya 92 94 91 9486152261
Commit:
SQL> commit;
Commit complete.
Savepoint: SQL>
savepoint gayu_12;
Savepoint created.
Rollback:
SQL> rollback;
Rollback complete.
Truncate:
SQL> truncate table gayu_12;
Table truncated.
Drop:
SQL> drop table gayu_12;
Table dropped.
EX NO: 2 CREATE A LIBRARY DATABASE AND PERFORM THE
DATE: AGGREGATE FUNCTION AND SET OPERATION

OBJECTIVE
To create a library database and perform the aggregate function and set operation.
DESCRIPTION:
SQL supports few Set operations which can be performed on the table data. These are used to
get meaningful results from data stored in the table, under different special conditions.
In this tutorial, we will cover 4 different types of SET operations, along with example:
• UNION

• UNION ALL

• INTERSECT

• MINUS

In database management an aggregate function is a function where the values of multiple


rows are grouped together as input on certain criteria to form a single value of more
significant meaning. • COUNT ()
• SUM ()

• AVG()

• MIN()

ALGORITHM:
CREATE:
Create a table as master045 with the attributes accno, bookname, author, publication,
copies and also create another table as client045 with the attributes accno, bookname,
author, publication, copies.
UNION:
The union operator is used to combine the result set of two or more select statement
union remove duplicate rows.
UNION ALL:
The SQL union all operator is used to combine the result set of two or more select
statement. It doesn’t remove duplicate rows between the various select statement all
rows returned each select statement within the union all must have the same number at
the fields in the result set with similar data type.
INTERSECT:
It is used to return the results of two or more select statement however it only returns
the rows selected by all query or data sets. If a record exists in the query and not in the
order, it will omitted from intersect results.
MINUS:
It is used to return all rows in the first selects statements that are not returned by the
second select statement. Each select statement will define a data sets. The minus will
retrieve all records from the first data set and then remove from second data sets.
AVERAGE:
It is used to return the average of expression in a select statement.
MINIMUM:
It is used to return the minimum values of an expression in a select statement.
MAXIMUM:
It is used to return the maximum values of an expression in a select statement.
COUNT:
It is used to count the number of rows return in a select statement.
SUM:
It is used to return the sum of an expression in select statement.
SOURCE CODE

Create:
SQL> create table sindhu_53(acc_no number(7),bookname varchar(18),author
varchar(18),publication varchar(16),copies number(7));
Table created.
View:
SQL> desc sindhu_53;
Name Null? Type
----------------------------------------- -------- --- ------------------------------
ACC_NO NUMBER(7)
BOOKNAME VARCHAR2(18)
AUTHOR VARCHAR2(18)
PUBLICATION VARCHAR2(16)
COPIES NUMBER(7)
Insert:
SQL> insert into sindhu_53 values('&acc_no','&bookname','&author',
'&publication','&copies')
Enter value for acc_no: 111
Enter value for bookname: Java
Enter value for author: HerbertSchieldt
Enter value for publication: TaTa
Enter value for copies: 4
old1: insert into master045 values('&acc_no','&bookname','&author',
'&publication','&copies')
new 1: insert into sindhu_53values('111','Java','HerbertSchieldt','TaTa','4')
1 row created.
SQL> /
Enter value for acc_no: 112
Enter value for bookname: C
Enter value for author: Bala
Enter value for publication: McHill
Enter value for copies: 5
old 1: insert into sindhu_53
values('&acc_no','&bookname','&author’ ,'&publication','&copies)
new 1: insert into sindhu_53 values('112','C','Bala','McHill','5')
1 row created.
SQL> /
Enter value for acc_no: 113
Enter value for bookname: C++
Enter value for author: Robert
Enter value for publication: TaTa
Enter value for copies: 6
old 1: insert into sindhu_53 values('&acc_no','&bookname','&author',
'&publication','&copies')
new 1: insert into sindhu_53 values('113','C++','Robert','TaTa','6')
1 row created.
SQL> /
Enter value for acc_no: 114
Enter value for bookname: DCN
Enter value for author: Forozan
Enter value for publication: McHill
Enter value for copies: 6
old1: insert into sindhu_53 values('&acc_no','&bookname','&author',
'&publication','&copies')
new 1: insert into sindhu_53 values('114','DCN','Forozan','McHill','6')
1 row created.
SQL> /
Enter value for acc_no: 115
Enter value for bookname: DMW
Enter value for author: Benrouz
Enter value for publication: TaTa
Enter value for copies: 8
old 1: insert into sindhu_53 values('&acc_no','&bookname','&author'
,'&publication','&copies')
new 1: insert into sindhu_53 values('115','DMW','Benrouz','TaTa','8')
1 row created.
SQL> /
Enter value for acc_no: 116
Enter value for bookname: OOAD
Enter value for author: Ali
Enter value for publication: McHil
Enter value for copies: 6
old 1: insert into sindhu_53 values('&acc_no','&bookname','&author'
,'&publication','&copies')
new 1: insert into sindhu_53 values('116','OOAD','Ali','McHill','6')
1 row created.
SQL> /
Enter value for acc_no: 117
Enter value for bookname: FA
Enter value for author: Reddy
Enter value for publication: TaTa
Enter value for copies: 9
old 1: insert into sindhu_53
values('&acc_no','&bookname’,'&author’ ,'&publication','&copies')
new 1: insert into sindhu_53 values('117','FA','Reddy','TaTa','9')
1 row created.
Select:
SQL> select * from sindhu_53;
ACC_NO BOOKNAME AUTHOR PUBLICATION COPIES
------------ ---------------------- -------------- --------------------- ---------------
111 Java HerbertSchieldt TaTa 4
112 C Bala McHill 5
113 C++ Robert TaTa 6
114 DCN Forozan McHill 6
115 DMW Benrouz TaTa 8
116 OOAD Ali McHill 6
117 FA Reddy TaTa 9
7 rows selected.
Create:
SQL> create table sindhu_23 (acc_no number(7),bookname varchar(18),author
varchar(18),publication varchar(16),copies number(7));
Table created.
View:
SQL> desc sindhu_23;
Name Null? Type
----------------------------------------- -------- ----- -----------------------
ACC_NO NUMBER(7)
BOOKNAME VARCHAR2(18)
AUTHOR VARCHAR2(18)
PUBLICATION VARCHAR2(16)
COPIES NUMBER(7)

Insert:
SQL> insert into sindhu_23
values('&acc_no','&bookname','&author’ ,'&publication','&copies');
Enter value for acc_no: 221
Enter value for bookname: C
Enter value for author: Robert
Enter value for publication: TaTa

Enter value for copies: 8

old 1: insert into sindhu_23


values('&acc_no','&bookname','&author’ ,'&publication','&copies')

new 1: insert into sindhu_23values('221','C','Robert','TaTa','8')

1 row created.
SQL> /
Enter value for acc_no: 222
Enter value for bookname: C++
Enter value for author: Anderson
Enter value for publication: McHill
Enter value for copies: 6
old 1: insert into
sindhu_23values('&acc_no','&bookname','&author’ ,'&publication','&copies')
new 1: insert into sindhu_23 values('222','C++','Anderson','McHill','6')
1 row created.
SQL> /
Enter value for acc_no: 223
Enter value for bookname: CA
Enter value for author: Bahrami
Enter value for publication: TaTa
Enter value for copies: 9

old 1: insert into sindhu_23 values('&acc_no','&bookname','&author’


,'&publication','&copies')
new 1: insert into sindhu_23 values('223','CA','Bahrami','TaTa','9')
1 row created.
SQL> /
Enter value for acc_no: 224
Enter value for bookname: Java
Enter value for author: Gurusamy
Enter value for publication: McHill
Enter value for copies: 6
old 1: insert into sindhu_23 values('&acc_no','&bookname','&author’
,'&publication','&copies')
new 1: insert into sindhu_23 values('224','Java','Gurusamy','McHill','6')
1 row created.
SQL> /
Enter value for acc_no: 225
Enter value for bookname: FM
Enter value for author: Moorthy
Enter value for publication: TaTa
Enter value for copies: 5
old 1: insert into sindhu_23 values('&acc_no','&bookname','&author’
,'&publication','&copies')
new 1: insert into sindhu_23 values('225','FM','Moorthy','TaTa','5')
1 row created.
SQL> /
Enter value for acc_no: 226
Enter value for bookname: CMA
Enter value for author: Acharya
Enter value for publication: McHill
Enter value for copies: 6
old 1: insert into sindhu_23 values('&acc_no','&bookname','&author’
,'&publication','&copies')
new 1: insert into sindhu_23 values('226','CMA','Acharya','McHill','6')
1 row created.
SQL> /
Enter value for acc_no: 227
Enter value for bookname: MA
Enter value for author: TSReddy
Enter value for publication: TaTa
Enter value for copies: 4 old 1: insert into sindhu_23
values('&acc_no','&bookname','&author'
,'&publication','&copies')
new 1: insert into sindhu_23 values('227','MA','TSReddy','TaTa','4')
1 row created.
Select:
SQL> select * from sindhu_23;
ACC_NO BOOKNAME AUTHOR PUBLICATION COPIES
------------- ------------------- --------------- ---------------------- ----------------
221 C Robert TaTa 8
222 C++ Anderson McHill 6
223 CA Bahrami TaTa 9
224 Java Gurusamy McHill 6
225 FM Moorthy TaTa 5
226 CMA Acharya McHill 6
227 MA TSReddy TaTa 4
7 rows selected.

Union:
SQL> select bookname from sindhu_53 UNION select bookname from sindhu_23;
BOOKNAME
------------------
C
C++
CA
CMA
DCN
DMW
FA
FM
Java
MA
OOAD
11 rows selected.
Intersect:
SQL> select bookname from sindhu_53 INTERSECT select bookname from
sindhu_23;
BOOKNAME
------------------
C
C++
Java
Union all:
SQL> select bookname from sindhu_53 UNION ALL select bookname from
sindhu_23;
BOOKNAME
------------------
Java
C
C++
DCN
DMW
OOAD
FA
C
C++
CA
Java
FM
CMA
MA
14 rows selected.
Minus:
SQL> select bookname from sindhu_53 MINUS select bookname from sindhu_23;
BOOKNAME
------------------
DCN
DMW
FA
OOAD
Average:
SQL> select avg(copies) from sindhu_53;
AVG(COPIES)
-----------
6.28571429
SQL> select avg(copies) from sindhu_23;
AVG(COPIES)
-----------
6.28571429
Minimum:
SQL> select min(copies) from sindhu_53;
MIN(COPIES)
-----------
4
SQL> select min(copies) from sindhu_23;
MIN(COPIES)
-----------
4
Maximum:
SQL> select max(copies) from sindhu_53;
MAX(COPIES)
-----------
9
SQL> select max(copies) from sindhu_23;
MAX(COPIES)
-----------
9
Count:
SQL> select count(*) from sindhu_53;
COUNT(*)
----------
7
SQL> select count(*) from sindhu_23;
COUNT(*)
----------
7

Sum:
SQL> select sum(copies) from sindhu_53;
SUM(COPIES)
-----------
44
SQL> select sum(copies) from sindhu_23;
SUM(COPIES)
-----------
44
EX NO: 3 CREATE A HOSPITAL DATATBASE AND PERFORM
DATE: THE INNER AND OUTER JOIN

OBJECTIVE:
To create a hospital database and perform the aggregate inner and outer join operations.
.DESCRIPTION:
Joins can be simply defined as the combining or merging the related tuples from the
two different relations into a single type.
INNER JOINS: These joins are the one that has the tuples that satisfy some conditions
and rest are discarded.
NATURAL JOIN – It does not utilize any of the comparison operator. Here the
condition is that the attributes should have same name and domain.
OUTER JOINS: These have all the tuples from either or both the relations.
LEFT OUTER JOIN – All the tuples of left table is displayed irrespective of whether
it satisfies the matching conditions.
RIGHT OUTER JOIN – All the tuples of right table are displayed irrespective of
whether it satisfies the matching conditions or not..
FULL OUTER JOIN– Tuples from both the relations takes part irrespective of
whether it has the matching or non-matching conditions.
ALGORITHM:
CREATE:
Create a table as sri_5 with the attributes pid, name, doctor, department, app_date and
also create table as hospital_clint045 with the attributes pid, name, doctor, department,
app_date.
JOINS OR INNER JOIN/ EQUI JOIN/ NATURAL JOIN:
The SQL join clause is used to combine a records from two or more tables in a database. A
join means for combining fields from two tables by using values common to each returns
all rows when there is atleast one match in both the tables. This is a simple join in which
the result is based on matched data as per the equality condition specified in the query.

LEFT OUTER JOIN:


The left outer join returns all rows from the left table with the matching rows in the
right table. The result is null in the right side when there is no match.
RIGHT OUTER JOIN:
The right outer join returns a result table with matched data of two tables then
remaining rows of the right table includes it.
CROSS JOIN OR CARTESIAN PRODUCT:
This type of join returns all the rows in all the tables listed in the query. Each row in
the first table is paired with all the rows in the second table. This happens when
there is no relationship defined between the two tables.
SOURCE CODE

Create:
SQL> create table sri_5 (pid number(6),name varchar(10),doctor varchar(10),dep
varchar(10),app_date date);
Table created.
View:
SQL> desc sri_5;
Name Null? Type
----------------------------------------- -------- ----------------------------
PID NUMBER(6)
NAME VARCHAR2(10)
DOCTOR VARCHAR2(10)
DEP VARCHAR2(10)
APP_DATE. DATE
SQL> insert into sri_5values('&pid','&name','&doctor','&dep','&app_date');
Enter value for pid: 001
Enter value for name: Dhaarani
Enter value for doctor: Rajeswari
Enter value for dep: Cardio
Enter value for app_date: 4-Jan-2018
old 1: insert intosri_5values('&pid','&name','&doctor','&dep','&app_date')
new 1: insert into sri_5values('001','Dhaarani','Rajeswari','Cardio','4-Jan-2018')
1 row created.
SQL> /
Enter value for pid: 002
Enter value for name: PreetiPal
Enter value for doctor: Sudha
Enter value for dep: Thyroid
Enter value for app_date: 5-Jan-2018
old 1: insert into sri_5values('&pid','&name','&doctor','&dep','&app_date')
new 1: insert into sri_5values('002','PreetiPal','Sudha','Thyroid','5-Jan-2018')
1 row created.
SQL> /
Enter value for pid: 004
Enter value for name: Jayanthi
Enter value for doctor: Devi
Enter value for dep: Heart
Enter value for app_date: 6-Jan-2018
old 1: insert into sri_5 values('&pid','&name','&doctor','&dep','&app_date')
new 1: insert into sri_5values('004','Jayanthi','Devi','Heart','6-Jan-2018')
1 row created.
SQL> /
Enter value for pid: 006
Enter value for name: Raji
Enter value for doctor: Aswathi
Enter value for dep: Eye
Enter value for app_date: 7-Jan-2018
old 1: insert into sri_5values('&pid','&name','&doctor','&dep','&app_date')
new 1: insert into sri_5values('006','Raji','Aswathi','Eye','7-Jan-2018')
1 row created.
Select:
SQL> select * from sri_5;
PID NAME DOCTOR DEP APP_DATE
------ ----------- --------------- --------- ------------------
1 Dhaarani Rajeswari. Cardio 04-JAN-18
2 PreetiPal Sudha Thyroid 05-JAN-18
4 Jayanthi Devi Heart 06-JAN-18
6 Raji Aswathi Eye 07-JAN-18
Create:
SQL> create table sri_6(pid number(6),fname varchar(10),lname varchar(10));
Table created.
View:
SQL> desc sri_6;
Name Null? Type
--------------------- --------------------------- -----------------
PID NUMBER(6)
FNAME VARCHAR2(10)
LNAME VARCHAR2(10)
Insert:
SQL> insert into values('&pid','&fname','&lname');
Enter value for pid: 001
Enter value for fname: Sridevi
Enter value for lname: Suresh
old 1: insert into sri_6 values('&pid','&fname','&lname')
new 1: insert into sri_6values('001','Sridevi','Suresh')
1 row created.
SQL> /
Enter value for pid: 002
Enter value for fname: Valli
Enter value for lname: Moorthy
old 1: insert into sri_6values('&pid','&fname','&lname')
new 1: insert into sri_6values('002','Valli','Moorthy')
1 row created.
SQL> /
Enter value for pid: 003
Enter value for fname: Niveditha
Enter value for lname: Sharath
old 1:insert into sri_6 values('&pid','&fname','&lname')
new 1: insert into sri_6values('003','Niveditha','Sharath')
1 row created.
SQL> /
Enter value for pid: 0045
Enter value for fname: Dharshini
Enter value for lname: Sudhaka
old 1: insert into sri_6values('&pid','&fname','&lname') n
ew 1: insert into sri_6values('0045','Dhrshini','Sudhakar')
1 row created.
Select:
SQL> select * from sri_6;
PID FNAME LNAME
------- -------------- ---------------
1 Sridevi Suresh
2 Valli Moorthy
3 Niveditha Sharath
45 Dharshini Sudhakar
Left outer join:
SQL> select * from sri_5 left outer join sri_6 on sri_5.pid=sri_6pid;
PID NAME DOCTOR DEP APP_DATE PID FNAME
LNAME
------- ------------- --------------- ------------ ----------------- ------ -----------
--------------
1 Dhaarani Rajeswari Cardio 04-JAN-18 1 Sridevi
Suresh
2 PreetiPal Sudha Thyroid 05-JAN-18 2 Valli
Moorthy
4 Jayanthi Devi Heart 06-JAN-18
6 Raji Aswathi Eye 07-JAN-18
Right outer join:
SQL> select * from sri_5 right outer join sri_6 on sri_5.pid= sri_6pid;
PID NAME DOCTOR DEP APP_DATE PID FNAME
LNAME
----- ------------- ---------------- -------- ------------------- ------ --------------
-------------
1 Dhaarani Rajeswari Cardio 04-JAN-18 1 Sridevi
Suresh
2 PreetiPal Sudha Thyroid 05-JAN-18 2 valli.
Sharath

Dharshini Sudhakar
Full outer join:
SQL> select * from sri_5 full outer join sri_6 on sri_5 pid=sri_6.pid;
PID NAME DOCTOR DEP APP_DATE PID FNAME
LNAME
----- -------------- --------------- -------- ------------------- ------- ------------
----------------
1 Dhaarani Rajeswari Cardio 04-JAN-18 1 Sridevi
Suresh
2 PreetiPal Sudha Thyroid 05-JAN-18 2 Valli
Moorthy
3 Jayanthi Devi Heart 06-JAN-18 3 Niveditha
Sharath
6 Raji Aswathi Eye 07-JAN-18 45 Dharashini
Sudhakar
Inner join:
SQL>select * from sri_5 inner join sri_6 on sri_5 pid=sri_6 pid;
PID NAME DOCTOR DEP APP_DATE PID FNAME
LNAME
------ ------------ -------------- ---------- --------------- --------- --------------
-------------
1 Dhaarani Rajeswari Cardio 04-JAN-18 1 Sridevi
Suresh
2 PreetiPal Sudha Thyroid 05-JAN-18 2 Valli
Moorthy
Natural join:
SQL>select * fromsri_5 natural join Sri_6;
PID NAME DOCTOR DEP APP_DATE PID FNAME
LNAME
------ ------------ -------------- ---------- --------------- --------- --------------
-------------
1 Dhaarani Rajeswari Cardio 04-JAN-18 1 Sridevi
Suresh
2 PreetiPal Sudha Thyroid 05-JAN-18 2 Valli
Moorthy
RESULT:
Thus, the hospital database using inner and outer join operation was created.
Ex no:4 CREATE A BANK DATABASE AND QUERY IT USING DATE:
GROUP BY HAVING ORDERBY

OBJECTIVE:
To create a bank database and query it by having orderby and groupby.
DESCRIPTION:
OEDER BY : It alters the order in which items are returned.
GROUP BY : It will aggregate records by the specified columns which allows you
to perform aggregation functions on non-grouped columns
ALGORITHM:
CREATE:
Create a table as bank045 and also create the attributes acc_no, name, bank, branch
and deposit.
ORDERBY:
It is used to sort data in ascending order, descending order based on one or
more columns.
GROUPBY:
It is used in collaboration with the select statement to arrange identical data into
Group.
4. CREATE A BANK DATABASE AND QUERY IT USING
GROUPBY HAVING ORDERBY

Create:
SQL> create table subasri_55(acc_no varchar(4),name varchar(15),bank
varchar(10),branch varchar(15),deposit number(15));
Table created.
View:
SQL> desc subasri_55;
Name Null? Type
----------------- -------------------- ---------------
ACC_NO
VARCHAR2(4)
NAME VARCHAR2(15)
BANK VARCHAR2(10)
BRANCH VARCHAR2(15)
DEPOSIT NUMBER(15)
Insert:
SQL> insert into subasri_55 values('&acc_no','&name','&bank','&branch','&deposit');
Enter value for acc_no: AC1
Enter value for name: Dhaarani
Enter value for bank: KVB
Enter value for branch: Neyveli Enter value for deposit: 6000 old 1: insert into
subasri_55 values('&acc_no','&name','&bank','&branch','&deposit') new 1:
insert into subasri_55 values('AC1','Dhaarani','KVB','Neyveli','6000')
1 row created.
SQL> /
Enter value for acc_no: AC2
Enter value for name: Preetipal
Enter value for bank: IOB
Enter value for branch: Kalpakkam Enter value for deposit: 6000 old 1: insert
into subasri_55 values('&acc_no','&name','&bank','&branch','&deposit') new
1: insert into subasri_55 values('AC2','Preetipal','IOB','Kalpakkam','6000')
1 row created.
SQL> /
Enter value for acc_no: AC3
Enter value for name: Anu
Enter value for bank: IB
Enter value for branch: Vpm Enter value for deposit: 8000 old 1: insert into
subasri_55 values('&acc_no','&name','&bank','&branch','&deposit') new 1:
insert into subasri_55 values('AC3','Anu','IB','Vpm','8000')
1 row created.
SQL> /
Enter value for acc_no: AC4
Enter value for name: Raji
Enter value for bank: CUB
Enter value for branch: Vpm Enter value for deposit: 7000 old 1: insert into
subasri_55 values('&acc_no','&name','&bank','&branch','&deposit') new 1:
insert into subasri_55 values('AC4','Raji','CUB','Vpm','7000')
1 row created.
SQL> /
Enter value for acc_no: AC5
Enter value for name: Suresh
Enter value for bank: BOB
Enter value for branch: VPM Enter value for deposit:2000 old 1: insert into
subasri_55values('&acc_no','&name','&bank','&branch','&deposit') new 1:
insert into subasri_55 values('AC5','Suresh','BOB','VPM','2000')
1 row created.
SQL> /
Enter value for acc_no: AC6
Enter value for name: Satheesh
Enter value for bank: SBI
Enter value for branch: Vpm Enter value for deposit: 6500 old 1: insert into
subasri_55values('&acc_no','&name','&bank','&branch','&deposit') new 1:
insert into subasri_55 values('AC6','Satheesh','SBI','Vpm','6500')
1 row created.
SQL> /
Enter value for acc_no: AC7
Enter value for name: Deepthi
Enter value for bank: CUB
Enter value for branch: Chennai Enter value for deposit: 10000 old 1: insert
into subasri_55values('&acc_no','&name','&bank','&branch','&deposit') new
1: insert into subasri_55values('AC7','Deepthi','CUB','Chennai','10000')
1 row created.
Select:
SQL> select * from subasri_55;
ACC_ NO NAME BANK BRANCH DEPOSIT
--------------- -------------- ------------ ----------------- -----------------
AC1 Dhaarani KVB Neyveli 6000
AC2 Preetipal IOB Kalpakkam 6000
AC3 Anu IB Vpm 8000
AC4 Raji CUB Vpm 7000
AC5 Suresh BOB VPM 2000
AC6 Satheesh SBI Vpm
6500 AC7 Deepthi CUB Chennai
10000
7 rows selected.
Order by:
Ascending order:
SQL> select bank,acc_no from subasri_55 ORDER BY bank ASC;
BANK ACC_NO
---------- ---------------
BOB AC5
CUB AC4
CUB
AC7 IB
AC3
IOB AC2
KVB AC1
SBI AC6
7 rows selected.
Descending order:
SQL> select bank,acc_no from subasri_55 ORDER BY bank DESC;
BANK ACC_NO
---------- ------------
SBI AC6
KVB AC1
IOB AC2
IB AC3
CUB AC7
CUB AC4
BOB AC5
7 rows selected.
SQL> select bank,acc_no from subasri_55 ORDER BY bank ASC,acc_no DESC;
BANK ACC_NO
---------- ------------
BOB AC5
CUB AC7
CUB AC4
IB AC3
IOB AC2
KVB AC1
SBI AC6
7 rows selected.
SQL> select name,deposit from subasri_55 ORDER BY acc_no;
NAME DEPOSIT
--------------- -----------------
Dhaarani 6000
Preetipal 6000
Anu 8000
Raji 7000
Suresh 2000
Satheesh 6500
Deepthi 10000
7 rows selected.
Group by:
Sum:
SQL> select bank,SUM(deposit)from subasri_55 GROUP BY bank;
BANK SUM(DEPOSIT)
---------- --------------------
KVB 6000
BOB 2000
SBI 6500
IOB 6000
IB 8000
CUB 17000
6 rows selected.
SQL> select SUM(deposit) from subasri_55 GROUP BY deposit;
SUM(DEPOSIT)
------------
6500
10000
12000
8000
7000
6 rows selected.
SQL> select bank,count(acc_no) from subasri_55 GROUP BY bank;
BANK COUNT(ACC_NO)
---------- -------------------
KVB 1
BOB 1
SBI 1
IOB 1
IB 1
CUB 2
6 rows selected.
Having sum:
SQL> select SUM(deposit) from subasri_55 GROUP BY deposit HAVING
SUM(deposit)>7000;
SUM(DEPOSIT)
------------
10000
12000
8000
Having:
SQL> select bank from subasri_55 GROUP BY bank HAVING count(*)>1;
BANK
----------
CUB
Result:
Thus, the bank database using the order by and group by was created successfully.

Ex no:5 CREATE AN EMPLOYEE DATABASE AND QUERY IT DATE:


USING SUB QUERIES

OBJECTIVE:
To create an employee database using nested queries.
DESCRIPTION:
In SQL a Subquery can be simply defined as a query within another query.
Subqueries are seen to be used most frequently with SELECT statement
ALGORITHM:
CREATE:
To create the table as emp_M45 into the fields of empid, fname, lname and contact.
INSERT:
Insert values into the database.
NOT IN:
NOT IN operators are used with statements/queries to select, update and delete
only particular records in a table those meet the condition that is., it filters records
from a table as per the condition.
IN:
The IN operator is used to filters the result set based on a list of discrete values.
EXISTS:
The EXISTS condition is used in combination with a subquery and is
considered to be met, if the subquery returns at least one row.
NOT EXISTS:
The NOT EXISTS will act quite opposite to EXISTS Operator. It is used to
restrict the number of rows returned by the SELECT Statement. The NOT
EXISTS will check the Subquery for rows existence.

CREATE AN EMPLOYEE DATABASE AND QUERY IT


USING SUB QUERIES

Create:
SQL> create table dhivya_11(emp_id number(5),fname varchar(10),lname
varchar(10),c_no number(10)); Table created.
View:
SQL> desc dhivya_11;
Name Null? Type
---------------------------------- -------- ----------------------------
EMP_ID NUMBER(5)
FNAME VARCHAR2(10)
LNAME VARCHAR2(10)
C_NO NUMBER(10)
Insert:
SQL> insert into dhivya_11 values('&empid','&fname','&lname','&contact');
Enter value for empid: 1001
Enter value for fname: Dhaarani
Enter value for lname: Selvam Enter value for contact: 8903090281
old 1: insert into dhivya_11
values('&empid','&fname','&lname','&contact') new 1: insert into
dhivya_11 values('1001','Dhaarani','Selvam','8903090281')
1 row created.
SQL> /
Enter value for empid: 1002
Enter value for fname: Anu
Enter value for lname: Raj
Enter value for contact: 9486152261 old 1: insert into dhivya_11
values('&empid','&fname','&lname','&contact') new 1: insert into
dhivya_11 values('1002','Anu','Raj','9486152261')
1 row created.
SQL> /
Enter value for empid: 1003
Enter value for fname: Deepthi
Enter value for lname: Satheesh Enter value for contact: 9789520968
old 1: insert into dhivya_11
values('&empid','&fname','&lname','&contact') new 1: insert into
dhivya_11 values('1003','Deepthi','Satheesh','9789520968')
1 row created.
SQL> /
Enter value for empid: 1004
Enter value for fname: Shruthi
Enter value for lname: Suresh Enter value for contact: 9856472525
old 1: insert into
dhivya_11values('&empid','&fname','&lname','&contact') new 1:
insert into dhivya_11values('1004','Shruthi','Suresh','9856472525')
1 row created.
Select:
SQL> select * from dhivya_11;
EMP_ID FNAME LNAME C_NO
---------- ---------- ---------- ---------- -------------- ---------------
1001 Dhaarani Selvam 8903090281
1002 Anu Raj 9486152261
1003 Deepthi Satheesh 9789520968
1004 Shruthi Suresh 9856472525

Create:
SQL> create table dhivya_19(emp_id number(5),ename varchar(10),desg
varchar(10),salary number(10)); Table created.
View:
SQL> desc dhivya_19;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMP_ID NUMBER(5)
ENAME VARCHAR2(10)
DESG VARCHAR2(10)
SALARY NUMBER(10)
Insert:
SQL> insert into dhivya_19 values('&emp_id','&ename','&desg','&salary');
Enter value for emp_id: 1001
Enter value for ename: Dhaarani
Enter value for desg: Manager Enter value for salary: 20000 old 1:
insert into dhivya_19values('&emp_id','&ename','&desg','&salary')
new 1: insert into dhivya_19
values('1001','Dhaarani','Manager','20000')
1 row created.
SQL> /
Enter value for emp_id: 1002
Enter value for ename: Anuja
Enter value for desg: Employee Enter value for salary: 10000 old 1:
insert into dhivya_19 values('&emp_id','&ename','&desg','&salary')
new 1: insert into dhivya_19
values('1002','Anuja','Employee','10000')

1 row created.
SQL> /
Enter value for emp_id: 1003
Enter value for ename: Deepthi
Enter value for desg: Worker Enter value for salary: 8900 old 1:
insert into dhivya_19 values('&emp_id','&ename','&desg','&salary')
new 1: insert into dhivya_19 values('1003','Deepthi','Worker','8900')
1 row created.
SQL> /
Enter value for emp_id: 1004
Enter value for ename: Nive
Enter value for desg: Technian Enter value for salary: 10000 old 1:
insert into dhivya_19 values('&emp_id','&ename','&desg','&salary')
new 1: insert into dhivya_19values('1004','Nive','technian','10000')
1 row created.
Select:
SQL> select * from dhivya_19;
EMP_ID ENAME DESG SALARY
------------- --------------- ------------- ---------------
1001 Dhaarani Manager 20000
1002 Anuja Employee 10000
1003 Deepthi Worker 8900 1004 Nive Technian
10000
IN:
fname:
SQL> select fname from dhivya_11where emp_id IN (select emp_id from
dhivya_19where fname=ename);

FNAME
----------
Dhaarani
Deepthi
ename:
SQL> select ename from dhivya_19 where emp_id IN (select emp_id from dhivya_11
where fname=ename);
ENAME
----------
Dhaarani
Deepthi
NOT IN:
fname:
SQL> select fname from dhivya_11 where emp_id NOT IN (select emp_id from
dhivya_19 where fname=ename);
FNAME
----------
Anu
Shruthi
ename:
SQL> select ename from dhivya_19 where emp_id NOT IN (select emp_id from
dhivya_11 where fname=ename);
ENAME
----------
Anuja
Nive
EXISTS:
SQL> select emp_id,desg from dhivya_19 where EXISTS (select * from dhivya_11
where fname=ename);
EMP_ID DESG
---------- ---------------
1001 Manager
1003 Worker
NOT EXISTS:
SQL> select fname,lname from dhivya_11 where NOT EXISTS (select * from
dhivya_19 where emp_id=emp_id); no rows selected Commit:
SQL> commit;
Commit complete.
Result:
Thus, the above employee details database using sub queries was created successfully.
EX NO:6 CREATE VIEWS AND INDEXES FOR PHONE
DATE: DIRECTORY DATABASE

OBJECTIVE:
To create a phone directory database using views and indexes.
DESCRIPTION:
A view can combine data from two or more table, using joins, and also just contain a
subset of information. Indexes are special lookup tables that the database search
engine can use to speed up data retrieval.
ALGORITHM:
CREATE:
Create a table for phone directory database as phonebook_45 and also set primary key
for the Ph_id.
SELECT:
After inserting values to the table, select command is used to view the existing table.
CREATE:
Create another table ph_book45 and also set foreign key for references to Ph_id by
considering the first created table.
VIEWS:
View command is used to take a duplicate copy of the table.
INDEXES:
The indexes command is used to declare the index value for a particular field.

CREATE A VIE W AND INDEXES FOR PHONE DIRECTORY


SYSTEM

CREATE:
SQL> create table jaya_15(pid number(5)constraint p primary key,name
varchar(10),street varchar(10),city varchar(10),district varchar(10));
Table created.
VIEW:
SQL> desc jaya_15;
Name Null? Type
-----------------------------------------------------------------------------
PID NOT NULL NUMBER(5)
NAME VARCHAR2(10)
STREET VARCHAR2(10)
CITY VARCHAR2(10)
DISTRICT VARCHAR2(10)
INSERT:
SQL> insert into jaya_15 values('11','Anu','raju
st','tvm','vpm'); 1 row created.
SQL> insert into jaya_15 values('12','Banu','rmk
st','vpm','vpm'); 1 row created.
SQL> insert into jaya_15 values('13','Charu','rkk
st','pondy','pondy'); 1 row created.
SQL> insert into jaya_15 values('14','Dhana','rk
st','pondy','pondy'); 1 row created.
SQL> insert into jaya_15 values('15','Elakiya','mg st','vpm','vom');
1 row created.
SELECT:
SQL> select* from jaya_15;

PID NAME STREET CITY DISTRICT


----------------------------------------------------------------
11 Anu raju st tvm vpm
12 Banu rmk st vpm vpm 13 Charu
rkk st pondy pondy
CREATE:
SQL> create table jaya_16 (pid number(5)constraint fk references ph24(pid),email
varchar(10),website varchar(10),phnum number(10));
Table created.
VIEW:
SQL> desc jaya_16;
Name Null? Type
----------------------------------------- -------- ----------------------------
PID NUMBER(5)
EMAIL VARCHAR2(10)
WEBSITE VARCHAR2(10)
PHNUM NUMBER(10)
INSERT:
SQL> insert into jaya_16
values('11','Anu@','www.n.com','9807654321'); 1 row created.
SQL> insert into jaya_16
values('12','Banu@','www.a.com','9867654321'); 1 row created.
SQL> insert into jaya_16
values('13','Charu@','www.b.com','9867654821'); 1 row created.
SQL> insert into jaya_16
values('14','Dhana@','www.h.com','9867654820'); 1 row created.
SQL> insert into jaya_16 values('15','Elakiya@','www.b.com','9867654821');
1 row created.
VIEW:
SQL> create view ammu_12 as select p1.pid,p2.website from ph24 p1,ph2 p2 where
p1.pid=p2.pid;
View created.
SQL> select* from ammu_12;
PID WEBSITE
---------- ----------
11 wwww.n.com
12 wwww.a.com 13 wwww.b.com
VIEW:
SQL> create or replace view ammu_12 as select p1.pid,p1.name,p2.phnum,p2.email
from
Jaya_15 p1,jaya_16 p2 where p1.pid=p2.pid;
View created.
SELECT:
SQL> select* from ammu_12;
PID NAME PHNUM EMAIL
---------- ---------- ---------- ---------- -------------------------
11 Anu 9807654321 Anu@
12 Banu 9867654321 Banu@
13 Charu 9867654821 Charu@ INDEX:
SQL> create index indexa on jaya_15(city);
Index created.
INDEX ALTERED:
SQL> alter index indexa rename to indexcity;
Index altered.
RESULT:
Thus the above creation of views and indexes for phone directory database is executed
Successfully.
EX NO:7 CREATE PL/SQL BLOCK FOR MARKSHEET DATE:
PROCESSING

OBJECTIVE:
To create a student database by using PL/SQL block for marksheet processing.
DESCRIPTION:
PL/SQL is a block structured language that enables developers to combine the power
of SQL with procedural statements.
ALGORITHM:
CREATE:
Create a table MP_45 with field Reg_no, Std_name, m1, m2,m3, tot, avg.
Create a main function block and then declare local variables vm1,vm2,vm3,vtot,
vavg.
SELECT:
Select the fields tot, avg from MP_045.
VIEW:
To view the output from block database management system output line (update
MP_045 set tot=vtot and update any table needed.
DISPLAY:
Display the result of PL/SQL block using marksheet processing.

CREATE PL/SQL BLOCK FOR MARKSHEET PROCESSING

Create:
SQL> create table surya_53 (Reg_no number(5),S_name varchar(10),dept varchar(5),year
varchar(5),dob date,m1 number(3),m2 number(3),m3 number(3),tot number(3),avg
number(4)); Table created.
View:
SQL> desc surya_53;
Name Null? Type
----------------------------------------- -------- ----------------------------
REG_NO NUMBER(5)
S_NAME VARCHAR2(10)
DEPT VARCHAR2(5)
YEAR VARCHAR2(5)
DOB DATE
M1 NUMBER(3)
M2 NUMBER(3)
M3 NUMBER(3)
TOT NUMBER(3)
AVG NUMBER(4)
Insert:
SQL> insert into surya_53 ('1','Dhaarani','BCA','2018','27-Apr-
98','98','96','92','0','0'); 1 row created.
SQL> insert into surya_53 values('2','Raji','Eng','2018','10-Dec-98','96','92','86','0','0');
1 row created.
SQL> insert into surya_53 values('3','Preetipal','B.Com','2018','26-May-
98','96','98','91','0','0'); 1 row created.
SQL> insert into surya_53 values('4','Keerthi','Maths','2018','29-Sep-
98','69','96','86','0','0');
1 row created.
SQL> insert into surya_53 values('5','Anuja','CS','2018','27-Aug-98','96','89','93','0','0');
1 row created.
Select:
SQL> select * from surya_53;
REG_NO S_NAME DEPT YEAR DOB M1 M2 M3
TOT AVG
------------ --------------- ------------ --------- --------- ------ -------- ---------
------- ----------
1 Dhaarani BCA 2018 27-APR-98 98 96 92 0
0
2 Raji Eng 2018 10-DEC-98 96 92 86 0
0
3 Preetipal B.Com 2018 26-MAY-98 96 98 91 0
0
4 Keerthi Maths 2018 29-SEP-98 69 96 86 0
0
5 Anuja CS 2018 27-AUG-98 96 89 93 0
0
Select:
SQL> declare
2 vm1 number;

3 vm2 number;

4 vm3 number;

5 vtot number;

6 vavg number;

7 begin

8 select m1,m2,m3,tot,avg into vm1,vm2,vm3,vtot,vavg from surya_53 where


Reg_no='1';
9 vtot:=vm1+vm2+vm3;

10 vavg:=vtot/3;

11 update surya_53 set tot=vtot,avg=vavg where Reg_no='1';

12 commit;

13 end;

14 /
Updation of regno 1:
Select:

SQL> select * from surya_53;


REG_NO S_NAME DEPT YEAR DOB M1 M2 M3
TOT AVG
---------- ---------- ----- ----- --------- ---------- ---------- --------------------
---------------------------
1 Dhaarani BCA 2018 27-APR-98 98 96 92
286 95
2 Raji Eng 2018 10-DEC-98 96 92 86
0 0 3 Preetipal B.Com 2018 26-MAY-98
96 98 91 0 0
4 Keerthi Maths 2018 29-SEP-98 69 96 86 0
0
5 Anuja CS 2018 27-AUG-98 96 89 93
0 0
PL/SQL procedure successfully completed.
SQL> declare
2 vm1 number;

3 vm2 number;

4 vm3 number;

5 vtot number;

6 vavg number;

7 begin

8 select m1,m2,m3,tot,avg into vm1,vm2,vm3,vtot,vavg from surya_53 where


Reg_no='2';
9 vtot:=vm1+vm2+vm3;
Updation of regno 2:
10 vavg:=vtot/3;

11 update surya_53 set tot=vtot,avg=vavg where Reg_no='2';

12 commit;

13 end;

14 /

PL/SQL procedure successfully completed.


Select:

REG_NO S_NAME DEPT YEAR DOB M1 M2 M3


TOT AVG
---------- ---------- ----- ----- --------- ---------- ---------- --------------------
------------------------- --------------
1 Dhaarani BCA 2018 27-APR-98 98 96 92 286
95
2 Raji Eng 2018 10-DEC-98 96 92 86 274
91
3 Preetipal B.Com 2018 26-MAY-98 96 98 91 0
0
4 Keerthi Maths 2018 29-SEP-98 69 96 86 0
0
5 Anuja CS 2018 27-AUG-98 96 89 93
0 0
SQL> declare
2 vm1 number;

3 vm2 number;

4 vm3 number;

5 vtot number;

6 vavg number;
Updation of regno 3:
7 begin

8 select m1,m2,m3,tot,avg into vm1,vm2,vm3,vtot,vavg from surya_53 where


Reg_no='3';
9 vtot:=vm1+vm2+vm3;

10 vavg:=vtot/3;

11 update surya_53 set tot=vtot,avg=vavg where Reg_no='3';

12 commit;

13 end;

14 /

PL/SQL procedure successfully completed.

SELECT:

REG_NO S_NAME DEPT YEAR DOB M1 M2 M3


TOT AVG
---------- ---------- ----- ----- --------- ---------- ---------- --------------------
--------------------- ---------------
1 Dhaarani BCA 2018 27-APR-98 98 96 92
286 95
2 Raji Eng 2018 10-DEC-98 96 92 86
274 91
3 Preetipal B.Com 2018 26-MAY-98 96 98 91
285 95
4 Keerthi Maths 2018 29-SEP-98 69 96 86
0 0
5 Anuja CS 2018 27-AUG-98 96 89 93
0 0
Updation of regno 4:
SQL> declare
2 vm1 number;

3 vm2 number;

4 vm3 number;

5 vtot number;

6 vavg number;

7 begin

8 select m1,m2,m3,tot,avg into vm1,vm2,vm3,vtot,vavg from surya-53 where


Reg_no='4';
9 vtot:=vm1+vm2+vm3;

10 vavg:=vtot/3;

11 update surya_53 set tot=vtot,avg=vavg where Reg_no='4';

12 commit;

13 end;

14 /

PL/SQL procedure successfully completed.


Select:
Updation of regno 4:
REG_NO S_NAME DEPT YEAR DOB M1 M2 M3
TOT AVG
---------- ---------- ----- ----- --------- ---------- ---------- --------------------
---------------------------
1 Dhaarani BCA 2018 27-APR-98 98 96 92 286 95
2 Raji Eng 2018 10-DEC-98 96 92 86 274 91
3 Preetipal B.Com 2018 26-MAY-98 96 98 91 285 95
4 Keerthi Maths 2018 29-SEP-98 69 96 86 251 84
5 Anuja CS 2018 27-AUG-98 96 89 93 0 0
SQL> declare
2 vm1 number;

3 vm2 number;

4 vm3 number;

5 vtot number;

6 vavg number;

7 begin

8 select m1,m2,m3,tot,avg into vm1,vm2,vm3,vtot,vavg from surya_53 where


Reg_no='5';
9 vtot:=vm1+vm2+vm3;

10 vavg:=vtot/3;

11 update surya_53 set tot=vtot,avg=vavg where Reg_no='5';

12 commit;

13 end;

14 /

PL/SQL procedure successfully completed.


Select:
Updation of regno 5:
REG_NO S_NAME DEPT YEAR DOB M1 M2 M3 TOT
AVG
---------- ---------- ----- ----- --------- ---------- ---------- --------------------
---------------------------
1 Dhaarani BCA 2018 27-APR-98 98 96 92 286
95
2 Raji Eng 2018 10-DEC-98 96 92 86 274
91 3 Preetipal B.Com 2018 26-MAY-98 96 98 91
285 95
4 Keerthi Maths 2018 29-SEP-98 69 96 86 251 84
5 Anuja CS 2018 27-AUG-98 96 89 93 278 93
Result:
Thus, the above PL/SQL block for marksheet processing is executed successfully.

EX NO : 8 CREATE PL/SQL FOR CURSOR


DATE:

OBJECTIVE:
To create a employee database using cursor.
DESCRIPTION:
A cursor is a temporary work area created in the system memory when a SQL statement is
executed. Implicit cursors are automatically generated by the Oracle engine. If a cursor is
opened for processing data through a PL/SQL block as per requirement like user defined
cursor, is known as an Explicit cursor.
ALGORITHM:
CREATE:
Create a table for employee database.
SELECT:
Select statement is used to select the particular fields from the database.
VIEW:
View command is used to view the detailed description of the data in the database.
DISPLAY:
Display the result of PL/SQL block for employee database.
CREATE PL/SQL FOR CURSOR.

CREATE:
SQL> create table ragu_26 (ID number(5),E_name varchar(10),desg varchar(5),salary
varchar(5)); Table created.
View:
SQL> desc ragu_26;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(5)
E_NAME VARCHAR2(10)
DESG VARCHAR2(25)
SALARY VARCHAR2(5)
Insert:
SQL> insert into ragu_26 values
('1','Albert','Programmer',’50000’);
1 row created.
SQL> insert into ragu_26
values('2','Anna','HR' ,’25000’); 1 row created.
SQL> insert into ragu_26 values('3','Mark','Analyst',’55000’);
1 row created.
SQL> insert into ragu_26 values('4','Jason','Content
writer',’21000’); 1 row created.
SQL> insert into
ragu_26values('5','Andrew','Programmer',’90000’);
1 row created.
Select:
SQL> select * from ragu_26;
ID E_NAME DESG SALARY
1 Albert Programmer 50000
2 Anna HR 25000
3 Mark Analyst 55000 5 Andrew Programmer 90000
4 Jason Content writer 21000
Procedure:
IMPLICIT:
DECLARE
total_rows
number(2);
BEGIN
UPDATE ragu_26 SET salary = salary + 1000; IF sql
%notfound THEN dbms_output.put_line('no Employee
updated'); ELSIF sql%found THEN total_rows := sql
%rowcount; dbms_output.put_line( total_rows || '
Employee updated ');
Procedure:
IMPLICIT:
DECLARE
total_rows
number(2);
BEGIN
UPDATE ragu_26 SET salary = salary + 1000; IF sql
%notfound THEN dbms_output.put_line('no Employee
updated'); ELSIF sql%found THEN total_rows := sql
%rowcount; dbms_output.put_line( total_rows || '
Employee updated ');
ID E_NAME DESG SALARY
1 Albert Programmer 51000
2 Anna HR 26000
3 Mark Analyst 56000
5 Andrew Programmer 91000
4 Jason Content writer 22000
EXPLICIT:
Procedure: DECLARE c_id
ragu_26.ID%type; c_name
ragu_26.E_name%type;
c_addr ragu_26.desg%type;
CURSOR c_employee is
SELECT ID, E_name, desg FROM ragu_26;
BEGIN
OPEN c_employee;
LOOP
FETCH c_employee into c_id, c_name, c_addr;
EXIT WHEN c_employee%notfound;
dbms_output.put_line(c_id||' '||c_name||' '||c_addr);
END LOOP;
CLOSE c_employee;
END;
/
Output
1 Albert Programmer

2 Anna HR

3 Mark Analyst

5 Andrew Programmer
4 Jason Content writer
Result:
Thus, the PL/SQL block for employee database is created successfully.

EX NO:9 CREATE A PROCEDURE FOR STOCK MANAGEMENT


DATABASE
DATE:

OBJECTIVE:
To create a procedure for stock management database using SQL.
DESCRIPTION:
A procedure is a subroutine like a subprogram in a regular computing language, stored in
database. SQL procedures can be used to create simple scripts for quickly querying
transforming, updating data, generating basic reports, improve application performance,
modularizing applications, and improve overall database design, and database security.
ALGORITHM:
CREATE:
To create a table with field name Prod_id, Prod_name, Price, Qty.
PROCEDURE:
Procedure is also an another way to insert rows to the existing table. It is also used to
make some updations to the existing table. We use notepad to enter the commands using
ed.
SELECT:
Select command is used to view the inserted rows in the existing table.
CREATE A PROCEDURE FOR STOCK MANAGEMENT DATABASE

Create:
SQL> create table sweet_35(Prod_id number(3) constraint p primary key,Prod_name
varchar(10),Price number(5),Qty number(3));
Table created.
View:
SQL> desc sweet_35;
Name Null? Type
----------------------------------------- -------- ----------------------------
PROD_ID NOT NULL NUMBER(3)
PROD_NAME VARCHAR2(10)
PRICE NUMBER(5)
QTY NUMBER(3)
Insert:
SQL> insert into sweet_35
values('001','Soap','35','10'); 1 row created.
SQL> insert into sweet_35
values('002','Powder','60','5'); 1 row created.
SQL> insert into sweet_35 values('003','Biscuit','40','8');
1 row created.
Select:
SQL> select * from sweet_35;
PROD_ID PROD_NAME PRICE QTY
---------- ---------- ---------- --------------------------------------
1 Soap 35 10
2 Powder 60 5
3 Biscuit 40 8
Procedure:
SQL> ed
Wrote file afiedt.buf
1 create or replace procedure stock_prog(pid1 in number,Qty1 in number)

2 is

3 oQty number(3);

4 nQty number(3);

5 pn varchar(12);

6 ppr varchar(5);

7 begin

8 select Qty into oQty from sweet_35 where Prod_id=pid1;

9 nQty:=oQty+Qty1;

10 update sweet_35 price set Qty=nQty where Prod_id=pid1;

11 dbms_output.put_line('Prod_id:'||pid1);

12 dbms_output.put_line('old Quantity:'||oQty);

13 dbms_output.put_line('new Quantity:'||nQty);

14 exception
15 when no_data_found then

16 pn:='&pname';

17 ppr:='&price';

18 insert into sweet_35 values(pid1,pn,ppr,Qty1);

19 dbms_output.put_line('New record inserted');

20 end;

SQL> /
Enter value for pname:
Shampoo old 16:
pn:='&pname'; new 16:
pn:='Shampoo'; Enter value
for price: 90

old 17: ppr:='&price';


new 17: ppr:='90';
Procedure created.
SQL> set serveroutput on;
SQL> / Enter value for
pname: Deo old 16:
pn:='&pname'; new 16:
pn:='Deo'; Enter value
for price: 60 old 17:
ppr:='&price'; new 17:
ppr:='60';
Procedure created.
SQL> execute stock_prog(0,5);
New record inserted
PL/SQL procedure successfully completed.
SQL> select * from sweet_35;
PROD_ID PROD_NAME PRICE QTY
---------- ---------- ---------- -----------------------
1 Soap 35 10
2 Powder 60 5 3 Biscuit
40 8
0 Deo 60 5 Result:
Thus, the above procedure for stock management database is executed successfully.
EX NO: 10 CREATE THE FUNCTION FOR REAL ESTATE
DATABASE DATE:

OBJECTIVE:
To create a real estate database using function.
DESCRIPTION:
A function is same as a procedure except that it returns a value.
ALGORITHM:
CREATE:
Create table RS_045 with the following fields Lan_id, Owner, Loc, Address,
No_of_bound,
Reg_date, Amt, C_no.
INSERT:
Insert the values to the corresponding field.
FUNCTION:
Create the function RS45_func for display the ones for corresponding Lan_id. Display
the result of add addressing using function.
CREATE THE FUNCTION FOR REAL ESTATE DATABASE

CREATE:
SQL> create table key_12 (lan_id number(2), owner varchar(10),location
varchar(10),address varchar(10),reg_date date,no_of_bound number(5),amt
number(6),c_no number(10));
Table created.
INSERT:
SQL> insert into key_12values('1','Dhaarani','Neyveli','KK nagar','12-Mar-18','5
','45000','8903090281');
1 row created.
SQL> insert into key_12values('2','Raji','Pudupet','T nagar','25-Apr-18','3','25
000','9486152261');
1 row created.
SQL> insert into key_12values('3','Deepthi','Chennai','Whitstone','18-Mar-18','6
','65000','9789520968');
1 row created.
SQL> insert into key_12values('4','Satheesh','Bangalore','Ashraya','15-Dec-18','
4','75000','9952534208');
1 row created.
SQL> insert into key_12 values('5','Selvam','Trichy','MK nagar','19-Feb-18','9','
67000','7509028520');
1 row created.
SELECT:
SQL> select * from key_12;
LAN_ID OWNER LOCATION ADDRESS REG_DATE NO_OF_BOUND
AMT C_NO
---------- ---------- ---------- ---------- --------- ----------- --------- ---------- ---------- ------------
----------- ------------- 1 Dhaarani Neyveli KK nagar 12-MAR-18
5 45000 8903090281
2 Raji Pudupet T nagar 25-APR-18 3 25000
9486152261
3 Deepthi Chennai Whitstone 18-MAR-18 6 65000
9789520968
4 Satheesh Bangalore Ashraya 15-DEC-18 4 75000
9952534208 5 Selvam Trichy MK nagar 19-FEB-18
9 67000 7509028520
FUNCTION:
SQL> create or replace function ragu_26
2 return varchar

3 is

4 fowner varchar(10);

5 begin

6 select owner into fowner

7 from ragu_26 where lan_id='5';

8 return fowner;

9 end;

10 /

Function created.
SQL> select ragu_22 from dual;
Ragu_22
--------------------------------------------------------------------------------
Selvam
SQL> commit;
Commit complete
RESULT:
Thus, the above function for real estate database is executed successfully.
EX NO: 11 CREATE DIFFERENT TYPES OF TRIGGERS FOR EVENT
DATE: MANAGEMENT DATABASE

OBJECTIVE:
To create different types of triggers for event management database.
DESCRIPTION:
A trigger is a stored procedure in database which automatically invokes whenever a
special event in the database occurs.
ALGORITHM:
CREATE:
Create a table for event management as event_45.
INSERT:
Insert values for the table.
SELECT:
Select command is used to view the inserted table.
TRIGGER:
Trigger is the one way to insert the rows to the existing table. You can view table before
and after trigger and you can give message to the viewer. By using the query set
serveroutput on you can insert the rows
CREATE DIFFERENT TYPES OF TRIGGERS FOR EVENT MANAGEMENT
DATABASE
Create:
SQL> create table ammu_92(E_id number(2),E_name varchar(10),E_date date,Hall_no
number(3),S_time varchar(5),E_time varchar(15));
Table created.
View:
SQL> desc ammu_92;
Name Null? Type
----------------------------------------- -------- ----------------------------
E_ID NUMBER(2)
E_NAME VARCHAR2(10)
E_DATE DATE
HALL_NO NUMBER(3)
S_TIME
VARCHAR2(5) E_TIME
VARCHAR2(15)
Insert:
SQL> insert into ammu_92 values('1','Debug','2-Feb-
19','10','9.30','10.30'); 1 row created.
SQL> insert into ammu_92 values('2','Azab','3-Feb-
19','11','10.30','11.30'); 1 row created.
SQL> insert into ammu_92 values('3','Quiz','4-Feb-
19','12','11.30','12.30'); 1 row created.
SQL> insert into ammu_92 values('4','Tech','5-Feb-
19','5','11.30','12.30'); 1 row created.
SQL> insert into ammu_92 values('5','Prog_W','6-Feb-19','1','11.30','12.30');
1 row created.
SQL> insert into ammu_92 values('6','PP','7-Feb-19','2','11.30','12.30');
1 row created.
Select:
SQL> select * from ammu_92;
E_ID E_NAME E_DATE HALL_NO S_TIM E_TIME
---------- ---------- --------- ---------- ----- ---------------------------------------------------
1 Debug 02-FEB-19 10 9.30 10.30
2 Azab 03-FEB-19 11 10.30 11.30
3 Quiz 04-FEB-19 12 11.30 12.30
4 Tech 05-FEB-19 5 11.30 12.30
5 Prog_W 06-FEB-19 1 11.30 12.30 6 PP
07-FEB-19 2 11.30 12.30
6 rows selected.
Before trigger:
SQL> create or replace trigger btrigger before insert or update on ammu_92
2 for each row

3 begin

4 if(:new.Hall_no>10) then

5 raise_application_error(-20001,'Enter the Hall_no less than 10');

6 end if;

7 end;

8/

Trigger created.
SQL> set serveroutput on;
SQL> insert into ammu_92 values('23','Tech','9-Feb-19','9','3.30','4.30');
Your registration is successfull and accepted
Thankyou, You are Welcome
1 row created.
SQL> insert into ammu_92 values('5','Seminar','12-Feb-19','8','2.30','3.30');
Your registration is successfull and accepted
Thankyou, You are Welcome
1 row created.
Select:
SQL> select * from ammu_92;
E_ID E_NAME E_DATE HALL_NO S_TIM E_TIME
---------- ---------- ------- -- ---------- ----- -------------------------------------------------
1 Debug 02-FEB-19 10 9.30 10.30
2 Azab 03-FEB-19 11 10.30 11.30
3 Quiz 04-FEB-19 12 11.30 12.30
4 Tech 05-FEB-19 5 11.30 12.30
5 Prog_W 06-FEB-19 1 11.30 12.30
6 PP 07-FEB-19 2 11.30 12.30
23 Tech 09-FEB-19 9 3.30 4.30
5 Seminar 12-FEB-19 8 2.30 3.30
8 rows selected.
After trigger:
SQL> create or replace trigger atrigger after insert or update on ammu_92
2 begin

3 dbms_output.put_line('Your registration is successfull and accepted');

4 dbms_output.put_line('Thankyou,you are Welcome');

5 end;

6/
Trigger created.
SQL> set serveroutput on;
SQL> insert into ammu_92 values('7','Prog_W','17-Feb-19','10','12.30','1.30');
Your registration is successfull and accepted
Thankyou,you are Welcome
1 row created.
Select:
SQL> select * from ammu_92;
E_ID E_NAME E_DATE HALL_NO S_TIM E_TIME
---------- ---------- ------- -- ---------- ----- -------------------------------------------------
1 Debug 02-FEB-19 10 9.30 10.30
2 Azab 03-FEB-19 11 10.30 11.30
3 Quiz 04-FEB-19 12 11.30 12.30
4 Tech 05-FEB-19 5 11.30 12.30
5 Prog_W 06-FEB-19 1 11.30 12.30
6 PP 07-FEB-19 2 11.30 12.30
23 Tech 09-FEB-19 9 3.30 4.30
5 Seminar 12-FEB-19 8 2.30
3.30 7 Prog_W 17-FEB-19 10
12.30 1.30
9 rows selected.
RESULT:
Thus, the creation of different types of trigger for event management database is executed
successfully.

Result:
Thus, the table of student mark details created using DDL,DML,TCL,DCL are
successfully create.

Result:
Thus, the library database using aggregate function and set operation are executed
successfully.

You might also like