You are on page 1of 85

CREATING USER:

To create a new user you should login as sys or system.


SQL> create user xcessme identified by pankaj1!"
#ser created.
SQL> connect"
$nter user%name& xcessme
$nter password& '''''''''
$(()(&
)(*%+1+,& user -.$SS/$ lacks .($*T$ S$SS0)1 pri2ilege" logon denied
3arning& 4ou are no longer connected to )(*.L$.
SQL> connect
$nter user%name& system
$nter password& '''''''''
.onnected.
SQL> grant connect5 resource to xcessme"
6rant succeeded.
SQL> connect
$nter user%name& xcessme
$nter password& '''''''''
.onnected.
FAILED_LOGIN_ATTEMPTS:
This option if set to ,7for e.g.85 then four
consecuti2e failed login attempts will be allowed for the account" the fifth will cause the
account to be blocked.
SQL> create profile L0/0T$9:;()<0L$ limit
= <*0L$9:L)601:*TT$/;TS ,"
;rofile created.
SQL> create user seema identified by seema1=!
= profile L0/0T$9:;()<0L$"
#ser created.
SQL> 6(*1T .($*T$ S$SS0)1 T) S$$/*"
6rant succeeded.
*fter fi2e unsuccessful attempts to connect to #ser seema. The conn command gi2es following
output.
SQL> conn
$nter user%name& seema
$nter password& '''
$(()(&
)(*%=>+++& the account is locked
To unlock the account the account unlock clause is used with alter user command.
1
SQL> alter user seema account unlock"
#ser altered.
SQL> conn
$nter user%name& seema
$nter password& ''''''''
.onnected
1)T$&
To lock an account manually we can use the following command.
SQL> alter user seema account lock"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
INSERT COMMAND:
SQL> insert into student 2alues 7?@name?5@rollno5?@class?5@marks8"
$nter 2alue for name& pankaj
$nter 2alue for rollno& ,A
$nter 2alue for class& mca
$nter 2alue for marks& =>,
old 1& insert into student 2alues 7?@name?5@rollno5?@class?5@marks8
new 1& insert into student 2alues 7?pankaj?5,A5?mca?5=>,8
SQL> B
$nter 2alue for name& seema
$nter 2alue for rollno& ,
$nter 2alue for class& msc
$nter 2alue for marks& !++
old 1& insert into student 2alues 7?@name?5@rollno5?@class?5@marks8
new 1& insert into student 2alues 7?seema?5,5?msc?5!++8
and so onC.
SQL> select ' from student"
1*/$ ()LL1) .L*SS /*(DS
%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%% %%%%%%%%%%
pankaj ,A mca =>,
seema , msc !++
rashmi =+ msc =A+
neerja 11 btech =E+
SQL> clear scr
SQL> create table employee 7 code char7,85 name 2archar=7!+85 desi
= 2archar=7!+85doj date5salary number8"
Table created.
SQL> desc employee"
1ame 1ullF Type
%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%
2
.)9$ .G*(7,8
1*/$ H*(.G*(=7!+8
9$S0 H*(.G*(=7!+8
9)I 9*T$
S*L*(4 1#/J$(
SQL> insert into employee 2alues 7?@code?5?@name?5?@desi?5?@doj?5@salary8"
$nter 2alue for code& a++1
$nter 2alue for name& pankaj
$nter 2alue for desi& programmer
$nter 2alue for doj& =%dec%=++,
$nter 2alue for salary& !++++
old 1& insert into employee 2alues 7?@code?5?@name?5?@desi?5?@doj?5@salary8
new 1& insert into employee 2alues 7?a++1?5?pankaj?5?programmer?5?=%dec%=++,?5!++++8
1 row created.
*nd so onC
SQL> set linesiKe 1++"
SQL> set pagesiKe !+"
SQL> select ' from employee"
.)9$ 1*/$ 9$S0 9)I S*L*(4
%%%%% %%%%%%%% %%%%%%%%%%%%% %%%%%%%%% %%%%%%%%%
a++1 pankaj programmer +=%9$.%+, !++++
a++= seema testing !1%9$.%+, !,+++
b++1 babul analyst =1%I#1%+L =,+++
c++1 you2ika designer =1%*;(%+A =++++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%
IMPOSING CONSTRAINTS ON TABLE:
NOT NULL CONSTRAINT:
SQL> create table workers 7name 2archar=7=+8 not null5wno number not null5phoneno
number5salary number not null8"
Table created.
SQL> desc workers"
1ame 1ullF Type
%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%
1*/$ 1)T 1#LL H*(.G*(=7=+8
31) 1)T 1#LL 1#/J$(
;G)1$1) 1#/J$(
S*L*(4 1)T 1#LL 1#/J$(
SQL> insert into workers 2alues 7?@name?5@wno5@phonenumber5@salary8"
$nter 2alue for name& ramsingh
$nter 2alue for wno& 1=!
3
$nter 2alue for phonenumber& =,A=1
$nter 2alue for salary& ,+++
old 1& insert into workers 2alues 7?@name?5@wno5@phonenumber5@salary8
new 1& insert into workers 2alues 7?ramsingh?51=!5=,A=15,+++8
1 row created.
SQL> B
$nter 2alue for name& sher singh
$nter 2alue for wno& null
$nter 2alue for phonenumber& =>E>>,
$nter 2alue for salary& ,=++
old 1& insert into workers 2alues 7?@name?5@wno5@phonenumber5@salary8
new 1& insert into workers 2alues 7?sher singh?5null5=>E>>,5,=++8
insert into workers 2alues 7?sher singh?5null5+1A!=%=>E>>,5,=++8
'
$(()( at line 1&
)(*%+1++& cannot insert 1#LL into 7MS.)TTM.M3)(D$(SM.M31)M8
SQL> B
$nter 2alue for name& sher singh
$nter 2alue for wno& 1=!>
$nter 2alue for phonenumber& null
$nter 2alue for salary& ,=++
old 1& insert into workers 2alues 7?@name?5@wno5@phonenumber5@salary8
new 1& insert into workers 2alues 7?sher singh?51=!>5null5,=++8
1 row created.
SQL> B
$nter 2alue for name& dilbag singh
$nter 2alue for wno& 1=!E
$nter 2alue for phonenumber& null
$nter 2alue for salary& ,++
old 1& insert into workers 2alues 7?@name?5@wno5@phonenumber5@salary8
new 1& insert into workers 2alues 7?dilbag singh?51=!E5null5,++8
1 row created.
SQL> select ' from workers"
1*/$ 31) ;G)1$1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%% %%%%%%%%%% %%%%%%%%%%
ramsingh 1=! =!E>E ,+++
hari ram 1=!, =EA++A ,,++
umed singh 1=!L =,,+11 +++
pala ram 1=!A =!,E+ ,++
sher singh 1=!> ,=++
dilbag singh 1=!E ,++
L rows selected.
UNIQUE CONSTRAINT:
SQL> create table items 7ino number uniNue5iname 2archar=71,85price number71+5=8 not
null5Nty number8"
4
Table created.
SQL> insert into items 2alues 7@ino5?@iname?5@price5@Nty8"
$nter 2alue for ino& 1
$nter 2alue for iname& computer
$nter 2alue for price& !++++
$nter 2alue for Nty& 1,
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 715?computer?5!++++51,8
1 row created.
SQL> B
$nter 2alue for ino& =
$nter 2alue for iname& laptop
$nter 2alue for price& L++++
$nter 2alue for Nty& >
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 7=5?laptop?5L++++5>8
1 row created.
SQL> B
$nter 2alue for ino& !
$nter 2alue for iname& mobile
$nter 2alue for price& =++++
$nter 2alue for Nty& =,
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 7!5?mobile?5=++++5=,8
1 row created.
SQL> B
$nter 2alue for ino&
$nter 2alue for iname& ipod
$nter 2alue for price& !++++
$nter 2alue for Nty& ,
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 75?ipod?5!++++5,8
1 row created.
SQL> B
$nter 2alue for ino& =
$nter 2alue for iname& microwa2e
$nter 2alue for price& =,+++
$nter 2alue for Nty&
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 7=5?microwa2e?5=,+++58
insert into items 2alues 7=5?microwa2e?5=,+++58
'
$(()( at line 1&
)(*%++++1& uniNue constraint 7S.)TT.S4S:.++=A!18 2iolated
SQL> select ' from items"
01) 01*/$ ;(0.$ QT4
5
%%%%%%%%% %%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%
1 computer !++++ 1,
= laptop L++++ >
! mobile =++++ =,
ipod !++++ ,
SQL> insert into items 2alues 7@ino5?@iname?5@price5@Nty8"
$nter 2alue for ino& null
$nter 2alue for iname& furniture
$nter 2alue for price& !,+++
$nter 2alue for Nty& =
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 7null5?furniture?5!,+++5=8
1 row created.
SQL> B
$nter 2alue for ino& null
$nter 2alue for iname& utensil
$nter 2alue for price& 1,+++
$nter 2alue for Nty& !
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 7null5?utensil?51,+++5!8
1 row created.
SQL> select ' from items"
01) 01*/$ ;(0.$ QT4
%%%%%% %%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
1 computer !++++ 1,
= laptop L++++ >
! mobile =++++ =,
ipod !++++ ,
furniture !,+++ =
utensil 1,+++ !
L rows selected.
PRIMARY KEY:
SQL> create table customer 7name 2archar=71,85cno number primary key5caddress
= 2archar=7=+85itembuy 2archar=7=+88"
Table created.
SQL> desc customer"
1ame 1ullF Type
%%%%%%%%%%%%% %%%%%%%%%%%%% %%%%%%%%%%%%
1*/$ H*(.G*(=71,8
.1) 1)T 1#LL 1#/J$(
.*99($SS H*(.G*(=7=+8
0T$/J#4 H*(.G*(=7=+8
SQL> insert into customer 2alues 7?@name?5@cno5?@caddress?5?@itembuy?8"
6
$nter 2alue for name& pankaj
$nter 2alue for cno& 1
$nter 2alue for caddress& OJ=
$nter 2alue for itembuy& laptop
old 1& insert into customer 2alues 7?@name?5@cno5?@caddress?5?@itembuy?8
new 1& insert into customer 2alues 7?pankaj?515?OJ=?5?laptop?8
1 row created.
SQL> B
$nter 2alue for name& seema
$nter 2alue for cno& =
$nter 2alue for caddress& OJ=
$nter 2alue for itembuy& furniture
old 1& insert into customer 2alues 7?@name?5@cno5?@caddress?5?@itembuy?8
new 1& insert into customer 2alues 7?seema?5=5?OJ=?5?furniture?8
1 row created.
SQL> B
$nter 2alue for name& rashmi
$nter 2alue for cno& !
$nter 2alue for caddress& O9,!
$nter 2alue for itembuy& computer
old 1& insert into customer 2alues 7?@name?5@cno5?@caddress?5?@itembuy?8
new 1& insert into customer 2alues 7?rashmi?5!5?O9,!?5?computer?8
1 row created.
SQL> B
$nter 2alue for name& (a2i
$nter 2alue for cno& =
$nter 2alue for caddress& O(T=1
$nter 2alue for itembuy& mobile
old 1& insert into customer 2alues 7?@name?5@cno5?@caddress?5?@itembuy?8
new 1& insert into customer 2alues 7?(a2i?5=5?O(T=1?5?mobile?8
insert into customer 2alues 7?(a2i?5=5?O(T=1?5?mobile?8
'
$(()( at line 1&
)(*%++++1& uniNue constraint 7S.)TT.S4S:.++=A!=8 2iolated
SQL> B
$nter 2alue for name& (a2i
$nter 2alue for cno& null
$nter 2alue for caddress& O<=
$nter 2alue for itembuy& mobile
old 1& insert into customer 2alues 7?@name?5@cno5?@caddress?5?@itembuy?8
new 1& insert into customer 2alues 7?(a2i?5null5?O<=?5?mobile?8
insert into customer 2alues 7?(a2i?5null5?O<=?5?mobile?8
'
$(()( at line 1&
)(*%+1++& cannot insert 1#LL into 7MS.)TTM.M.#ST)/$(M.M.1)M8
SQL> select ' from customer"
7
1*/$ .1) .*99($SS 0T$/J#4
%%%%%%% %%% %%%%%%%%%%%% %%%%%%%%%%%
pankaj 1 OJ= laptop
seema = OJ= furniture
rashmi ! O9,! computer
SQL> select ' from tab"
T1*/$ T*JT4;$ .L#ST$(09
%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%
J)1#S T*JL$
.#ST)/$( T*JL$
$/;L)4$$ T*JL$
0T$/S T*JL$
S*L6(*9$ T*JL$
ST#1 T*JL$
ST#9$1T T*JL$
3)(D$(S T*JL$
> rows selected.
SQL> desc customer"
1ame 1ullF Type
%%%%%%%%%%%%% %%%%%%%%%%% %%%%%%%%%%%%%%%%
1*/$ H*(.G*(=71,8
.1) 1)T 1#LL 1#/J$(
.*99($SS H*(.G*(=7=+8
0T$/J#4 H*(.G*(=7=+8
SQL> create table college 7sname 2archar=7=+85rollno number5class 2archar=7,85
= primary key 7rollno5class88"
Table created.
SQL> insert into college 2alues 7?@sname?5@rollno5?@class?8"
$nter 2alue for sname& pankaj
$nter 2alue for rollno& ,A
$nter 2alue for class& mca
old 1& insert into college 2alues 7?@sname?5@rollno5?@class?8
new 1& insert into college 2alues 7?pankaj?5,A5?mca?8
SQL> B
$nter 2alue for sname& seema
$nter 2alue for rollno& ,
$nter 2alue for class& msc
old 1& insert into college 2alues 7?@sname?5@rollno5?@class?8
new 1& insert into college 2alues 7?seema?5,5?msc?8
1 row created.
SQL> B
$nter 2alue for sname& pankaj
$nter 2alue for rollno& ,A
$nter 2alue for class& msc
8
old 1& insert into college 2alues 7?@sname?5@rollno5?@class?8
new 1& insert into college 2alues 7?pankaj?5,A5?msc?8
1 row created.
SQL> B
$nter 2alue for sname& rashmi
$nter 2alue for rollno& !+
$nter 2alue for class& msc
old 1& insert into college 2alues 7?@sname?5@rollno5?@class?8
new 1& insert into college 2alues 7?rashmi?5!+5?msc?8
1 row created.
SQL> B
$nter 2alue for sname& ra2i
$nter 2alue for rollno& !+
$nter 2alue for class& msc
old 1& insert into college 2alues 7?@sname?5@rollno5?@class?8
new 1& insert into college 2alues 7?ra2i?5!+5?msc?8
insert into college 2alues 7?ra2i?5!+5?msc?8
'
$(()( at line 1&
)(*%++++1& uniNue constraint 7S.)TT.S4S:.++=A!!8 2iolated
SQL> B
$nter 2alue for sname& ra2i
$nter 2alue for rollno& null
$nter 2alue for class& mca
old 1& insert into college 2alues 7?@sname?5@rollno5?@class?8
new 1& insert into college 2alues 7?ra2i?5null5?mca?8
insert into college 2alues 7?ra2i?5null5?mca?8
'
$(()( at line 1&
)(*%+1++& cannot insert 1#LL into 7MS.)TTM.M.)LL$6$M.M()LL1)M8
SQL> desc college
1ame 1ullF Type
%%%%%%%%%%%%%% %%%%%%%%%%%%% %%%%%%%%%%%%%%%%%
S1*/$ H*(.G*(=7=+8
()LL1) 1)T 1#LL 1#/J$(
.L*SS 1)T 1#LL H*(.G*(=7,8
SQL> select ' from college"
S1*/$ ()LL1) .L*SS
%%%%%%%%%%%% %%%%%%%%%% %%%%%%%
pankaj ,A mca
seema , msc
pankaj ,A msc
rashmi !+ msc
SQL> insert into college 2alues 7?@sname?5@rollno5?@class?8"
$nter 2alue for sname& ra2i
$nter 2alue for rollno& 1
$nter 2alue for class&
old 1& insert into college 2alues 7?@sname?5@rollno5?@class?8
new 1& insert into college 2alues 7?ra2i?515??8
9
insert into college 2alues 7?ra2i?515??8
'
$(()( at line 1&
)(*%+1++& cannot insert 1#LL into 7MS.)TTM.M.)LL$6$M.M.L*SSM8
SQL> insert into college 2alues 7?@sname?5@rollno5?@class?8"
$nter 2alue for sname& pankaj
$nter 2alue for rollno& ,A
$nter 2alue for class& mca
old 1& insert into college 2alues 7?@sname?5@rollno5?@class?8
new 1& insert into college 2alues 7?pankaj?5,A5?mca?8
insert into college 2alues 7?pankaj?5,A5?mca?8
'
$(()( at line 1&
)(*%++++1& uniNue constraint 7S.)TT.S4S:.++=A!!8 2iolated
REFERENTIAL INTEGRITY CONSTRAINT:
SQL> create table employee 7eno number primary key5name 2archar=7=+85
= dept 2archar=7=+85dmgrno number references employee8"
Table created.
SQL> insert into employee 2alues 71=5?pankaj?5?production?51=8"
1 row created
SQL> insert into employee 2alues 71,5?ra2i?5?maintenance?51=8"
1 row created
SQL> insert into employee 2alues 7=,5?nitesh?5?testing?5=+8"
insert into employee 2alues 7=,5?nitesh?5?testing?5=+8
'
$(()( at line 1&
)(*%+==E1& integrity constraint 7S.)TT.S4S:.++=AL8 2iolated % parent key not
<ound
SQL> create table items 7ino number primary key5iname 2archar=7=+85price number5Nty
= number8"

Table created.
SQL> insert into items 2alues 7@ino5?@iname?5@price5@Nty8"
$nter 2alue for ino& 1
$nter 2alue for iname& computer
$nter 2alue for price& !++++
$nter 2alue for Nty& =
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 715?computer?5!++++5=8
1 row created.
SQL> B
$nter 2alue for ino& =
10
$nter 2alue for iname& laptop
$nter 2alue for price& L++++
$nter 2alue for Nty& ,
old 1& insert into items 2alues 7@ino5?@iname?5@price5@Nty8
new 1& insert into items 2alues 7=5?laptop?5L++++5,8
1 row created.
SQL> select ' from items"
01) 01*/$ ;(0.$ QT4
%%%% %%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%
1 computer !++++ =
= laptop L++++ ,
! mobile =++++ 1+
mp! player 1,+++
SQL> desc items"
1ame 1ullF Type
%%%%%%%%%%%%% %%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%
01) 1)T 1#LL 1#/J$(
01*/$ H*(.G*(=7=+8
;(0.$ 1#/J$(
QT4 1#/J$(
SQL> create table sale 7ino number references items5 sprice number5Nty number8"
Table created.
SQL> insert into sale 2alues 7@ino5@sprice5@Nty8"
$nter 2alue for ino& 1
$nter 2alue for sprice& !,+++
$nter 2alue for Nty& =
old 1& insert into sale 2alues 7@ino5@sprice5@Nty8
new 1& insert into sale 2alues 715!,+++5=8
1 row created.
SQL> B
$nter 2alue for ino& =
$nter 2alue for sprice& =,+++
$nter 2alue for Nty& !
old 1& insert into sale 2alues 7@ino5@sprice5@Nty8
new 1& insert into sale 2alues 7=5=,+++5!8
1 row created.
SQL> B
$nter 2alue for ino& L
$nter 2alue for sprice& 1=+++
$nter 2alue for Nty& =
old 1& insert into sale 2alues 7@ino5@sprice5@Nty8
new 1& insert into sale 2alues 7L51=+++5=8
insert into sale 2alues 7L51=+++5=8
'
11
$(()( at line 1&
)(*%+==E1& integrity constraint 7S.)TT.S4S:.++=A!L8 2iolated % parent key not found
SQL> select ' from sale"
01) S;(0.$ QT4
%%%%%%% %%%%%%%%%% %%%%%
1 !,+++ =
= =,+++ !
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%
SQL> create table company 7department 2archar=7!+85dno number uniNue5dmgr 2archar=7!+88"
Table created.
SQL> insert into company 2alues 7?@department?5@dno5?@dmgr?8"
$nter 2alue for department& research
$nter 2alue for dno& 1=!
$nter 2alue for dmgr& ;ankaj
old 1& insert into company 2alues 7?@department?5@dno5?@dmgr?8
new 1& insert into company 2alues 7?research?51=!5?;ankaj?8
1 row created.
SQL> B
$nter 2alue for department& 9e2elopment
$nter 2alue for dno& 1=
$nter 2alue for dmgr& seema
old 1& insert into company 2alues 7?@department?5@dno5?@dmgr?8
new 1& insert into company 2alues 7?9e2elopment?51=5?seema?8
1 row created.
SQL> B
$nter 2alue for department& testing
$nter 2alue for dno& 1=,
$nter 2alue for dmgr& you2ika
old 1& insert into company 2alues 7?@department?5@dno5?@dmgr?8
new 1& insert into company 2alues 7?testing?51=,5?you2ika?8
SQL> B
$nter 2alue for department& maintenance
$nter 2alue for dno& 1=L
$nter 2alue for dmgr& ra2i
old 1& insert into company 2alues 7?@department?5@dno5?@dmgr?8
new 1& insert into company 2alues 7?maintenance?51=L5?ra2i?8
SQL> select ' from company"
9$;*(T/$1T 91) 9/6(
%%%%%%%%%%%%%%% %%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
research 1=! ;ankaj
9e2elopment 1= seema
12
testing 1=, you2ika
maintenance 1=L ra2i
SQL> create table dlocation 7dno number references company7dno85location 2archar=7!+88"

Table created.
SQL> insert into dlocation 2alues 7@dno5?@location?8"
$nter 2alue for dno& 1=!
$nter 2alue for location& delhi
old 1& insert into dlocation 2alues 7@dno5?@location?8
new 1& insert into dlocation 2alues 71=!5?delhi?8
1 row created and so on.
SQL> select ' from dlocation"
91) L).*T0)1
%%%%%%% %%%%%%%%%
1=! delhi
1= delhi
1=, mumbai
1=, bangalore
1=L pune
SQL> insert into dlocation 2alues 71=A5PchandigarhP8"
insert into dlocation 2alues 71=A5?chandigarh?8
'
$(()( at line 1&
)(*%+==E1& integrity constraint 7S.)TT.S4S:.++=A18 2iolated % parent key not found
SQL> create table items 7ino number7,8 primary key5 iname 2archar=7=+85cprice
= number71+5=88"
SQL> select ' from items"
01) 01*/$ .;(0.$
%%%% %%%%%%%%% %%%%%%%%%%
1 computer !++++
= laptop L++++
! mobile =++++
i;od =,+++
SQL> create table sale 7ino number7,8 references items on delete set null5sprice
= number71+5=88"
SQL> select ' from sale"
01) S;(0.$
%%%%%%% %%%%%%%%%%
1 !,+++
= L>+++
1 ++++
13
! =!+++
! =+++
SQL> delete from items where inoQ1"
1 row deleted
SQL> select ' from sale"
01) S;(0.$
%%%%%% %%%%%%%%%%
= L>+++
! =!+++
! =+++
!,+++
++++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table items 7ino number7,8 primary key5 iname 2archar=7=+85cprice
2 number71+5=88"
SQL> select ' from items"
01) 01*/$ .;(0.$
%%%% %%%%%%%%%%%%% %%%%%%%%%%
1 computer !++++
= laptop L++++
! mobile =++++
i;od =,+++
SQL> create table sale 7ino number7,8 references items on delete cascade5sprice
number71+5=88"
SQL> select ' from sale"
01) S;(0.$
%%%%% %%%%%%%%%%
1 !,+++
1 !++++
= L!+++
1 !,+++
! =L+++
,++++
= A++++
SQL> delete from items where inoQ1"
SQL> select ' from items"

01) 01*/$ .;(0.$
%%%% %%%%%%%%% %%%%%%%%
= laptop L++++
! mobile =++++
i;od =,+++
SQL> select ' from sale"
14
01) S;(0.$
%%%%% %%%%%%%%%
= L!+++
! =L+++
,++++
= A++++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table employee 7code char7,85name 2archar=7=+85dept 2archar=71,85
2 salary number5constraint sal check 7salary > +88"
SQL> insert into employee 2alues 7?a++1?5?pankaj?5?programming?5!++++8"
1 row created.
SQL> insert into employee 2alues 7?b++1?5?ra2i?5?testing?5+8"
insert into employee 2alues 7?b++1?5?ra2i?5?testing?5+8
'
$(()( at line 1&
)(*%+==E+& check constraint 7S.)TT.S*L8 2iolated
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table employee 7code char7,85name 2archar=71,85desi 2archar=7=+8
= check7desi in7?programmer?5?manager?5?debugger?885salary number check
7salary between 1++++ and !++++88"
SQL> insert into employee 2alues 7?a++1?5?pankaj?5?programmer?51=+++8"
1 row created
SQL> insert into employee 2alues 7?a++1?5?nitesh?5?programmer?51++++8"
1 row created
SQL> insert into employee 2alues 7?a++1?5?nitesh?5?designing?51++++8"
$(()( at line 1&
)(*%+==E+& check constraint 7S.)TT.S4S:.++=AL8 2iolated
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table employee 7code char7,85name 2archar=7=+85desi 2archar=7=+8
constraint profile check7desi in7?programmer?5?debugger?5?analyst?885
salary number constraint sal check7salary between 1++++ and !++++88"
Table created.
SQL> insert into employee 2alues 7?p++1?5?babul?5?programmer?51=+++8"
1 row created
SQL> insert into employee 2alues 7?a++1?5?tarun?5?programmer?51++++8"
1 row created
SQL> insert into employee 2alues 7?d++1?5?you2ika?5?administrator?5=,+++8"
15
insert into employee 2alues 7?d++1?5?you2ika?5?administrator?5=,+++8
'
$(()( at line 1&
)(*%+==E+& check constraint 7S.)TT.;()<0L$8 2iolated
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table community 7member 2archar=7=+85ccode 2archar=71+8 constraint
= code check 7ccode like ?ja2aR?88"
Table created
SQL> insert into community 2alues 7?pankaj?5?ja2a+1?8"
1 row created.
SQL> insert into community 2alues 7?babul?5?ja2a+=?8"
1 row created.
SQL> insert into community 2alues 7?ra2i?5?linux+=?8"
$(()( at line 1&
)(*%+==E+& check constraint 7S.)TT..)9$8 2iolated
ALTER COMMAND:
SQL> alter table items add 72endor 2archar=7=+8 not null8"
alter table items add 72endor 2archar=7=+8 not null8
'
$(()( at line 1&
)(*%+1A,>& table must be empty to add mandatory 71)T 1#LL8 column
SQL> alter table items add 72endor 2archar=71,88"
Table altered.
SQL> desc items"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%
01) 1)T 1#LL 1#/J$(7,8
01*/$ H*(.G*(=7=+8
.;(0.$ 1#/J$(71+5=8
H$19)( H*(.G*(=71,8
SQL> select ' from items"
01) 01*/$ .;(0.$ H$19)(
%%%%%%% %%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
= laptop L++++
! mobile =++++
i;od =,+++
SQL> alter table items drop column 2endor"
16
Table altered.
SQL> select ' from items"
01) 01*/$ .;(0.$
%%%%%% %%%%%%%%%%%%%%%%% %%%%%%%%%%
= laptop L++++
! mobile =++++
i;od =,+++
SQL> alter table items add 72endor 2archar=7=+8 uniNue8"
Table altered.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table mobiles 7mobileno 2archar=7=+85company 2archar=7=+85price
number71+5=88"
Table created.
SQL> insert into mobiles 2alues 7?1E!?5?nokia?5!=+++8"
1 row created.
SQL> insert into mobiles 2alues 7?D,++i?5?sony erricson?51++++8"
1 row created.
SQL> insert into mobiles 2alues 7?r,,+l?5?reliance?51+++8"
1 row created.
SQL> select ' from mobiles"
/)J0L$1) .)/;*14 ;(0.$
%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
1E! nokia !=+++
D,++i sony erricson 1++++
r,,+l reliance 1+++
SQL> alter table mobiles add primary key 7mobileno8"
Table altered.
SQL> desc mobiles"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%
/)J0L$1) 1)T 1#LL H*(.G*(=7=+8
.)/;*14 H*(.G*(=7=+8
;(0.$ 1#/J$(71+5=8
SQL> alter table mobiles drop primary key"
Table altered.
17
SQL> desc mobiles"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%
/)J0L$1) H*(.G*(=7=+8
.)/;*14 H*(.G*(=7=+8
;(0.$ 1#/J$(71+5=8
SQL> alter table mobiles add primary key 7mobileno5company8"
Table altered.
SQL> desc mobiles"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
/)J0L$1) 1)T 1#LL H*(.G*(=7=+8
.)/;*14 1)T 1#LL H*(.G*(=7=+8
;(0.$ 1#/J$(71+5=8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table countries 7cname 2archar=7=+85population number5president 2archar=7=+88"
Table created.
SQL> insert into countries 2alues 7?0ndia?51=++++++++5?;ratibha ;atil?8"
1 row created.
SQL> insert into countries 2alues 7?(ussia?5!++++++5?Hladimir ;utin?8"
1 row created.
SQL> insert into countries 7cname5population8 2alues 7?Saudi *rabia?51+++++++8"
1 row created.
SQL> select ' from countries"
.1*/$ ;);#L*T0)1 ;($S09$1T
%%%%%%%%%%%%%% %%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%
0ndia 1=++++++++ ;ratibha ;atil
(ussia !++++++ Hladimir ;utin
Saudi *rabia 1+++++++
SQL> alter table countries add primary key 7president8"
alter table countries add primary key 7president8
'
$(()( at line 1&
)(*%+1E& column contains 1#LL 2alues" cannot alter to 1)T 1#LL
SQL> delete from countries where populationQ1+++++++"
1 row deleted.
SQL> alter table countries drop primary key"
18
Table altered.
SQL> alter table countries add primary key 7cname8"
Table altered.
SQL> create table 61, 7country 2archar=7=+85joiningdate date5members number not
= null8"
Table created.
SQL> alter table 61, add foreign key 7country8 references countries"
Table altered.
SQL> insert into 61, 2alues 7?0ndia?5?1E%dec%1E>,?5,8"
1 row created.
SQL> insert into 61, 2alues 7?Shri Lanka?5?=!%june%1EE1?5=8"
1 row created.
SQL> insert into 61, 2alues 7?.hina?5?1=%may%1EEE?5!8"
insert into 61, 2alues 7?.hina?5?1=%may%1EEE?5!8
'
$(()( at line 1&
)(*%+==E1& integrity constraint 7S.)TT.S4S:.++=A,E8 2iolated % parent key not found
SQL> select ' from countries"
.1*/$ ;);#L*T0)1 ;($S09$1T
%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%
0ndia 1=++++++++ ;ratibha ;atil
(ussia !++++++ Hladimir ;utin
Shri Lanka L+++++++ Dumartunga
Jangladesh ,+++++++ /ufti /ohammed Saeed
;akistan A+++++++ ;er2eK /usharaf
SQL> alter table 61, add constraint mem check 7members SQ,8"
Table altered.
SQL> insert into 61, 2alues 7?;akistan?5?=!%no2%1EEA?5E8"
insert into 61, 2alues 7?;akistan?5?=!%no2%1EEA?5E8
'
$(()( at line 1&
)(*%+==E+& check constraint 7S.)TT./$/8 2iolated
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> desc countries"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
.1*/$ 1)T 1#LL H*(.G*(=7=+8
19
;);#L*T0)1 1#/J$(
;($S09$1T H*(.G*(=7=+8
SQL> alter table countries modify 7president 2archar=7=,88"
Table altered.
SQL> desc countries"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%
.1*/$ 1)T 1#LL H*(.G*(=7=+8
;);#L*T0)1 1#/J$(
;($S09$1T H*(.G*(=7=,8
SQL> rename countries to asian:countries"
Table (enamed
SQL> drop table countries cascade constraints"
Table dropped.
SQL> drop table 61,"
Table dropped.
SQL> create table 0ndia 7place 2archar=7=+85state 2archar=7=+85status 2archar=7=+8
default ?capital?8"
Table created
SQL> insert into 0ndia 2alues 7?pune?5?maharashtra?5?city?8"
1 row created.
SQL> insert into 0ndia 2alues 7?mysoore?5?karnataka?5?district?8"
1 row created.
SQL> insert into 0ndia 7place5state8 2alues 7?chandigarh?5?Garyana?8"
1 row created.
SQL> insert into 0ndia 2alues 7?jaipur?5?(ajasthan?5default8"
1 row created.
SQL> insert into 0ndia 2alues 7?patna?5?Jihar?5null8"
1 row created.
SQL> select ' from 0ndia"
;L*.$ ST*T$ ST*T#S
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%
pune maharashtra city
20
mysoore karnataka district
chandigarh Garyana capital
patna Jihar
jaipur (ajasthan capital
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> desc sony"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
.)9$ .G*(7,8
1*/$ 1)T 1#LL H*(.G*(=7=+8
9$S0 H*(.G*(=7!+8
S*L*(4 1#/J$(71+5=8
SQL> select constraint:name5constraint:type from all:constraints where table:nameQ?S)14?"
.)1ST(*01T:1*/$ .
%%%%%%%%%%%%%%%%%%%%%%%% %
S4S:.++=AL1 .
D#D# .
SQL> insert into sony 2alues 7?b++1?5?*nu?5?clerk?5=++++8"
insert into sony 2alues 7?b++1?5?*nu?5?clerk?5=++++8
'
$(()( at line 1&
)(*%+==E+& check constraint 7S.)TT.D#D#8 2iolated
SQL> insert into sony 7code5name5salary8 2alues 7?b++1?5?;riya?5=++++8"
insert into sony 7code5name5salary8 2alues 7?b++1?5?;riya?5=++++8
'
$(()( at line 1&
)(*%+==E+& check constraint 7S.)TT.D#D#8 2iolated
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select ' from workers"
1*/$ 31) ;G)1$1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
ramsingh 1=! =!E>E ,+++
hari ram 1=!, =EA++A ,,++
umed singh 1=!L =,,+11 +++
pala ram 1=!A =!,E+ ,++
sher singh 1=!> ,=++
dilbag singh 1=!E ,++
L rows selected.
SQL> update workers set phonenoQ=!E>A, where wnoQ1=!>"
1 row updated.
SQL> update workers set phonenoQ=L,A= where phoneno is null"
21
1 row updated.
SQL> select ' from workers"
1*/$ 31) ;G)1$1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
ramsingh 1=! =!E>E ,+++
hari ram 1=!, =EA++A ,,++
umed singh 1=!L =,,+11 +++
pala ram 1=!A =!,E+ ,++
sher singh 1=!> =!E>A, ,=++
dilbag singh 1=!E =L,A= ,++
L rows selected.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select name 3orker5wno 3orker0d from workers"
3)(D$( 3)(D$(09
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%
ramsingh 1=!
hari ram 1=!,
umed singh 1=!L
pala ram 1=!A
sher singh 1=!>
dilbag singh 1=!E
SQL> select name5wno5salary5name from workers"
1*/$ 31) S*L*(4 1*/$
%%%%%%%%%%%%% %%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%%%%%
ramsingh 1=! ,+++ ramsingh
hari ram 1=!, ,,++ hari ram
umed singh 1=!L +++ umed singh
pala ram 1=!A ,++ pala ram
sher singh 1=!> ,=++ sher singh
dilbag singh 1=!E ,++ dilbag singh
L rows selected.
SQL> select ' from workers where wno in71=!51=!>51=!L8"
1*/$ 31) ;G)1$1) S*L*(4
%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
ramsingh 1=! =!E>E ,+++
umed singh 1=!L =,,+11 +++
sher singh 1=!> =!E>A, ,=++
SQL> select ' from workers where name in 7?umed singh?5?pala ram?5?dilbag singh?8"
1*/$ 31) ;G)1$1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%%
umed singh 1=!L =,,+11 +++
pala ram 1=!A =!,E+ ,++
dilbag singh 1=!E =L,A= ,++
22
SQL> select ' from workers"
1*/$ 31) ;G)1$1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
ramsingh 1=! =!E>E ,+++
hari ram 1=!, =EA++A ,,++
umed singh 1=!L =,,+11 +++
pala ram 1=!A =!,E+ ,++
sher singh 1=!> =!E>A, ,=++
dilbag singh 1=!E =L,A= ,++
satyanarayan 1=+ =,LA> !++
bhiku lal 1=1 =LA>E= L=++
ka2al bihari 1== =1!, L+++
karam singh 1=! =E>EE= ,!++
kasturi lal 1= =LAE>E ++
11 rows selected.
SQL> select name5wno from workers where salary between !++ and ++"
1*/$ 31)
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
umed singh 1=!L
satyanarayan 1=+
kasturi lal 1=
SQL> select name5wno5salary from workers where salary in 7!++5++8 or
= wno between 1== and 1="
1*/$ 31) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
satyanarayan 1=+ !++
ka2al bihari 1== L+++
karam singh 1=! ,!++
kasturi lal 1= ++
SQL> select name5salary from workers where name like ?kR?"
1*/$ S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
ka2al bihari L+++
karam singh ,!++
kasturi lal ++
SQL> select ?worker no& ?TT wno TT ? is ? TT name TT ? and got ? TT salary TT ? (s.?
from
= 3orkers"
worker no& 1=! is ramsingh and got ,+++ (s.
worker no& 1=!, is hari ram and got ,,++ (s.
worker no& 1=!L is umed singh and got +++ (s.
worker no& 1=!A is pala ram and got ,++ (s.
worker no& 1=!> is sher singh and got ,=++ (s.
worker no& 1=!E is dilbag singh and got ,++ (s.
worker no& 1=+ is satyanarayan and got !++ (s.
23
worker no& 1=1 is bhiku lal and got L=++ (s.
worker no& 1== is ka2al bihari and got L+++ (s.
worker no& 1=! is karam singh and got ,!++ (s.
worker no& 1= is kasturi lal and got ++ (s.
11 rows selected.
SQL> select name wno from workers"
31)
%%%%%%%%%%%%%%%%%%%%
ramsingh
hari ram
umed singh
pala ram
sher singh
dilbag singh
satyanarayan
bhiku lal
ka2al bihari
karam singh
kasturi lal
11 rows selected.
SQL> select name5wno from workers where name between ?amarnath? and ?kaka ram?"
1*/$ 31)
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
hari ram 1=!,
dilbag singh 1=!E
bhiku lal 1=1
anand 1=,
SQL> select name5salary from workers where name between ?abhay singh? and ?kasturi
= lal?"

1*/$ S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
hari ram ,,++
dilbag singh ,++
bhiku lal L=++
karam singh ,!++
kasturi lal ++
anand !>++
abhay singh +++
A rows selected.
SQL> select name5wno from workers where name like ?aR? or name like ?kR?"
1*/$ 31)
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
ka2al bihari 1==
24
karam singh 1=!
kasturi lal 1=
anand 1=,
abhay singh 1=L
SQL> select name5wno from workers where name between ?a? and ?p?"
1*/$ 31)
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
hari ram 1=!,
dilbag singh 1=!E
bhiku lal 1=1
ka2al bihari 1==
karam singh 1=!
kasturi lal 1=
anand 1=,
abhay singh 1=L
> rows selected.
JOIN:
SQL> create table T.S 7code 2archar=71+85 name 2archar=7=+85desi 2archar=7=+85
= salary number71+5=88"
SQL> create table salinfo 7code 2archar=71+85basic number71+5=85da number7>5=88"
SQL> select ' from T.S"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 anurag dba !++++
f++1 amit dba !++++
g++1 you2ika programmer !++++
a++1 pankaj programmer !,+++
A rows selected.
SQL> select ' from salinfo"
.)9$ J*S0. 9*
%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
a++1 1++++ !+++
c++1 A+++ 1,++
e++1 1++++ !+++
k++1 1=+++ ,++
p++1 >+++ =+++
, rows selected.
25
$Q#0%I)01&
SQL> select T.S.code5name5salary5basic5da from T.S5salinfo
= where T.S.codeQsalinfo.code"
.)9$ 1*/$ S*L*(4 J*S0. 9*
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
a++1 pankaj !,+++ 1++++ !+++
c++1 ra2i =++++ A+++ 1,++
e++1 anurag !++++ 1++++ !+++
1)1%$Q#0 I)01&
SQL> select T.S.code5name5salary5basic5da from T.S5salinfo
= where T.S.code UQ salinfo.code"
.)9$ 1*/$ S*L*(4 J*S0. 9*
%%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%
b++1 seema ++++ 1++++ !+++
c++1 ra2i =++++ 1++++ !+++
d++1 nitesh =,+++ 1++++ !+++
e++1 anurag !++++ 1++++ !+++
f++1 amit !++++ 1++++ !+++
g++1 you2ika !++++ 1++++ !+++
b++1 seema ++++ A+++ 1,++
d++1 nitesh =,+++ A+++ 1,++
e++1 anurag !++++ A+++ 1,++
f++1 amit !++++ A+++ 1,++
g++1 you2ika !++++ A+++ 1,++
a++1 pankaj !,+++ A+++ 1,++
b++1 seema ++++ 1++++ !+++
c++1 ra2i =++++ 1++++ !+++
d++1 nitesh =,+++ 1++++ !+++
f++1 amit !++++ 1++++ !+++
g++1 you2ika !++++ 1++++ !+++
a++1 pankaj !,+++ 1++++ !+++
b++1 seema ++++ 1=+++ ,++
c++1 ra2i =++++ 1=+++ ,++
d++1 nitesh =,+++ 1=+++ ,++
e++1 anurag !++++ 1=+++ ,++
f++1 amit !++++ 1=+++ ,++
g++1 you2ika !++++ 1=+++ ,++
a++1 pankaj !,+++ 1=+++ ,++
b++1 seema ++++ >+++ =+++
c++1 ra2i =++++ >+++ =+++
d++1 nitesh =,+++ >+++ =+++
e++1 anurag !++++ >+++ =+++
f++1 amit !++++ >+++ =+++
g++1 you2ika !++++ >+++ =+++
a++1 pankaj !,+++ >+++ =+++
!= rows selected.
)#T$( I)01&
L$<T )#T$( I)01&
26
SQL> select T.S.code5name5desi5basic from T.S5salinfo
= where T.S.code7V8Qsalinfo.code"
.)9$ 1*/$ 9$S0 J*S0.
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
a++1 pankaj programmer 1++++
c++1 ra2i super2isor A+++
e++1 anurag dba 1++++
1=+++
>+++
SQL> select salinfo.code5basic5da5name5salary from salinfo5T.S
= where salinfo.code7V8QT.S.code
.)9$ J*S0. 9* 1*/$ S*L*(4
%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
a++1 1++++ !+++ pankaj !,+++
seema ++++
c++1 A+++ 1,++ ra2i =++++
nitesh =,+++
e++1 1++++ !+++ anurag !++++
amit !++++
you2ika !++++
(06GT )#T$( I)01&
SQL> select T.S.code5name5desi5basic from T.S5salinfo
= where T.S.codeQsalinfo.code7V8"
.)9$ 1*/$ 9$S0 J*S0.
%%%%%%%%%% %%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%
a++1 pankaj programmer 1++++
b++1 seema designer
c++1 ra2i super2isor A+++
d++1 nitesh debugger
e++1 anurag dba 1++++
f++1 amit dba
g++1 you2ika programmer
A rows selected.
SQL> select salinfo.code5name5basic5da5salary from salinfo5T.S
= where salinfo.codeQT.S.code7V8"
.)9$ 1*/$ J*S0. 9* S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
a++1 pankaj 1++++ !+++ !,+++
c++1 ra2i A+++ 1,++ =++++
e++1 anurag 1++++ !+++ !++++
k++1 1=+++ ,++
p++1 >+++ =+++
S$L<%I)01 )( 011$( I)01&
SQL> select *.code5*.name5*.salary from T.S *5T.S J
27
= where *.salary > J.salary and J.nameQ?nitesh?"
.)9$ 1*/$ S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema ++++
e++1 anurag !++++
f++1 amit !++++
g++1 you2ika !++++
a++1 pankaj !,+++
S#JQ#$(0$S&
SQL> select ' from emp"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)// 9$;T1)
%%%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%%%
%%%%%%%%%%
A!LE S/0TG .L$(D AE+= 1A%9$.%>+ >++
=+
AEE *LL$1 S*L$S/*1 ALE> =+%<$J%>1 1L++ !++ !+
A,=1 3*(9 S*L$S/*1 ALE> ==%<$J%>1 1=,+ ,++ !+
A,LL I)1$S /*1*6$( A>!E +=%*;(%>1 =EA,
=+
AL, /*(T01 S*L$S/*1 ALE> =>%S$;%>1 1=,+ 1++ !+
ALE> JL*D$ /*1*6$( A>!E +1%/*4%>1 =>,+
!+
AA>= .L*(D /*1*6$( A>!E +E%I#1%>1 =,+ 1+
AA>> S.)TT *1*L4ST A,LL +E%9$.%>= !+++
=+
A>!E D016 ;($S09$1T 1A%1)H%>1 ,+++
1+
A> T#(1$( S*L$S/*1 ALE> +>%S$;%>1 1,++ + !+
A>AL *9*/S .L$(D AA>> 1=%I*1%>! 11++
=+
AE++ I*/$S .L$(D ALE> +!%9$.%>1 E,+
!+
AE+= <)(9 *1*L4ST A,LL +!%9$.%>1 !+++
=+
AE! /0LL$( .L$(D AA>= =!%I*1%>= 1!++
1+
1 rows selected.
SQL> select ' from emp where sal > 7select sal from emp where enameQ?I)1$S?8"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)// 9$;T1)
%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
AA>> S.)TT *1*L4ST A,LL +E%9$.%>= !+++ =+
A>!E D016 ;($S09$1T 1A%1)H%>1 ,+++ 1+
28
AE+= <)(9 *1*L4ST A,LL +!%9$.%>1 !+++ =+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select ' from dept"
9$;T1) 91*/$ L).
%%%%%%%%%% %%%%%%%%%%%%%% %%%%%%%%%%%%%
1+ *..)#1T016 1$3 4)(D
=+ ($S$*(.G 9*LL*S
!+ S*L$S .G0.*6)
+ );$(*T0)1S J)ST)1
SQL> select ' from emp where deptnoQ 7select deptno from dept where locQ?.G0.*6)?8"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)//
9$;T1)
%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%%%%%% %%%%%%
%%%%%%%%%% %%%%%%%%%
AEE *LL$1 S*L$S/*1 ALE> =+%<$J%>1 1L++ !++ !+
A,=1 3*(9 S*L$S/*1 ALE> ==%<$J%>1 1=,+ ,++ !+
AL, /*(T01 S*L$S/*1 ALE> =>%S$;%>1 1=,+ 1++ !+
ALE> JL*D$ /*1*6$( A>!E +1%/*4%>1 =>,+ !+
A> T#(1$( S*L$S/*1 ALE> +>%S$;%>1 1,++ + !+
AE++ I*/$S .L$(D ALE> +!%9$.%>1 E,+
!+
SQL> select ' from emp where sal Q 7select sal from emp where jobQ?S*L$S/*1?8"
select ' from emp where sal Q 7select sal from emp
'
$(()( at line 1&
)(*%+1=A& single%row subNuery returns more than one row
SQL> select ' from emp where jobQ?S*L$S/*1?"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)// 9$;T1)
%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%% %%%%%% %%%%%%%%%%%% %%%%%%% %%%%%%%%%%
%%%%%%%%%%
AEE *LL$1 S*L$S/*1 ALE> =+%<$J%>1 1L++ !++ !+
A,=1 3*(9 S*L$S/*1 ALE> ==%<$J%>1 1=,+ ,++ !+
AL, /*(T01 S*L$S/*1 ALE> =>%S$;%>1 1=,+ 1++ !+
A> T#(1$( S*L$S/*1 ALE> +>%S$;%>1 1,++ + !+
SQL> select ' from emp where sal S *147select sal from emp where jobQ?S*L$S/*1?8"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)//
9$;T1)
%%%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%% %%%%%%%%%%%%% %%%%%%
%%%%%%%%%% %%%%%%%%%%
A!LE S/0TG .L$(D AE+= 1A%9$.%>+ >++ =+
A,=1 3*(9 S*L$S/*1 ALE> ==%<$J%>1 1=,+ ,++ !+
29
AL, /*(T01 S*L$S/*1 ALE> =>%S$;%>1 1=,+ 1++ !+
A> T#(1$( S*L$S/*1 ALE> +>%S$;%>1 1,++ + !+
A>AL *9*/S .L$(D AA>> 1=%I*1%>! 11++ =+
AE++ I*/$S .L$(D ALE> +!%9$.%>1 E,+ !+
AE! /0LL$( .L$(D AA>= =!%I*1%>= 1!++
1+
A rows selected.
SQL> select ' from emp where sal > *147select sal from emp where jobQ?S*L$S/*1?8"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)// 9$;T1)
%%%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%%
%%%%%%%%%% %%%%%%%%%%
AEE *LL$1 S*L$S/*1 ALE> =+%<$J%>1 1L++ !++ !+
A,LL I)1$S /*1*6$( A>!E +=%*;(%>1 =EA, =+
ALE> JL*D$ /*1*6$( A>!E +1%/*4%>1 =>,+ !+
AA>= .L*(D /*1*6$( A>!E +E%I#1%>1 =,+ 1+
AA>> S.)TT *1*L4ST A,LL +E%9$.%>= !+++
=+
A>!E D016 ;($S09$1T 1A%1)H%>1 ,+++
1+
A> T#(1$( S*L$S/*1 ALE> +>%S$;%>1 1,++ + !+
AE+= <)(9 *1*L4ST A,LL +!%9$.%>1 !+++
=+
AE! /0LL$( .L$(D AA>= =!%I*1%>= 1!++
1+
SQL> select ' from emp where sal > *LL7select sal from emp where jobQ?S*L$S/*1?8"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)// 9$;T1)
%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%% %%%%%%%%%%
%%%%%%%%% %%%%%%%
A,LL I)1$S /*1*6$( A>!E +=%*;(%>1 =EA,
=+
ALE> JL*D$ /*1*6$( A>!E +1%/*4%>1 =>,+ !+
AA>= .L*(D /*1*6$( A>!E +E%I#1%>1 =,+
1+
AA>> S.)TT *1*L4ST A,LL +E%9$.%>= !+++
=+
A>!E D016 ;($S09$1T 1A%1)H%>1 ,+++
1+
AE+= <)(9 *1*L4ST A,LL +!%9$.%>1 !+++
=+

This command finds the employees who get more salary than a salesman who get maximum
salary amongst all salesmen.
SQL> select ' from emp where sal S *LL7select sal from emp where jobQ?S*L$S/*1?8"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)//
9$;T1)
%%%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%%
%%%%%%%%%% %%%%%%%%%%
A!LE S/0TG .L$(D AE+= 1A%9$.%>+ >++ =+
30
A>AL *9*/S .L$(D AA>> 1=%I*1%>! 11++
=+
AE++ I*/$S .L$(D ALE> +!%9$.%>1 E,+ !+
This command finds the employees who get less salary than the salary of a salesman who
get minimum salary amongst all salesman.
SQL> select ' from emp where sal >Q *LL7select sal from emp 8"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)// 9$;T1)
%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
A>!E D016 ;($S09$1T 1A%1)H%>1 ,+++ 1+
UNION:
SQL> select ' from employee"
1*/$ $1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
;ankaj 1=! !++++
Seema 1= !+++
ra2i 1=, !++++
nitesh 1=L =,+++
rashmi 1=A =++++
babul 1=> !,+++
prince 1=E =>+++
A rows selected.
SQL> select ' from 6oogle"
1*/$ $1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
pankaj 1=! !++++
seema 1= !+++
babul 1=> !,+++
4ou2ika 111 ++++
paras 1!1 ,+++
nipun 1A= !++++
L rows selected.
SQL> select ' from employee union select ' from 6oogle"
1*/$ $1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
4ou2ika 111 ++++
babul 1=> !,+++
nipun 1A= !++++
nitesh 1=L =,+++
pankaj 1=! !++++
paras 1!1 ,+++
prince 1=E =>+++
rashmi 1=A =++++
ra2i 1=, !++++
seema 1= !+++
31
1+ rows selected.
SQL> select ' from employee union all select ' from 6oogle"
1*/$ $1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
pankaj 1=! !++++
seema 1= !+++
ra2i 1=, !++++
nitesh 1=L =,+++
rashmi 1=A =++++
babul 1=> !,+++
prince 1=E =>+++
pankaj 1=! !++++
seema 1= !+++
babul 1=> !,+++
4ou2ika 111 ++++
paras 1!1 ,+++
nipun 1A= !++++
1! rows selected.
INTERSECTION
SQL> select ' from employee intersect select ' from 6oogle"
1*/$ $1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
babul 1=> !,+++
pankaj 1=! !++++
seema 1= !+++
MINUS:
SQL> select ' from employee minus select ' from 6oogle"
1*/$ $1) S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
nitesh 1=L =,+++
prince 1=E =>+++
rashmi 1=A =++++
ra2i 1=, !++++
VIEWS:
SQL> create 2iew friends as select name5code5salary from T.S where salary>Q!++++"
Hiew created.
SQL> select ' from friends"
1*/$ .)9$ S*L*(4
%%%%%%%%%%%%%% %%%%%%%%%%%%% %%%%%%%%%%
seema b++1 ++++
32
anurag e++1 !++++
amit f++1 !++++
you2ika g++1 !++++
pankaj a++1 !,+++
SQL> insert into T.S 2alues 7?i++1?5?babul?5?analyst?5,+++8"
1 row created.
SQL> select ' from friends"
1*/$ .)9$ S*L*(4
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
seema b++1 ++++
anurag e++1 !++++
amit f++1 !++++
you2ika g++1 !++++
pankaj a++1 !,+++
babul i++1 ,+++
L rows selected.
SQL> delete from T.S where code in 7We++1?5Pfoo1P8"
= rows deleted
SQL> select ' from friends"
1*/$ .)9$ S*L*(4
%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
seema b++1 ++++
you2ika g++1 !++++
pankaj a++1 !,+++
babul i++1 ,+++
SQL> delete from friends where codeQ?g++1?"
1 row deleted.
SQL> select ' from T.S"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%% %%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
SQL> delete from friends where codeQ?g++1?"
1 row deleted.
SQL> insert into friends 2alues 7?megha?5?j++1?5=++++8"
1 row created.
33
SQL> select ' from T.S"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
j++1 megha =++++
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
L rows selected.
CREATING READ-ONLY VIEW:
SQL> create table Jooks 7title 2archar=7=,8 not null5author 2archar=7!+8 not null5
= price number5pub 2archar=7=+88"
SQL> select ' from books"
T0TL$ *#TG)( ;(0.$ ;#J
%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%
3indows ;rogramming Iim .onger =,+ wrox
.omputer *rchitechure Sima 9e !,+ pearson 2ue
Let #s . 4ash2ant Danitkar 1>+ J;J
;ointers in . 4ash2ant Danitkar =++ J;J
Thinking in Ia2a Jruce $ckle !++ /ind Hiew
0nside Ser2lets 9ustin .allaway !,+ pearson 2ue
L rows selected.
SQL> create 2iew myJooks as select title5author5pub from Jooks with read only"
Hiew created.
SQL> select ' from myJooks"
T0TL$ *#TG)( ;#J
%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%
3indows ;rogramming Iim .onger wrox
.omputer *rchitechure Sima 9e pearson 2ue
Let #s . 4ash2ant Danitkar J;J
;ointers in . 4ash2ant Danitkar J;J
Thinking in Ia2a Jruce $ckle /ind Hiew
0nside Ser2lets 9ustin .allaway pearson 2ue
L rows selected.
SQL> insert into mybooks 2alues 7?)perating System?5?6al2in?5?J;J?8"
insert into mybooks 2alues 7?)perating System?5?6al2in?5?J;J?8
'
$(()( at line 1&
)(*%+1A!!& 2irtual column not allowed here
SQL> delete from mybooks where pubQ?J;J?"
34
delete from mybooks where pubQ?J;J?
'
$(()( at line 1&
)(*%+1A,=& cannot delete from 2iew without exactly one key%preser2ed table
NOTE:
There are two kinds of Hiews& Simple and .omplex.
.omplex 2iew is created on table obtained from I)01 operation.
9/L commands can not be applied to .omplex 2iews.
Hiews can be created on multiple tables.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select ' from T.S"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
SQL> select ' from salinfo"
.)9$ J*S0. 9*
%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
a++1 1++++ !+++
c++1 A+++ 1,++
e++1 1++++ !+++
k++1 1=+++ ,++
d++1 ,+++ !+++
SQL> create 2iew multHiew as select T.S.code5name5basic5da
= from T.S5salinfo where T.S.codeQsalinfo.code"
Hiew created.
SQL> select ' from multHiew"
.)9$ 1*/$ J*S0. 9*
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
a++1 pankaj 1++++ !+++
c++1 ra2i A+++ 1,++
d++1 nitesh ,+++ !+++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SEQUENCES:
35
SQL> create table customer 7code number5name 2archar=7=+85expenses number71+5=88"
Table created.
SQL> create seNuence 09 start with 1 max2alue ,"
SeNuence created.
SQL> select ' from customer"
.)9$ 1*/$ $-;$1S$S
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
1 pankaj =,+++
= seema ,++
! rashmi =++++
you2ika !++++
, sonika ++++
SQL> insert into customer 2alues 709.next2al5?@name?5@expenses8"
$nter 2alue for name& 0sha
$nter 2alue for expenses& 1>+++
old 1& insert into customer 2alues 709.next2al5?@name?5@expenses8
new 1& insert into customer 2alues 709.next2al5?0sha?51>+++8
insert into customer 2alues 709.next2al5?0sha?51>+++8
'
$(()( at line 1&
)(*%+>++& seNuence 09.1$-TH*L exceeds /*-H*L#$ and cannot be instantiated
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select seNuence:name5cache:siKe from all:seNuences where seNuence:nameQ?.)#1T$(?"
S$Q#$1.$:1*/$ .*.G$:S0X$
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
.)#1T$( =+
SQL> create seNuence counter min2alue 1+++ max2alue 1++++ increment by 1+++ cycle
= cache ,"
SQL> select seNuence:name5cache:siKe from all:seNuences where
= seNuence:nameQ?.)#1T$(?"

S$Q#$1.$:1*/$ .*.G$:S0X$
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
.)#1T$( ,
SQL> create table 2ehicle 72no number5 model 2archar=7=+85price number8"
Table created.
SQL> create seNuence 2id start with 1 max2alue L increment by ="
SQL> select ' from 2ehicle"
36
H1) /)9$L ;(0.$
%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
1 maruti>++ =,++++
! toyota !=,+++
, skoda octa2ia 1,+++++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table Jikes 7bno number5model 2archar=7=+85rating number8"
Table created.
SQL> create seNuence bid start with 11++ max2alue 11,+ increment by 1+ cycle"
SeNuence created.
SQL> select ' from Jikes"
J1) /)9$L (*T016
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%
11++ splendor nxg !
111+ t2s apache >
11=+ pulsar dtsi >
11!+ disco2er dtsi L
11+ glamour L
11,+ t2s 2ictor A
1 honda shine >
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create seNuence id start with 1 max2alue =+ increment by cycle cache !
SQL> create table 0J/ 7eno number5name 2archar=7=+85location 2archar=7=+88"
SQL> select ' from 0J/"

$1) 1*/$ L).*T0)1
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%
1 pankaj london
, seema paris
E babul newyork
1! prince bangalore
1A nipun singapore
1 ra2i germany
, karunesh Gyderabad
A rows selected.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create seNuence count min2alue 1++ max2alue ++ increment by 1++
37
= cycle cache !"
SQL> select ' from 0J/"
$1) 1*/$ L).*T0)1
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%
1++ you2ika Jangalore
=++ seema paris
!++ rashmi mumbai
++ sonika delhi
1++ shweta Gongkong
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SYNONYM:
There are two types of Synonym& pri2ate5public.
Synonym can be applied to only one table.
SQL> select ' from T.S"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
SQL> create synonym abc for T.S"
Synonym created.
SQL> grant select5 insert on abc to pankaj"
6rant succeeded.
SQL> connect
$nter user%name& pankaj
$nter password& '''''''''
.onnected.
SQL> select ' from scott.abc"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
SQL> insert into scott.abc 2alues 7?e++1?5?nipun?5?manager?5>++++8"
SQL> select ' from scott.abc"

.)9$ 1*/$ 9$S0 S*L*(4
38
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
SQL> connect
$nter user%name& scott
$nter password& '''''
.onnected.
SQL> select ' from T.S"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
L rows selected.
NOTE:
Gere user pankaj has the rights of synonym abc created by scott. Jut pankaj canPnt
further assign rights of abc to other user. 0f we want that pankaj can gi2e rights on
synonym abc to other user then it can be done as&
SQL> gra! "#$#%!&'"#r! ( )*+ !( ,a-a. /'!0 gra! (,!'(1
6rant succeeded.
SQL> connect
$nter user%name& pankaj
$nter password& '''''''''
.onnected.
SQL> grant select5insert on scott.xyK to seema"
6rant succeeded.
SQL> connect
$nter user%name& seema
$nter password& '''''''''
.onnected.
SQL> select ' from scott.xyK"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
39
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
L rows selected.
NOTE:
0f we want that all user7whether existed or created after synonym is created8 should ha2e
rights of synonym then the command will be gi2en as&
SQL> create synonym rst for salinfo"
Synonym created.
SQL> grant select5insert on rst to public"
6rant succeeded.
SQL> connect
$nter user%name& pankaj
$nter password& '''''''''
.onnected.
SQL> select ' from scott.rst"
.)9$ J*S0. 9*
%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
a++1 1++++ !+++
c++1 A+++ 1,++
e++1 1++++ !+++
k++1 1=+++ ,++
d++1 ,+++ !+++
SQL> connect
$nter user%name& seema
$nter password& '''''''''
.onnected.
SQL> select ' from scott.rst"
.)9$ J*S0. 9*
%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
a++1 1++++ !+++
c++1 A+++ 1,++
e++1 1++++ !+++
k++1 1=+++ ,++
d++1 ,+++ !+++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
PUBLIC SYNONYM:
SQL> create table 30;() 7ename 2archar=7=+85eno number5desi 2archar=7=+85salary
= number71+5=85 primary key 7eno5desi88"
40
SQL> select ' from wipro"

$1*/$ $1) 9$S0 S*L*(4
%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
;ankaj 1=! programmer =,+++
seema 1= debugger !++++
babul 1=, analyst ++++
prince 1=L designer =,+++
SQL> create public synonym xyK for 30;()"
Synonym created.
SQL> grant select5insert on xyK to pankaj"
6rant succeeded.
SQL> connect
$nter user%name& pankaj
$nter password& '''''''''
.onnected.
SQL> select ' from xyK"
$1*/$ $1) 9$S0 S*L*(4
%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
;ankaj 1=! programmer =,+++
seema 1= debugger !++++
babul 1=, analyst ++++
prince 1=L designer =,+++
FUNCTIONS:
T):9*T$7string,format8&
.on2erts a string in a gi2en format into an )racle date.
0t will also accept a number instead of a string5with certain limits. format is
restricted.
T):.G*(7date,format): (eformats date according to format.
SQL> create table G)L09*4 7Goliday H*(.G*(=7=,85*ctual9ate 9*T$5
= .elebrated9ate 9*T$8"
Table created.
SQL> insert into G)L09*4 2alues 7?1$3 4$*( 9*4?5 T):9*T$7?+1%I*1%=++=?5
= ?99%/)1%4444?85T):9*T$7?+1%I*1%=++=?5?99%/)1%4444?88"
SQL> select ' from G)L09*4"
SQL> S$L$.T ' <()/ G)L09*4"
G)L09*4 *.T#*L9*T .$L$J(*T$
%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%
L)G(0 1!%I*1%+A 1%I*1%+A
41
G)L0 1E%/*(%+A 1E%/*(%+A
(*DG0 +E%*#6%+A 1+%*#6%+A
9$$;*3*L0 1=%1)H%+A 1,%1)H%+A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
1$-T:9*47date,day):
0t computes the date of the next named day of the week7i.e
Sunday5 /onday8 after the gi2en date.
SQL> select 1$-T:9*47*.T#*L9*T$5?S#19*4?8 *S .G#TT0 <()/ G)L09*4"
.G#TT0
%%%%%%%%%%%%%
1%I*1%+A
=,%/*(%+A
1=%*#6%+A
1>%1)H%+A
SQL> select sysdate from dual"
S4S9*T$
%%%%%%%%%%%%
=E%I#L%+A
SQL> create table Jirthday 7name H*(.G*(=7=,85location 2archar=7=+85 bday 9*T$8"
Table .reated
SQL> select ' from Jirthday"
1*/$ L).*T0)1 J9*4
%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%% %%%%%%%%%%%%%
seema ;unjab =E%9$.%>=
pankaj Garyana !1%9$.%>1
sonika ;unjab =!%I#1%>1
SQL> select name5bday5 /)1TGS:J$T3$$17sysdate5bday8B1= as age from Jirthday"
1*/$ J9*4 *6$
%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%
seema =E%9$.%>= =.,>!!!!!
pankaj !1%9$.%>1 =,.,AE+>>L
sonika =!%I#1%>1 =L.1++,E!E
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select sysdate as Today5L*ST:9*47*99:/)1TGS7sysdate5L88V1 (e2iew from 9ual"
T)9*4 ($H0$3
%%%%%%%%% %%%%%%%%%%%%
=E%I#L%+A +1%<$J%+>
42
Gere *99:/)1TGS takes the sysdate and adds six months to it. L*ST:9*4 takes this result
and figures the last day of that month. Then 1 is added to get the first day of the next
month.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%
SQL> select months:between7?!+%I#L%=++A?5?=%9$.%=++,?8 from dual"
/)1TGS:J$T3$$17?!+%I#L%=++A?5?=%9$.%=++,?8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1E.E+!==,>
SQL> select trunc7months:between7?!+%I#L%=++A?5?=%9$.%=++,?85+8 /)1TGS from dual"
/)1TGS
%%%%%%%%%%
1E
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select ascii7?a?8 from dual"
*S.007?*?8
%%%%%%%%%%
EA
SQL> select ascii7?pankaj?8 from dual"
*S.007?;*1D*I?8
%%%%%%%%%%%%%%%%%%%
11=
SQL> select ascii7?p?8 from dual"
*S.007?;?8
%%%%%%%%%%
11=
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
T(*1SL*T$7string,if,then8&
This function looks at each character in
string then checks if to see whether that character is there. 0f it is 5it notes the
position in if where it found the character5and then looks at the same position in then.
T(*1SL*T$ substitute whiche2er character it finds there for character in string
SQL> select T(*1SL*T$ 7LE>,A1!5A+>==L,E5?*;$Q(S/$?8 <()/ 9#*L"
T(*1SL*T
%%%%%%%%%%%%
S$$/*1!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select 9$.)9$ 7desi5?programmer?5?prog?5?manager?5?mgr?5?supr?8 job5name5desi
= from T.S"
43
I)J 1*/$ 9$S0
%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%
supr seema designer
supr ra2i super2isor
supr nitesh debugger
mgr nipun manager
prog pankaj programmer
supr babul analyst
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table Jirthday 7name 2archar=7=+85actualdate date5celebrated date8"
SQL> insert into Jirthday 2alues 7?Seema?5to:date7?=E%9$.%1E>! +&=!&1+ ;/?5
?dd%/)1%yyyy GG&/0&SS ;/P85=E%9$.%1E>!8"
SQL> select name5to:char7actualdate5?dd%/)1%yyyy GG&/0&SS?8 actualdate from
= Jirthday"
1*/$ *.T#*L9*T$
%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Seema =E%9$.%1E>! +&=!&1+
pankaj !1%9$.%1E>1 +1&!+&!+
Sonika =!%I#L%1E>1 11&,&!+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create table 3ords 7word 2archar=7=+88"
Table created.
SQL> select ' from 3ords"
3)(9
%%%%%%%%%%%%%%%%%%%%
Too
To
Tom
Tomy
*s
*SS
Je
Jee
> rows selected.
SQL> select ' from 3ords where S)#19$-73)(98 Q S)#19$-7?T)?8"
3)(9
%%%%%%%%%%%%%%%%%%%%
Too
To
44
SQL> select ' from words where S)#19$-73)(98 QS)#19$-7?*S?8"
3)(9
%%%%%%%%%%%%%%%%%%%%
*s
*SS
/*TG$/*T0.*L <#1.T0)1S&
SQL> select .$0L7=.18 from dual
.$0L7=.18
%%%%%%%%%
!
SQL> select <L))(7=.E8 from dual"
<L))(7=.E8
%%%%%%%%%%
=
SQL> select /)971++5>8 from dual"
/)971++5>8
%%%%%%%%%%

SQL> select /)97=,.,5=.=8 from dual"
/)97=,.,5=.=8
%%%%%%%%%%%%%
1.!
SQL> select ;)3$(7>5!8 from dual"
;)3$(7>5!8
%%%%%%%%%%
,1=
SQL> select SQ(T7,8 from dual"
SQ(T7,8
%%%%%%%%%%
=.=!L+LAE>
SQL> select .)#1T7ename8 from emp"
.)#1T7$1*/$8
%%%%%%%%%%%%
1
SQL> select count7sal8 from emp where sal>1+++"
.)#1T7S*L8
%%%%%%%%%%
1=
45
SQL> select .)#1T790ST01.T 9$;T1)85.)#1T79$;T1)8 <()/ $/;"
.)#1T790ST01.T9$;T1)8 .)#1T79$;T1)8
%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%
! 1
SQL> S$L$.T =B=B <()/ 9#*L"
=B=B
%%%%%%%%%%
.=,
SQL> S$L$.T =B7=B8 <()/ 9#*L"
=B7=B8
%%%%%%%%

SQL> select L;*97?pankaj?51+5?O?8 as name from dual"
1*/$
%%%%%%%%%%
OOOOpankaj
GROUP BY %$a2"#:
SQL> create table .*T$6)(4 7.ategory1ame H*(.G*(=71=8 primary key5
= ;arent.ategory H*(.G*(=7>85 Sub.ategory H*(.G*(=7=+88"
SQL> select ' from category"
.*T$6)(41*/$ ;*($1T.* S#J.*T$6)(4
%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%
*9#LT($< *9#LT ($<$($1.$
*9#LT<0. *9#LT <0.T0)1
*9#LT1< *9#LT 1)1<0.T0)1
.G0L9($1;0. .G0L9($1 ;0.T#($ J))D
.G0L9($1<0. .G0L9($1 <0.T0)1
.G0L9($11< .G0L9($1 1)1<0.T0)1
L rows selected.
SQL> create table J))DSG$L< 7Title H*(.G*(=71++8 primary key5 ;ublisher
= H*(.G*(=7=+85 .ategory1ame H*(.G*(=7=+85(ating H*(.G*(=7=85
! constraint .*T<D foreign key 7.ategory1ame8 references
.*T$6)(47.ategory1ame88"
46
SQL> select ' from bookshelf"
T0TL$ ;#JL0SG$( .*T$6)(41*/$ (*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%
T) D0LL * /).D016J0(9 G*(;$(.)LL01S *9#LT<0. ,
3)19$(<#L L0<$ 3.3.1)(T)1 *9#LT1< ,
011#/$(*.4 H01T*6$ J))DS *9#LT1<
D0$(D$6*(9 *1TG)L)64 ;(01.$T)1 #10H ;( *9#LT($< !
*11$ )< 6($$1 6*JL$S 6(*//$(.4 .G0L9($1<0. !
6))9 9)65 .*(L L0TTL$ S0/)1 .G0L9($1;0. 1
L$TT$(S 1 ;*;$(S <()/ ;(0S)1 S.(0J1$( *9#LT1<
TG$ 90S.)H$($(S (*19)/ G)#S$ *9#LT1<
TG$ /0S/$*S#($ )< /*1 3.3. 1)(T)1 *9#LT1< ,
$0TG$(B)( ;$16#01 *9#LT($< !
;)L*( $-;($SS G)#6GT)1 /0<<L01 .G0L9($1;0. 1
(#1*3*4 J#114 G*(;$(<$ST0H*L .G0L9($1;0. 1
/4 L$96$( D).G ;($SS *9#LT1< ,
.)/;L$T$ ;)$/S )< I)G1 D$*TS H0D016 *9#LT($< =
#19$( TG$ $4$ )< TG$ .L).D *(.*9$ ;#J .G0L9($11< !
I)G1 *9*/S S0/)1 S.G#ST$( *9#LT1<
T(#/*1 S0/)1 S.G#ST$( *9#LT1<
I)#(1*LS )< L$30S *19 .L*(D /*(01$( *9#LT1<
6)S;$L ;0.*9)( *9#LT<0.
$//* 3G) S*H$9 /4 L0<$ ST /*(T01?S ;($SS *9#LT<0. !
/09106GT /*60. S.G)L*ST0. .G0L9($1<0. 1
G*((4 ;)TT$( 1 6)JL$T )< <0($ S.G)L*ST0. .G0L9($1<0.
SG)$L$SS I)$ /*(01$( *9#LT<0. !
J)- S).0*LS J*LL*1T01$ *9#LT<0. !
T(#/;$T )< TG$ S3*1 G*(;$(.)LL01S .G0L9($1<0. !
.G*(L)TT$?S 3$J G*(;$(T();G4 .G0L9($1<0. !
3$ST 30TG TG$ 106GT 1)(TG ;)01T ;($SS *9#LT1< !
TG$ 6))9 J))D J*(9 *9#LT($<
;($*.G016 T) G$*9 1 G$*(T *J0169)1 ;($SS *9#LT($<
TG$ .)ST )< 90S.0;L$SG0; T)#.GST)1$ *9#LT($< !
TG$ SG0;;016 1$3S S0/)1 S.G#ST$( *9#LT<0.
!1 rows selected.
SQL> select .ategory1ame5.)#1T7'8 from J))DSG$L gr(2, 3* .ategory1ame"
.*T$6)(41*/$ .)#1T7'8
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
*9#LT<0. L
*9#LT1< 1+
*9#LT($< L
.G0L9($1<0. ,
.G0L9($11< 1
.G0L9($1;0. !
L rows selected.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select .ategory1ame5.)#1T7'8 from J))DSG$L<
= gr(2, 3* .ategory1ame ha2ing .)#1T7'8 > ,"
.*T$6)(41*/$ .)#1T7'8
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
*9#LT<0. L
*9#LT1< 1+
*9#LT($< L
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
47
SQL> select .ategory1ame5.)#1T7'8 5*H67(ating8 from J))DSG$L<
= group by .ategory1ame"
.*T$6)(41*/$ .)#1T7'8 *H67(*T0168
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%
*9#LT<0. L !.LLLLLLLA
*9#LT1< 1+ .=
*9#LT($< L !.1LLLLLLA
.G0L9($1<0. , =.>
.G0L9($11< 1 !
.G0L9($1;0. ! 1
L rows selected.
SQL> select .ategory1ame5.)#1T7'8 5*H67(ating8 from J))DSG$L<
group by .ategory1ame
ha2ing *H67(ating8 > 7select *H67(ating8 from J))DSG$L<8"
.*T$6)(41*/$ .)#1T7'8 *H67(*T0168
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%
*9#LT<0. L !.LLLLLLLA
*9#LT1< 1+ .=
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select .ategory1ame5.)#1T7'8 5*H67(ating8 from J))DSG$L<
= group by .ategory1ame ha2ing .ategory1ame like ?*R?
! order by .)#1T7'8 desc"
.*T$6)(41*/$ .)#1T7'8 *H67(*T0168
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%%
*9#LT1< 1+ .=
*9#LT<0. L !.LLLLLLLA
*9#LT($< L !.1LLLLLLA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select .ategory1ame5.)#1T7'8 5*H67(ating8 from J))DSG$L<
= where (ating > 1 and .ategory1ame like ?*R?
! group by .ategory1ame order by .)#1T7'8 desc"
.*T$6)(41*/$ .)#1T7'8 *H67(*T0168
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%%%%%
*9#LT1< 1+ .=
*9#LT<0. L !.LLLLLLLA
*9#LT($< L !.1LLLLLLA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create 2iew .*T$6)(4:.)#1T as
= select .ategory1ame5.)#1T7'8 *s .)#1T$( from Jookshelf
! group by .ategory1ame"
48
Hiew created.
SQL> select ' from .*T$6)(4:.)#1T"
.*T$6)(41*/$ .)#1T$(
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
*9#LT<0. L
*9#LT1< 1+
*9#LT($< L
.G0L9($1<0. ,
.G0L9($11< 1
.G0L9($1;0. !
L rows selected.
1)T$&
Since the .)#1T7'8 column is a function 5 you ha2e to gi2e it a column alias7in
this case5.ounter8 when using the Query as the base for a 2iew.
VIEWS OF GROUPS:
SQL> select .ategory1ame5.)#1T$(57.)#1T$(BJ))D.)#1T8'1++ as ;ercent
= from .*T$6)(4:.)#1T5J))D.)#1T order by .ategory1ame"
.*T$6)(41*/$ .)#1T$( ;$(.$1T
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%%%
*9#LT<0. L 1E.!,>!>A
*9#LT1< 1+ !=.=,>+L,
*9#LT($< L 1E.!,>!>A
.G0L9($1<0. , 1L.1=E+!=!
.G0L9($11< 1 !.==,>+L,
.G0L9($1;0. ! E.LAA1E!,
L rows selected.
SQL> select .ategory1ame5.)#1T7'857.)#1T7'8B/*-7Jook.ount88'1++ as ;ercent
= from J))DSG$L<5J))D.)#1T group by .ategory1ame ha2ing *H67(ating8 >
! order by .ategory1ame
.*T$6)(41*/$ .)#1T7'8 ;$(.$1T
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%
*9#LT1< 1+ !=.=,>+L,
SQL4PLUS REPORT:
SQL> S$L$.T ' <()/ T.S"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
49
i++1 babul analyst ,+++
L rows selected.
SQL> set underline M'M"
SQL> select ' from T.S"
.)9$ 1*/$ 9$S0 S*L*(4
'''''''''' '''''''''''''''''''' '''''''''''''''''''' ''''''''''
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
L rows selected.
SQL> column salary heading Mpresent salaryM format EE5EEE"
SQL> select ' from T.S"
.)9$ 1*/$ 9$S0 present salary
'''''''''' '''''''''''''''''''' '''''''''''''''''''' ''''''''''''''
b++1 seema designer +5+++
c++1 ra2i super2isor =+5+++
d++1 nitesh debugger =,5+++
e++1 nipun manager >+5+++
a++1 pankaj programmer !,5+++
i++1 babul analyst ,5+++
L rows selected.
SQL> column desi heading MI)JT9$S061*T0)1M format a=+
SQL> select ' from T.S"
I)J
.)9$ 1*/$ 9$S061*T0)1 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
L rows selected.
SQL> Ttitle MT.S $/;L)4$$SM
SQL> S$L$.T ' <()/ T.S"
3ed *ug +> page 1
T.S $/;L)4$$S
I)J
.)9$ 1*/$ 9$S061*T0)1 S*L*(4
50
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
L rows selected.
SQL> Ttitle center MT.S $/;L)4$$SM skip 1 center MOJ1= L)DG*193*L*M %
> skip 1 center M/#/J*0%++++1M skip = left M+>%*#6%=++AM right M1M
SQL> select ' from T.S"
SQL> S$L$.T ' <()/ T.S"
T.S $/;L)4$$S
OJ1= L)DG*193*L*
mumbai%++++1
+>%*#6%=++A 1
I)J
.)9$ 1*/$ $S061*T0)1 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
L rows selected.
SQL> S$L$.T ' <()/ $/; )(9$( J4 9$;T1)"
T.S $/;L)4$$S
OJ1= L)DG*193*L*
mumbai%++++1
+>%*#6%=++A
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)//
9$;T1)
%%%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%% %%%%%%%%
%%%%%%%%% %%%%%%%%%
AA>= .L*(D /*1*6$( A>!E +E%I#1%>1 =,+
1+
A>!E D016 ;($S09$1T 1A%1)H%>1 ,+++
AE! /0LL$( .L$(D AA>= =!%I*1%>= 1!++
51
A!LE S/0TG .L$(D AE+= 1A%9$.%>+ >++
=+
A>AL *9*/S .L$(D AA>> 1=%I*1%>! 11++
AE+= <)(9 *1*L4ST A,LL +!%9$.%>1 !+++
AA>> S.)TT *1*L4ST A,LL +E%9$.%>= !+++
A,LL I)1$S /*1*6$( A>!E +=%*;(%>1 =EA,
AEE *LL$1 S*L$S/*1 ALE> =+%<$J%>1 1L++ !++
!+
ALE> JL*D$ /*1*6$( A>!E +1%/*4%>1 =>,+
AL, /*(T01 S*L$S/*1 ALE> =>%S$;%>1 1=,+ 1++
AE++ I*/$S .L$(D ALE> +!%9$.%>1 E,+
A> T#(1$( S*L$S/*1 ALE> +>%S$;%>1 1,++ +
A,=1 3*(9 S*L$S/*1 ALE> ==%<$J%>1 1=,+ ,++
PL5SQL:
6 Pr(gra7 !( 8'9 (2! !0# ar#a (8 a :-D 8'g2r#
declare
dim1 number71+8"
dim= number71+8"
area number7=+8"
begin
dim1 &Q @dim1"
dim= &Q @dim="
if7dim1Qdim=8 then
area &Q dim1'dim="
dbms:output.put:line7?The area of sNuare is &? TTarea8"

else
area&Qdim1'dim="
dbms:output.put:line7?The area of rectangle is &?TTarea8"
end if"
end"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
6 Pr(gra7 !( 8'9 (2! !0# 7'99$# (8 !0r## 273#r"
52
declare
a number71+8"
b number71+8"
c number71+8"
lar number71+8"
small number71+8"
begin
a&Q@a"
b&Q@b"
c&Q@c"
if a>b and a>c then
lar&Qa"
elsif aSb and aSc then
small&Qa"
end if"
if b>a and b>c then
lar &Qb"
elsif bSa and bSc then
small &Qb"
end if"
if c>a and c>b then
lar&Qc"
elsif cSb and cSa then
small&Qc"
end if"
if a UQlar and aUQsmall then
dbms:output.put:line7?/iddle number is& ? TTa8"
elsif b UQlar and bUQsmall then
dbms:output.put:line7?/iddle number is& ? TTb8"
else
dbms:output.put:line7?/iddle number is& ? TTc8"
end if"
end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
6 D#7( (8 /0'$# $((,
declare
a number71+8&Q1"
b number71+8"
begin
while aSQ1+ loop
dbms:output.put:line7a8"
a&QaV1"
end loop"
end"
B
Y 9emo of 9o Zwhile loop
53
declare
a number71+8&Q1"
begin
loop
dbms:output.put:line7a8"
exit when a>Q1+"
a&QaV1"
end loop"
end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
6 D#7( (8 8(r $((,
begin
for i in 1..1+ loop
dbms:output.put:line7i8"
end loop"
end"
6 G##ra!# !0# 8($$(/'g "#r'#"
1
= =
! ! !

, , , , ,
begin
for i in 1.., loop
for j in 1..i loop
dbms:output.put7i8"
end loop"
dbms:output.put:line7??8"
end loop"
end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
1
1 =
1 = !
1 = !
1 = ! ,
begin
for i in 1.., loop
for j in 1..i loop
dbms:output.put7i8"
end loop"
dbms:output.put:line7??8"
end loop"
end"
B
54
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
1
= !
, L
A > E 1+
11 1= 1! 1 1,
declare
k number71+8&Q1"
begin
for i in +.., loop
for j in 1..i loop
dbms:output.put7k8"
k&QkV1"
end loop"
dbms:output.put:line7??8"
end loop"
end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y 3rite a program to insert record in S*L$ table based on user input. 0f the input code
exist in 0T$/S table and itPs Nuantity is >+ then message W0tem SoldP is inserted in Sale
table otherwise message W0tem not SoldP is insertedP.
SQL> desc items"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%
.)9$ .G*(78
01*/$ H*(.G*(=7=+8
0QT4 1#/J$(
0;(0.$ 1#/J$(71+5=8
SQL> select ' from items"
.)9$ 01*/$ 0QT4 0;(0.$
%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
a++1 computer =+ !,+++
b++1 mobile 1, =++++
c++1 i;od , !++++
d++1 laptop > L,+++
e++1 watch + =,+++
SQL> desc sale"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%
.)9$ .G*(78
55
9*T$:)<:S*L$ 9*T$
ST*T#S H*(.G*(=7!+8
declare
item.ode char78"
itemQty number"
begin
item.ode&Q?@item.ode?"
select iNty into itemQty from items where codeQitem.ode"
if itemQty> + then
insert into sale 2alues 7item.ode5sysdate5?0tem Sold?8"
else
insert into sale 2alues 7item.ode5sysdate5?0tem not Sold?8"
end if"
end"
B
SQL> Y proA
$nter 2alue for itemcode& a++1
old ,& item.ode&Q?@item.ode?"
new ,& item.ode&Q?a++1?"
;LBSQL procedure successfully completed.
SQL> Y proA
$nter 2alue for itemcode& e++1
old ,& item.ode&Q?@item.ode?"
new ,& item.ode&Q?e++1?"
;LBSQL procedure successfully completed.
SQL> select ' from sale"
.)9$ 9*T$:)<:S ST*T#S
%%%% %%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a++1 1!%*#6%+A 0tem Sold
e++1 1!%*#6%+A 0tem not Sold
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
O 3rite a program to do the following task
*ccept itemQty and code from user.
0f code is present in 0T$/ table and itemQtySQiNty present in the 0T$/ table then
0tems a2ailale in 0T$/S table QiNty%itemQty and
0nsert record in the S*L$ table.
Y
declare
item.ode char78"
itemQty number"
Qty;resent number"
Qty(emaining number"
begin
item.ode&Q?@item.ode?"
itemQty&Q@itemQty"
select iNty into Qty;resent from items where codeQitem.ode"
Qty(emaining&QQty;resent%itemQty"
56
if Qty(emaining >Q+ then
update items set iNtyQQty(emaining where codeQitem.ode"
insert into sale 2alues 7item.ode5sysdate5?0tem Sold?5itemQty8"
else
dbms:output.put:line7?Sorry 0tems present are less than the demand?8"
end if"
end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y *ccept item code from user and if a record exist related to that code in 0T$/S table
then insert the same record in $lec0tem table.
SQL> desc $lec0tems"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
.)9$ .G*(78
01*/$ H*(.G*(=7=+8
0QT4 1#/J$(
0;(0.$ 1#/J$(71+5=8
declare
item.ode items.codeRtype"
begin
item.ode&Q?@item.ode?"
insert into $lec0tems 7code5iname5iNty5iprice8
select code5iname5iNty5iprice from items
where codeQitem.ode"
end"
B
SQL> Y proE
$nter 2alue for itemcode& b++1
old & item.ode&Q?@item.ode?"
new & item.ode&Q?b++1?"
;LBSQL procedure successfully completed.
SQL> select ' from $lec0tems"
.)9$ 01*/$ 0QT4 0;(0.$
%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%
b++1 mobile 1, =++++
CURSOR:
Y 3rite a program which inserts records from employee1 table based on designation but
changes designations to short forms as 9r for doctor etc.
SQL> select ' from employee1"
57
.)9$ 1*/$ 9$S0 S*L*(4
%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
a++1 pankaj engineer !++++
b++1 seema engineer !,+++
c++1 ra2i super2isor =++++
d++1 nitesh designer =,+++
e++1 babul doctor ,+++
f++1 nipun manager ,++++
g++1 amit programmer =,+++
h++1 paras manager ,,+++
i++1 tarun doctor =+++
j++1 anu super2isor =++++
k++1 umesh teacher 1,+++
11 rows selected.
Y
declare
edesi employee1.desiRtype"
erec employee1Rrowtype"
cursor e1 is select name5code5
9$.)9$7desi5?engineer?5?$ng?5?doctor?5?9r?5?manager?5?/gr?5?)thers?85salary
from employee1 where desiQedesi"
begin
edesi&Q?@edesi?"
open e1"
loop
fetch e1 into erec"
exit when e1R1)T<)#19"
insert into employee= 2alues7erec.code5erec.name5erec.desi5erec.salary8"
end loop"
close e1"
end"
B
SQL> Y pro11
$nter 2alue for edesi& doctor
old L& edesi&Q?@edesi?"
new L& edesi&Q?doctor?"
;LBSQL procedure successfully completed.
SQL> Y pro11
$nter 2alue for edesi& programmer
old L& edesi&Q?@edesi?"
new L& edesi&Q?programmer?"
;LBSQL procedure successfully completed.
SQL> select ' from employee="
.)9$ 1*/$ 9$S0 S*L*(4
%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
babul e++1 9r ,+++
58
tarun i++1 9r =+++
amit g++1 )thers =,+++

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y 3rite a program which search for a suitable match from a candidate table based on the
following conditions
O *ge difference between boy and girl should lie in 7=5,8 years
O Geight difference lie in7+.!51.=8 feet
<irst we create a table which has following fields
SQL> desc candidate
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
1*/$ H*(.G*(=7=+8
S$- .G*(718
*6$ 1#/J$(
*99($SS H*(.G*(=7!+8
G$06GT 1#/J$(7=518
Then we create another table named match which has the same structure as of candidate
table. This table contains the records of matches found from candidate table
when user enters its data.
Y
declare
cSex candidate.sexRtype"
cGeight candidate.heightRtype"
c*ge candidate.ageRtype"
Geight9iff candidate.heightRtype"
*ge9iff candidate.ageRtype"
counter number&Q+"
crec candidateRrowtype"
cursor c1 is select ' from candidate where sex 1)T 01 7#;;$(7csex88"
begin
cSex &Q?@cSex?"
cGeight &Q@cGeight"
c*ge &Q@c*ge"
delete from match"
open c1"
loop
fetch c1 into crec"
exit when c1R1)T<)#19"
if cSex in 7?/?5?m?8 then
Geight9iff &QcGeight%crec.height"
*ge9iff &Qc*ge%crec.age"
else
Geight9iff &Qcrec.height%cGeight"
*ge9iff &Qcrec.age%c*ge"
end if"
if Geight9iff J$T3$$1 +.! *19 1.= and *ge9iff J$T3$$1 = *19 , then
insert into match 2alues
7crec.name5crec.sex5crec.age5crec.height5crec.address8"
counter&QcounterV1"
end if"
59
end loop"
close c1"
if counterQ+ then
dbms:output.put:line7?Sorry 1o /atch <ound?8"
end if"
end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y 3rite a program to check Gow many doctors are a2ailable for a patient ha2ing a
particular disease for e.g heart5ent etc.
The table created for this application has the structure as
SQL> desc doctors
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1*/$ H*(.G*(=7=+8
S;$.0*L0ST H*(.G*(=7=+8
T0/$:01 1#/J$(7=8
T0/$:)#T 1#/J$(7=8
;*T0$1T:*TT$19$9 1#/J$(7=8
/*-:;*T0$1T 1#/J$(7=8
Y declare
disease doctors.specialistRtype"
patTime0n doctors.time:inRtype"
drec doctorsRrowtype"
counter number&Q+"
cursor d1 is select ' from doctors where specialistQdisease and patTime0n>Qtime:in
and patTime0nStime:out"
begin
disease&Q?@disease?"
patTime0n&Q@patTime0n"
open d1"
loop
fetch d1 into drec"
exit when d1R1)T<)#19"
if drec.patient:attended S drec.max:patient then
dbms:output.put:line7?9octor ?TTdrec.nameTT? is a2ailable?8"
counter&Q1"
end if"
end loop"
if counterQ+ then
dbms:output.put:line7?Sorry no 9octor is a2ailable?8"
end if"
end"
B
CURSOR WIT; FOR LOOP:
declare
cSex candidate.sexRtype"
cGeight candidate.heightRtype"
c*ge candidate.ageRtype"
60
Geight9iff candidate.heightRtype"
*ge9iff candidate.ageRtype"
counter number&Q+"
crec candidateRrowtype"
cursor c1 is select ' from candidate where sex 1)T 01
7#;;$(7csex85L)3$(7cSex88"
begin
cSex &Q?@cSex?"
cGeight &Q@cGeight"
c*ge &Q@c*ge"
delete from match"
for crec in c1 loop
if cSex in 7?/?5?m?8 then
Geight9iff &QcGeight%crec.height"
*ge9iff &Qc*ge%crec.age"
else
Geight9iff &Qcrec.height%cGeight"
*ge9iff &Qcrec.age%c*ge"
end if"
if Geight9iff J$T3$$1 +.! *19 1.= and *ge9iff J$T3$$1 = *19 , then
insert into match 2alues
7crec.name5crec.sex5crec.age5crec.height5crec.address8"
counter&QcounterV1"
end if"
end loop"
if counterQ+ then
dbms:output.put:line7?Sorry 1o /atch <ound?8"
end if"
end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y 3rite a program to depositBwithdraw amount from an account based on following
conditions&
O There should be at least 1+++ (s in account
O)nly two transactions should be possible within a week
SQL> desc ;1J
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
.1*/$ H*(.G*(=7!+8
*..)#1T1) 1#/J$(71+8
T(*1S*.T0)1 .G*(718
*/)#1T 1#/J$(
T(*1S9*T$ 9*T$
30TG9(*3L.)#1T 1#/J$(7=8
Y
declare
acc0d ;1J.accountnoRtype"
amt ;1J.amountRtype"
transType ;1J.transactionRtype"
date9iff number"
brec ;1JRrowtype"
cursor b1 is select ' from ;1J where accountnoQacc0d"
61
begin
acc0d&Q@acc0d"
amt&Q@amt"
transType&Q?@transType?"
open b1"
loop
fetch b1 into brec"
exit when b1R1)T<)#19"
date9iff&Qtrunc7to:char7sysdate%brec.transdate88"
if transType in 7?9?5?d?8 then
update ;1J set amountQamountVamt5trans9ateQsysdate5transactionQtransType
where
accountnoQacc0d"
elsif transType in7?3?5?w?8 then
if 7brec.withdrawlcount>Q= and datediffSQA8 )( 7brec.amountS1+++ or
amt>brec.amount 8 then
dbms:output.put:line7?Sorry 3ithdrawl is not possible?8"
else
brec.amount&Qbrec.amount%amt"
update ;1J set
transactionQtransType5amountQbrec.amount5transdateQsysdate5withdrawlcountQbrec.withdrawlco
untV1 where accountnoQacc0d"
end if"
end if"
end loop"
close b1"
end "
B

SQL> S$L$.T ' <()/ ;1J"
.1*/$ *..)#1T1) T */)#1T T(*1S9*T$ 30TG9(*3L.)#1T
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% % %%%%%%%%%% %%%%%%%%% %%%%%%%%%%%%%%
;ankaj EEEL1LA=!= 9 1!A++ 1E%*#6%+A =
Seema EEE1>A=>= 9 >+++ 1+%*#6%+A
ra2i E>EL1,1L+! 9 1=+++ 1+%*#6%+A
SQL> Y ;()!
$nter 2alue for accid& EEEL1LA=!=
old E& acc0d&Q@acc0d"
new E& acc0d&QEEEL1LA=!="
$nter 2alue for amt& 1,++
old 1+& amt&Q@amt"
new 1+& amt&Q1,++"
$nter 2alue for transtype& 3
old 11& transType&Q?@transType?"
new 11& transType&Q?3?"
Sorry 3ithdrawl is not possible
PROCEDURE:
O * procedure that updates a database.
SQL> select ' from tcs"
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
62
b++1 seema designer ++++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
Y
create or replace procedure incr7a char8 as
begin
update T.S set salaryQsalaryVsalary'+.1 where code in7a8"
end"
SQL> Y pr1
;rocedure created.
SQL> execute incr7?b++1?8"
;LBSQL procedure successfully completed.
.)9$ 1*/$ 9$S0 S*L*(4
%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
b++1 seema designer +++
c++1 ra2i super2isor =++++
d++1 nitesh debugger =,+++
e++1 nipun manager >++++
a++1 pankaj programmer !,+++
i++1 babul analyst ,+++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
O * procedure that returns more than one 2alue.
SQL> desc T.S
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%
%%%%%%%%%%%%%%%%%%
.)9$ 1)T 1#LL H*(.G*(=71+8
1*/$ H*(.G*(=7=+8
9$S0 H*(.G*(=7=+8
S*L*(4 1#/J$(71+5=8
Y
create or replace procedure displ7a char5b out 2archar=5c out number8 as
begin
select name5salary into b5c from T.S where codeQa"
end"
B
SQL> Y pr=
;rocedure created.
SQL> 2ariable sal number"
SQL> 2ariable naam 2archar=7=+8
SQL> execute displ7?b++1?5&naam5&sal8"
;LBSQL procedure successfully completed.
63
SQL> print naam
1**/
%%%%%%%%%%%%
seema
SQL> print sal
S*L
%%%%%%%%%%
+++
SQL> ed pr=
TRIGGERS:
SQL> 9$S. J*1D"
1ame 1ullF Type
%%%%%%%%%%%%%%%%% %%%%%%% %%%%%%%%%%
.#ST)/$( H*(.G*(=7=+8
*..)#1T:1) 1#/J$(
Y
create or replace trigger #pper1ame before insert or update on bank
for each row
begin
&new.customer&Q#;;$(7&new.customer8"
end"
B
SQL> Y tr1
Trigger created.
SQL> insert into bank 2alues7?pankaj?5EEE1>1E>8"
1 row created.
SQL> insert into bank 2alues 7?(a2i parkash?5ELLA+A,!!8"
1 row created.
SQL> select ' from bank"
.#ST)/$( *..)#1T:1)
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
;*1D*I EEE1>1E>
(*H0 ;*(D*SG ELLA+A,!!
64
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y
create or replace trigger trgr before insert or update or delete on T.S
for each row
begin
if &new.salaryS1++++ then
(aise:*pplication:error7%=+++=5?Salary is less than 1++++?8"
end if"
end"
B
SQL> insert into T.S 2alues 7?p++1?5?*man?5?designer?5>+++8"
insert into T.S 2alues 7?p++1?5?*man?5?designer?5>+++8
'
$(()( at line 1&
)(*%=+++=& Salary is less than 1++++
)(*%+L,1=& at MS.)TT.T(6(M5 line !
)(*%++>>& error during execution of trigger ?S.)TT.T(6(?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y
create or replace trigger trg11 before insert on employee! for each row
begin
update deptinfo set empcountQempcountV1 where didQ&new.deptid"
end"
B
SQL> select ' from employee!"
.)9$ 1*/$ 9$;T09
%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
a++1 pankaj prg+1
b++1 seema test+=
c++1 ra2i dbg+!
d++1 you2ika dbg+!
SQL> select ' from deptinfo"
909 $/;.)#1T
%%%%%%%%%% %%%%%%%%%%
prg+1 =+
test+= 1+
dbg+! =L
SQL> insert into employee! 2alues 7?e++1?5?babul?5?prg+1?8"
1 row created.
SQL> select ' from deptinfo"
909 $/;.)#1T
%%%%%%%%%% %%%%%%%%%%
65
prg+1 =1
test+= 1+
dbg+! =L
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> select trigger:name from all:triggers where table:nameQ?;1J?"
T(066$(:1*/$
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T(061
SQL> drop trigger trig1"
Trigger dropped.
SQL> select trigger:name from all:triggers where table:nameQ?;1J?"
no rows selected
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
E<CEPTION ;ANDLING:
Y
declare
a number"
b number"
c number"
begin
a&Q@a"
b&Q@b"
c&QaBb"
exception
when Kero:di2ide then
dbms:output.put:line7?9i2ide by Kero error?8"

end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y
declare
icode items.codeRtype"
iNty items.NtyRtype"
iname items.nameRtype"
begin
icode&Q?@icode?"
select Nty5name into iNty5iname from items where codeQicode"
66
if iNty > + then
insert into sale 2alues 7icode5iname8"
end if"
exception
when no:data:found then
dbms:output.put:line7?0tem is not a2ailable?8"
end"
B
SQL> Y pr=
$nter 2alue for icode& a++1
old L& icode&Q?@icode?"
new L& icode&Q?a++1?"
;LBSQL procedure successfully completed.
SQL> select ' from sale"
.)9$ 0T$/
%%%%% %%%%%%%%%%%%%%%%%%%%
a++1 computer
SQL> Y pr=
$nter 2alue for icode& k++1
old L& icode&Q?@icode?"
new L& icode&Q?k++1?"
0tem is not a2ailable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y To print default error messages a2ailable in )racle.
declare
a 2archar=71++8"
begin
for i in %1++++..+ loop
a&QsNlerrm7i8"
insert into errors 2alues7a8"
end loop"
end"
B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
Y To print a specific error message.
declare
a number"
b 2archar=71++8"
begin
a&Q%+1=="
b&QsNlerrm7a8"
dbms:output.put:line7b8"
end"
B
SQL> Y pr
)(*%+1==& exact fetch returns more than reNuested number of rows
67
;LBSQL procedure successfully completed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
O #ser 9efined $xception.
Y
declare
a number"
b number"
c number"
d exception"
begin
a&Q@a"
b&Q@b"
if a > =+ then
raise d"
else
c&QaBb"
end if"
exception
when d then
dbms:output.put:line7?Halue of a is greater than =+?8"
when others then
dbms:output.put:line7?9i2ide by Kero error?8"
end"
B
Ora%$# Pa%-ag#":
* package is an )racle object5 which holds other objects within it. )bjects commonly
held within a package are procedure5 functions5 2ariables5 constants5 cursor and
exceptions.
Y
create or replace package mypack as
function fun7a number8 return number"
procedure proc7b number8"
end"
O <irst method to run a function written in a package
SQL> 2ariable aa number"
SQL> begin
= &aa&Qmypack.fun7=+8"
! end"
B
;LBSQL procedure successfully completed.
SQL> print aa"
**
%%%%%%%%%%
++
68
O Second method to run a function written in a package.
SQL> edit pt,
declare
k number"
begin
k&Qmypack.fun7=!8"
dbms:output.put:line7?SNuare of number is ?TTk8"
end"
B
SQL> Y pt,
SNuare of number is ,=E
O Third method to run a function written in a package.
SQL> select mypack.fun7=E8 from dual"
/4;*.D.<#17=E8
%%%%%%%%%%%%%%%%%%%%
>1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
O /ethod to run a procedure written in a package.
SQL> execute mypack.proc7=8"
.ube of number is1!>=
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
ADVANCE SQL:
SQL> create or replace type myaddress as object 7hno char7>85street 2archar=7=+85
= city 2archar=7=+85state 2archar71,85pin number7L88"
! B
Type created.
SQL> create table customer7name 2archar=7=+85code char7,85address myaddress8"
Table created.
SQL> desc customer
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%
1*/$ H*(.G*(=7=+8
.)9$ .G*(7,8
*99($SS /4*99($SS
SQL> insert into customer 2alues 7?/r ;ankaj?5?p++1?5myaddress7?OJ=?5?(adaur
= (oad?5?4amuna 1agar?5 ?Garyana?51!,++188"
SQL> select ' from customer"
1*/$ .)9$ *99($SS7G1)5 ST($$T5 .0T45 ST*T$5 ;018
69
%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/r ;ankaj p++1 /4*99($SS7?OJ=?5 ?(adaur (oad?5 ?4amuna 1agar?5 ?Garyana?5
1!,++18
Var*'g Arra*":
SQL> create or replace type orderarr as 2array7,8 of 2archar=7!+8"
= B
Type created.
SQL> create table orders7ono number5odate date5product orderarr8"
Table created.
SQL> desc orders"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
)1) 1#/J$(
)9*T$ 9*T$
;()9#.T )(9$(*((
SQL> insert into orders 2alues71+15sysdate5orderarr7?computer?5?laptop?5?mobile?88"
1 row created.
SQL> insert into orders 2alues71+15sysdate5orderarr7?i;od?88"
1 row created.
SQL> select ' from orders"
)1) )9*T$ ;()9#.T
%%%%%%%%% %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1+1 =A%*#6%+A )(9$(*((7?computer?5 ?laptop?5 ?mobile?8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> create or replace type itemdet as object 7icode char7,85iname 2archar=7!+85
= iNty number8"
! B
SQL> create or replace type 222 as 2array7,8 of itemdet"
= B
Type created.
SQL> create table order= 7ocode number5odate date5ordetail 2228"
Table created.
SQL> insert into order= 2alues
715sysdate52227itemdet7?a++1?5?computer?5=+85itemdet7?b++1?5= ?i;od?5!,888"
1 row created.
70
SQL> insert into order= 2alues 7=5?1E%aug%=++A?52227itemdet7?c++1?5?mobile?5!+888"
1 row created.
SQL> select ' from order="
).)9$ )9*T$ )(9$T*0L70.)9$5 01*/$5 0QT48
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1 =A%*#6%+A HHH70T$/9$T7?a++1 ?5 ?computer?5 =+85
0T$/9$T7?b++1 ?5 ?i;od?5
!,88
= 1E%*#6%+A HHH70T$/9$T7?c++1 ?5 ?mobile?5 !+88
SQL> select a.ordetail from order= a"
)(9$T*0L70.)9$5 01*/$5 0QT48
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
HHH70T$/9$T7?a++1 ?5 ?computer?5 =+85 0T$/9$T7?b++1 ?5 ?i;od?5 !,88
HHH70T$/9$T7?c++1 ?5 ?mobile?5 !+88
SQL> select a.ordetail from order= a where ocodeQ1"
)(9$T*0L70.)9$5 01*/$5 0QT48
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
HHH70T$/9$T7?a++1 ?5 ?computer?5 =+85 0T$/9$T7?b++1 ?5 ?i;od?5 !,88
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
T*,# /'!0' T*,#:
SQL> create or replace types grams as object 7g1++ number5 g1,+ number5 g=++
= number8"
Type created.
SQL> create or replace type items as object7colgate grams5pepsodent grams5closeup grams8"
= B
Type created.
SQL> create table order!7ordno number5orddate date5product items8"
SQL> insert into order! 2alues715sysdate5items7grams71=51,5,85grams7=+51L51+85
= grams7,55E888"
SQL> select ' from order!"
)(91) )(99*T$ ;()9#.T7.)L6*T$761++5 61,+5 6=++85 ;$;S)9$1T761++5
61,+5 6=++85 .L)S$#;761++5 61,+56=++8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1 =A%*#6%+A 0T$/S76(*/S71=5 1,5 ,85 6(*/S7=+5 1L5 1+85
6(*/S7,5 5 E88
71
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
O .reate a college hierarchy.
SQL> create or replace type course:details is object7class 2archar=71+85semeter
2archar=71+88"
= B
Type created.
SQL> create or replace type stu:details is object7name 2archar=7=+85rollno number785
= course course:details8"
! B
Type created.
SQL> create table #ni2ersity 7stu stu:details8"
Table created.
SQL> insert into #ni2ersity 2alues
7stu:details7?pankaj?51=!5course:details7?/.*?5?fifth?888"
1 row created.
SQL> insert into #ni2ersity 2alues
7stu:details7?seema?51=!!5course:details7?/.sc?5?fourth?888"
1 row created.
SQL> select ' from #ni2ersity"
ST#71*/$5 ()LL1)5 .)#(S$7.L*SS5 S$/$T$(88
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ST#:9$T*0LS7?pankaj?5 1=!5 .)#(S$:9$T*0LS7?/.*?5 ?fifth?88
ST#:9$T*0LS7?seema?5 1=!!5 .)#(S$:9$T*0LS7?/.sc?5 ?fourth?88
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
N(!#&
"#! 9#"%r'3# 9#,!0 7SQL' ;L#S command8 can be used to show the datatypes
attributes for tables that use object%relational feature. 9epth 2alue can be specified
from 1 to ,+.
SQL> set describe depth=
SQL> desc #ni2ersity
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
ST# ST#:9$T*0LS
1*/$ H*(.G*(=7=+8
()LL1) 1#/J$(78
.)#(S$ .)#(S$:9$T*0LS
SQL> set describe depth !
72
SQL> desc #ni2ersity
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
ST# ST#:9$T*0LS
1*/$ H*(.G*(=7=+8
()LL1) 1#/J$(78
.)#(S$ .)#(S$:9$T*0LS
.L*SS H*(.G*(=71+8
S$/$T$( H*(.G*(=71+8
N(!#:
3hen Nuerying the attributes of abstract datatype5 you must use a correlation
2ariable for the table.
SQL> select s.stu.name5s.stu.course.class from #ni2ersity s where s.stu.rollnoQ1=!"
ST#.1*/$ ST#..)#(S$
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%
pankaj /.*
Cr#a!'g I9#):
SQL> create index stu:rollno on #ni2ersity7stu.rollno8"
0ndex created.
NESTED TABLE:
SQL> create or replace type item1 as object 7code char7,85name 2archar=7=+85Nty number8"
= B
Type created.
SQL> create or replace type iitemms as table of item1"
= B
Type created.
SQL> create table orders 7ordno number5orddate date5itemdet iitemms8 nested
= table itemdet store as iii"
SQL> set describe depth !"
SQL> desc orders"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%
)(91) 1#/J$(
)(99*T$ 9*T$
0T$/9$T 00T$//S
.)9$ .G*(7,8
1*/$ H*(.G*(=7=+8
QT4 1#/J$(
SQL> insert into orders 2alues 71=!5sysdate5iitemms7item17?a++1?5?computer?51=888"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
73
SQL> create or replace type *10/*L:T4 as object 7Jreed 2archar=7=,851ame
= 2archar=7=,85Jirth9ate date8"
B
Type created.
SQL> create type *10/*L:1T as table of *10/*L:T4"
= B
Type created.
SQL> desc Jreeder
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%
J($$9$(1*/$ H*(.G*(=7=,8
*10/*LS *10/*L:1T
J($$9 H*(.G*(=7=,8
1*/$ H*(.G*(=7=,8
J0(TG9*T$ 9*T$
SQL> create table J($$9$( 7Jreeder1ame 2archar=7=,85*nimals *10/*L:1T8 nested
= table *nimals store as *10/*L:1T:T*J"
Table created.
SQL> insert into J($$9$( 2alues 7?I*1$ I*/$S?5 *10/*L:1T7
*10/*L:T4 7?9)6?5?J#T.G?5?!1%/*(%=++,?85
*10/*L:T47?9)6?5?()H$(?5?1=%I#1%=++L?85
*10/*L:T47?9)6?5?I#L0)?5?=!%*;(%=++!?888"
SQL> select Jreeder1ame51.1ame51.Jirth9ate
= from J($$9$(5T*JL$7J($$9$(.*nimals8 1
! where 1.1ameQ?I#L0)?"
J($$9$(1*/$ 1*/$ J0(TG9*T$
%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%
I*1$ I*/$S I#L0) =!%*;(%+!
insert, update and delete with TABLE function:
SQL> insert into T*JL$7select *nimals from J($$9$(
= where Jreeder1ameQ?I*1$ I*/$S?8
! 2alues 7*10/*L:T47?dog?5?marcus?5?+1%aug%=++1?88"
SQL> update T*JL$7select *nimals from J($$9$(
= where Jreeder1ameQ?I*1$ I*/$S?8 1
! set 1.Jirth9ateQ?+1%sep%1EEE?
where 1.1ameQ?I#L0)?"
1 row updated.
SQL> select Jreeder1ame51.1ame51.Jirth9ate from
= J($$9$(5T*JL$7J($$9$(.*nimals8 1"
J($$9$(1*/$ 1*/$ J0(TG9*T$
74
%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%
I*1$ I*/$S J#T.G !1%/*(%+,
I*1$ I*/$S ()H$( 1=%I#1%+L
I*1$ I*/$S I#L0) +1%S$;%EE
I*1$ I*/$S marcus +1%*#6%+1
SQL> select Jreeder1ame51.1ame51.Jirth9ate from
= J($$9$(5T*JL$7J($$9$(.*nimals8 1"
J($$9$(1*/$ 1*/$ J0(TG9*T$
%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%
I*1$ I*/$S J#T.G !1%/*(%+,
I*1$ I*/$S ()H$( 1=%I#1%+L
I*1$ I*/$S I#L0) +1%S$;%EE
OBJECT TABLE AND OID:
SQL> create table *10/*L of *10/*L:T4"
Table created.
SQL> 9$S. *10/*L"
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%
J($$9 H*(.G*(=7=,8
1*/$ H*(.G*(=7=,8
J0(TG9*T$ 9*T$
INSERTING ROWS INTO OBJECT TABLE:
SQL> insert into *10/*L 2alues 7*10/*L:T47?/#L$?5?<(*1.$?5?+=%I*1%1EEE?88"
SQL> S$L$.T ' <()/ *10/*L"
J($$9 1*/$ J0(TG9*T$
%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%
/#L$ <(*1.$ +=%I*1%EE
9)6 J$1I0 +!%/*4%+,
.().)90L$ L4L$ !+%1)H%+
SQL> select 1ame from *nimal where JreedQ?.().)90L$?"
1*/$
%%%%%%%%%%%%%%%%%%%%%%%%%
L4L$
REF FUNCTION:
SQL> S$L$.T ($<7*8 <()/ *10/*L * 3G$($ 1*/$Q?<(*1.$?"
($<7*8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
++++=>+=+E.*$!L,99=<=*,L+*!AA>,E...,19*>+A.11==JEE>!.JL>>J.,>E*AL,E*+++<+,+++++
USING T;E DEREFERENCE FUNCTION:
SQL> create table D$$;$( 7Deeper1ame 2archar=7=,85*nimalDept ($< *10/*L:T48"
75
Table created.
SQL> desc D$$;$(
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
D$$;$(1*/$ H*(.G*(=7=,8
*10/*LD$;T ($< )< *10/*L:T4
SQL> desc D$$;$("
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%
D$$;$(1*/$ H*(.G*(=7=,8
*10/*LD$;T ($< )< *10/*L:T4
SQL> desc D$$;$(
1ame 1ullF Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%
D$$;$(1*/$ H*(.G*(=7=,8
*10/*LD$;T ($< )< *10/*L:T4
J($$9 H*(.G*(=7=,8
1*/$ H*(.G*(=7=,8
J0(TG9*T$ 9*T$
SQL> S$L$.T 9$($<7D.*nimalDept8 from D$$;$( D
3G$($ Deeper1ameQ?.*TG$(01$ 3$0LX? "
9$($<7D.*10/*LD$;T87J($$95 1*/$5 J0(TG9*T$8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*10/*L:T47?9)6?5 ?J$1I0?5 ?+!%/*4%+,?8
TABLESPACE:
SQL> create tablespace tspace1 datafile ?9&[pankaj1.txt? siKe 1++k
= default storage 7initial 1k next ,k minextents 1 maxextents 1++8 online"
Tablespace created.
SQL> create tablespace tspace= datafile ?9&[pankaj=.txt? siKe 1++k
= default storage 7initial 1k next ,k minextents 1 maxextents 1++8 online"
Tablespace created.
SQL> create tablespace tspace! datafile ?9&[pankaj!.txt? siKe 1++k
= default storage 7initial 1k next ,k minextents 1 maxextents 1++8 online"
Tablespace created.
SQL> create tablespace tspace datafile ?9&[pankaj.txt? siKe 1++k
= default storage 7initial 1k next ,k minextents 1 maxextents 1++8 online"
Tablespace created.
O
create table salinfo7code char7,85salary number8 partition by range7salary8
7partition part1 2alues less than7>+++8 tablespace tspace15
76
partition part= 2alues less than7=,+++8 tablespace tspace=5
partition part! 2alues less than7++++8 tablespace tspace!5
partition part 2alues less than7>++++8 tablespace tspace8
B
SQL> insert into salinfo 2alues 7?a++1?5L+++8"
SQL> insert into salinfo 2alues 7?b++1?5>+++8"
SQL> insert into salinfo 2alues 7?c++1?51++++8"
SQL> insert into salinfo 2alues 7?d++1?5=++++8"
SQL> insert into salinfo 2alues 7?e++1?5=,+++8"
SQL> insert into salinfo 2alues 7?f++1?5!,+++8"
SQL> insert into salinfo 2alues 7?g++1?5,+++8"
SQL> insert into salinfo 2alues 7?h++1?5L++++8"
SQL> insert into salinfo 2alues 7?i++1?5E++++8"
insert into salinfo 2alues 7?i++1?5E++++8
'
$(()( at line 1&
)(*%1++& inserted partition key does not map to any partition
SQL> select ' from salinfo"
.)9$ S*L*(4
%%%%% %%%%%%%%%%
a++1 L+++
b++1 >+++
c++1 1++++
d++1 =++++
e++1 =,+++
f++1 !,+++
g++1 ,+++
h++1 L++++
> rows selected.
SQL> select ' from salinfo partition7part18"
.)9$ S*L*(4
%%%%% %%%%%%%%%%
a++1 L+++
SQL> select ' from salinfo partition7part=8"
.)9$ S*L*(4
%%%%% %%%%%%%%%%
b++1 >+++
c++1 1++++
d++1 =++++
SQL> select ' from salinfo partition7part!8"
77
.)9$ S*L*(4
%%%%% %%%%%%%%%%
e++1 =,+++
f++1 !,+++
;AS; FUNCTION:
SQL> create tablespace tabspace1 datafile ?9&[Tablespace[ts1.txt? siKe 1++k
= default storage 7initial 1k next ,k minextents 1 maxextents 1++8 online"
Tablespace created.
SQL> create tablespace tabspace= datafile ?9&[Tablespace[ts=.txt? siKe 1++k
= default storage 7initial 1k next ,k minextents 1 maxextents 1++8 online"
Tablespace created.
SQL> create tablespace tabspace! datafile ?9&[Tablespace[ts!.txt? siKe 1++k
= default storage 7initial 1k next ,k minextents 1 maxextents 1++8 online"
Tablespace created.
To create a hash partition5 we use partition by hash clause in place of partition by
range clause as shown.
O
create table wipro 7code char7,85salary number8 partition by hash7salary8
partitions ! store in 7tabspace15tabspace=5tabspace!8
B
*s shown in the preceeding listing5we can specify the number of partitions and the
tablespace to use&
partitions ! store in 7tabspace15tabspace=5tabspace!8
This method will create the partitions with system generated names of the format S4S:;nn.
The number of tablespace specified in store in clause does not ha2e to eNual the number of
partitions. 0f more partitions than tablespace are specified5 the partition will be
assigned to tablespace in a round robin fashion.
SQL> select ' from wipro where salaryS++++"
.)9$ S*L*(4
%%%%% %%%%%%%%%%
a++1 >+++
c++1 !++++
b++1 1++++
f++1 !,+++
USING LARGE OBJECTS:
*2ailable datatypes.
JL)J Jinary L)J" binary data upto 6J in length5 stored in 9atabase. * JL)J could
contain 2ideo or picture information.
78
.L)J .haracter L)J" character data upto 6J in length5 stored in 9atabase.
1.L)J * .L)J column that supports a multibyte charset.
J<0L$ Jinary file" read only binary data stored outside the database5 the length of
which is limited by operating system.
SQL> create table empinfo 7code char7,85info clob8"
SQL> insert into empinfo 7?a++1?5?*mit Sehgal has the employee code a++1 and G$ is
working in this organisation since last years. Ge is a dedicated and efficient
programmer. Gis skills are upto the mark and doing any task is like a cake walk
for him?8"
O.L
declare
loc:2ar clob"
amount:2ar integer"
offset:2ar integer"
output:2ar 2archar7,+8"
begin
amount:2ar&Q@amount:2ar"
offset:2ar&Q@offset:2ar"
select info into loc:2ar from empinfo where codeQ?a++1?"
dbms:lob.read7loc:2ar5amount:2ar5offset:2ar5output:2ar8"
dbms:output.put:line7output:2ar8"
end"
B
SQL> Y .L
$nter 2alue for amount:2ar& =+
old A& amount:2ar&Q@amount:2ar"
new A& amount:2ar&Q=+"
$nter 2alue for offset:2ar& 1
old >& offset:2ar&Q@offset:2ar"
new >& offset:2ar&Q1"
*mit Sehgal has the
SQL> Y .L
$nter 2alue for amount:2ar& ,+
old A& amount:2ar&Q@amount:2ar"
new A& amount:2ar&Q,+"
$nter 2alue for offset:2ar& ,
old >& offset:2ar&Q@offset:2ar"
new >& offset:2ar&Q,"
Sehgal has the employee code a++1 and G$ is worki
;LBSQL procedure successfully completed.
S!(r'g '7ag#" ' 9a!a3a"#:
SQL> create directory Large)bj as ?9&[?"
9irectory created.
79
SQL> create table myimages 7name 2archar=7=+85image J<0L$8"
Table created.
SQL> insert into myimages 2alues 7?Seema?5J<0L$1*/$7?Large)bj?5?seema.gif?88"
1 row created.
SQL> insert into myimages 2alues 7?Sonika?5J<0L$1*/$7?9&[Large)bj[?5?sona.jpeg?88"
1 row created.
NOTE:
To 2iew images stored in the table we ha2e to write a de2elop a front
end application.
NOTE:
To set a L)J column to an empty locator 2alue 5you ha2e to know its datatype&
JL)J #se $/;T4:JL)J78
.L)J #se $/;T4:.L)J78
1.L)J #se $/;T4:.L)J78
J<0L$ #se J<0L$1*/$
USING SQL4PLUS LOADER TO LOAD DATA:
Steps to load data using SQL';L#S loader&
3rite mo2ies.txt file and sa2e it in )racleBoraE+Bbin
(ang 9e basanti5*mir Dahn5Git
9hoom%=5Grithik (oshan5Git
)mkara5*jay 9e2gan5<lop
3rite mo2ies.ctl file and sa2e it in )racleBoraE+Bbin
Load 9ata
0nfile ?mo2ies.txt?
*ppend 0nto T*JL$ mo2ies
<ields terminated by M5M
7title5hero5status8
create a table named mo2ies under a user created in )racle.
SQL> "0(/ 2"#r
USER '" =SCOTT=
SQL> %r#a!# !a3$# 7(>'#" ?!'!$# >ar%0ar:?@AB&0#r( >ar%0ar:?:AB&"!a!2" %0ar?CBB1
80
Table created.
1ow on command prompt gi2e the following command.
D:D(ra%$#D(raEADBIN>"F$$9r "%(!!5!'g#r %(!r($G7(>'#"H%!$ $(gG7(>'#"H$(g
SQL'Loader& (elease E.+.1.1.1 % ;roduction on Sun Sep = =1&++&1E =++A
7c8 .opyright =++1 )racle .orporation. *ll rights reser2ed.
.ommit point reached % logical record count !
9&[oracle[oraE+[J01>sNlplus
SQL';lus& (elease E.+.1.+.1 % ;roduction on Sun Sep = =1&1!&++ =++A
7c8 .opyright =++1 )racle .orporation. *ll rights reser2ed.
$nter user%name& scott
$nter password& '''''
.onnected to&
)racleEi $nterprise $dition (elease E.+.1.1.1 % ;roduction
3ith the ;artitioning option
ISer2er (elease E.+.1.1.1 % ;roduction
SQL> "#$#%! 4 8r(7 7(>'#"1
T0TL$ G$() ST*T#
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% %%%%%
(ang 9e basanti *mir Dahn Git
9hoom%= Grithik (oshan Git
)mkara *jay 9e2gan <lop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SQL> conn
$nter user%name& scott
$nter password& '''''
.onnected.
SQL> select ' from tab"
T1*/$ T*JT4;$ .L#ST$(09
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% %%%%%%%%%%
*10/*L T*JL$
*10/*L:1T:T*J T*JL$
J)1#S T*JL$
J($$9$( T*JL$
.#ST)/$( T*JL$
9$;T T*JL$
$/; T*JL$
000 T*JL$
D$$;$( T*JL$
81
MOVIES TABLE
)(9$(= T*JL$
)(9$(! T*JL$
)(9$(S T*JL$
;1J T*JL$
S*L6(*9$ T*JL$
#10H$(S0T4 T*JL$
1L rows selected.
D:D(ra%$#D(raEADBIN>#),
$xport& (elease E.+.1.1.1 % ;roduction on Sun Sep = =1&1>&=E =++A
7c8 .opyright =++1 )racle .orporation. *ll rights reser2ed.
U"#ra7#: "%(!!
Pa""/(r9:
.onnected to& )racleEi $nterprise $dition (elease E.+.1.1.1 % ;roduction
3ith the ;artitioning option
ISer2er (elease E.+.1.1.1 % ;roduction
$nter array fetch buffer siKe& +EL >
$xport file& $-;9*T.9/; > pan.dmp
7=8#7sers85 or 7!8T7ables8& 7=8# > =
$xport grants 7yesBno8& yes > y
$xport table data 7yesBno8& yes > y
.ompress extents 7yesBno8& yes > y
$xport terminated successfully with warnings.
D:D(ra%$#D(raEADBIN>'7,
0mport& (elease E.+.1.1.1 % ;roduction on Sun Sep = =1&=1&+! =++A
7c8 .opyright =++1 )racle .orporation. *ll rights reser2ed.
#sername& pankaj
;assword& '''''''''
.onnected to& )racleEi $nterprise $dition (elease E.+.1.1.1 % ;roduction
3ith the ;artitioning option
ISer2er (elease E.+.1.1.1 % ;roduction
0mport file& $-;9*T.9/; > pan.dmp
$nter insert buffer siKe 7minimum is >1E=8 !+A=+> =++++
$xport file created by $-;)(T&H+E.++.+1 2ia con2entional path
82
3arning& the objects were exported by S.)TT5 not by you
import done in 3$>/S3011=,= character set and *L1L#T<1L 1.G*( character set
List contents of import file only 7yesBno8& no > n
0gnore create error due to object existence 7yesBno8& no > n
0mport grants 7yesBno8& yes > y
0mport table data 7yesBno8& yes > y
0mport entire export file 7yesBno8& no > y
0mport terminated successfully with warnings.
SQL> conn
$nter user%name& pankaj
$nter password& '''''''''
.onnected.
SQL> select ' from tab"
T1*/$ T*JT4;$ .L#ST$(09
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% %%%%%%%%%%
J)1#S T*JL$
9$;T T*JL$
$/; T*JL$
/)H0$S T*JL$
;1J T*JL$
S*L6(*9$ T*JL$
L rows selected.
LOCKING:
SQL> create user pankaj identified by seema1!"
#ser created.
SQL> grant dba to pankaj"
6rant succeeded.
SQL> grant select on emp to pankaj"
6rant succeeded.
.ommand gi2en by scott.
SQL> select ' from emp where empnoQA!LE for update"
$/;1) $1*/$ I)J /6( G0($9*T$ S*L .)// 9$;T1)
%%%%%%%%%% %%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%%%%%%%%%%% %%%%%%%
%%%%%%%%%% %%%%%%%%%%
A!LE S/0TG .L$(D AE+= 1A%9$.%>+ >++ =+
.ommand gi2en by pankaj.
83
SQL> select ' from scott.emp where empnoQA!LE for update"
The )racle engine here ensures that .lient pankajPs SQL statement waits for the lock to be
released on $/; table by a .)//0T or ()LLJ*.D statement fired by .lient scott.
Gence the command gi2en abo2e will not show any result.
0n order to a2oid unnecessary waiting time5 a 1)3*0T option can be used to inform the
)racle engine to terminate the SQL statement if the record has already been locked. 0f
this happens the )racle engine terminates the running 9/L and comes up with a message
indicating that the resource is busy.
SQL> select ' from scott.emp where empnoQA!LE for update 1)3*0T"
.ommand gi2en by pankaj
select ' from scott.emp where empnoQA!LE for update 1)3*0T
'
$(()( at line 1&
)(*%+++,& resource busy and acNuire with 1)3*0T specified
U"'g L(%- Ta3$# "!a!#7#!:
SQL> S$L$.T ' <()/ ST#9$1TS"
1*/$ ()LL1) .L*SS /*(DS
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%% %%%%%%%%%%
;*1D*I ,A /.* !++
S$$/* , /S. !=,
(*H0 1 /.* =E+
;(01.$ 1+ JT$.G !+1
SQL> L).D T*JL$ $/; 01 $-.L#S0H$ mode 1)3*0T"
.lient Scott has locked the table in exclusi2e mode 7i.e only Nuerying of records is
allowed on the ST#9$1TS table by .lient ;ankaj.
SQL> 01S$(T 01T) ST#9$1TS H*L#$S7?J*J#L?51,5?/.*?5!!!8"
1 row created.
.lient Scott performs an insert operation but does not commit the transaction.
SQL> S$L$.T ' <()/ ST#9$1TS"
1*/$ ()LL1) .L*SS /*(DS
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%% %%%%%%%%%%
;*1D*I ,A /.* !++
S$$/* , /S. !=,
(*H0 1 /.* =E+
;(01.$ 1+ JT$.G !+1
J*J#L 1, /.* !!!
.lient ;ankaj performs a 2iew operation&
SQL> S$L$.T ' <()/ S.)TT.ST#9$1TS"
84
1*/$ ()LL1) .L*SS /*(DS
%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%% %%%%%%%%%%
;*1D*I ,A /.* !++
S$$/* , /S. !=,
(*H0 1 /.* =E+
;(01.$ 1+ JT$.G !+1
.lient ;ankaj performs an insert operation.
SQL> 01S$(T 01T) S.)TT.ST#9$1TS H*L#$S7?J0TT)?5!,5?JT$.G?5=A+8"
This command cause the system to enter into an indefinite wait period till all locks are
released by .lient Scott taken on ST#9$1TS table.
85