Você está na página 1de 51

List of Experiments

1. Creation of a database and writing SQL queries to retrieve information from the
database.
2. Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records
based on
conditions.
3. Creation of Views, Synonyms, Sequence, Indexes, Save point.
4. Creating an Employee database to set various constraints.
5. Creating relationship between the databases.
6. Study of PL/SQL block.
7. Write a PL/SQL block to satisfy some conditions by accepting input from the user.
8. Write a PL/SQL block that handles all types of exceptions.
9. Creation of Procedures.
10. Creation of database triggers and functions
11. Mini project (Application Development using Oracle/ Mysql )
a) Inventory Control System.
b) Material Requirement Processing.
c) Hospital Management System.
d) Railway Reservation System.
e) Personal Information System.
f) Web Based User Identification System.
g) Timetable Management System.
h) Hotel Management System

Ex.No:1

DATA DEFINITION LANGUAGE

AIM:
To implement all Data Definition Language Commands.
DDL commands are:
(1) Create
(2) Describe
(3) Alter
(i)Add
(ii)Modify
(4) Truncate
(5) Drop
(1) Create:
This DDL command is used to create a table.
Syntax:
create table <table name>(fieldname 1 datatype( size ),fieldname 2
datatype(size).fieldname n datatype(size));
(2) Describe:
This DDL command is used to describe the table structure (displays the
fields and their types).
Syntax:
desc <table name>;
(3) Alter:
This DDL command is used to modify the already existing table, but we
cannot change or rename the table.
(i)Add:
This DDL command is used to add the column to an existing table.
Syntax:
alter table <table name> add (fieldname 1 datatype(size),fieldname2
datatype(size));
(ii)Modify:
It is used to modify column in already existing table.
Syntax:
alter table <table name> modify ( column datatype(size));
(4) Truncate:
This DDL command is used to delete data in the table.
Syntax:
truncate table <tablename>;
(5) Drop:
This DDL command is used to drop a table.
Syntax:
drop table <tablename>;
_____________________________________________________________________

____________________
OUTPUT:*CREATE
SQL> create table emp1(ename varchar(7),eno number(5),addr varchar(15),pho numb
er(7),dept number(5));
Table created.
*DESCRIBE
SQL> desc empl1;
Name
Type
---------------------------------- --------------ENAME
VARCHAR2(7)
ENO
NUMBER(5)
ADDR
VARCHAR2(15)
PHO
NUMBER(7)
DEPT
NUMBER(5)
*ALTER (add entry)
SQL> alter table emp1 add(salary number(8,2));
Table altered.
SQL> desc emp1;
Name
Type
-------------------------------- ---------------------------ENAME
VARCHAR2(7)
ENO
NUMBER(5)
ADDR
VARCHAR2(15)
PHO
NUMBER(7)
DEPT
NUMBER(5)
SALARY
NUMBER(8,2)
*ALTER (modify entry)
SQL> alter table emp1 modify(ename varchar(8));
Table altered.
SQL> desc emp1;
Name
Type
-------------------------------- ---------------------------ENAME
VARCHAR2(8)
ENO
NUMBER(5)
ADDR
VARCHAR2(15)
PHO
NUMBER(7)
DEPT
NUMBER(5)
SALARY
NUMBER(8,2)
*TRUNCATE
SQL> truncate table emp1;
Table truncated.

*DROP
SQL> drop table emp1;
Table dropped.
SQL> desc emp1;
ERROR:
ORA-04043: object emp1 does not exist

Ex.No:2

DATA MANIPULATION LANGUAGE

AIM:
To implement all DML commands
(1)Insert (2) Select (3)Update (4)Delete
(1)Insert:
This DML command is used to insert the details into the table.
Syntax:
insert into tablename values (&field1, &field2, );
(2)Select:
This command is used to show the details present in a table.
Syntax:
select * from <table name >;
(3) Update:
This command is used to change a value of field in a row.
Syntax:
update <tablename> set < field=new value> where
<field=oldvalue>;
(4)Delete:
This command is used to delete a row in table.
Syntax:
delete from <tablename> where< fieldname=value>;
_____________________________________________________________________
____________________
OUTPUT:SQL> create table emp2(empno number(5),empname varchar(7),age number(3),job
varchar(20) ,deptno number(5),salary number(8,2));
Table created.
SQL> desc emp2;
Name
-------------------------EMPNO
EMPNAME
AGE
JOB
DEPTNO
SALARY

Type
---------------------------NUMBER(5)
VARCHAR2(7)
NUMBER(3)
VARCHAR2(8)
NUMBER(5)
NUMBER(8,2)

*INSERT (single entry)


SQL> insert into emp2 values(100,'John',36,'Manager',455,17500);

1 row created.
SQL> select * from emp2;
EMPNO EMPNAME AGE JOB DEPTNO SALARY
---------- ----------------- ------- -------- ------------ ----------100
John
36 Manager
455
17500
*INSERT (multiple entries)
SQL> insert into emp2 values(&empno,&empname,&age,&job,&deptno,&salary);
Enter value for empno: 101
Enter value for empname: 'Smith'
Enter value for age: 29
Enter value for job: 'Clerk'
Enter value for deptno: 43
Enter value for salary: 1200
1 row created.
SQL> /
Enter value for empno: 102
Enter value for empname: 'A'
Enter value for age: 32
Enter value for job: 'Assistant Manager'
Enter value for deptno: 43
Enter value for salary: 13500
1 row created.
SQL> /
Enter value for empno: 103
Enter value for empname: 'B'
Enter value for age: 37
Enter value for job: 'General Manager'
Enter value for deptno: 355
Enter value for salary: 19000
1 row created.
SQL> /
Enter value for empno: 104
Enter value for empname: 'E'
Enter value for age: 28
Enter value for job: 'Clerk'
Enter value for deptno: 35
Enter value for salary: 1500
1 row created.
SQL> /
Enter value for empno: 105
Enter value for empname: 'F'
Enter value for age: 38
Enter value for job: 'General Manager'
Enter value for deptno: 40

