You are on page 1of 49

IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 01
SINGLE LINE QUERY AND GROUP FUNCTION
DATE: 19.02.2023

AIM
To create a single line query and group function using simple queries.

E-R DIAGRAM

DESCRIPTION

CREATE

Create command is also used to create a table.


Syntax:
create table table-name{column-name1 datatype1, column-name2 datatype2,
column-name3 datatype3};
INSERT
Insert command is used to insert data into a table.

Syntax:
INSERT into table-name values(data1,data2,..);

GROUP FUCTION
The GROUP BY statement groups rows that have the same values into
summary rows, ( COUNT() , MAX() , MIN() , SUM() , AVG() ). The types of group
functions (also called aggregate functions).

1. avg: average value


2. min: minimum value
3. max:maximum value
4. sum: sum of values
5. count: number of values

RDBMS LAB [CPCA46] 1


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

AVG()
Average returns average value after calculating from values in a numeric
column.

Syntax:
SELECT AVG(column_name) from table_name
COUNT()
Count returns the number of rows present in the table either based on some
condition or without condition.
Its general Syntax is,
SELECT COUNT(column_name) from table-name
MAX()
MAX function returns maximum value from selected column of the table.
Syntax of MAX function is,
SELECT MAX(column_name) from table-name
MIN()
MIN function returns minimum value from a selected column of the table.
Syntax for MIN function is,
SELECT MIN(column_name) from table-name
SUM()
SUM function returns total sum of a selected columns numeric values.
Syntax for SUM is,
SELECT SUM(column_name) from table-name

RDBMS LAB [CPCA46] 2


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

1.SINGLE LINE QUERY AND GROUP FUNCTION


TABLE CREATION
create table custo(id number(10)primary key,namevarchar(20),address varchar(20),
salary number(10));

Table created.

INSERTION
insert into custo values(1,’Ramesh’,’Chennai’,4000);
1 row(s) inserted.

insert into custo values(2,’Krishna’,’Delhi’,1500);


1 row(s) inserted.

insert into custo values(3,’Keerthi’,’Mumbai’,4500);


1 row(s) inserted.

insert into custo values(4,’Chaitali’,’Indore’,10000);


1 row(s) inserted.

select * from custo;

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Chennai 4000
2 Krishna Delhi 1500
3 Keerthi Mumbai 4500
4 Chaitali Indore 10000

AVG()
selectavg(salary) from custo;

AV G ( S AL AR Y)
5000

MAX()
select max(salary) from custo;

M AX ( S AL AR Y)
10000

RDBMS LAB [CPCA46] 3


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

MIN()
select min(salary) from custo;

M IN( S AL AR Y)
1500

SUM()

select sum(salary) from custo;

SU M( S AL AR Y)
20000
COUNT()

select count(salary) from custo;

C OU N T(S AL AR Y )
4

RESULT

Thus the table was created and also performed successfully.

RDBMS LAB [CPCA46] 4


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 02
DDL COMMANDS
DATE: 27.01.2023

AIM
To create and execute a DDL commands using query.

E-R DIAGRAM

DESCRIPTION

Data Definition Language (DDL)

DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
o All the command of DDL are auto-committed that means it permanently save
all the changes in the database.

DDL COMMANDS
o CREATE
o ALTER
o DROP
o TRUNCATE

CREATE
It is used to create a new table in the database.
Syntax: Java Program for Beginners
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
DROP
It is used to delete both the structure and record stored in the table.
Syntax:
DROP TABLE table_name;

ALTER

It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.

RDBMS LAB [CPCA46] 5


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

Syntax:

To add a new column in the table

ALTER TABLE table_name ADD column_name COLUMN-definition;

To modify existing column in the table:

ALTER TABLE table_name MODIFY (column_definitions....);

TRUNCATE

It is used to delete all the rows from the table and free the space containing the
table.

Syntax:

TRUNCATE TABLE table_name;

RDBMS LAB [CPCA46] 6


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

2. DDL COMMANDS
TABLE CREATION
create table emp(Empid number(10),Name varchar(20), Address varchar(20),
Designation varchar(20),Salary number(10),pf number(10));
Table created.

TABLE DESCRIPTION

desc emp;

T ab le Co lum n D ata Ty pe L engt h Precisio n Pr im ary K ey N ullab le D efa ult


EMP EMPID Number - 10 - -

NAME Varchar2 20 - - -

ADDRESS Varchar2 20 - - -

DESIGNATION Varchar2 20 - - -

SALARY Number - 10 - -

PF Number - 10 - -

ALTER

Modify:

alter table emp modify(address varchar(30));


Table altered.

T ab le Co lum n D ata Ty pe L engt h Precisio n Pr im ary K ey N ullab le D efa ult


EMP EMPID Number - 10 - -

NAME Varchar2 20 - - -

ADDRESS Varchar2 30 - - -

DESIGNATION Varchar2 20 - - -

SALARY Number - 10 - -

PF Number - 10 - -

Add:

alter table emp add(da number(10));


Table altered.

T ab le Co lum n D ata Ty pe L engt h Precisio n Pr im ary K ey N ullab le D efa ult


EMP EMPID Number - 10 - -

NAME Varchar2 20 - - -

ADDRESS Varchar2 30 - - -

DESIGNATION Varchar2 20 - - -

SALARY Number - 10 - -

PF Number - 10 - -

DA Number - 10 - -

RDBMS LAB [CPCA46] 7


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

INSERTION

insert into emp values(101,'Anbu','2 car st,Chennai','Manager',15000,1200);


1 row(s) inserted.

insert into emp values(102,'Baby','2/4 Nehru st,Delhi','Ast.Manager',19500,1350);


1 row(s) inserted.

insert into emp values(103,'Chintu','Temple st,Noida','Designer',12000,900);


