J.M.J College For Women (Autonomous) : Tenali: Certificate
J.M.J College For Women (Autonomous) : Tenali: Certificate
CERTIFICATE
External Examiner
INDEX
1 INTRODUCTION TO ORACLE 3
2 CREATION OF TABLES 7
5 PROGRAMS ON PL/SQL 24
7 TRIGGERS 32
8 CURSORS 36
INTRODUCTION TO ORACLE
1. DDL: Data Definition Language (DDL) statements are used to define the database
structure or schema.
the database
TRUNCATE - remove all records from a table, including all spaces allocated for the records are
2. DML:DataManipulationLanguage(DML)statementsareusedformanagingdatawithinsche
ma objects and to manipulate data of a databaseobjects.
DELETE - deletes all records from a table, the space for the records
3. DCL:DataControlLanguage(DCL)statementsareusedtocreateroles,permissions,andreferentia
l integrityaswellitisusedtocontrolaccesstodatabasebysecuringit.Tocontrolthedataof adatabase.
4. TCL:TransactionControl(TCL)statements areusedto
managethechangesmadebyDML statements. It allows statements to be grouped together
into logicaltransactions.
3
SAVEPOINT - identify a point in a transaction to which you can later roll back
Syntax:
create table [table name] (column1 datatype[size], column 2 datatype[size],… column n datatype[size]
);
Ex:
S_NAME VARCHAR2(10)
GENDER VARCHAR2(5)
DOB DATE
ADDR1 VARCHAR2(10)
ADDR2 VARCHAR2(10)
CITY VARCHAR2(10)
PERCENTAGE NUMBER(4)
4
Constraints are two types:
1. NOT NULL:
Syntax:
<col><datatype>(size)not null
SQL > create table emp(e_id varchar(5) NOT NULL,e_name varchar(10), e_design varchar(10),dept
varchar(10),mgr varchar(10),salary number(10));
2. UNIQUE:
level. Syntax:
<col><datatype>(size)unique Ex:-
Syntax:
Ex:-
3. PRIMARYKEY:
Syntax:
<col><datatype>(size)primary key;
Ex:-
5
Primary key constraint at table level.
Syntax:
Ex:-
4. CHECK:
5. FOREIGNKEY:
Syntax:
Syntax:
WEEK-1
CREATION OF TABLES
Name Type
Empno Number
Ename Varchar2(10)
Job Varchar2(10)
Mgr Number
Sal Number
SOLUTION:
SQL> create table employee(empnonumber,ename varchar2(10),job
varchar2(10),mgrnumber,sal number);
Table created.
SQL> desc employee;
Name Null? Type
EMPNO NUMBER
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
MGR NUMBER
SAL NUMBER
EMPNO NUMBER
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
MGR NUMBER
SAL NUMBER
COMMISSION NUMBER
SQL>
insert into employee values(103,'david','analyst',3456,9000,'65')
1 row created.
Table altered.
8
EMPNO NUMBER
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
MANAGER_NO NUMBER
SAL NUMBER
COMMISSION NUMBER
Name Type
Deptno Number
Deptname Varchar2(10)
location Varchar2(10)
SOLUTION:
SQL> create table department(deptnonumber,deptname varchar2(10),locationvarchar2(10));
Tablecreated.
DEPTNO NUMBER
DEPTNAME VARCHAR2(10)
LOCATION VARCHAR2(10)
9
SQL> desc department;
Name Null? Type
DEPTNO NUMBER
DEPTNAME VARCHAR2(10)
LOCATION VARCHAR2(10)
DESIGNATION VARCHAR2(10)
1 row created.
SQL>insert into department values(10,'research','chennai','professor')
1 row created.
SQL>
insert into department values(11,'sales','banglore','salesman')
1 row created.
SQL>
insert into department values(12,'operations','mumbai','operator')
1 row created.
DEPTNO DEPTNAME
9accounting
12operations
10 research
11 sales
10
SQL> update department set designation='accountant' where deptno=9;
2 rows updated.
SQL> select * from department;
DEPTNO DEPTNAME LOCATION DESIGNATION
9 accounting hyderabad
10 research chennai
11 sales banglore
12 operations mumbai
9 accounting Chennai
11
WEEK -2
QUERIES USING DDL ANDDML
SOLUTION:
--Provide roles
--Assigning privileges
b) Insert the any three records in the employee table and use rollback. Check theresult.
SQL> rollback;
Rollback complete.
12
EMPNO ENAME JOB MANAGER_NO SAL COMMISSION
c) Add primary key constraint and not null constraint to the employeetable.
SQL> alter table employee modify(empno number primary key, ename varchar2(10) not null);
Table altered.
SOLUTION:
--Provide roles
--Assigning privileges
SQL> commit;
Commit complete.
9 accounting hyderabad
10 research chennai
11 sales banglore
12 operations mumbai
9 accounting chennai
13 sales delhi
6 rows selected.
c) Add constraints like unique and not null to the departmenttable.
Tablealtered.
Tablealtered.
DEPTNO NUMBER
DEPTNAME VARCHAR2(10)
14
LOCATION NOT NULLVARCHAR2(10)
AIM:Creation,altering and dropping of tables and inserting rows in to a table(use constraints while
creating a table).
Examples using select command:
Student(sid,name,age,college,remarks);
Enroll(sid,cid ,grade);
STUDENT Table:
Table created.
DESCRIPTION OF TABLE:
1 row created.
1 row created.
1 row created.
15
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
DISPLAYING TABLE:
10 rows selected.
ENROLL Table:
16
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL QUERIES
10 rows selected.
SID
--------------------
J18A31156
J18A31161
J18A31173
J18A31174
SID
--------------------
J18A31156
J18A31161
NAME
--------------------
rukmini
manju
anitha
manasa
Prasanthi
5)Find the names of the students whose age is in between 16 and 20?
SQL> select name from students4 where age>16 and age<20;
NAME
--------------------
divya
18
harshitha
rukmini
manju
manasa
vasavi
6 rows selected.
NAME
--------------------
divya
harshitha.
ALTERING A TABLE:
Table altered.
Table altered.
DROPPING A TABLE:
Table dropped.
Table dropped.
UPDATING A TABLE:
1 row updated.
SQL> select * from students4;
SID NAME AGE COLLEGE REMARKS MARKS
-------------------- -------------------- ---------- -------------------- -------------------- -----
J18A31156 divya 19 JMJ Excellent
J18A31161 harshitha 19 JMJ Excellent
J18A31173 rukmini 19 JMJ Good
J18A31174 manju 18 JMJ Good
09JM1A0505 krishnaveni 12 Mrr Average
09JM1A0506 anitha 15 GIST Good
09JM1A0507 anusha 14 Amrita sai Average
09JM1A0508 manasa 17 MVR Good
09JM1A0509 vasavi 18 vasaviengg Average
09JM1A0510 Prasanthi 20 Nimra Good
20
WEEK -3
QUERIES USING AGGREGATE FUNCTIONS
EID NUMBER
ENAME VARCHAR2(10)
AGE NUMBER
SALARY NUMBER
44
(iv) Find the Minimum age from employeetable.
SQL> select min(age) from emp;
21
MIN(AGE)
22
22
(v) Display the Sum of age employeetable.
SQL> select sum(age) from emp;
SUM(AGE)
220
(vi) Display the Average of age from Employeetable.
SQL> select avg(age) from emp;
AVG(AGE)
31.4285714
(vii) Create a View for age in employeetable.
SQL> create or replace view A as select age from emp where
age<30; View created.
(viii) Displayviews
SQL> select * from A;
AGE
22
29
27
29
(ix) Find grouped salaries of employees.(group byclause)
SQL> select salary from emp group by
salary; SALARY
9000
10000
8000
6000
7000
(x).Find salaries of employee in Ascending Order.(order by clause)
SQL>select ename,salary from emp order by
salary; ENAME SALARY
rohan 6000
alex 7000
shane 8000
23
abhi 8000
tiger 8000
anu 9000
scott 10000
7 rows selected.
ENAME SALARY
-
scott 10000
anu 9000
shane 8000
abhi 8000
tiger 8000
alex 7000
rohan 6000
7 rows selected.
(xii) HavingClause.
SQL> select ename,salary from emp where age<29 group by ename,salary
having salary<10000;
ENAME SALARY
-
alex 7000
anu 9000
24
WEEK – 4
PROGRAMS ON PL/SQL
declare
a number:=10;
b number:=12;
c number:=5;
begin
dbms_output.put_line('a='||a||' b='||b||' c='||c);
if a>b AND a>c
then
dbms_output.put_line('a is greatest');
else
if b>a AND b>c
then
dbms_output.put_line('b is greatest');
else
dbms_output.put_line('c is greatest');
end if;
end if;
end;
OUTPUT
Statement processed.
25
2) Write PL/SQL program for swapping 2 numbers.
declare
a number;
b number;
temp number;
begin
a:=:a;
b:=:b;
dbms_output.put_line('before swapping:');
dbms_output.put_line('a='||a||' b='||b);
temp:=a;
a:=b;
b:=temp;
dbms_output.put_line('after swapping:');
dbms_output.put_line('a='||a||' b='||b);
end;
OUTPUT
:a 5
:b 10
before swapping:
a=5 b=10
after swapping:
a=10 b=5
Statement processed.
26
3) Write a PL/SQL program to accept a number and find the sum of the digits
declare
num int :=0;
i int;
s int :=0;
r int;
begin
num:=:num;
while num > 0 loop
r:= MOD(num, 10);
s := s + r;
num:=floor(num/10);
end loop;
dbms_output.put_line(' the sum of digits is '||s );
end;
Output
:NUM 123
the sum of digits is 6
Statement processed.
27
4) Write a PL/SQL Program to accept a number from user and print number in reverse order.
Declare
Num1 number(5);
Num2 number(5);
Rev number(5)
begin
Num1:=:Num1;
Rev:=0;
while Num1>0 loop
Num2:=Num1 mod 10;
Rev:=Num2+(Rev*10);
Num1:=floor(Num1/10);
end loop;
dbms_output.put_line('Reverse number is'||rev);
end;
/
Output
:NUM1 321
28
5) Write a PL/SQL program to find the factorial of a givennumber.
declare
i number(4):=1;
n umber(4):=:n; f
number(4):=1;
begin
for i in1..n
loop
f:=f*i;
end loop;
dbms_output.put_line('the factorial of '||n||' is:'||f);
end;
Output
:N 5
the factorial of 5 is: 120
29
6) Write a PL/Sql Program to calculate the area of a circle
declare
pi constant number(4,2) := 3.14;
radius number(5);
area number(14,2);
Begin
radius := 3;
area := pi* power(radius,2);
dbms_output.put_line(‘area=’||area);
end;
/
Output
Area=28.26
Statement processed.
30
WEEK -5
PROCEDURES AND FUNCTIONS
1) calculate the net salary and year salary if da is 30% of basic, hra is 10% of basic and pf is
7% if basic salary is less than 8000, pf is 10% if basic sal between 8000 to160000.
Declare
ename varchar2(15);
basic number;
da number;
hra number;
pf number;
netsalary number;
yearsalarynumber;
begin
ename:=':ename';
basic:=:basic;
da:=basic*(30/100);
hra:=basic * (10/100);
if (basic < 8000)
then
pf:=basic * (7/100);
elseif (basic >= 8000 and basic <= 16000) then
pf:=basic * (10/100);
end if;
netsalary:=basic + da + hra - pf;
yearsalary := netsalary*12;
dbms_output.put_line('Employee name : ' || ename);
dbms_output.put_line('Providend Fund : ' || pf);
dbms_output.put_line('Net salary : ' || netsalary);
dbms_output.put_line('Year salary : '|| yearsalary);
end;
/
31
2) Create a function to find the factorial of a given number and hence findNCR.
IS
DECLARE
reverse varchar2(50);
BEGIN
FOR i in reverse 1..length(input) LOOP
reverse := reverse||''||substr(input, i, 1);
END LOOP;
dbms_output.put_line(reverse);
END;
32
/
WEEK-6
TRIGGERS
1. Create a row level trigger for the customers table that would fire for INSERT orUPDATE
or DELETE operations performed on the CUSTOMERS table. This trigger will display the
salary difference between the old values and newvalues:
CUSTOMERS table:
Here following two points are important and should be noted carefully:
OLD and NEW references are not available for table level triggers, rather you can use them
for record level triggers.
If you want to query the table in the same trigger, then you should use the AFTER keyword,
33
because triggers can query the table or change it again only after the initial changes are applied
and the table is back in a consistent state.
Above trigger has been written in such a way that it will fire before any DELETE or INSERT or
UPDATE operation on the table, but you can write your trigger on a single or multiple operations,
for example BEFORE DELETE, which will fire whenever a record will be deleted using
DELETE operation on the table.
Let us perform some DML operations on the CUSTOMERS table. Here is one INSERT statement,
which will create a new record in the table:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (7, 'Kriti',
22, 'HP', 7500.00 );
Old salary:
New salary:
7500 Salary
difference:
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
Table dropped.
3) Trigger before deleting a record from emp table. Trigger will insert the row tobe
deleted into another table and also record the user who has deleted therecord.
Trigger created.
8 rows deleted.
ID SALARY DELETE_DADELETED_BY
01 1234.56 09-SEP-06JAVA2S
02 6661.78 09-SEP-06JAVA2S
03 6544.78 09-SEP-06JAVA2S
04 2344.78 09-SEP-06JAVA2S
05 2334.78 09-SEP-06JAVA2S
36
06 4322.78 09-SEP-06JAVA2S
07 7897.78 09-SEP-06JAVA2S
08 1232.78 09-SEP-06JAVA2S
8 rows selected.
Table dropped.
WEEK-8
CURSORS
DEFINITION OF A CURSOR
CURSOR EXAMPLE:
declare
cursor xx is select empno,ename,sal from emp26;
a_empno emp26.empno%type;
a_ename emp26.ename%type;
a_sal emp26.sal%type;
begin
open xx;
loop
fetch xx into a_empno,a_ename,a_sal;
exit when xx% not found;
dbms_output.put_line(a_empno||' '||a_ename||' '||a_sal);
end loop;
close xx;
end;
SQL>/
37