Enter value for salary: 19000


1 row created.
SQL> select * from emp2;
EMPNO EMPNAME AGE
JOB
DEPTNO SALARY
---------- ------------------- ---------- ------------------------ ------------ ---------100
John
36
Manager
455
17500
101 Smith
29
Clerk
43
1200
102 A
32
Assistant Manager
43
13500
103
B
37
General Manager
355
19000
104
E
28
Clerk
35
1500
105
F
38
General Manager
40
19000
6 rows selected.
*SELECT (specific attributes)
SQL> select empno,salary from emp2;
EMPNO SALARY
---------- ---------100
17500
101
1200
102
13500
103
19000
104
1500
105
19000
6 rows selected.
*SELECT (specific attributes on condition)
SQL> select empno,salary from emp2 where salary>15000;
EMPNO SALARY
---------- ---------100
17500
103
19000
105
19000
6 rows selected.
*SELECT (all attributes)
SQL> select * from emp2;
EMPNO EMPNAME AGE
JOB
DEPTNO SALARY
---------- ------------------- ---------- ------------------------ ------------ ---------100
John
36
Manager
455
17500
101
Smith
29
Clerk
43
1200
102 A
32
Assistant Manager
43
13500
103
B
37
General Manager
355
19000
104
E
28
Clerk
35
1500
105
F
38
General Manager
40
19000
6 rows selected.
*UPDATE (all entries)
SQL> update emp2 set salary=salary+500;

6 rows updated.
SQL> select * from emp2;
EMPNO EMPNAME AGE
JOB
DEPTNO SALARY
---------- ------------------ ------- ---------------------- ------------- ---------100
John
36 Manager
455
18000
101
Smith
29 Clerk
43
1700
102
A
32 Assistant Manager 43
14000
103
B
37 General Manager
355
19500
104
E
28 Clerk
35
2000
105
F
38 General Manager
40
19500
6 rows selected.
*UPDATE (specific entries)
SQL> update emp2 set salary=salary+500 where age>35;
3 rows updated.
SQL> select * from emp2;
EMPNO EMPNAME AGE
JOB
DEPTNO SALARY
---------- ------------------ ------- ---------------------- ------------- ---------100
John
36
Manager
455
18500
101
Smith
29
Clerk
43
1700
102
A
32 Assistant Manager 43
14000
103
B
37
General Manager
355
20000
104
E
28 Clerk
35
2000
105
F
38
General Manager
40
20000
6 rows selected.
*DELETE (specific entries)
SQL> delete from emp2 where deptno=43;
2 rows deleted.
SQL> select * from emp2;
EMPNO EMPNAME AGE
JOB
DEPTNO SALARY
---------- ------------------ ------- ---------------------- ------------- ------------100 John
36
Manager
455 18500
103
B
37
General Manager
355
20000
104
E
28
Clerk
35
2000
105
F
38
General Manager
40
20000
4 rows selected.
*DELETE (specific entries)
SQL> delete from emp2;
4 rows deleted.
SQL> select * from emp2;
no rows selected

Ex.No:3

VIEWS, SYNONYMS, SEQUENCES, INDEXES

AIM:
To create views Synonyms, Sequence, Indexes, Save point for the
table and perform operations on it.
DEFINITION:
A view is an object that gives the user the logical view of data from the underlying
table. Any relation that is not part of the logical model but is made visible to the user
as a virtual relation is called a view. They are generally used to avoid duplication of
data.
Views are created for the following reasons,

Data simplicity

To provide data security

Structural simplicity (because view contains only limited number of rows and
colmns)
i) VIEW:-is an imaginary table.
Syntax:
Create view <viewname> as select * from <tablename> where
fieldname=value;
ii) SEQUENCE:- Automatic generation of primary or unique key integer value.
Syntax:
Create sequence <seq_name>increment by n start with n [maxvalue n]
[Minvalue n] [Cycle/no cycle] [Cache/no cache].
If you want to see the next current available value from the sequence apply the
following
Query: Select seq1.currval from dual;
If you want to see the next value from the sequence apply the following
Query: Select seq1.nextval from dual;
iii) SYNONYM: Which is used as an alias name (alternative name) for a table, view
or sequence.
Syntax: CREATE synonym<synonym_name>for<table_name>
iv) INDEXES: Improves the performance of the queries.
_____________________________________________________________________
____________________
OUTPUT:
VIEWS

*PARENT TABLE
Table created.
SQL> select * from res33;
COURSE SUBJECT MARKS ROLLNO
---------- ----------------------------BE
CS6202
80
1
BE
PH6151
62
1
BE
CS6202
91
2
ME
EE6252
72
3
ME
MC3124
55
3
BE
CY6251
84
1
BE
CS2126
72
5
BE
MA6301
96
6
8 rows selected.
*CREATE VIEW
SQL> create view resv44 as select rollno,marks from res33;
View created.
SQL> select * from resv44;
ROLLNO MARKS
---------- ---------1
80
1
62
2
91
3
72
3
55
1
84
5
72
6
96
8 rows selected.
*SELECT
SQL> select marks from resv44;
MARKS
---------80
62
91
72
55
84
72
96
8 rows selected.
*UPDATE
SQL> update resv44 set marks=60 where rollno=2;