1 row(s) inserted.

insert into emp values(104,'Durga','Koil st,Kovai','Clerk',9000,750);


1 row(s) inserted.

insert into emp values(105,'Ezhil','15/3 Kri,Mumbai','Gen Manager',25000,1800);


1 row(s) inserted.

select * from emp;

EM P ID N AM E AD D R E S S DE S IG N AT I ON S AL AR Y PF
101 Anbu 2 car st,Chennai Manager 15000 1200
102 Baby 2/4 Nehru st,Delhi Asst.Manager 19500 1350
103 Chintu Temple st,Noida Designer 12000 900
104 Durga Koil st,Kovai Clerk 9000 750
105 Ezhil 15/3 Krish,Mumbai Gen Manager 25000 1800

ALTER
Add:
alter table emp add(da number(10));
Table altered.

Select * from emp;

EM P ID N AM E AD D R E S S DE S IG N AT I ON S AL AR Y PF DA
101 Anbu 2 car st,Chennai Manager 15000 1200 -
102 Baby 2/4 Nehru st,Delhi Asst.Manager 19500 1350 -
103 Chintu Temple st,Noida Designer 12000 900 -
104 Durga Koil st,Kovai Clerk 9000 750 -
105 Ezhil 15/3 Krish,Mumbai Gen Manager 25000 1800 -

RDBMS LAB [CPCA46] 8


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

RENAME
rename emp to emp1;
Statement processed.

Select * from emp1;

EM P ID N AM E AD D R E S S DE S IG N AT I ON S AL AR Y PF DA
101 Anbu 2 car st,Chennai Manager 15000 1200 -
102 Baby 2/4 Nehru st,Delhi Asst.Manager 19500 1350 -
103 Chintu Temple st,Noida Designer 12000 900 -
104 Durga Koil st,Kovai Clerk 9000 750 -
105 Ezhil 15/3 Krish,Mumbai Gen Manager 25000 1800 -

Truncate

truncate table emp;


Table truncated.

select * from emp1;

no data found.

Drop

drop table emp;


Table droped.

RESULT

Thus the table was created and performed also successfully.

RDBMS LAB [CPCA46] 9


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 03
DML COMMANDS
DATE: 02.02.2023

AIM
To create and execute a DML commands using query.

E-R DIAGRAM

DESCRIPTION

Data Manipulation Language


DML commands are used to modify the database. It is responsible for all form
of changes in the database.The command of DML is not auto-committed that means it
can't permanently save all the changes in the database. They can be rollback.

DML COMMANDS
o INSERT
o UPDATE
o DELETE
o SELECT

INSERT
It is used to insert data into the row of a table.
Syntax
INSERT INTO TABLE_NAME (col1, col2,.. col N) VALUES (value1, value2,.. valueN);
Or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
UPDATE
It is used to update or modify the value of a column in the table.
Syntax
UPDATE table_name SET [col_name1= value1,...col_nameN = valueN] [where condition];

DELETE
It is used to remove one or more row from a table.
Syntax
DELETE FROM table_name [WHERE condition];

RDBMS LAB [CPCA46] 10


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

3.DML COMMANDS

TABLE CREATION
create table emp(Empid number(10),Name varchar(20), Address varchar(20),
Designation varchar(20),Salary number(10),pf number(10));
Table created.

TABLE DESCRIPTION

desc emp;

T ab le Co lum n D ata Ty pe L engt h Precisio n Pr im ary K ey N ullab le D efa ult

EMP EMPID Number - 10 - -

NAME Varchar2 20 - - -

ADDRESS Varchar2 20 - - -

DESIGNATION Varchar2 20 - - -

SALARY Number - 10 - -

PF Number - 10 - -

INSERTION

insert into emp values(101,'Anbu','2 car st,Chennai','Manager',15000,1200);


1 row(s) inserted.

insert into emp values(102,'Baby','2/4 Nehru st,Delhi','Ast.Manager',19500,1350);


1 row(s) inserted.

insert into emp values(103,'Chintu','Temple st,Noida','Designer',12000,900);


1 row(s) inserted.

insert into emp values(104,'Durga','Koil st,Kovai','Clerk',9000,750);


1 row(s) inserted.

insert into emp values(105,'Ezhil','15/3 Kri,Mumbai','Gen Manager',25000,1800);


1 row(s) inserted.

RDBMS LAB [CPCA46] 11


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

select * from emp;

EM P ID N AM E AD D R E S S DE S IG N AT I ON S AL AR Y PF
101 Anbu 2 car st,Chennai Manager 15000 1200
102 Baby 2/4 Nehru st,Delhi Asst.Manager 19500 1350
103 Chintu Temple st,Noida Designer 12000 900
104 Durga Koil st,Kovai Clerk 9000 750
105 Ezhil 15/3 Krish,Mumbai Gen Manager 25000 1800

SELECT
SINGLE ROW

select name from emp;

N AM E
Anbu
Baby
Chintu
Durga
Ezhil

Select where IN
select * from emp where salary in 15000;

EM P ID N AM E AD D R E S S DE S IG N AT I ON S AL AR Y PF
103 Chintu Temple st,Noida Designer 13000 900
104 Durga Koil st,Kovai Clerk 10000 750

UPDATION
update emp set salary=salary+1000;
4 row(s) updated.

select * from emp;

EM P ID N AM E AD D R E S S DE S IG N AT I ON S AL AR Y PF
101 Anbu 2 car st,Chennai Manager 16000 1200
102 Baby 2/4 Nehru st,Delhi Asst.Manager 20500 1350
103 Chintu Temple st,Noida Designer 13000 900
104 Durga Koil st,Kovai Clerk 10000 750
105 Ezhil 15/3 Krish,Mumbai Gen Manager 26000 1800

