Date post: | 04-Jun-2018 |
Category: |
Documents |
Upload: | jayaprahas |
View: | 217 times |
Download: | 0 times |
of 65
8/13/2019 51393086-dbms-lab
1/65
STUDY OF SQL QUERIES
DATA DEFINITION LANGUAGE
The DDLcommands are:
Create Table
Alter Table
Drop Table Create View
Drop View
EMPLOYEE DETAILS:
TABLE STRUCTURE
FIELD NAME DATA TYPE SIZE
EMP_ID NUMBE !
EMP_NAME A"P#ANUMEIC $!
CIT% A"P#ANUMEIC $!
DE&I'NATI(N A"P#ANUMEIC $!
&A"A% NUMBE $)
1.Create Comman:! S"nta#:
create table * table name+ ,col-mn.name data t/pe ,si0e1 constraints12
De$%r&'t&on:The create command when applied with abo3e speci4ication creates the 4ields o4
di44erent data t/pe5
(. A)ter Comman:! S"nta#:
a1 alter table *table name+ add,col-mn.name data t/pe ,si0e112
De$%r&'t&on:The alter command when -sed with add allows -s to add an additional col-mn to an
alread/ e6istin7 table5
S"nta#: b1 alter table *table name+ modi4/,col-mn.name data t/pe ,si0e112
De$%r&'t&on:The alter command when -sed with modi4/ rede4ines the col-mn with the 7i3en 3al-es b-t cannot chan7e the
col-mn names5
S"nta#: a1 alter table *table name+ drop,col-mn.name12
8/13/2019 51393086-dbms-lab
2/65
De$%r&'t&on: The alter command when -sed with drop deletes the speci4ied col-mn in the table5
*. Dro' Comman:! S"nta#: Drop Table *Table_Name+ 2
De$%r&'t&on:Atable can be dropped ,deleted1 b/ -sin7 a drop table command5
+. Create ,&e- Comman:! S"nta#:
Create 3iew *3iew.name + as select *col-mn.name 8 9 + 4rom*table.name+where *condition+
De$%r&'t&on:A 3iew is named deri3ed 3irt-al table5 A 3iew ta;es the o-tp-t o4 a
8/13/2019 51393086-dbms-lab
3/65
PROGRAM TO LEARN DDL COMMANDS
CREATE TABLE:&?"+ create table empp,emp_id n-mber,!1emp_name 3archar,$!1cit/ 3archar,$!1desi7nation 3archar,$!1salar/
n-mber,$!112
Table created5
&?"+ desc empp2
Name N-ll@ T/pe......................................... ........ .......................EMP_ID NUMBE,!1EMP_NAME VAC#A),$!1CIT% VAC#A),$!1DE&I'NATI(N VAC#A),$!1&A"A% NUMBE,$!1
ALTER TABLE:&?"+ alter table empp add,do datebasic n-mber,$)112
Table altered5
&?"+ desc empp2
Name N-ll@ T/pe......................................... ........ ..................EMP_ID NUMBE,!1EMP_NAME VAC#A),$!1CIT% VAC#A),$!1DE&I'NATI(N VAC#A),$!1&A"A% NUMBE,$!1D( DATEBA&IC NUMBE,$)1
&?"+ alter table empp modi4/,salar/ n-mber,112
Table altered5
&?"+ desc empp2Name N-ll@ T/pe......................................... ........ ............................
EMP_ID NUMBE,!1EMP_NAME VAC#A),$!1CIT% VAC#A),$!1DE&I'NATI(N VAC#A),$!1&A"A% NUMBE,1D( DATEBA&IC NUMBE,$)1
&?"+ alter table empp drop,dobasic12Table altered5
8/13/2019 51393086-dbms-lab
4/65
&?"+ desc empp2Name N-ll@ T/pe......................................... ........ ............................EMP_ID NUMBE,!1EMP_NAME VAC#A),$!1CIT% VAC#A),$!1DE&I'NATI(N VAC#A),$!1&A"A% NUMBE,1
CREATE ,IE2:
&?"+ create 3iew empp3iew as select 9 4rom empp2View created5
&?"+ desc empp 3iew2
Name N-ll@ T/pe......................................... ........ ............................EMP_ID NUMBE,!1EMP_NAME VAC#A),$!1CIT% VAC#A),$!1DE&I'NATI(N VAC#A),$!1&A"A% NUMBE,1
DROP ,IE2:
&?"+ drop 3iew empp3iew2View dropped5
&?"+ desc empp3iew2E(:(A.!!: obect empp3iew does not e6ist
DROP TABLE:&?"+ drop table empp2Table dropped5
TRUNCATE COMMAND:
&?"+ tr-ncate table empp2Table tr-ncated5
&?"+desc empp2
Name N-ll@ T/pe
......................................... ........ ............................EMP_ID NUMBE,!1EMP_NAME VAC#A),$!1CIT% VAC#A),$!1DE&I'NATI(N VAC#A),$!1&A"A% NUMBE,$!1&?"+rename empp to emplo/2Table renamed5
&?"+ desc empp2E(:(A.!!: obect empp does not e6ist
8/13/2019 51393086-dbms-lab
5/65
&?"+desc emplo/2
Name N-ll@ T/pe......................................... ........ ............................EMP_ID NUMBE,!1EMP_NAME VAC#A),$!1CIT% VAC#A),$!1DE&I'NATI(N VAC#A),$!1&A"A% NUMBE,$!1
RESULT:
Th-s the DD" command are e6ec-ted and 3eri4ied5
8/13/2019 51393086-dbms-lab
6/65
DATA MANIPULATION LANGUAGE
The DMLcommands are:
Insert
Delete
Update
1. In$ert:
S"nta#:
Insert into *table.name+ 3al-es ,3al$ 3al) 3alF53aln12
De$%r&'t&on:
The Ginsert intoH command insert the 3al-es in the speci4ied table 5In the insert into &?" sentence the col-mns and3al-es ha3e a one to one relationship ,i5e1 the 4irst 3al-e described into the 4irst col-mn the second 3al-e described bein7inserted into the second col-mn and so on5
(. De)ete:
S"nta#:
Delete 4rom *table.name+ where *condition+J2
De$%r&'t&on:
The delete in &?" is -sed to remo3e rows 4rom table5 To remo3e$5 All the rows 4rom a table5
,(r1)5 A select set o4 rows 4rom a table5
*. U'ate:
S"nta#:
Update *table.name+ set 4ieldnameK*e6pression+ where conditionJ2
De$%r&'t&on:
The -pdate command is -sed to chan7e or modi4/ data 3al-es in a table5 To -pdate$5 All the rows 4rom a table5 ,(r1)5 A select set o4 rows 4rom a table5
PROGRAM TO LEARN DML COMMANDS
INSERT COMMAND:
&?"+ insert into empp 3al-es ,Lemp_idLemp_nameLcit/Ldesi7nationLsalar/12Enter 3al-e 4or emp_id: $Enter 3al-e 4or emp_name: amesEnter 3al-e 4or cit/: ban7aloreEnter 3al-e 4or desi7nation: modleaderEnter 3al-e 4or salar/: )
8/13/2019 51393086-dbms-lab
7/65
old $: insert into empp 3al-es ,Lemp_idLemp_nameLcit/Ldesi7nationLsalar/1new $: insert into empp 3al-es,$amesban7aloremodleader)1
$ row created5
&?"+ 8Enter 3al-e 4or emp_id: $$Enter 3al-e 4or emp_name: 7aneshEnter 3al-e 4or cit/: chennaiEnter 3al-e 4or desi7nation: so4ten7iEnter 3al-e 4or salar/: )$old $: insert into empp 3al-es,Lemp_idLemp_nameLcit/Ldesi7nationLsalar/1new $: insert into empp 3al-es,$$7aneshchennaiso4ten7i)$1
$ row created5
&?"+ 8Enter 3al-e 4or emp_id: $Enter 3al-e 4or emp_name: pri/aEnter 3al-e 4or cit/: calc-ttaEnter 3al-e 4or desi7nation: mana7erEnter 3al-e 4or salar/: old $: insert into empp 3al-es ,Lemp_idLemp_nameLcit/Ldesi7nationLsalar/1
new $: insert into empp 3al-es,$pri/acalc-ttamana7er1
$ row created5
&?"+ select 9 4rom empp2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%........... ................. .............. .............. .......... ............. $ ames ban7alore modleader ) $$ 7anesh chennai so4ten7i )$
$ pri/a calc-tta mana7er
UPDATE COMMAND:
&?"+ -pdate empp set desi7nationKen7ineerwhere emp_idK$$2$ row -pdated5
&?"+ select 9 4rom empp2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%........... ................. .............. .............. .......... ................. $ ames ban7alore modleader ) $$ 7anesh chennai en7ineer )$ $ pri/a calc-tta mana7er
DELETE COMMAND:
&?"+ delete 4rom empp where emp_idK$2
$ row deleted5
&?"+ select 9 4rom empp2EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%........... ................. .............. .............. .......... ............. $$ 7anesh chennai en7ineer )$
8/13/2019 51393086-dbms-lab
8/65
$ pri/a calc-tta mana7er
RESULT:
Th-s the DM" commands are e6ec-ted and 3eri4ied5
DATA ?UE% "AN'UA'E
The DQLcommands are: &elect
'ro-p b/
#a3in7
(rder b/
N-ll
1. Se)e%t Statement:
S"nta#: &elect *attrib-te list+ 4rom *table list+where cla-seJ2
De$%r&'t&on: &elect command is -sed to retrie3e data 4rom one or more tables or col-mns5 The attrib-te list is a list o4attrib-tes name whose 3al-es are displa/ed b/
8/13/2019 51393086-dbms-lab
9/65
&elect *col-mn name+ 4rom *table name+ where *condition+ 'ro-p b/ *condition+ ha3in7*condition+2
De$%r&'t&on: The Gha3in7H cla-se is -sed to speci4/ certain conditions on rows retrie3ed b/ -sin7 7ro-p b/ cla-se5 Thiscla-se sho-ld be preceded b/ a G7ro-p b/H cla-se5
. N0)) Comman:
S"nta#: &elect *col-mn.name+ NV",s-bstit-tion col-mns-bstit-ted 3al-e1 4rom *table name+ where *condition+2
De$%r&'t&on: The NV" 4-nction helps in s-bstit-tin7 the 3al-e in the n-ll 4ields5b-t this 4-nction onl/ displa/s the chan7eand does not -pdate the col-mns5
PROGRAM TO LEARN DQL COMMANDS
SELECT COMMAND:
&?"+ select 9 4rom empp2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%........... ............. .... .............. .............. .......... ............$ ames ban7alore modleader )$$ 7anesh chennai en7ineer )$
$) pri/a calc-tta mana7er
$ ;amal coimbatore prolead !
$! 3ishn- bomba/ ceo $
$ ;irthi;a chennai acco-ntant $
rows selected5
SELECT AND 23ERE CLAUSE:
1. L&$t o-n em')o"ee re%or$ -6o$e $a)ar" &$ 71888.
&?"+ select 9 4rom empp where salar/+$2
EMP_ID EMP_NAME CIT%DE&I'NATI(N &A"A%........... ................. .............. .............. .......... .............$ ames ban7alore modleader )$$ 7anesh chennai en7ineer )$$) pri/a calc-tta mana7er $ ;amal coimbatore prolead !
(. L&$t o-n t6e em'9&em')o"ee name an $a)ar" ;&e)$.
&?"+ select emp_idemp_namesalar/ 4rom empp2
8/13/2019 51393086-dbms-lab
10/65
EMP_ID EMP_NAME &A"A%.......... .............. ........................... $ ames ) $$ 7anesh )$ $) pri/a $ ;amal ! $! 3ishn- $ $ ;irthi;a $
rows selected5
SELECT!ORDER BY:
*. L&$t o-n t6e em')o"ee $a)ar" &n a$%en&n5 orer.
&?"+ select 9 4rom empp order b/ salar/2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%........... ................. .............. .............. .......... .............$! 3ishn- bomba/ ceo $
$ ;irthi;a chennai acco-ntant $$ ames ban7alore modleader )$$ 7anesh chennai en7ineer )$$ ;amal coimbatore prolead !$) pri/a calc-tta mana7er rows selected5
+. L&$t o-n t6e em')o"ee $a)ar" &n e$%en&n5 orer.
&?"+ select 9 4rom empp order b/ salar/ desc2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%........... ................. .............. .............. .......... .............$) pri/a calc-tta mana7er
$ ;amal coimbatore prolead !$$ 7anesh chennai en7ineer )$$ ames ban7alore modleader )$! 3ishn- bomba/ ceo $$ ;irthi;a chennai acco-ntant $ rows selected5
SELECT DISTINCT:
&?"+ select 9 4rom empp2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%........... ................. .............. .............. .......... .............
$ ames ban7alore modleader )$$ 7anesh chennai en7ineer )$$) pri/a calc-tta mana7er $ ;amal coimbatore prolead !$! 3ishn- bomba/ ceo $$ ;irthi;a chennai acco-ntant $$ s-b- bomba/ en7ineer )$Q pri/a ban7alore ceo ) rows selected5
8/13/2019 51393086-dbms-lab
11/65
&?"+ select distinct cit/ 4rom empp2
CIT%..............ban7alorebomba/calc-ttachennaicoimbatore
SELECT < GROUP BY:
&?"+ select desi7nation ma6,salar/1 4rom empp 7ro-p b/ desi7nation2
DE&I'NATI(N MAO,&A"A%1.............. ..............................acco-ntant $ceo )en7ineer )mana7er modleader )prolead !
rows selected5
SELECT < 3A,ING CLAUSE:
&?"+ select ma6,salar/1min,salar/1s-m,salar/1 4rom empp 7ro-p b/ desi7nation ha3in7 desi7nationKceo 2
MAO,&A"A%1 MIN,&A"A%1 &UM,&A"A%1....................................... ........... .............. ) $
NULL COMMAND:
&?"+ select 9 4rom empp2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%.......... .............. .............. .............. ............................$ ames ban7alore modleader )$$ 7anesh chennai en7ineer )$$) pri/a calc-tta mana7er $ ;amal coimbatore prolead !$! 3ishn- bomba/ ceo $$ ;irthi;a chennai acco-ntant $$ s-b- bomba/ en7ineer )$Q 3ia/ ban7alore ceo )
rows selected5
&?"+ select emp_idNV",emp_namepri/a14rom empp whereemp_id K$Q
EMP_ID NV",EMP_NAME pri/a1.......... ..................................... $Q pri/a
&?"+ select 9 4rom empp2
EMP_ID EMP_NAME CIT%DE&I'NATI(N &A"A%.......... .............. .............. ......................... .............
8/13/2019 51393086-dbms-lab
12/65
$ ames ban7alore modleader )$$ 7anesh chennai en7ineer )$$) pri/a calc-tta mana7er $ ;amal coimbatore prolead !$! 3ishn- bomba/ ceo $$ ;irthi;a chennai acco-ntant $$ s-b- bomba/ en7ineer )$Q 3ia/ ban7alore ceo )
rows selected5
BET2EEN CLAUSE:
&?"+ select 9 4rom empp2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%.......... .............. .............. .............. ............................$ ames ban7alore modleader )$$ 7anesh chennai en7ineer $$) pri/a calc-tta mana7er $ ;amal coimbatore prolead Q$! 3ishn- bomba/ ceo $ ;irthi;a chennai acco-ntant $
rows selected5
&?"+ select 9 4rom empp where salar/ between ) and 2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%.......... .............. .............. .............. ...........................$ ames ban7alore modleader )$$ 7anesh chennai en7ineer )$$) pri/a calc-tta mana7er
&?"+ select 9 4rom empp where salar/ not between ) and 2
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%.......... .............. .............. .............. ............................$ ;amal coimbatore prolead Q$! 3ishn- bomba/ ceo $$ ;irthi;a chennai acco-ntant $
IN AND NOT IN PREDICATE:
&?"+ select 9 4rom empp where cit/ in ,chennaibomba/12
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%.......... .............. .............. .............. ............................$$ 7anesh chennai en7ineer )$$! 3ishn- bomba/ ceo $$ ;irthi;a chennai acco-ntant $
&?"+ select 9 4rom empp where cit/ not in ,chennaibomba/12
EMP_ID EMP_NAME CIT% DE&I'NATI(N &A"A%.......... ............. . .............. .............. ...........................
8/13/2019 51393086-dbms-lab
13/65
$ ames ban7alore modleader )$) pri/a calc-tta mana7er $ ;amal coimbatore prolead Q
RESULT:
Th-s the D?" commands are e6ec-ted and 3eri4ied5
TCL COMMANDSTRANSACTION CONTROL LANGUAGE
The DCLcommands are:
Commit
ollbac;
&a3e point
1. COMMIT:
S"nta#:&?"+ set a-to commit on 2&?"+ set a-to commit o44 2&?"+commit 2
De$%r&'t&on:
Commit command tells the DBM& to ma;e permanent chan7es made to temporar/ copies o4 the data -pdatin7 thepermanent database tables to match the -pdated temporar/ copies5
(. ROLL BAC=:
S"nta#:&?"+ rollbac; 2
De$%r&'t&on:
ollbac; tells the DBM& to -ndo an/ chan7es made to the DBM& a4ter the most recent commit5
*. SA,E POINT:
S"nta#: &?"+&a3epoint*name+2
8/13/2019 51393086-dbms-lab
14/65
De$%r&'t&on:
&a3e point are li;e mar;ers to di3ide a 3er/ len7th/ transaction to smaller ones5 The/ are -sed to identi4/ a point intransaction to which we can later rollbac;5 Th-s sa3epoint is -sed in con-nction with rollbac; to rollbac; portions o4 thec-rrent transaction5
PROGRAM TO LEARN TCL COMMANDS
ROLLBAC=:
&?"+ select 9 4rom item4ile2
ITEMC(DE ITEMC"(&E PDT_CT'%?T%_#AND E"EVE" MAO"EVE" ITEMATE............................ .......... .......... .......... .......... ..........
$ eraser stationar/ $ ) ) chain 4anc/ ! R ) ) tomato 3e7etable ! ! $Q ! pen stationar/
! rows selected5
&?"+ delete 4rom item4ile where itemcodeK!2
$ row deleted5
&?"+ select 9 4rom item4ile2ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE" ITEMATE.......... .............. .............. .............. ............................ $ eraser stationar/ $ ) ) chain 4anc/ ! R ) )
tomato 3e7etable ! ! $Q
rows delected5
&?"+ rollbac;2ollbac; complete5
&?"+ select 9 4rom item4ile2
ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE" ITEMATE.......... .............. .............. .............. ............................ $ eraser stationar/ $ ) ) chain 4anc/ ! R ) ) tomato 3e7etable ! ! $Q ! pen stationar/
COMMIT:
&?"+ delete 4rom item4ile where itemcodeK!2$ row deleted5
&?"+ commit2Commit complete5
8/13/2019 51393086-dbms-lab
15/65
&?"+ select 9 4rom item4ile2 ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE" ITEMATE.......... .............. .............. .............. ............................ $ eraser stationar/ $ ) ) chain 4anc/ ! R ) ) tomato 3e7etable ! ! $Q &?"+ rollbac;2
ollbac; complete5
&?"+ select 9 4rom item4ile2
ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE" ITEMATE.......... .............. .............. .............. ............................ $ eraser stationar/ $ ) ) chain 4anc/ ! R ) ) tomato 3e7etable ! ! $Q
SA,EPOINT:
&?"+ sa3epoint s$2&a3epoint created5
&?"+ -pdate item4ile set itemrateK) where itemcodeK2$ row -pdated5
&?"+ sa3epoint s)2&a3epoint created5
&?"+ -pdate item4ile set itemrateK$ where itemcodeK$2$ row -pdated5
&?"+ select 9 4rom item4ile2
ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE" ITEMATE
.......... .............. .............. .............. ............................ $ eraser stationar/ $ ) $ ) chain 4anc/ ! R ) ) tomato 3e7etable ! ! $Q )
&?"+ rollbac; to s)2
ollbac; complete5
&?"+ select 9 4rom item4ile2
ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE" ITEMATE.......... .............. .............. .............. ............................
$ eraser stationar/ $ ) ) chain 4anc/ ! R ) ) tomato 3e7etable ! ! $Q )
&?"+ rollbac; to s$2
ollbac; complete5
&?"+ select 9 4rom item4ile2
ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE" ITEMATE.......... .............. .............. .............. ............................
8/13/2019 51393086-dbms-lab
16/65
$ eraser stationar/ $ ) ) chain 4anc/ ! R ) ) tomato 3e7etable ! ! $Q
RESULT:
Th-s the DC" commands are e6ec-ted and 3eri4ied5
DCL COMMANDS
DATA CONTROL LANGUAGEThe DCLcommands are:
'rant
e3o;e
1. GRANT:
S"nta#:
&?"+7rant *pri3ile7es+ on *table name+ to -ser 2
De$%r&'t&on:
'rant 7i3es speci4ic &?" statement access or indi3id-al data obects to a -ser or a 7ro-p o4 -sers5
(. RE,O=E:
S"nta#: &?"+ e3o;e *pri3ile7es+ on *tablename+ 4rom -ser 2
De$%r&'t&on:
e3o;e remo3es speci4ic &?" statement access pre3io-sl/ 7ranted on indi3id-al database obects 4rom a -ser or7ro-p o4 -sers5
PREDICATES:&?"+select94rom itemlist2
C(DE ITEM PICE........ ........ .........$ Pen )5) noteboo; Q5Q Bat RR5RR
1. ALL
8/13/2019 51393086-dbms-lab
17/65
S"nta#:&elect94rom table_name where col-mn_name +all,col-mn_3al-e1
E6ample:&: &elect94rom table_name ehere col-mn_name li;e,G3al-eH1
E#am')e: &?"+select 94rom itemlist where item li;e,GPSH12
C(DE ITEM PICE........... ............ .............
$ PEN )5999999999999999999999999999999999999999999999999999999999999999999999999999
(. ENTITY INTEGRITY CONSTRAINTS:
PRIMARY =EY:
S"nta#: Create table table _name ,4ield datat/pe1 constraints *label+ primar/ ;e/,col-mn_name12
&?"+ Create table note ,id n-mber,1constraint 4lo not n-ll n-m n-mber,1primar/ ;e/12
Table created
8/13/2019 51393086-dbms-lab
18/65
&?"+ desc note2
NAME NU""@ T%PE.............. .............. .............ID N(TNU"" NUMBE ,1
NUM N(TNU"" NUMBE ,1&?"+ create table mic;/ ,id n-mber ,1name 3archar,1 constraints mo-se primar/ ;e/,idname112
Table created
NAME NU""@ T%PE ................ ................ ............... ID NUMBE ,1 NAME VAC#A ,1
UNIQUE:
S"nta#: Create table table _name ,4ield datat/pe1 constraints *label+ -ni
8/13/2019 51393086-dbms-lab
19/65
BUILT!IN FUNCTION$5 'ro-p 4-nction or A77re7ate 4-nction:
S).No F0n%t&on De$%r&'t&on
$ AV' Determines the A3era7e o4 the speci4ied 3al-es o4 col-mn I7norin7 n-ll 3al-es5) C(UNT Determines the total n-mber o4 3al-es o4 a speci4ied col-mn5
MAO Determines the ma6im-m 3al-e o4 a speci4ied col-mn i7norin7 n-ll 3al-es5
! MIN Determines the minim-m 3al-es o4 a speci4ied col-mn i7norin7 n-ll 3al-es5
&UM Determines the s-m o4 a speci4ied col-mn i7norin7 n-ll 3al-es5
ATDDEV Determines the standard deri3ation o4 a speci4ied col-mn i7norin7 n-ll 3al-es5
Q VAIANCE Determines the 3ariance o4 e6pression i7norin7 n-ll 3al-es5
)5 Character -nction:
S).No F0n%t&on De$%r&'t&on
$ ""( C(NCAT Concatenates two strin7 to7ether chan7es the 4irst letter o4 a word or series o4 wordsinto -pper case5
) INIT CAP ind the location o4 character in a strin7 otherwise ret-rn 5
IN&T ind the "ocation o4 character in a strin7 otherwise ret-rn 5
! "EN'T# et-rns the len7th o4 a strin75
"(E Con3erts e3er/ letter in a strin7 to lower case5
UPPE Con3erts e3er/ letter in a strin7 to -pper case5
Q "PAD Ma;es a strin7 a certain len7th b/ a addin7 a certain set o4 character to the le4t5
PAD Ma;es a strin7 a certain len7th b/ a addin7 a certain set o4 character to the i7ht5
R "TIM Trim all the occ-rrence o4 an/ one o4 a set o4 character o4 the "e4t side o4 strin75
$ TIM Trim all the occ-rrence o4 an/ one o4 a set o4 character o4 the i7ht side o4 strin75
$$ &(UNDEO inds words that so-nd li;e e6amples5
$) &UB&T Clip o-t a piece o4 a strin75
$ C# et-rn the character o4 the speci4ied A&CII 3al-es5$! A&CII et-rn the A&CII 3al-es o4 the speci4ied character5
$ TAN&"ATE eplace the character b/ character5
$ EP"ACE eplace the speci4ied strin7 or character b/ the e6istin7 strin7 or character i4 thatstrin7 in 4o-nd case sensiti3e5
5 N-mber -nction:
S).No F0n%t&on De$%r&'t&on$ AB& et-rn absol-te 3al-es5
) CE"" &mallest inte7er lar7e than or e
8/13/2019 51393086-dbms-lab
20/65
$! 'EATE&T 'reatest 3al-e o4 a list5
$ "EA&T "east 3al-e o4 a llist5
!5 Date -nction:
S).No F0n%t&on De$%r&'t&on$ &%& DATE et-rns s/stem date
) ADD_M(NT#& Add or s-bstracts months to or 4rom a date ret-rn date as res-lt5 NEOT_DA% et-rns the date o4 ne6t speci4ied da/ o4 the wee; a4ter the date5
! CA&E_DA% et-rns the date o4 the last da/ o4 the months speci4ied5
M(NT#&_BETEEN et-rns n-mber o4 months between dates5
(UND o-nd the date d b/ the speci4ied 4ormat i4 4ormat is not speci4ied it de4a-lts to GDDH5hich date to the neatest da/5
Q TUNC o-nds the date d tr-ncates to the -nit speci4ied b/ omitted is de4a-lts to GDPH whichtr-ncates to the nearest da/5
5 Con3ertion -nction:
S).No F0n%t&on De$%r&'t&on
$ T(_C#A Con3ert the dateGdH to character 4ormat G4H5) T(_DATE Con3ert the dateGdH to date 4ormat G4H5
DEC(DE ecords the speci4ied date to another representation5
! T(_NUMBE Con3ert the character to n-mber5
C3ARACTER FUNCTION:
&?"+ select 9 4rom st-d2
(""N( NAME DEPT MAW&.......... ........................... .............. ..........
mohan cse ! parandha bme Q raan cse Q $ ;-maran cse R ) midh-n ece
&?"+ select -pper,name1 4rom st-d2
UPPE,NAME1...............M(#ANPAAND#AAAN
WUMAANMID#UN
&?"+ select lower,name1 4rom st-d2
"(E,NAME1...............mohanparandharaan;-maran
8/13/2019 51393086-dbms-lab
21/65
midh-n
&?"+ select initcap,name1 4rom st-d2
INITCAP,NAME1...............MohanParandhaaanW-maranMidh-n
&?"+ select s-bstr,7iria$!1 4rom d-al2
&UB&....7iri
&?"+ select ltrim,welcomewel1 4rom d-al2
"TI
....come
&?"+ select rtrim,welcomecome1 4rom d-al2
T...wel
&?"+ select lpad,welcome$X1 4rom d-al2
"PAD,E"C(ME.............
XXXXXXwelcome
&?"+ select rpad,welcome$X1 4rom d-al2
PAD,E"C(ME.............welcomeXXXXXX
&?"+ select name 4rom st-d where name li;e mS2
NAME...............mohan
midh-n
&?"+ select replace,ac; ill o/b1 4rom d-al2
EP"ACE,ACW.............bac; bill bo/
&?"+ select translate,ac; all o/ab1 4rom d-al2
TAN&"ATE,...........
8/13/2019 51393086-dbms-lab
22/65
bc; bll bo/
DATE FUNCTION:&?"+ select add_months,).dec.Q!1 4rom d-al2
ADD_M(NT#
.........).AP.
&?"+ select last_da/,).dec.Q1 4rom d-al2
"A&T_DA%,.........$.DEC.Q
&?"+ select months_between,$.dec.Q$.a-7.Q1 4rom d-al2
M(NT#&_BETEEN,$.DEC.Q$.AU'.Q1....................................... !5!$$
&?"+ select ne6t_da/,$.-l.Q4rida/1 4rom d-al2
NEOT_DA%,..........U".Q
&?"+ select 7reatest,)).ma/.Q)).dec.Q1 4rom d-al2
'EATE&T,.........)).ma/.Q
AGGREGATE FUNCTION:
&?"+ select a37,mar;s1 4rom st-d2
AV',MAW&1.......... Q5)
&?"+ select min,mar;s1 4rom st-d2
MIN,MAW&1..........
&?"+ select ma6,mar;s1 4rom st-d2
MAO,MAW&1.......... R
&?"+ select co-nt,mar;s1 4rom st-d2C(UNT,MAW&1............
8/13/2019 51393086-dbms-lab
23/65
&?"+ select s-m,mar;s1 4rom st-d2&UM,MAW&1.......... Q
&?"+ select stdde3,$Q1 4rom d-al2&TDDEV,$Q1............
&?"+ select 3ariance,$Q1 4rom d-al2VAIANCE,$Q1..............
NUMERIC FUNCTIONS:
&?"+ &E"ECT AB&,.$1 (M DUA"2 AB&,.$1
.......... $
&?"+ select abs,.$1 4rom d-al2 AB&,.$1.......... $
&?"+ select power,)1 4rom d-al2P(E,)1.......... &?"+ select ro-nd,$R5RRRR)1 4rom d-al2
(UND,$R5RRRR)1................. $R&?"+ select tr-nc,$R5RRRR)1 4rom d-al2TUNC,$R5RRRR)1................. $R5RR
&?"+ select mod,$1 4rom d-al2M(D,$1.......... $&?"+ select si7n,$1 4rom d-al2 &I'N,$1.......... $
&?"+ select si7n,.$1 4rom d-al2&I'N,.$1.......... .$&?"+ select sin,!1 4rom d-al2 &IN,!1..........5R)
8/13/2019 51393086-dbms-lab
24/65
&?"+ select cos,!1 4rom d-al2 C(&,!1..........5))$RR
&?"+ select sinh,1 4rom d-al2 &IN#,1..........5Q$EY)
&?"+ select cosh,1 4rom d-al2C(,1..........5Q$EY)
&?"+ select ceil,$!5Q1 4rom d-al2CEI",$!5Q1............ $&?"+ select 4loor,$!5Q1 4rom d-al2"((,$!5Q1.............
$!
&?"+ select ln,1 4rom d-al2
"N,1..........5!
&?"+ select lo7,$$1 4rom d-al2
"(',$$1............
&?"+ select s
8/13/2019 51393086-dbms-lab
25/65
O'erator$
RELATIONAL?COMPERISONOPERATIONS
LOGICALOPERATORS
SPECIALOPERATORS
ARIT3METICOPERATORS
SETOPERATORS
K AND In Y Union
+ ( Between . Union all
* N(T "i;e 9 Intersect
+K Is 8 Min-s
*K An/
*+ All
ZK The
Set O'erator$
1. Un&on
De$%r&'t&on:M-ltiple
8/13/2019 51393086-dbms-lab
26/65
ohnsononeslindsa/smitht-rner$ rows selected5
*. Inter$e%t&on
De$%r&'t&on:
M-ltiple
8/13/2019 51393086-dbms-lab
27/65
EMP_ID EMP_NAME CIT% &A"A%............ .................. .......... ............ $ Allwin Pd/ ) E3elin Chennai es-s orld ! Comp-ter Chennai !! Poornima Pd/
Dra3id Pd/
rows selected5
SQL+ select 9 4rom dept$2
DEPT_N( EMP_NAME "(CATI(N.............. ................. ................ es-s orld
Dra3id Chennai Poornima Delhi ) CPU CompaCEPTION 3ANDLING
Error condition in P"8&?" is termed as an e6ception5 There are two t/pes o4
E6ception5 The/ are
Prede4ined E6ception
User.de4ined E6ception
An E6ception is raised when an error occ-rs5 In case o4 an error normal e6ec-tion stopsand the control is immediatel/ trans4erred to the e6ception handlin7 part o4 the P"[&?"
bloc;5 Prede4ined E6ception is raised a-tomaticall/ b/ the s/stem d-rin7 r-n timewhereas -ser.de4ined e6ceptions are raised e6plicitl/ -sin7 AI&E statements5
PREDEFINED E>CEPTION:
S"nta#:
Be7in&e
8/13/2019 51393086-dbms-lab
44/65
USER!DEFINED E>CEPTION
AIM:
To write a -serde4ined e6ception handlin7 pro7ram -sin7 P"8&?"5
PROGRAM:
declareco n-mber2e6ceeds_3al-e e6ception2
be7inselect co-nt,91 into co 4rom emplo/e2i4 co * theninsert into emplo/e 3al-es ,LempnoLempnameLempstLempsal12else
raise e6ceeds_3al-e2end i42e6ceptionwhen e6ceeds_3al-e thendbms_o-tp-t5p-t_line,rows will not be inserted12end2
SAMPLE INPUT OUTPUT:
&?"+ select 9 4rom emplo/e2
EMPN( EMPNAME EMP&T &A"E%.......... ......................... .......... ..........$ radha leader )) ;a3i mana7er ! malini anal/st $!! arthi admin )$
&?"+ 8Enter 3al-e 4or empno: Enter 3al-e 4or empname: raeswariEnter 3al-e 4or empst: so4twareEnter 3al-e 4or empsal: )old Q: insert into emplo/e 3al-es ,LempnoLempnameLempstLempsal12new Q: insert into emplo/e 3al-es ,raeswariso4tware)12
P"8&?" proced-re s-ccess4-ll/ completed5
8/13/2019 51393086-dbms-lab
45/65
8/13/2019 51393086-dbms-lab
46/65
select empname into emname 4rom emplo/e where empnoKempid2dbms_o-tp-t5p-t_line,The emplo/e name is:\\emname12e6ceptionwhen no_data_4o-nd thendbms_o-tp-t5p-t_line,data not 4o-nd12end2
SAMPLE INPUT OUTPUT :
Enter 3al-e 4or empid: $old : empid:KLempid2new : empid:K$2The emplo/e name is:radha
P"8&?" proced-re s-ccess4-ll/ completed5
&?"+ 8Enter 3al-e 4or empid: old : empid:KLempid2new : empid:K2data not 4o-ndP"8&?" proced-re s-ccess4-ll/ completed5
8/13/2019 51393086-dbms-lab
47/65
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 pre.de4ined e6ception handler is e6ec-tedand 3eri4ied5
CURSORS
AIM:
Create a P"8&?" bloc; that deletes st-dentHs records whose department is C&Eand displa/ the n-mber o4 records deleted and the remainin7 n-mber o4 records5
PROGRAM:
declarec-rsor c is select 9 4rom st-ddd cse2
a cS rowt/pe2n n-mber:K2
be7inopen c2loop4etch c into a2e6it when cS not4o-nd2i4 a5deptKcse thendelete 4rom st-ddd where deptKcse2
n:KnY$2end i42end loop2dbms_o-tp-t5p-t_line,Deleted record \\n12dbms_o-tp-t5p-t_line,emainin7 records \\,cS rowco-nt.n112close c2commit2end2
8/13/2019 51393086-dbms-lab
48/65
SAMPLE INPUT OUTPUT:
&?"+ 8Deleted record )emainin7 records
P"8&?" proced-re s-ccess4-ll/ completed5
&?"+ select 9 4rom st-ddd2
"N( NAME DEPT.......... ........ ......$ abhilash cse) ;amala eee mareesh bme! samson cse
saran/a it
Q rows selected5
&?"+ select 9 4rom st-ddd2
"N( NAME DEPT.......... ........ ......) ;amala eee
mareesh bme saran/a it
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 c-rsor is e6ec-ted and 3eri4ied5
8/13/2019 51393086-dbms-lab
49/65
CURSORS
AIM:
Create a P"8&?" bloc; to determine the top scores 4rom the st-dent table and toinsert these records into a new table5
PROGRAM:
declarec-rsor c is select 9 4rom st-dddd order b/ total desc2a st-dddd S rowt/pe2n n-mber:K2
be7inopen c2loop4etch c into a2
e6it when cS not4o-nd or cS rowco-nt+2insert into detail$ 3al-es,a5rollnoa5namea5depta5total12end loop2close c2commit2end2
SAMPLE INPUT OUTPUT:
&?"+8P"8&?" proced-re s-ccess4-ll/ completed5
&?"+ select 9 4rom st-dddd2
(""N( NAME DEPT T(TA".......... ........ .............................$ s3 cse )Q) arch- cse )R
re;;a cse )R! aarthi cse ) thamarai it tharani bme $Q ranani cse )Q rows selected5&?"+ select 9 4rom detail$2
(""N( NAME DEPT T(TA"
8/13/2019 51393086-dbms-lab
50/65
.......... ........ ...... .......... thamarai it re;;a cse )R) arch- cse )R$ s3 cse )Q! aarthi cse )
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 c-rsor is e6ec-ted and 3eri4ied5
CURSORS
AIM:
Create a P"8&?" bloc; to -pdate the total and 7rade o4 the st-dent table b/comparin7 mar;s in the indi3id-al s-bect5
PROGRAM:
declarec-rsor c is select 9 4rom st-ddd$2a cS rowt/pe2
8/13/2019 51393086-dbms-lab
51/65
tot n-mber27 char2
be7inopen c2loop4etch c into a2e6it when cS not4o-nd2
tot:Ka5m$Ya5m)Ya5m2i4 a5m$+ and a5m)+ and a5m+ then7:K#2else7:K2end i42-pdate st-ddd) set totalKtot7radeK7 where rollnoKa5rollno2end loop2commit2
close c2end2
SAMPLE INPUT OUTPUT:
&?"+ 8
P"8&?" proced-re s-ccess4-ll/ completed5
&?"+ select 9 4rom st-ddd$2
(""N( NAME M$ M) M........................... ........ .......... .......... .......... $ s3s Q Q ) re;;a Q Q arch- Q !
&?"+ alter table st-ddd$ add,total n-mber7rade 3archar,112
Table altered5
&?"+ desc st-ddd$2Name N-ll@ T/pe......................................... ........ ............................(""N( NUMBE,Q1
NAME VAC#A),1M$ NUMBE,1
8/13/2019 51393086-dbms-lab
52/65
M) NUMBE,1M NUMBE,1T(TA" NUMBE'ADE VAC#A),1
&?"+ select 9 4rom st-ddd$2
(""N( NAME M$ M) M T(TA" 'ADE.......... ........ .......... ....................... .......... .......... ...... $ s3s Q Q $R! # ) re;;a Q Q )$ # arch- Q ! $
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 c-rsor is e6ec-ted and 3eri4ied5
A -nction is a s-bpro7ram that comp-tes a 3al-e5 The s/nta6 4orcreatin7 a 4-nction is 7i3en below5
Create or replace 4-nction * 4-nction_name + ar7-ment Jret-rn datat/pe is
,local declaration1
Be7in
,e6ec-table statements1
E6ceptionJ
,e6ception handlers1
end2
8/13/2019 51393086-dbms-lab
53/65
AIM:
To write a P"8&?" bloc; -sin7 4-nction to chec; whether the
8/13/2019 51393086-dbms-lab
54/65
a 3archar),12b n-mber2be7ina:KLa2
b:Kitems,a12dbms_o-tp-t5p-t_line,the 3al-es ret-rned is \\ b12end2
SAMPLE INPUT OUTPUT:
&?"+8Enter 3al-e 4or a: i)$old : a:KLa2new : a:Ki)$2the 3al-es ret-rned is RP"8&?" proced-re s-ccess4-ll/ completed5
&?"+ select 9 4rom item4ile2
IC(DE IDE&C P_CAT'(% ?_#AND E_"EVE" MAO_"EVE" I_ATE.......... .......... ............... .......... .......... .......... ............ ....... ...........
i)$ n-ts spares ! $)
i)) bolts spares )! $) $5
i)! holders spares $) Q $$)
i) co3ers accessories )! $ !
i) panels accessories )! $ !
i) brac;ets spares )! Q ) $) rows selected5
8/13/2019 51393086-dbms-lab
55/65
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 4-nction is e6ec-ted and 3eri4ied
FUNCTION
AIM:
To write a P"8&?" bloc; -sin7 4-nction to 4ind the 7rade o4 the st-dent -sin7c-rsors5
PROGRAM:
&?"+ create or replace 4-nction 7rade,m$ n-mberm) n-mberm n-mber1ret-rn char is
be7ini4 m$+ and m)+ and m+ thenret-rn PA&&2elseret-rn AI"2end i42end2
8/13/2019 51393086-dbms-lab
56/65
&?"+ 8-nction created5
&?"+ declarec-rsor c is select idm$m)m 4rom st-dent2a cSrowt/pe2
be7inopen c2loop4etch c into a2e6it when cSnot4o-nd2-pdate st-dent set 7radeK7rade,a5m$a5m)a5m1 where idKa5id2end loop2commit2close c2
end2
SAMPLE INPUT OUTPUT:
&?"+8P"8&?" proced-re s-ccess4-ll/ completed5&?"+ select 9 4rom st-dent2
ID M$ M) M T(TA" 'ADE.................................................................cs$ ! R $cs) R Q ))cs ! $cs! Q Q ))cs ! ! $
&?"+ select 9 4rom st-dent2
ID M$ M) M T(TA" 'ADE................................................................cs$ ! R $ AI"cs) R Q )) PA&&cs ! AI"cs! Q Q )) PA&&
8/13/2019 51393086-dbms-lab
57/65
cs ! ! $ AI"
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 4-nction is e6ec-ted and 3eri4ied
PROCEDURE
A proced-re is a s-bpro7ram that per4orms a speci4ic action5
SYNTA>:
Create or replace proced-re *proc_name+parameter listJis*local declaration+Be7in,e6ec-table statements1e6ceptionJ ,e6ception handlers1End2
NOTE:
hile declarin7 3ariables in the declarati3e part o4 the proced-re bod/we sho-ldnot speci4/ the width o4 the datat/pe5E75Proced-re pro,name char,!11 isBe7in,set o4 statements12End2
In the abo3e e6amplechar,!1 sho-ld be replaced b/ char5
SYNTA> TO E>ECUTE:
&
8/13/2019 51393086-dbms-lab
58/65
AIM:
To write a P"8&?" bloc; to chec; whether the
8/13/2019 51393086-dbms-lab
59/65
&?"+ select 9 4rom item4ile2
ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE"ITEMATE.......... .......... .......... .......... .......... .......... ..........$ eraser stationar/ $ ) ) chain 4anc/ $ R ) ) tomato 3e7etable ! ! $Q ! pen stationar/
&?"+ e6ec item4iles,!12
P"8&?" proced-re s-ccess4-ll/ completed5
&?"+ select 9 4rom item4ile2
ITEMC(DE ITEMC"(&E PDT_CT'% ?T%_#AND E"EVE" MAO"EVE"ITEMATE.......... .......... .......... .......... .......... .......... ..........$ eraser stationar/ $ ) ) chain 4anc/ $ R ) ) tomato 3e7etable ! ! $Q
! pen stationar/
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 proced-re is e6ec-ted and 3eri4ied5
PROCEDURE
AIM:
8/13/2019 51393086-dbms-lab
60/65
To write a P"8&?" bloc; to chec; whether the
8/13/2019 51393086-dbms-lab
61/65
be7initem4iles,ab12dbms_o-tp-t5p-t_line,The 3al-e o4 b is :\\to_char,b112end2&?"+8Enter 3al-e 4or a: )old ): a n-mber:KLa2
new ): a n-mber:K)2The 3al-e o4 b is :$
P"8&?" proced-re s-ccess4-ll/ completed5
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 proced-re is e6ec-ted and 3eri4ied5
PAC=AGES
Pa%a5e S'e%&;&%at&on:
S"nta#:
Create pac;a7e *pac;a7e_name+ is *declaration+Be7in,e6ec-table statements1End pac;a7e_nameJ
Pa%a5e Bo"
S"nta#:
Create pac;a7e bod/ *pac;a7e_name+ is declaration+Be7in,e6ec-table statements1End bod/_nameJ2
8/13/2019 51393086-dbms-lab
62/65
AIM:
To write a P"8&?" pac;a7e that contains a proced-re and 4-nctions to chec;whether the 7i3en n-mber is a prime or not2 odd or e3en and positi3e or ne7ati3e5
PROGRAM:
Pa%a5e S'e%&;&%at&on:
create or replace pac;a7e pac;s isproced-re displa/24-nction prime,n n-mber1ret-rn 3archar24-nction odd,n n-mber1ret-rn 3archar24-nction positi3e,n n-mber1ret-rn 3archar2end pac;s2
SAMPLE INPUT OUTPUT:&?"+8Pac;a7e created5
Pa%a5e Bo"
create or replace pac;a7e bod/ pac;s asproced-re displa/ is
8/13/2019 51393086-dbms-lab
63/65
a 3archar,$12m n-mber2
be7inm:KLm2a:Kprime,m12i4 aKprime thendbms_o-tp-t5p-t_line,m \\ is a prime12
elsedbms_o-tp-t5p-t_line,m \\ is not prime12end i42a:Kodd,m12i4 aKodd thendbms_o-tp-t5p-t_line,m \\ is odd12elsedbms_o-tp-t5p-t_line,m \\ is e3en12end i42
a:Kpositi3e,m12i4 aKpositi3e thendbms_o-tp-t5p-t_line,m \\ is a positi3e n-mber12elsedbms_o-tp-t5p-t_line,m \\ is a ne7ati3e n-mber12end i42end displa/24-nction prime,n n-mber1ret-rn 3archar isi n-mber24la7 n-mber2
be7in4la7:K24or i in )55n8)loopi4 mod,ni1K then4la7:K$2ret-rn Not prime2end i42end loop2
i4 4la7K thenret-rn prime2end i42end prime24-nction odd,n n-mber1ret-rn 3archar is
be7ini4 mod,n)1K thenret-rn e3en2else
8/13/2019 51393086-dbms-lab
64/65
ret-rn odd2end i42end odd24-nction positi3e,n n-mber1ret-rn 3archar is
be7ini4 n+ thenret-rn positi3e2
elseret-rn ne7ati3e2end i42end positi3e2end pac;s2
SAMPLE INPUT OUTPUT:
&?"+ 8Enter 3al-e 4or m: Q
old : m:KLm2new : m:KQ2
Pac;a7e bod/ created5
&?"+ e6ec pac;s5displa/2Qis a primeQis oddQis a positi3e n-mber
P"8&?" proced-re s-ccess4-ll/ completed5
RESULT:
Th-s the P"8&?" bloc; pro7ram -sin7 pac;a7e is e6ec-ted and 3eri4ied5
8/13/2019 51393086-dbms-lab
65/65