1 row updated.
SQL> select * from resv44;
ROLLNO MARKS
---------- ---------1
80
1
62
2
60
3
72
3
55
1
84
5
72
6
96
8 rows selected.
SQL> select * from res33;
COURSE SUBJECT MARKS
---------- ------------------BE
CS6202
80
BE
PH6151
62
BE
CS6202
60
ME
EE6252
72
ME
MC3124
55
BE
CY6251
84
BE
CS2126
72
BE
MA6301
96
8 rows selected.

ROLLNO
---------1
1
2
3
3
1
5
6

SQL> update resv44 set marks=marks+2;


8 rows updated.
SQL> select * from resv44;
ROLLNO MARKS
---------- ---------1
82
1
64
2
62
3
74
3
57
1
86
5
74
6
98
8 rows selected.
SQL> select * from res33;
COURSE SUBJECT MARKS ROLLNO
------------ ------------- ------------------BE
CS6202
82
1
BE
PH6151
64
1
BE
CS6202
62
2

ME
EE6252
ME
MC3124
BE
CY6251
BE
CS2126
BE
MA6301
8 rows selected.

74
57
86
74
98

3
3
1
5
6

*INSERT
SQL> insert into resv44 values(7,92);
1 row created.
SQL> select * from resv44;
ROLLNO MARKS
---------- ---------1
80
1
62
2
60
3
72
3
55
1
84
5
72
6
96
7
92
9 rows selected.
SQL> select * from res33;
COURSE SUBJECT MARKS
------------ ---------------------BE
CS6202
80
BE
PH6151
62
BE
CS6202
91
ME
EE6252
72
ME
MC3124
55
BE
CY6251
84
BE
CS2126
72
BE
MA6301
96
92
8 rows selected.

ROLLNO
------------1
1
2
3
3
1
5
6
7

*DELETE
SQL> delete from resv44 where rollno=3;
2 rows deleted.
SQL> select * from resv44;
ROLLNO MARKS
---------- -------1
80
1
62
2
60
1
84

5
72
6
96
7
90
7 rows selected.
SQL> select * from res33;
COURSE SUBJECT MARKS
----------- ------------- ---------BE
CS6202
80
BE
PH6151
62
BE
CS6202
91
BE
CY6251
84
BE
CS2126
72
BE
MA6301
96
92

ROLLNO
-----------1
1
2
1
5
6
7

SQL> delete from resv44;


7 rows deleted.
SQL> select * from resv44;
no rows selected
SQL> select * from res33;
no rows selected
*DROP
SQL> drop view resv44;
View dropped.
SQL> select * from resv44;
select * from resv44
ERROR at line 1:
ORA-00942: table or view does not exist
SEQUENCE
SQL> create table ssi(id number(2),name varchar(5));
Table created.
*CYCLE
*CREATE
SQL> create sequence seq34 minvalue 1 maxvalue 3 start with 1 increment by 1 cycle
cache 2;
Sequence created.
*INSERT
SQL> insert into ssi values(seq34.nextval,'A');
1 row created.
SQL> insert into ssi values(seq34.nextval,'B');

1 row created.
SQL> insert into ssi values(seq34.nextval,'C');
1 row created.
SQL> select *from ssi;
ID NAME
------ --------1 A
2 B
3 C
SQL> insert into ssi values(seq34.nextval,'a');
1 row created.
SQL> insert into ssi values(seq34.nextval,'b');
1 row created.
SQL> insert into ssi values(seq34.nextval,'d');
1 row created.
SQL> select *from ssi;
ID NAME
-------- --------1 A
2 B
3 C
1 a
2 b
3 d
6 rows selected.
*CURRENT VALUE
SQL> select seq34.currval from dual;
CURRVAL
---------3
*NEXT VALUE
SQL> select seq34.nextval from dual;
NEXTVAL
---------1
SQL> drop sequence seq34;
Sequence dropped.
SQL> select seq34.nextval from dual;
select seq34.nextval from dual
ERROR at line 1:
ORA-02289: sequence does not exist
*NO CYCLE
SQL> create table ssi2(id number(2), name varchar(5));
Table created.

*CREATE
SQL> create sequence seq34 minvalue 1 maxvalue 3 start with 1 increment by 1
nocycle cache 5;
Sequence created.
*INSERT
SQL> insert into ssi2 values(seq34.nextval,'r');
1 row created.
SQL> insert into ssi2 values(seq34.nextval,'e');
1 row created.
SQL> insert into ssi2 values(seq34.nextval,'a');
1 row created.
SQL> select *from ssi2;
ID NAME
------ --------1 r
2 e
3 a
SQL> insert into ssi2 values(seq34.nextval,'r');
ERROR at line 1:
ORA-08004: sequence SEQ34.NEXTVAL exceeds MAXVALUE and cannot be
instantiated
SQL> insert into ssi2 values(seq34.currval,'r');
1 row created.
SQL> insert into ssi2 values(seq34.currval,'h');
1 row created.
SQL> select *from ssi2;
ID NAME
------ ------1
r
2
e
3
a
3
r
3
h
SYNONYM
*CREATE
SQL> create synonym syn34 for ssi;
Synonym created.
*INSERT
SQL> insert into syn34 values(&id,'&name');
Enter value for id: 4

Enter value for name: e