RDBMS LAB [CPCA46] 12


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

update emp set name=’Kavin’ where empid=102;


1 row(s) updated.

select * from emp;


EM P ID N AM E AD D R E S S DE S IG N AT I ON S AL AR Y PF
101 Anbu 2 car st,Chennai Manager 16000 1200
102 Kavin 2/4 Nehru st,Delhi Asst.Manager 20500 1350
103 Chintu Temple st,Noida Designer 13000 900
104 Durga Koil st,Kovai Clerk 10000 750
105 Ezhil 15/3 Krish,Mumbai Gen Manager 26000 1800

DELETION
delete from emp where empid='103';
1 row(s) deleted.

select * from emp;

EM P ID N AM E AD D R E S S DE S IG N AT I ON S AL AR Y PF
101 Anbu 2 car st,Chennai Manager 16000 1200
102 Baby 2/4 Nehru st,Delhi Asst.Manager 20500 1350
104 Durga Koil st,Kovai Clerk 10000 750
105 Ezhil 15/3 Krish,Mumbai Gen Manager 26000 1800

delete from emp;


4 row(s) deleted.

select * from emp;


no data found.

RESULT

Thus the table was created and performed also successfully.

RDBMS LAB [CPCA46] 13


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 04
DCL AND TCL COMMANDS
DATE: 10.02.2023

AIM
To create and execute a DCL and TCL commands using query.

E-R DIAGRAM

DESCRIPTION

DCL :Data Control Language


DCL commands are used to grant and take back authority from any
database user.
Here are some commands that come under DCL:
o Grant
o Revoke
Grant
It is used to give user access privileges to a database.
Syntax
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
Revoke
It is used to take back permissions from the user.
Syntax

REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

TCL : Transaction Control Language

TCL commands can only use with DML commands like INSERT, DELETE and
UPDATE only. These operations are automatically committed in the database that's
why they cannot be used while creating tables or dropping them.

o COMMIT
o ROLLBACK
o SAVEPOINT

RDBMS LAB [CPCA46] 14


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

Commit

Commit command is used to save all the transactions to the database.

Syntax

COMMIT;

Rollback

Rollback command is used to undo transactions that have not already been
saved to the database.

Syntax

ROLLBACK;

SAVEPOINT

It is used to roll the transaction back to a certain point without rolling back the
entire transaction.

Syntax

SAVEPOINT SAVEPOINT_NAME;

RDBMS LAB [CPCA46] 15


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

4. DCL COMMANDS & TCL COMMANDS

TABLE CREATION

Create Table Employee(Emp_Name VARCHAR2(20), DOB number(6), Mobile


number(10), Email VARCHAR2(20));
Table created.

desc emp;

Tabl e Colu mn Da ta Typ e Le ng th Pr eci sion Prim ary K ey Null abl e De f aul t

EMP Emp_Name Varchar 20 - - -

DOB Number - 6 - -

mobile Number - 10 - -

Email Varchar 20 - - -

INSERTION

insert into employee values('Joe', 040493, 7812865845, 'joe@gmail.com');

1 row inserted

insert into employee values('abi', 060693, 1256348953, 'abi@gmail.com');

1 row inserted

insert into employee values('viji', 120493, 9965231256, 'viji@gmail.com');

1 row inserted

insert into employee values('anu', 030493, 4563217896, 'joe@gmail.com');

1 row inserted

select * from employee;

EM PNA M E DO B MOB I LE EM AI L
Joe 04493 464525655 joe@gmail.com

abi 0693 1256348953 abi@gmail.com

viji 120493 9965231256 viji@gmail.com

anu 30493 4563217896 joe@gmail.com

RDBMS LAB [CPCA46] 16


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

DCL

Grant
GRANT SELECT, UPDATE ON employee TO System;

ORA-01749: you may not GRANT/REVOKE privileges to/from yourself

REVOKE SELECT, UPDATE ON employee FROM system;

ORA-01749: you may not GRANT/REVOKE privileges to/from yourself

TCL

delete from employee where emp_name = 'abi';

1 row(s) deleted.

select * from employee;

EM PNA M E DO B MOB I LE EM AI L
Joe 04493 464525655 joe@gmail.com
viji 120493 9965231256 viji@gmail.com

anu 30493 4563217896 joe@gmail.com

commit;
Commit statement not applicable. All statements are automatically committed.

Rollback and savepoint

DELETE FROM employee WHERE Emp_Name = 'abi';

EM PNA M E DO B MOB I LE EM AI L
Joe 04493 464525655 joe@gmail.com
viji 120493 9965231256 viji@gmail.com

anu 30493 4563217896 joe@gmail.com

ROLLBACK;
Sql > Rollback statement not applicable. All statements are automatically committed

RESULT

Thus the table was created and performed also successfully.

RDBMS LAB [CPCA46] 17


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 05
NESTED QUERIES
DATE: 17.02.2023

AIM

To create a table using nested queries.

E-R DIAGRAM

DESCRIPTION

NESTED QUERIES

SQL provides a mechanism for the nesting of subqueries.A subquery is a


select-from-where expression that is nested within another query and it nested
inside another query and it returns intermediate output. The output of inner query is
put in the condition of the outer query.

The output of inner query is put in the condition of the outer query.

(i)in

The in connective tests for set membership, where the set is a collection of
values produced by a select clause.

(ii)not in

The not in connective tests for the absence of set membership.

RDBMS LAB [CPCA46] 18


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

CORRELATED QUERY

A correlated subquery is a nested subquery that is executed once for each


‘candidate row’ considered by the main query(outer query). The correlated subquery
which is within parentheses uses a value from a column in the outer query. The
subquery is executed repeatedly once for each row of the main-outer query table.

SOME