1 row created.
SQL> /
Enter value for id: 5
Enter value for name: r
1 row created.
SQL> select *from ssi;
ID NAME
----- --------1
A
2
B
3
C
1
a
2
b
3
d
4
e
5
r
8 rows selected.
SQL> select *from syn34;
ID NAME
------ ---------1
A
2
B
3
C
1
a
2
b
3
d
4
e
5
r
8 rows selected.
*UPDATE
SQL> update syn34 set id=id+2;
8 rows updated.
SQL> select *from syn34;
ID NAME
------ --------3
A
4
B
5
C
3
a
4
b
5
d
6
e
7
r
8 rows selected.

SQL> update syn34 set id=id+1 where id=7;


8 rows updated.
SQL> select *from syn34;
ID NAME
----- --------3
A
4
B
5
C
3
a
4
b
5
d
6
e
8
r
8 rows selected.
SQL> select *from ssi;
ID NAME
------ ------3
A
4
B
5
C
3
a
4
b
5
d
6
e
8
r
8 rows selected.
*DELETE
SQL> delete from syn34 where id=5;
2 rows deleted.
SQL> select *from syn34;
ID NAME
----- ---------3
A
4
B
3
a
4
b
6
e
8
r
6 rows selected.
SQL> select *from ssi;
ID NAME
------ --------3
A
4
B
3
a

4
b
6
e
8
r
6 rows selected.
SQL> delete from syn34;
6 rows deleted.
SQL> select *from syn34;
no rows selected
SQL> select *from ssi;
no rows selected
SQL> drop synonym syn34;
Synonym dropped.
INDEX
*CREATE
SQL> create index ind34 on ssi(id,name);
Index created.
*ALTER
SQL> alter index ind34 rename to ind56;
Index altered.
*DROP
SQL> drop index ind56;
Index dropped.

Ex.No 4

CONSTRAINTS

AIM:
To implement all the constraints on table created.
DEFINITION:
It restricts the data value that can be inserted or deleted into the database
or restricted, enforced on the values of an attribute.
TYPES:
*Domain Integrity Constraints
*Entity Integrity Constraints
*Referential Integrity Constraints
DOMAIN INTEGRITY CONSTRAINTS:
(1)Not Null:
If this constraint is enforced to a particular field, the field should contain a
value compulsorily.
Syntax:
Create table <tablename>(fieldname datatype(size) <notnull>);
(2)Check:
It sets the condition that the column must be satisfied to exist in the table.
Syntax:
Create table <tablename>(fieldname datatype(size) check <condition>);
(3)Unique:
It is used to set a condition that the column must be unique in the table.
Syntax:
Create table <tablename>(fieldname datatype(size) constraint
<constraintname> unique);
ENTITY INTEGRITY CONSTRAINT:
(4)Primary key:
It is one or more column in a table used to identify each row in the table
uniquely.once it is defined, the value must be both not null and unique.
Syntax:
Create table <tablename>(<fieldname> datatype(size) primary key);
REFERENTIAL INTEGRITY CONSTRAINT:
(5)Foreign key:
It represents relationship between two tables. A foreign key column whose
value has derived from the primary key of the other table. Every legal value in the
foreign key column must match the same value that appears in primary key.
Syntax:

Create table <tablename1>(fieldname1 datatype(size) primary


key,fieldname2 datatype(size));
Create table <tablename2> (fieldname1 datatype(size) primary key,
fieldname2 datatype(size) references <tablename1> (fieldname));
_____________________________________________________________________
____________________
OUTPUT:
*NOT NULL
SQL> create table sst1(name char(10),regno number(10)not null);
Table created.
SQL> insert into sst1 values('&name',&regno);
Enter value for name: a
Enter value for regno: 2
1 row created.
SQL> /
Enter value for name: c
Enter value for regno: NULL
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."SST1"."REGNO")
SQL>select * from sst1;
NAME REGNO
---------- --------a
2
*CHECK
SQL> create table sst2(name char(10),place varchar(10)check(place in('chennai','
delhi','mumbai')));
Table created.
SQL> insert into sst2 values('a','delhi');
1 row created.
SQL> insert into sst2 values('b','trichi');
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.SYS_C009914) violated
SQL>select * from sst2;
NAME PLACE
---------- --------a
delhi
*PRIMARY KEY
SQL> create table sst3(name char(10),regno number(10)primary key);
Table created.
SQL> insert into sst3 values('&name',&regno);

Enter value for name: b


Enter value for regno: 11
1 row created.
SQL> /
Enter value for name: c
Enter value for regno: 10
1 row created.
SQL> /
Enter value for name: d
Enter value for regno: null
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."SST3"."REGNO")
SQL> /
Enter value for name: a
Enter value for regno: 11
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C009923) violated
SQL>select * from sst3;
NAME REGNO
---------- --------b
11
c
10
*UNIQUE KEY
SQL> create table sst4(name char(10),regno number(10)unique);
Table created.
SQL> insert into sst4 values('&name',&regno);
Enter value for name: a
Enter value for regno: 10
1 row created.
SQL> /
Enter value for name: b
Enter value for regno: 10
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C009927) violated
SQL> /
Enter value for name: c
Enter value for regno: null
1 row created.
SQL>select * from sst4;
NAME REGNO
---------- ---------

a
c

10

*FOREIGN KEY
SQL> create table prnt5(name char(10),age number(3),des varchar(5)primary key);
Table created.
SQL> create table chld5(id number(5),des varchar(5)references prnt5(des));
Table created.
SQL> insert into prnt5 values('a',40,'ch');
1 row created.
SQL> insert into prnt5 values('b',45,'ad');
1 row created.
SQL> insert into chld5 values(1,'ch');
1 row created.
SQL> insert into chld5 values(2,'tr');
insert into chld5 values(2,'tr')
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.SYS_C009959) violated - parent key not
found
SQL> select * from prnt5;
NAME AGE DES
---------- ---------- ----a
4
ch
b
45
ad
SQL> select * from chld5;
ID DES
------- ----1 ch

Ex.No:5

JOINS

AIM:
To write and execute queries in SQL.
DEFINITION:
(1) INNER JOIN/ NATURAL JOIN/ JOIN:
It is a binary operation that allows us to combine certain selections and
a Cartesian product into one operation.
(2) OUTER JOIN:
It is an extension of join operation to deal with missing information.
(i) Left Outer Join: It takes tuples in the left relation that did not match with
any tuple in the right relation, pads the tuples with null values for all other attributes
from the right relation and adds them to the result of the natural join.
(ii) Right Outer Join: It takes tuples in the right relation that did not match
with any tuple in the left relation, pads the tuples with null values for all other
attributes from the left relation and adds them to the result of the natural join.
(iii)Full Outer Join: It combines tuples from both the left and the right relation
and pads the tuples with null values for the missing attributes and them to the result of
the natural join.
_____________________________________________________________________
____________________
OUTPUT:
Table created.
SQL> select * from categ1;
CATEID DESCRIPTION
---------- --------------101
Fiction
102
IT
103
Journal
104
General
105
Non-Fiction
Table created.
SQL> select * from catal1;
ID TITLE
PUBID CATEID PRICE
---------- --------------------- ------------- ---------- ---------1001
Science-Fiction 2001
101
3500
1002
Sandstime
2003
105
450
1003
Truth
2004
103
150
1004
DBMS
2006
102
350
1005
Autography
2008
103
1000

1006
Sportsplus
6 rows selected.

2010

104

200

Table created.
SQL> select * from publshr1;
PUBID NAME
CITY
COUNTRY
---------- --------------- ------------------ ---------------2001 Foreign
San Francisco America
2002 Lakshmi
Delhi
India
2004 Brooks
New York
America
2005 Prakash
Chennai
India
2009 Shanthi
Chennai
India
2006 Ellie
Harrison
England
6 rows selected.
+) SQL> select title,name from catal1,publshr1 where catal1.pubid=publshr1.pubid;
TITLE
NAME
-------------------- --------------Science-Fiction Foreign
Truth
Brooks
Sportsplus
Ellie
+) SQL> select catal1.*,categ1.* from catal1,categ1 where catal1.price>1000 and
catal1.cateid =categ1.cateid;
ID
TITLE
PUBID CATEID PRICE CATEID DESCRIPTION
---------- ------------------- ---------- ------------ ---------- ------------ ---------------1001 Science-Fiction 2001
101
3500
101
Fiction
+) SQL> select title,name from catal1 c,publshr1 p where c.pubid=p.pubid;
TITLE
NAME
--------------- --------------Science-Fiction Foreign
Truth
Brooks
Sportsplus Ellie
*INNER/NATURAL JOIN
SQL>select title,name from catal1 inner join publshr1 on catal1.pubid=publshr1.pubid;
TITLE
NAME
--------------------- --------------Science-Fiction
Foreign
Truth
Brooks

Sportsplus

Ellie

*LEFT OUTER JOIN


SQL>select title,name from catal1 left outer join publshr1 on catal1.pubid=
publshr1.pubid;
TITLE
NAME
-------------------- --------------Science-Fiction Foreign
Truth
Brooks
Sportsplus
Ellie
DBMS
Sandstime
Autography
6 rows selected.

*RIGHT OUTER JOIN


SQL> select title,name from catal1 right outer join publshr1 on
catal1.pubid=publshr1.pubid;
TITLE
NAME
------------------- --------------Science-Fiction Foreign
Truth
Brooks
Sportsplus
Ellie
Shanthi
Prakash
Lakshmi
6 rows selected.
*FULL OUTER JOIN
SQL> select title,name from catal1 full outer join publshr1 on catal1.pubid=
pubshr1.pubid;
TITLE
NAME
-------------------- --------------Science-Fiction Foreign
Lakshmi
Truth
Brooks
Prakash
Shanthi
Sportsplus
Ellie
DBMS
Sandstime

Autography
9 rows selected.

+) SQL> select catal1.*,categ1.* from catal1,categ1 where catal1.cateid=categ1.cateid;


ID
TITLE
PUBID CATEID PRICE CATEID DESCRIPTION
-------- ---------------------- ----------- ------------ --------- ---------- --------------1001 Science-Fiction 2001
101
3500
101
Fiction
1002 Sandstime
2003
105
450
105
Non-Fiction
1003 Truth
2004
103
150
103
Journal
1004 DBMS
2006
102
350
102
IT
1005 Autography
2008
103
1000
103
Journal
1006 Sportsplus
2010
104
200
104
General
6 rows selected.
+) SQL> select title,price,cateid,name,country,city from catal1,publshr1 where
catal1.pubid=publshr1.pubid;
TITLE
PRICE CATEID NAME COUNTRY CITY
------------------- ------------ ----------- ------------- ----------------- --------------Science-Fiction 3500
101
Foreign America San Francisco
Truth
150
103
Brooks America New York
Sportsplus
200
104
Ellie
England Harrison

Ex.No:6

PL/SQL PROGRAM