SQL allows <some,>some,<=some,>=some,=some,and<>some comparisons.

 =some is identical to in and <>some is identical to not in.

EXISTS

The existsconstruct returns the value true if the argument subquery is


nonempty.

ALL

ALL operator, in contrast to ANY operator, returns the maximum value from a
given set of values.

NOT EXISTS

The EXISTS operator is used in correlated subqueries, generally. It checks if a


value is there, and then it returns TRUE else FALSE. If subqueryreturns NULL then
NOT EXISTS is more appropriate to use.

RDBMS LAB [CPCA46] 19


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

5. NESTED QUERIES
TABLE CREATION:1
create table bk(bkid number(10)primary key,Titlevarchar(20),Author varchar(20),Price
number(20),Year number(20));

Table created.

INSERTION
insert into bk values(101,'DBMS','Rajesh Narang',450,2008);
1 row(s) inserted.

insert into bk values(102,'OS','Silberscath',350,2005);


1 row(s) inserted.

insert into bk values(103,'Artificial','Narang',200,2009);


1 row(s) inserted.

insert into bk values(104,'Designer','Basu',400,2010);


1 row(s) inserted.

select * from bk;


BK ID T IT L E AU T H OR PR IC E YE AR
101 DBMS Rajesh Narang 450 2008
102 OS Silberscath 350 2005
103 Artificial Narang 200 2009
104 Designer Basu 400 2010

TABLE CREATION:2

create table ord_dt(orid number(10),bkid number(10),quantity number(10));


INSERTION
insert into ord_dt values(11,101,25);
1 row(s) inserted.

insert into ord_dt values(12,102,50);


1 row(s) inserted.

insert into ord_dt values(13,105,45);


1 row(s) inserted.

select * from ord_dt;

OR ID BK ID QU AN T IT Y
11 101 25
12 102 50
13 105 45

RDBMS LAB [CPCA46] 20


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

IN
selecttitle,author,price from bk where year in('2005');

T IT L E AU T H OR PR IC E
OS Silberscath 350
NOT IN
selecttitle,author,price from bk where year not in('2005');

T IT L E AU T H OR PR IC E
DBMS Rajesh Narang 450
Artificial Narang 200
Designer Basu 400

SOME
select distinct title from bk where price>some (select price from bk where year='2005');
T IT L E
Designer
DBMS

ALL
select distinct title from bk where price>all (select price from bk where year='2010');
T IT L E
DBMS
EXISTS
select title from bk where exists (select *from ord_dt where bk.bkid=ord_dt.bkid);
T IT L E
DBMS
OS
NOT EXISTS
select title from bk where not exists (select *from ord_dt where bk.bkid=ord_dt.bkid);
T IT L E
Artificial
Designer

RESULT

Thus the table was created and also performed successfully.

RDBMS LAB [CPCA46] 21


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 06
JOIN OPERATIONS
DATE: 22.02.2023

AIM

To create a table to perform join relation.

E-R DIAGRAM

DESCRIPTION

Joins are used to combine columns from different tables. The tables can be
connected using WHERE clause.
Types of Joins are:
1. Equi Join
2. Cross Joins
3. Outer Joins
4. Self-Join
Syntax of a Join is:
SELECT <select-list>FROM <tablel>, <table 2>, ...<table n>
WHERE ctable 1, column 1> = <table 2, column 2> AND
<table n-1, column i> = <table n, column j>
Equi-Join
Equi-Join joins two tables on the basis of equality of one or more columns.
Syntax
SELECT column-name-list from table-name1 INNER JOIN (or) JOIN table-name2
WHERE table-name1.column-name = table-name2.column-name;
Natural Join
Natural Join is a type of Inner join which is based on column having same
name and same data type present in both the tables to be joined.

Syntax
SELECT * from table-name1 NATURAL JOIN table-name2;

RDBMS LAB [CPCA46] 22


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

Cross Join

If WHERE Clause is not specified in a SELECT statement then each row of one
table is joined to all the rows of another table. The result is a Cartesian product of the
tuples(rows) of two tables.

Syntax is,
SELECT column-name-list from table-name1 CROSS JOIN table-name2;
Outer Join
Outer Join is based on both matched and unmatched data. Outer Joins
subdivide further into,

1. Left Outer Join


2. Right Outer Join
3. Full Outer Join

Left Outer Join


The left outer join returns a result table with the matched data of two tables
then remaining rows of the left table and null for the right table's column.

Left Outer Join syntax is,


SELECT column-name-list from table-name1 LEFT OUTER JOIN table-
name2on table-name1.column-name = table-name2.column-name;
Right Outer Join
The right outer join returns a result table with the matched data of two tables
then remaining rows of the right table and null for the left table's columns.
Right Outer Join Syntax is,

select column-name-list from table-name1 RIGHT OUTER JOIN table-name2


on table-name1.column-name = table-name2.column-name;
Full Outer Join
The full outer join returns a result table with the matched data of two table
then remaining rows of both left table and then the right table.
Full Outer Join Syntax is,

select column-name-list from table-name1 FULL OUTER JOIN table-name2


on table-name1.column-name = table-name2.column-name;

Self-Join
It is an equi-join on the same table. Suppose we want to get a list of titles
which have same price in table title.

RDBMS LAB [CPCA46] 23


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

6. TABLE CREATION WITH VARIOUS JOIN


TABLE CREATION:1
create table t1(id number(20),Name varchar(20));
Table created.

INSERTION
insert into t1 values(11,'Anu');
1 row(s) inserted.

insert into t1 values(12,'Baby');


1 row(s) inserted.

insert into t1 values(13,'Chintu');


1 row(s) inserted.

select * from t1;

ID N AM E
11 Anu
12 Baby
13 Chintu

TABLE CREATION: 2
create table t2 (id number(20),Address varchar(20));
Table created.