AIM:
To study and implement PL/SQL using simple programs.
STRUCTURE OF PL/SQL:
declare
<<declar active statements>>;
begin
<<executable statements>>;
exception
<<exception handling>>;
end;
SQL BLOCK STRUCTURE:
DECLARE:
Declarations of memory variables used later.
BEGIN:
SQL executable statements for manipulating table data.
EXCEPTIONS:
SQL and/or PL/SQL code to handle errors that may crop up
During the execution of the above code block.
END;
1. Conditional Control:
A sequence of statements can be executed based on some condition
using IF.
Syntax:
If<condition> then <Action>
Else <Action>
End if;
2. Iterative Control:
A sequence of statements can be executed any number of times using
loop constructs.
a) Simple loop

Syntax:
Loop
Statements;
End loop;
b) While loop
Syntax:
While<condition>
Loop
Statements;
End loop;
c) For loop
Syntax:
For counter in[reverse] lower bound .. upper bound
Loop
statements
End loop;

Ex.No:7a

SUM OF 2 NUMBERS

AIM
To study and implement the PL/SQL for adding two numbers and displaying the
sum.
ALGORITHM
*Declare a,b and c.
*Get input of a and b.
*Do arithmetic addition operation between a and b.
*And store the value in c.
*Display the sum as output.
PROGRAM
declare
a number(3);
b number(3);
c number(3);
begin
a:=&a;
b:=&b;
c:=a+b;
dbms_output.put_line('Sum='||c);
end;
OUTPUT
Enter value for a: 34
Enter value for b: 52
Sum=86
PL/SQL procedure successfully completed.

Ex.No:7b

SERIES OF NUMBERS (FOR LOOP)

AIM
To study and implement the PL/SQL to print a series of n numbers using for
loop.
ALGORITHM
*Declare i and n.
*Get input of n.
*Initialize the value of i=1.
*Inside a for loop of i in n, print the value of i.
*Display output.
PROGRAM
declare
i number(2):=1;
n number(3):=&n;
begin
for i in 1..n
loop
dbms_output.put_line(i);
end loop;
end;
OUTPUT
Enter value for n: 6
1
2
3
4
5
6
PL/SQL procedure successfully completed.

Ex.No:7c

SERIES OF NUMBERS (WHILE LOOP)

AIM
To study and implement the PL/SQL to print a series of n numbers using while
loop.
ALGORITHM
*Declare i and n.
*Get input of n.
*Initialize the value of i=1.
*Inside a while loop with condition i<=n, print the value of i.
*Increment the value of i.
*Display output.
PROGRAM
declare
i number(2):=1;
n number(3):=&n;
begin
while(i<=n)
loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
OUTPUT
Enter value for n: 15
1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
PL/SQL procedure successfully completed.

Ex.No:7d

DEBIT FROM ACCOUNT

AIM
To study and implement the PL/SQL for debiting an amount from an entry in a
table by this program
ALGORITHM
*Declare accno,balance,debit,minbal as 500.
*Get input of accno and debit from user .
*Debit the amount from entry in the table account33 using select command.
*Using if condition, update bal attribute in table only if it is greater than minbal.
*Else print that the account is too low to debit.
*End the program.
PROGRAM
declare
accno number(5);
balance number(5);
debit number(5);
minbal number(5);
begin
minbal:=500;
accno:=&accno;
debit:=&debit;
select bal into balance from account33 where accno=Ano;
balance:=balance-debit;
if(balance>minbal)then
update account33 set bal=balance where accno=Ano;
else
dbms_output.put_line('Your account is too low to debit');
end if;
end;
OUTPUT
SQL> create table account33(Ano number(2),name varchar(5),bal number(7));
Table created.
SQL> select * from account33;
ANO NAME BAL
------- --------- ----------

1 A
5000
2 B
1000
3
C
4000
4
D
5000
--------------------------------------------------------------Enter value for accno: 1
Enter value for debit: 2000
PL/SQL procedure successfully completed.
SQL> select * from account33;
ANO NAME BAL
-------- -------- ---------1
A
3000
2
B
1000
3
C
4000
4
D
5000
--------------------------------------------------------------Enter value for accno: 2
Enter value for debit: 2000
Your account is too low to debit
PL/SQL procedure successfully completed.

Ex.No:7e

SUM OF A SERIES

AIM
To study and implement the PL/SQL to determine the sum of a series of n
numbers.
ALGORITHM
*Declare n,i and x.
*Get input of n.
*Initialize the values of i=1 and x=0.
*Inside a simple loop, calculate the sum of numbers upto n.
*Display the sum as output.
PROGRAM
declare
n number(3):=&n;
i number(3):=1;
x number(3):=0;
begin
loop
x:=x+i;
exit when i=n;
i:=i+1;
end loop;
dbms_output.put_line('Sum = '||x);
end;
OUTPUT
Enter value for n: 6
Sum = 21
PL/SQL procedure successfully completed.

Ex.No:7e

FACTORIAL OF A NUMBER

AIM
To study and implement the PL/SQL to determine the factorial of the given
number.
ALGORITHM
*Declare n,i and x.
*Get input of n.
*Initialize the values of i=1 and x=1.
*Inside a while loop, calculate the product of numbers upto n.
*Display the factorial as output.
PROGRAM
declare
n number(3):=&n;
i number(3):=1;
x number(3):=1;
begin
while(i<=n)
loop
x:=x*i;
i:=i+1;
end loop;
dbms_output.put_line('Factorial = '||x);
end;
OUTPUT
Enter value for n: 5
Factorial = 120
PL/SQL procedure successfully completed.

Ex.No:7g

REVERSING A STRING

AIM
To study and implement PL/SQL for reversing a string using the for loop.
ALGORITHM
*Get the input string.
*Find the length of the string.
*Extract the characters one by one from the end of the string.
*Concatenate the extracted characters.
*Display the concatenated reversed string.
*Stop the program.
PROGRAM
declare
b varchar2(10):='&b';
c varchar2(10);
l number(2);
i number(2);
g number(2);
d varchar2(10);
begin
l:=length(b);
g:=l;
for i in 1..l
loop
c:=substr(b,g,1);
g:=g-1;
d:=d||c;
end loop;
dbms_output.put_line('Reversed string is:');
dbms_output.put_line(d);
end;
OUTPUT
Enter value for b: hello
Reversed string is

olleh
PL/SQL procedure successfully completed.

Ex.No:7h

FIBONACCI SERIES

AIM:
To study and implement PL/SQL for Fibonacci series.
ALGORITHM:
*Initialise a,b,c,n where a=-1,b=1,c=0.
*Use n for forloop
*Add a,b and store it in c.
*Exchange b to a and c to b.
*Get output from c.
PROGRAM :
declare
a number(3);
b number(3);
c number(3);
n number(3);
i number(3);
begin
n:=&n;
c:=0;
b:=1;
a:=-1;
for i in 1..n
loop
c:=a+b;
a:=b;
b:=c;
dbms_output.put_line(c);
end loop;
end;
OUTPUT
Enter value for n: 6
0
1
1

2
3
5
PL/SQL procedure successfully completed.

Ex.No:7i

ARITHMETIC OPERATIONS

AIM
To study and implement the PL/SQL for arithmetic operations.
ALGORITHM
*Declare a,b,c,d,e,f,g.
*Get input of a and b.
*Do arithmetic operations between a and b.
*And store it in c,d,e,f,g.
*Display output.
PROGRAM
declare
a number(10);
b number(10);
c number(10);
d number(10);
e number(10);
f number(10,2);
g number(10);
begin
a:=&a;
b:=&b;
c:=a+b;
d:=a-b;
e:=a*b;
f:=a/b;
g:=a mod b;
dbms_output.put_line('Addition of a and b is '||c);
dbms_output.put_line('Subtraction of a and b is '||d);
dbms_output.put_line('Multiplication of a and b is '||e);
dbms_output.put_line('Division of a and b is '||f);
dbms_output.put_line('Modulus of a and b is '||g);
end;
OUTPUT
Enter value for a: 9
Enter value for b: 2
Addition of a and b is 11

Subtraction of a and b is 7
Multiplication of a and b is 18
Division of a and b is 4.5
Modulus of a and b is 1
PL/SQL procedure successfully completed.

Ex.No:7j

DIPLAY ENTIRE ROW FROM TABLE

AIM
To study and implement the PL/SQL for displaying an entire row from a table.
ALGORITHM
*Declare a variable cus of rowtype of table customers33.
*Copy the values of an entry from the table using select command.
*Using cus variable, access the attributes name, id and grade.
*Display output of the name and grade.
PROGRAM
declare
cus customers33%rowtype;
begin
select * into cus from customers33 where id=5;
dbms_output.put_line('ID='||cus.id);
dbms_output.put_line('Name='||cus.name);
dbms_output.put_line('Grade='||cus.grade);
end;
OUTPUT
SQL> create table customers33(id number(3),name varchar(5),grade char(3));
Table created.
SQL> select * from customers33;
ID NAME GRADE
----- --------- ----------1 AA
S
3 BB
E
5 CC
B
ID=5
Name=CC
Grade=B
PL/SQL procedure successfully completed.

Ex.No:7k

DIPLAY SPECIFIC ATTRIBUTE FROM TABLE

AIM
To study and implement the PL/SQL for displaying a particular attribute from a
table.
ALGORITHM
*Declare a,b,c,d,e,f,g.
*Get input of a and b.
*Do arithmetic operations between a and b.
*And store it in c,d,e,f,g.
*Display output.
PROGRAM
declare
vsal number(6);
rid number(6):=11;
begin
select salary into vsal from emp33 where id=rid;
dbms_output.put_line(vsal);
dbms_output.put_line('The employee '||rid|| has salary ||vsal);
end;
OUTPUT
SQL> create table emp33(id number(3),name varchar(5),salary number(5));
Table created.
SQL> select * from emp33;
ID NAME SALARY
----- --------- ----------9 AA
5000
10 BB
10000
11 CC
3000
3000
The employee 11 has salary 3000.
PL/SQL procedure successfully completed.

Ex.No:7l

SIMPLE IF STATEMENT

AIM
To study and implement the PL/SQL for implementing the simple if statement in
this program.
ALGORITHM
*Declare i,n and j.
*Get input of n.
*Initialize values of i=1 and j=2.
*Using for loop of i in n, give condition to print only the even numbers.
*Display the output.
PROGRAM
declare
i number(2):=1;
n number(3):=&n;
j number(3):=2;
begin
dbms_output.put_line('Printing even nos.');
for i in 1..n
loop
if((i mod j)=0)then
dbms_output.put_line(i);
end if;
end loop;
end;
OUTPUT
Enter value for n: 10
Printing even nos.
2
4
6
8
10
PL/SQL procedure successfully completed.

Ex.No:7m

IF ELSE STATEMENT