INSERTION
insert into t2 values(11,'Delhi');
1 row(s) inserted.

insert into t2 values(13,'Mumbai');


1 row(s) inserted.

insert into t2 values(14,'Chennai');


1 row(s) inserted.

RDBMS LAB [CPCA46] 24


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

select * from t2;

ID AD D R E S S
11 Delhi
13 Mumbai
14 Chennai
INNER JOIN
selectid,address from t2 inner join t1 using(id);
ID AD D R ESS

11 Delhi
13 Mumbai

NATURAL JOIN
selectid,address from t1 natural join t2;
ID AD D R ESS

11 Delhi
13 Mumbai

LEFT OUTER JOIN


select * from t1 left outer join t2 on(t1.id=t2.id);
ID N AM E ID AD D R ESS

11 Anu 11 Delhi
13 Chintu 13 Mumbai
12 Baby - -

RIGHT OUTER JOIN


select * from t1 right outer join t2 on(t1.id=t2.id);
ID N AM E ID AD D R ESS

11 Anu 11 Delhi
13 Chintu 13 Mumbai
- - 14 Chennai

FULL OUTER JOIN

select * from t1 full outer join t2 on(t1.id=t2.id);


ID N AM E ID AD D R ESS

11 Anu 11 Delhi
13 Chintu 13 Mumbai
12 Baby - -
- - 14 Chennai

RDBMS LAB [CPCA46] 25


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

CROSS JOIN
selectname,address from t1 cross join t2;
N AM E AD D R ESS

Anu Delhi
Anu Mumbai
Anu Chennai
Baby Delhi
Baby Mumbai
Baby Chennai
Chintu Delhi
Chintu Mumbai
Chintu Chennai

SELF JOIN
selecta.name,b.address from t1 a,t2 b where a.id<b.id;
N AM E AD D R ESS

Anu Mumbai
Anu Chennai
Baby Mumbai
Baby Chennai
Chintu Chennai

THETA JOIN
selectname,address from t1 a,t2 b where a.id<=b.id;
N AM E AD D R ESS

Anu Delhi
Anu Mumbai
Anu Chennai
Baby Mumbai
Baby Chennai
Chintu Mumbai
Chintu Chennai

RESULT

Thus the table was created and performed also successfully.

RDBMS LAB [CPCA46] 26


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 07
VIEW
DATE: 02.03.2023

AIM

To create a table for view creation and manipulation.

E-R DIAGRAM

DESCRIPTION

VIEW CREATION
A view is nothing more than a SQL statement that is stored in the database
with an associated name. A view is actually a composition of a table in the form of a
predefined SQL query.

A view can contain all rows of a table or select rows from a table. A view can be
created from one or many tables which depends on the written SQL query to create a
view.
Creating Views

Database views are created using the CREATE VIEW statement. Views can be
created from a single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege according
to the specific implementation.
Syntax
CREATE VIEW view_name AS
SELECT column1, column2..... FROM table_name WHERE [condition];

The WITH CHECK OPTION


The WITH CHECK OPTION is a CREATE VIEW statement option. The purpose
of the WITH CHECK OPTION is to ensure that all UPDATE and INSERTs satisfy the
condition(s) in the view definition.
If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.

RDBMS LAB [CPCA46] 27


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

Updating a View
A view can be updated under certain conditions which are given below −

 The SELECT clause may not contain the keyword DISTINCT.


 The SELECT clause may not contain summary functions.
 The SELECT clause may not contain set functions.
 The SELECT clause may not contain set operators.
 The SELECT clause may not contain an ORDER BY clause.
 The FROM clause may not contain multiple tables.
 The WHERE clause may not contain subqueries.
 The query may not contain GROUP BY or HAVING.
 Calculated columns may not be updated.

Inserting Rows into a View

Rows of data can be inserted into a view. The same rules that apply to the
UPDATE command also apply to the INSERT command.

Deleting Rows into a View