AIM
To study and implement the PL/SQL for implementing the if else statement in this
program.
ALGORITHM
*Declare a variable c for choice.
*Get input of c.
*If its value is 1, print RED.
*Else print the colour BLUE.
PROGRAM
declare
c number(3);
begin
dbms_output.put_line('Colours');
dbms_output.put_line('1.Red');
dbms_output.put_line('2.Blue');
dbms_output.put_line('Your choice:');
c:=&c;
if(c=1)then
dbms_output.put_line('RED');
else
dbms_output.put_line('BLUE');
end if;
end;
OUTPUT
Enter value for c: 2
Colours
1.Red
2.Blue
Your choice:
BLUE
PL/SQL procedure successfully completed.

Ex.No:7n

GREATEST OF 3 NUMBERS

AIM AIM
AIM
To study and implement the PL/SQL to find the greatest of three numbers using if
else statement.
ALGORITHM
*Declare a,b,c,.
*Get input of a,b and c.
*Using if else condition compare the values of a,b and c.
*Display the respective output.
PROGRAM
declare
a number(3);
b number(3);
c number(3);
begin
a:=&a;
b:=&b;
c:=&c;
if(a>b)and(a>c)then
dbms_output.put_line('a is the greatest');
elsif(b>c)then
dbms_output.put_line('b is the greatest');
else
dbms_output.put_line('c is the greatest');
end if;
end;
OUTPUT
Enter value for a: 8
Enter value for b: 5
Enter value for c: 11
c is the greatest

PL/SQL procedure successfully completed.

Ex.No:8

PROCEDURES

AIM
To implement procedures using PL/SQL.
PROCEDURE
A procedure is a stored sub-program that performs a specific function.
Syntax:
Create or replace procedure <procedure name> (parameter-1,parameter-2)
[local declarations]
Begin
<<executable statements>>
Exception
<<exception handling>>
End;
PROGRAM
set serveroutput on;
create or replace procedure proc(no number) is
num c.sno%type;
m1 c.mark1%type;
m2 c.mark2%type;
p number(10);
begin
select sno,mark1,mark2 into num,m1,m2 from c where sno=no;
p:=(m1+m2)/2;
update c set percent=p where sno=no;
dbms_output.put_line(stud no:||num);
dbms_output.put_line(percentage:||p);
dbms_output.put_line(Table updated);
end proc;
OUTPUT
SQL> create table c(sno number(5),mark1 number(10),mark2 number(10),percent
number(10));
Table created.

SQL> insert into c values('9','52','52','');


1 row created.
SQL> insert into c values('27','95','95',');
1 row created.
SQL> insert into c values('8',65',21,);
1 row created.
SQL> select * from c;
SNO
9
27
8

MARK1
52
95
65

MARK2 PERCENT
52
95
21

/
Procedure created.
SQL>exec proc(27);
stud no:27
percentage:95
Table updated.
PL/SQL procedure successfully completed.
SQL>select * from c;
SNO
9
27
8

MARK1
52
95
65

MARK2 PERCENT
52
95
95
21

Ex.No:9

FUNCTIONS

AIM
To implement functions using PL/SQL.
FUNCTION
It is stored sub-program that computes a value.
Syntax:
Create or replace function <function-name>(parameter1,parameter2) is
returntype is
[local declarations]
Begin
<<executable statements>>
Exception
<<exception handling>>
End;
PROGRAM
SQL>set serveroutput on;
SQL> create or replace function fun1(no number) return number is
num c.sno%type;
m1 c.mark1%type;
m2 c.mark2%type;
p number;
begin
select sno,mark1,mark2 into num,m1,m2 from c where sno=no;
p:=(m1+m2)/2;
return p;
end fun1;
OUTPUT
SQL> create table c(sno number(5),mark1 number(10),mark2 number(10));
Table created.
SQL> insert into c values('9','52','52');
1 row created.

SQL> insert into c values('27','95','95');


1 row created.
SQL> insert into c values('8',65',21);
1 row created.

SQL> select * from c;


SNO
9
27
8

MARK1
52
95
65

MARK2
52
95
21

/
Function created.
SQL>select fun1(8) from c where sno=8;
FUN1(8)
65

Ex.No 10

TRIGGERS

AIM
To write a trigger for database model.
TRIGGER
It is a statement that system executes automatically as a slide effect of modification
to the database. A database trigger is a stored procedure that is associated with a table.
Syntax:
Create or replace trigger[triggername]
[before/after][insert/update/delete]on <table name>
[for each statement or row] [when <condition>]
Types:
Before
After
For each row
For each statement

Ex.No:10a

ROW LEVEL TRIGGER

AIM
To implement a row level trigger.
ALGORITHM
*Start the program.
*Create a trigger.
*Check each row and print a row updated or not.
*Stop the program.
PROGRAM
create trigger trg1 after update on student for each row
begin
dbms_output.put_line(1 row updated);
end;
OUTPUT
SQL> select * from student;
REGNO NAME
102
103
104

kiran
hari
kish

DEPT ADDRESS
it
paris
ece trichy
it
pune

/
Trigger Created
SQL> update student set address=chennai where dept=it;
1 row updated
1 row updated
2 rows updated

Ex.No:10b

STATEMENT LEVEL TRIGGER

AIM
To create a Statement level trigger.
ALGORITHM
* Start a program.
* Create a trigger that executes after updation on table emp.
*Check the table on the whole and print 1 row updated.
*Stop the program.
PROGRAM
create trigger trg2 after update on emp
begin
dbms_output.put_line(1 row updated);
end;
OUTPUT
SQL> select * from emp;
EMPNO
102
103
104

SAL
4000
5000
4000

/
Trigger Created
SQL> update emp set sal=10000 where sal=4000;
1 row updated
2 rows updated

Você também pode gostar