Rows of data can be deleted from a view. The same rules that apply to the
UPDATE and INSERT commands apply to the DELETE command.
[[

Dropping Views

Obviously, where we have a view, we need a way to drop the view if it is no


longer needed.
The syntax is very simple and is given below −

DROP VIEW view_name;

RDBMS LAB [CPCA46] 28


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

7. VIEW CREATION
TABLE CREATION
create table cu(id number(10)primary key,namevarchar(20),address varchar(20),
salary number(10));

Table created.

INSERTION
insert into custo values(1,’Ramesh’,’Chennai’,2000);
1 row(s) inserted.

insert into custo values(2,’Krishna’,’Delhi’,1500);


1 row(s) inserted.

insert into custo values(3,’Keerthi’,’Mumbai’,2000);


1 row(s) inserted.

insert into custo values(4,’Chaitali’,’Indore’,6500);


1 row(s) inserted.

insert into custo values(5,’Hardik’,’Bhopal’,8500);


1 row(s) inserted.

select * from cu;

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Chennai 2000
2 Krishna Delhi 1500
3 Keerthi Mumbai 2000
4 Chaitali Indore 6500

5 Hardik Bhopal 8500

VIEW
create view cu_view asselect name, address from cu;

N AM E AD D R E S S
Ramesh Chennai
Krishna Delhi
Keerthi Mumbai
Chaitali Indore

Hardik Bhopal

RDBMS LAB [CPCA46] 29


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

WITH CHECK OPTION


create view cu_view as
select name, address from cuwhere address is not nullwith check option;

UPDATION
updatecu_view set address = bangalorewhere name = 'ramesh';

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Bangalore 2000
2 Krishna Delhi 1500
3 Keerthi Mumbai 2000
4 Chaitali Indore 6500

5 Hardik Bhopal 8500

DELETION
delete from cu_view where address = ‘delhi’;

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Bangalore 2000
3 Keerthi Mumbai 2000
4 Chaitali Indore 6500

5 Hardik Bhopal 8500

RESULT

Thus the table was created and also performed successfully.

RDBMS LAB [CPCA46] 30


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 08
LOCKS
DATE: 13.03.2023

AIM

To create a table for view creation and manipulation.

E-R DIAGRAM

DESCRIPTION
To lock a table using the MySQL LOCK TABLES Statement you need have the
TABLE LOCK and SELECT privileges.

These locks are used to solve the concurrency problems. There are two kinds of
MYSQL table locks −

 READ LOCK − If you apply this lock on a table the write operations on it are
restricted. i.e., only the sessions that holds the lock can write into this table.

 WRITE LOCK − This lock allows restricts the sessions (that does not possess
the lock) from performing the read and write operations on a table.

Command: LOCK TABLES table_name READ|WRITE;

Syntax (READ LOCK):

LOCK TABLES table-name read;

LOCK TABLES table-name write;

RDBMS LAB [CPCA46] 31


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

8.LOCKS
CREATION
create table stu(name varchar(3),mark int);
INSERT
insert into stu values('abi',50);
1 row(s) inserted.

insert into stu values('ani',50);


1 row(s) inserted.

LOCKING A TABLE

The following statement locks the stu table in exclusive mode but
does not wait if another user already has locked the table:
LOCK TABLE stu
IN EXCLUSIVE MODE
NO WAIT;

Statement processed.

LOCK TABLE stu


IN SHARE MODE
NOWAIT;
Statement processed.

The following statement locks the remote stu table that is accessible
through the database link remote:
LOCK TABLE stu@remote
IN SHARE MODE;
Connection description forremote database not found.

LOCK AND UNLOCK A USER ACCOUNT


If we want to lock a user account for a short period of time, and unlock it
later, we can use the ALTER USER ... ACCOUNT command

ALTER USER system ACCOUNT LOCK


User altered.

ALTER USER system ACCOUNT UNLOCK


User altered.

RESULT

Thus the table was created and also performed successfully.

RDBMS LAB [CPCA46] 32


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 09
EXCEPTION HANDLING
DATE: 21.03.2023

AIM
To create a table for exception handling using PL/SQL.

E-R DIAGRAM

DESCRIPTION
Exception Handling

An error occurs during the program execution is called Exception in PL/SQL.


PL/SQL facilitates programmers to catch such conditions using exception block in the
program and an appropriate action is taken against the error condition.

There are two type of exceptions:

o System-defined Exceptions
o User-defined Exceptions

Syntax

DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;

RDBMS LAB [CPCA46] 33


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

PRE-DEFINED EXCEPTIONS:

Exception Description

ACCESS_INTO_NULL It is raised when a NULL object is automatically assigned


a value.

CASE_NOT_FOUND It is raised when none of the choices in the "WHEN"


clauses of a CASE statement is selected, and there is no
else clause.

COLLECTION_IS_NULL It is raised when a program attempts to apply collection


methods other than exists to an uninitialized nested table
or varray, or the program attempts to assign values to the
elements of an uninitialized nested table or varray.

DUP_VAL_ON_INDEX It is raised when duplicate values are attempted to be


stored in a column with unique index.

INVALID_CURSOR It is raised when attempts are made to make a cursor


operation that is not allowed, such as closing an unopened
cursor.

INVALID_NUMBER It is raised when the conversion of a character string


into a number fails because the string does not represent
a valid number.

LOGIN_DENIED It is raised when s program attempts to log on to the


database with an invalid username or password.

NO_DATA_FOUND It is raised when a select into statement returns no rows.

NOT_LOGGED_ON It is raised when a database call is issued without being


connected to the database.

PROGRAM_ERROR It is raised when PL/SQL has an internal problem.

ROWTYPE_MISMATCH It is raised when a cursor fetches value in a variable


having incompatible data type.

SELF_IS_NULL It is raised when a member method is invoked, but the


instance of the object type was not initialized.

STORAGE_ERROR It is raised when PL/SQL ran out of memory or memory was


corrupted.

TOO_MANY_ROWS It is raised when a SELECT INTO statement returns more


than one row.

VALUE_ERROR It is raised when an arithmetic, conversion, truncation,


or size-constraint error occurs.

ZERO_DIVIDE It is raised when an attempt is made to divide a number by


zero.

RDBMS LAB [CPCA46] 34


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

9.EXCEPTION HANDLING

TABLE CREATION

create table cu(id number(10),name varchar(20), age number(5),address varchar(20),


salary number(20));
Table created.

INSERTION
insert into cu values(1,'Ramesh',23,'Chennai',2000);
1 row(s) inserted.

insert into cu values(2,'suresh',22,'kanpur',22000);


1 row(s) inserted.

insert into cu values(3,'mahesh',24,'ghaziabad',24000);


1 row(s) inserted.

insert into cu values(4,'Chandan',25,'noida',26000);


1 row(s) inserted.

insert into cu values(5,'alex',21,'paris',28000);


1 row(s) inserted.

select* from cu;

ID NAME AGE ADDRESS SALAR

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

3 Mahesh 24 Ghaziabad 24000

4 Chandan 25 Noida 26000

5 Alex 21 Paris 28000

RDBMS LAB [CPCA46] 35


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

PREDEFINED FUNCTIONS
DECLARE
c_id cu.id%type := 8;
c_name cu.name%type;
c_addr cu.address%type;
BEGIN
SELECT name,address INTO c_name, c_addr FROM cu WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;

No such customer!
PL/SQL procedure successfully completed.

EXCEPTION
DECLARE
c_id cu.id%type := 5;
c_name cu.name%type;
c_addr cu.address%type;
BEGIN
SELECT name,address INTO c_name, c_addr FROM cu WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;

Name :Alex
Address :Paris
PL/SQL procedure successfully completed.

RESULT

Thus the table was created and also performed successfully.

RDBMS LAB [CPCA46] 36


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 10
CURSOR
DATE: 31.03.2023

AIM

To create a table using PL/SQL in Cursor.

E-R DIAGRAM

DESCRIPTION

CURSOR

A cursor is a pointer to this context area. PL/SQL controls the context area
through a cursor. A cursor holds the rows (one or more) returned by a SQL statement.
The set of rows the cursor holds is referred to as the active set.
We can name a cursor so that it could be referred to in a program to fetch and
process the rows returned by the SQL statement, one at a time.

There are two types of cursors −


1. Implicit cursors
2. Explicit cursors
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL
statement is executed, when there is no explicit cursor for the statement. Programmers
cannot control the implicit cursors and the information in it.
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over
the context area. An explicit cursor should be defined in the declaration section of the
PL/SQL Block. It is created on a SELECT Statement which returns more than one row.
Syntax

CURSOR cursor_name IS select_statement;


Step for Using Explicit Cursor

1. Open cursor
2. Fetch cursor
3. Close cursor

RDBMS LAB [CPCA46] 37


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

Opening the Cursor


Opening the cursor allocates the memory for the cursor and makes it ready for
fetching the rows returned by the SQL statement into it.

The General Form is :


OPEN cursorname;
Fetching the Cursor
Fetching the cursor involves accessing one row at a time.
The General Form is:

FETCH cursor name INTO variablename;


Closing the Cursor
Closing the cursor means releasing the allocated memory.

The General Form is:


CLOSE cursorname;

ATTRIBUTES

Attribute Description

%ROWCOUNT Number of rows affected by the most recent SQL


statement.

%FOUND Boolean attribute that evaluates to TRUE if the most


recent SQL statement affects one or more rows.

%NOTFOUND Boolean attribute that evaluates to TRUE if the most


recent SQL statement affects any rows does not affect

%ISOPEN Always evaluates to FALSE because PL/SQL closes


implicit cursors immediately after they are executed.

RDBMS LAB [CPCA46] 38


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

10. CURSOR

TABLE CREATION
create table cu(id number(10),name varchar(20),address varchar(20),salary
number(20));
Table created.

INSERTION
insert into cu values(1,'Ramesh','Chennai',2000);
1 row(s) inserted.

insert into cu values(2,'Krishna','Delhi',1500);


1 row(s) inserted.

insert into cu values(3,'Keerthi','Mumbai',2000);


1 row(s) inserted.

insert into cu values(4,'Chaitali','Indore',6500);


1 row(s) inserted.

insert into cu values(5,'Hardik','Bhopal',8500);


1 row(s) inserted.

select * from cu;

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Chennai 2000
2 Krishna Delhi 1500
3 Keerthi Mumbai 2000
4 Chaitali Indore 6500

5 Hardik Bhopal 8500

CURSOR
declare
totalrows number(20);
begin
update cu set salary=salary+500;
ifsql%notfound then
dbms_output.put_line('No customers updated');
elsifsql%found then
totalrows:=sql%rowcount;
dbms_output.put_line(totalrows||'customers updated');
end if;
end;
/

5 customers updated

Statement processed.

RDBMS LAB [CPCA46] 39


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

select * from cus;

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Chennai 2500
2 Krishna Delhi 2000
3 Keerthi Mumbai 2500
4 Chaitali Indore 7000

5 Hardik Bhopal 9000

EXPLICIT CURSOR

declare
c_idcu.id%type;
c_namecu.name%type;
c_addresscu.address%type;
cursorc_cu is select id,name,address from cu;
begin
openc_cu;
loop
fetchc_cu into c_id,c_name,c_address;
exit
whenc_cu%notfound;
dbms_output.put_line(c_id||''||c_name||''||c_address);
end loop;
closec_cu;
end;
/

ID N AM E AD D R E S S
1 Ramesh Chennai
2 Krishna Delhi
3 Keerthi Mumbai
4 Chaitali Indore

5 Hardik Bhopal

RESULT

Thus the table was created and also performed successfully.

RDBMS LAB [CPCA46] 40


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 11
PROCEDURE AND FUNCTION
DATE: 06.04.2023

AIM

To create a table using PL/SQL in Procedure and Function.

E-R DIAGRAM

DESCRIPTION

Procedure
The PL/SQL stored procedure or simply a procedure is a PL/SQL block which
performs one or more specific tasks. A procedure may or may not return any value.
The procedure contains a header and a body.
1. Header: The header contains the name of the procedure and the
parameters or variables passed to the procedure.
2. Body: The body contains a declaration section, execution section and
exception section similar to a general PL/SQL block.

Syntax

CREATE [OR REPLACE] PROCEDURE [procedure_name


[ (parameter [(parameter_name [IN | OUT | IN OUT] type]) ] IS
variable declarations;]
BEGIN
[executable_section]
[EXCEPTION]
[exception_section]
END [procedure_name];

There is three ways to pass parameters in procedure:

 IN parameters: The IN parameter can be referenced by the procedure or


function. The value of the parameter cannot be overwritten by the
procedure or the function.

RDBMS LAB [CPCA46] 41


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

 OUT parameters: The OUT parameter cannot be referenced by the


procedure or function, but the value of the parameter can be overwritten by
the procedure or function.
 INOUT parameters: The INOUT parameter can be referenced by the
procedure or function and the value of the parameter can be overwritten by
the procedure or function.

Function
A function must always return a value. A standalone function is created using
the CREATE FUNCTION statement.

 Like a procedure, a function has a header, a declarative, an executable


part, and an optional exception-handling part.

The simplified syntax for the CREATE OR REPLACE PROCEDURE statement is


as follows −

CREATE [OR REPLACE] FUNCTION function_name


[ (parameter [(parameter_name [IN | OUT | IN OUT] type [, ...])]
] RETURN return_datatype IS | AS
variable declarations;
BEGIN
[executable_section
EXCEPTION]
[exception_section ]
END [function_name];

RDBMS LAB [CPCA46] 42


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

11.PROCEDURE & FUNCTION


TABLE CREATION
create table cu(id number(10),name varchar(20),address varchar(20),salary
number(20));

Table created.

INSERTION
insert into cu values(1,'Ramesh','Chennai',2000);
1 row(s) inserted.

insert into cu values(2,'Krishna','Delhi',1500);


1 row(s) inserted.

insert into cu values(3,'Keerthi','Mumbai',2000);


1 row(s) inserted.

select * from cu;

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Chennai 2000
2 Krishna Delhi 1500
3 Keerthi Mumbai 2000

PROCEDURE
createor replace procedure pp(id in number,name in
varchar,addressinnumber,salary in number) is
begin
insert into cu values(id,name,Address,Salary);
end;
/
Procedure created.

begin
pp(4,'Chaitali','Indore',6500);
pp(5,'Hardik','Bhopal',8500);
dbms_output.put_line('Record inserted Successfully');
end;
/
Record inserted successfully
Statement processed.

RDBMS LAB [CPCA46] 43


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

select * from cu;

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Chennai 2000
2 Krishna Delhi 1500
3 Keerthi Mumbai 2000
4 Chaitali Indore 6500

5 Hardik Bhopal 8500

FIND MINIMUM NUMBER USING PROCEDURE WITHOUT CREATING TABLE

declare
a number;
b number;
c number;
procedurefindmin(x in number, y in number, z out number) is
begin
if x< y then
z:=x;
else
z:=y;
end if;
end;
begin
a:=23;
b:=45;
findmin(a,b,c);
dbms_output.put_line('Minimum of (23,45):'||c);
end;
/

OUTPUT
Minimum of (23,45):23
Statement processed.

RDBMS LAB [CPCA46] 44


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

FUNCTION CREATION

create or replace function totalcustomer return number is


total number(2):=0;
begin
select count(*) into total from custom;
return total;
end;
/
Function created.

declare
c number(2);
begin
c:=totalcustomer();
dbms_output.put_line('Total No.Of.Customers:'||c);
end;
/

OUTPUT

Total No.Of.Customers:3
Statement processed.

FIND MAXIMUM NUMBER USING FUNCTION WITHOUT CREATING TABLE

declare
a number;
b number;
c number;
functionfindmax(x in number, y in number) return number is z number;
begin
if x>y then
z:=x;

RDBMS LAB [CPCA46] 45


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

else
z:=y;
end if;
return z;
end;
begin
a:=23;
b:=45;
c:=findmax(a,b);
dbms_output.put_line('Maximum of (23,45):'||c);
end;
/

OUTPUT

Maximum of (23,45):45
Statement processed.

RESULT

Thus the table was created and also performed successfully.

RDBMS LAB [CPCA46] 46


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

EX:NO: 12
PACKAGES
DATE: 02.05.2022

AIM

To create a table using PL/SQL in Packages.

E-R DIAGRAM

DESCRIPTION

PACKAGES

Packages are schema objects that groups logically related PL/SQL types,
variables, and subprograms.
A package will have two mandatory parts −

1. Package specification
2. Package body or definition

Package Specification

The specification is the interface to the package. It just DECLARES the types,
variables, constants, exceptions, cursors, and subprograms that can be referenced
from outside the package. In other words, it contains all information about the content
of the package, but excludes the code for the subprograms.

Package Body

The package body has the codes for various methods declared in the package
specification and other private declarations, which are hidden from the code outside
the package.

Using the Package Elements

The package elements (variables, procedures or functions) are accessed with the
following syntax −

package_name.element_name;

RDBMS LAB [CPCA46] 47


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

12. PACKAGES
TABLE CREATION
create table cu(id number(10),name varchar(20),address varchar(20),salary
number(20));

Table created.

INSERTION
insert into cu values(1,'Ramesh','Chennai',2000);
1 row(s) inserted.

insert into cu values(2,'Krishna','Delhi',1500);


1 row(s) inserted.

insert into cu values(3,'Keerthi','Mumbai',2000);


1 row(s) inserted.

insert into cu values(4,'Chaitali','Indore',6500);


1 row(s) inserted.

insert into cu values(5,'Hardik','Bhopal',8500);


1 row(s) inserted.

select * from cu;

ID N AM E AD D R E S S S AL AR Y
1 Ramesh Chennai 2000
2 Krishna Delhi 1500
3 Keerthi Mumbai 2000
4 Chaitali Indore 6500

5 Hardik Bhopal 8500

PACKAGE SPECIFICATION

create package cust_sal as


procedurefind_sal(c_idcustomers.id%type);
endcust_sal;
/

Package created.

RDBMS LAB [CPCA46] 48


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN, PUDUPALAYAM

PACKAGE BODY
create or replace package body cust_sal as
procedurefind_sal(c_idcustomers.id%type) is
c_salcustomers.salary%type;
begin
select salary into c_sal from customers where id = c_id;
dbms_output.put_line('salary: '|| c_sal);
endfind_sal;
endcust_sal;
/
Package Body created.

USING THE PACKAGE ELEMENT

declare
codecustomers.id%type := &cc_id;
begin
cust_sal.find_sal(code);
end;
/

Enter value for cc_id: 1


Salary: 3000

PL/SQL procedure successfully completed.

RESULT

Thus the table was created and also performed successfully.

RDBMS LAB [CPCA46] 49

You might also like