Sie sind auf Seite 1von 28

Work With Structured Query Language

Day 21
Structured Query Language
Overview:
This module enables the student to understand the concept SQL.
Outline:
Topics covered,
SQL/400
reation o! Table
"pdatin# o! Table
Deletion o! Table
$mbedded SQL
Objective:
%y the end o! this module, the student should be able to&
"nderstand the concept o! SQL/400
"nderstand the concept o! DDL
"nderstand the concept o! D'L
( Du)eStar Technolo#ies *vt. Ltd.
1/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Structured Query Language:
Structured Query Lan#ua#e ,SQL- is an industry.standard lan#ua#e !or de/nin# and
manipulatin# data contained in a relational database. 0t is used !or all tas)s to relational
data mana#ement. 1or e2ample& creatin# tables, maintain tables, 3ueryin# tables !or
in!ormation, modi!yin# the data, #rantin# privile#es to users, and so on.
SQL includes Data De/nition Lan#ua#e ,DDL- statements to retrieve and update the
contents o! a database
Characteristics of SQL
o $2tremely 4e2ible
o 1ree !orm synta2
o $ach SQL statement is parsed by the 5D%'S be!ore e2ecution, to chec)
!or synta2 and to optimi6e the re3uest.
o SQL statements can be 7ritten in variety o! 7ays 8 e.#. Dynamic
e2ecution, Static e2ecution embedded in an 9LL pro#ram.
Dynamic e2ecution o! an SQL statement means that you construct a statement as a te2t
strin# and have the SQL routines interpret and e2ecute the te2t strin#. Thus the structure
o! the statement is chan#eable.
:erity
;n alternative 7ay o! e2ecutin# SQL statements )no7n as static e2ecution lets you code
9LL variables 7ithin an SQL statement, thus providin# the ability to chan#e some o! the
selection bet7een subse3uent e2ecution o! the same statements. <ith static statement,
a statement=s structure al7ays remains the same. >nly thin# chan#es are values o! the
pro#ram variables.
Advantages of SQL
o <hile usin# SQL, the pro#rammin# can speci!y 7hat data is needed but
need not speci!y ho7 to retrieve it. That is, SQL is coded 7ithout data
navi#ational instructions.
o 0ncreased acceptance and availability o! SQL enable end users to deal 7ith
di?erent Database 'ana#ement Systems includin# >racle, 'icroso!t SQL
server, and D%2 on other plat!orms ,e.#. 0%' 'ain!rames and <indo7s
@T-.
o The application 7ritten in SQL can be easily ported across Database
'ana#ement Systems.
o SQL is independent in a 7ay that it is implemented internally.
o Thou#h the lan#ua#e is simple to use, it can handle comple2 3ueries.
o The lan#ua#e has stron# theoretical base, and there is no ambi#uity about
the 7ay a 3uery 7ill interpret the data and produce the result.
o SQL can be used to de/ne data structure control access to the data and
delete, insert and modi!y occurrences o! the data. 0t is not merely a 3uery
lan#ua#e.
( Du)eStar Technolo#ies *vt. Ltd.
2/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
o SQL has set-at-a-time !eature. That is one SQL statement a?ects more
than one record.
DDL: Data Denition Language
!ables
Tables are the basic building blocks of any D!"S. # table consists of a row of
column headings and rows $records% of data. &e can create, modify and delete
tables using data definition language $DDL%.
o C"#A!# !A$L#: reate table command creates a ne7 table.
Synta%: 5$;T$ T;%L$ ATable @ameB,1ield1, 1ield2,..-
e&g& C"#A!# !A$L# #'(LO)## *#'(+O ,+!#-#". #'(+' C/A"*0122
o AL!#" !A$L#: ;lter table command modi/es structure o! a table. 0t allo7s
user to add ne7 /eld or to modi!y e2istin# /led.
o D"O( !A$L#: Drop table command deletes a table. ;ll dependent obCects
includin# vie7s, inde2es and !orei#n )ey constraints de/ned in other base
tables that re!erence the table bein# dropped are also deleted .
D'L: Data 'ani3ulation Language
The !our main D'L statements are&
,+S#"!: Dou can use the 0@S$5T statement to add ne7 ro7s to a table or vie7
one o! the !ollo7in# 7ays&
o Speci!yin# values in the 0@S$5T statement !or columns o! the sin#le ro7 to
be added.
o Speci!yin# proper synta2 to add multiple ro7s into the tar#et table. ; multi
ro7 insert copies data !rom one table to another. ;lthou#h the 0nsert
statement can add ro7s to only a sin#le table, the inserted ro7s can be
constructed !rom more than one table.
( Du)eStar Technolo#ies *vt. Ltd.
E/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Synta%: ;LT$5 T;%L$ ATable @ameB,1ield1, 1ield2,..-
e&g& AL!#" !A$L# #'(LO)## ADD *SALA") ,+!#-#"2
Synta%: D5>* T;%L$ ATable @ameB
e&g& D5>* T;%L$ $'*L>D$$
Synta%: 0@S$5T 0@T>ATable @ameB:;L"$S,:alue1,:alue2F.-
or 0@S$5T 0@T> ATable @ameB ,1ield1, 1ield2,F- :;L"$S ,:alue1,:alue2,F-
e.#. 0@S$5T 0@T> $'*L>D$$ ,$'*@>,$'*@',S;L;5D- :;L"$S ,1001,
GS;@H;D=,.=20000=-
Work With Structured Query Language
Day 21
0! SQL /nds an error 7hile runnin# the 0@S$5T statement, it stops insertin# data.
4(DA!#: "pdate command is used to modi!y data in e2istin# table. 0t can a?ect
more than one record at a time. <ith the "*D;T$ statement, you can chan#e the
value o! one or more columns in each ro7 that satis/es the search condition o!
the <9$5$ clause. The result o! the "*D;T$ statement is one or more chan#ed
column values in 6ero or more ro7s o! a table ,dependin# on ho7 many ro7s
satis!y the search condition speci/ed in the <9$5$ clause-. "se the S$T clause to
speci!y a ne7 value !or each column you 7ant to update. The S$T clause names
the columns you 7ant updated and provides the values you 7ant them chan#ed
to. The value you speci!y can be&
o A colu5n na5e: 5eplace the column=s current value 7ith the contents o!
another column in the same ro7.
o A constant: 5eplace the column=s current value 7ith the value provided
in the S$T clause.
o A null value: 5eplace the column=s current value 7ith null value, usin#
the )ey7ord @"LL. The column must be de/ned as capable o! containin# a
null value 7hen table is created or an error occurs.
o A host variable: 5eplace the column=s current value 7ith the contents o!
a host variable.
o An e%3ression: 5eplace the column=s current value 7ith the value that
results !rom an e2pression !rom an e2pression. The e2pression can contain
any o! the values in this list.
To identi!y the ro7s to be updated, use the <9$5$ clause&
o To update a sin#le ro7, use a <9$5$ clause that selects only one ro7.
o To update several ro7s, use a <9$5$ clause that selects only the ro7s you
7ant to update.
Dou can omit the <9$5$ clause. 0! you do, SQL updates each ro7 in the table or vie7
7ith the values you supply.
D#L#!#: Delete command deletes e2istin# record/s !rom a table. The result o!
the D$L$T$ statement is the removal o! 6ero or more ro7s o! a table ,dependin#
on ho7 many ro7s satis!y the search condition speci/ed in the <9$5$ clause-. 0!
you omit the <9$5$ clause !rom a D$L$T statement, SQL removes all the ro7s o!
the table.
( Du)eStar Technolo#ies *vt. Ltd.
4/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Synta%: 0@S$5T 0@T>ATable @ameB15>',Table @ameB
or 0@S$5T 0@T> ATable @ameB ,1ield1, 1ield2,F- S$L$T ,/eld1,1ield2,F-
e.#. 0@S$5T 0@T> $'*L>D$$ ,$'*@>,$'*@',S;L;5D- S$L$T $'*@>, $'*@',
S;L;5D 15>' >LD10L$
Syntax: UPDATE<Table Name>SET <Field Name>=<Value> WHERE <Field
Name>=<Condition>
e.g. UPDATE, EMP!"EE SET SAAR"=##$$$ WHERE EMPN!=%$$%
Work With Structured Query Language
Day 21
S#L#C!: Simple S$L$T statement retrieves records !rom a database. 5etrieval
an be !rom more than one table at the same time. GS$L$T= statement is po7er!ul
and can ta)e 3uite comple2 !orms. 'asterin# it is the )ey to usin# SQL
success!ully.
Select statement is composed o! three main clauses&
o The S$L$T clause 7hich speci/es those columns containin# the desired
data. Dou can speci!y that only column be retrieved, or as many as +000
columns. The value o! each column you name is retrieved in the order
speci/ed in the S$L$T clause. 0! you 7ant to retrieve all columns ,in the
same order as they appear in the ro7-, use an asteris) ,I- instead o!
namin# the columns.
o The 15>' clause, 7hich speci/es the table or tables containin# the
columns 7ith the desired data.
o The <9$5$ clause, 7hich supplies conditions that determine 7hich ro7s
o! data are retrieved.
SQL allo7s you to #et in!ormation !rom columns contained in more than one table. This
operation is called a Coin operation. 0n SQL, a Coin operation is speci/ed by placin# the
names o! those tables you 7ant to Coin to#ether into the same 15>' clause o! a S$L$T
statement. <henever the same column name e2ists in t7o or more tables bein# Coined,
the column name must be 3uali/ed by the table name to speci!y 7hich column is really
bein# re!erenced.
<hen SQL evaluates a select statement, several ro7s mi#ht 3uali!y to be in the result
table, dependin# on the number o! ro7s that satis!y the select.statement=s search
condition. Some o! the ro7s in the result table mi#ht be duplicates. Dou can speci!y that
you do not 7ant any duplicates by usin# the D0ST0@T )ey7ord, !ollo7ed by the list os
column names. D0ST0@T means you 7ant to select only the uni3ue ro7s.
(redicates
;n SQL predicate is a lo#ical condition that is true, !alse or un)no7n !or a #iven ro7.
; predicate is un)no7n i! it involves a comparison and one or both o! the values
bein# compared is null. ; Select statement=s result table contains only those ro7s !or
7hich the <9$5$ clause search condition is true. 0! the search condition is !alse or
un)no7n, the ro7 is omitted.
W/#"# clause
<9$5$ clause is used in S$L$T statement to retrieve records, 7hich satis!y a #iven
condition.
( Du)eStar Technolo#ies *vt. Ltd.
J/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Syntax: DEETE FR!M <Table Name>WHERE<Field>=<Condition>
e.g. DEETE FR!M EMP!"EE WHERE EMPN! = %$$%
Syntax: SEECT&'( <Field i)t>FR!M<Table Name>
Asterisk (*) will display all the fields (columns) in that table.
e.g. SEECT 'FR!M EMP!"EE
o* SEECT EMPN!, EMPNM FR!M EMP!"EE
Syntax: SEECT FR!M <Table Name>WHERE<Condition>
e.g. SEECT FR!M EMP!"EE WHERE EMPN! = $%$%
Work With Structured Query Language
Day 21
The simplest <9$5$ clause contains an SQL search condition that is a sin#le SQL
predicate, as above.
; <9$5$ clause 7ith a primary )ey column lets you use SQL !or sin#le ro7 operations.
The <9$5$ clause also may contain a search condition that is t7o or more SQL
predicates connected 7ith G;@D= or G>5=.
o 'ulti3le Search Conditions within a W/#"# Clause:
Dou sa7 ho7 to 3uali!y a re3uest usin# one search condition. Dou can 3uali!y your
re3uest !urther by codin# a search condition that includes several predicates. The search
condition, is the ne#ated value o! the speci/ed search condition. ; <9$5$ clause can
have as many predicates as you 7ant.
A+D says that, !or a ro7 to 3uali!y, the ro7 must satis!y both predicates o! the search
condition. 1or e2ample to /nd out 7hich employees in department D21 7ere hired a!ter
December E1,1K+L, you 7ould speci!y&
<9$5$ <>5MD$*TN=D21=;@D 905$D;T$B=1K+L.12.E1=
O" says that, !or a ro7 to 3uali!y, the ro7 can satis!y the condition set by either or both
predicates o! the search condition. 1or e2ample , to /nd out 7hich employees are in
either department 01 or D11, you could speci!y ,1-&
<9$5$ <>5M*%01"01D N=01= >5 <>5MD$*TN=D11=
+O! says that, to 3uali!y, a ro7 must not meet the criteria set by the search condition or
predicate that !ollo7s the @>T. 1or e2ample, to /nd all employees in department $11
e2cept those 7ith a Cob code e3ual to analyst, you could speci!y &
<9$5$ <>5MD$*TN=$11= ;@D @>T H>%N=;@;LDST=
<hen SQL evaluates search conditions that contain these connectors, it does so in a
speci/c order. SQL /rst evaluates the @>T clauses, ne2t evaluates the ;@D clauses, and
then the >5 clauses. Dou can chan#e the order o! evaluation by usin# parentheses. The
search conditions enclosed in parentheses are evaluated /rst. 1or e2ample, to select all
employees in departments $11 and $21 7ho have education levels #reater than 12,you
could speci!y&
W/#"# #DL#6#L780 A+D
*WO"9D#(!:;#88; O" WO"9D#(!:;#80;2
The parentheses determine the meanin# o! the search condition. 0n this e2ample, you
7ant all ro7s that have a &
<>5MD$*T value o! $11 or $12, and
$DL$:$L value #reater than 12
0! you did not use parentheses&
<9$5$ $DL$:$L B 12 ;@D <>5MD$*TN=$11= >5 <>5MD$*TN=$21=
Dour result is di?erent. The selected ro7s are ro7s that have&
<>5MD$*T N$11 and $DL$:$L.12, or
<>5MD$*TN$21, re#ardless o! the $DL$:$L value
,1- Dou could also use 0@ to speci!y this re3uest& <9$5$ <>5MD$*T 0@ ,G01=. GD11-.
9eywords for 4se in Search Conditions
( Du)eStar Technolo#ies *vt. Ltd.
O/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
; search condition can contain any o! the )ey7ords %$T<$$@, ;@D, F, 0@, 0S @"LL, and
L0M$.
$#!W##+ &&A+D&&is used to speci!y a search condition that is satis/ed by any value
that !alls on or bet7een t7o other values. 1or e2ample. To /nd all employees 7ho 7ere
hired in 1K+L, you could use this&
F<9$5$ 905$D;T$.BN,=1K+L.01.01=;@D= 1K+L.12.E1=
,+ says you are interested in ro7s in 7hich the value o! the speci/ed e2pression is
amon# the values you listed. 1ort e2ample, to /nd the names o! all employees in
departments ;00, 01, and $21,you could speci!y&
F<9$5$ <>5MD$*T 0@ ,G;00=, G01=, G$21=-
L,9# says you are interested in ro7s in 7hich column value is similar to the value you
supply. The value on the le!t is an e2pression o! character or #raphic data. The value on
the ri#ht must be character data i! the e2pression on strin#, or host variable on, the ri#ht
must be #raphic. 0t can be a #raphic strin# or a #raphic host variable.
<hen you use L0M$, SQL searches !or a character strin# similar to the one you speci!y.
The de#ree o! similarity is determined by special character P used in the strin# that you
include in the search condition&
P. ; percent si#n stands !or an un)no7n strin# o! 0 or more characters. 0! the
percent si#n starts the search strin#, then SQL allo7s 0 or more character,s- to precede
the matchin#, value in the column. >ther7ise, the search strin# must be#in in the /rst
position o! the column.
F.<9$5$ 0TD L0M$ G@aP=.
/ow to work with SQL:
<rite command ST5SQL on command line and press enter as #iven belo7&
Wo*+ ,it- Membe*) U)in. PDM S/01#234

File 5 5 5 5 5 5 M"S6
ib*a*7 5 5 5 5 P4$%U$%S Po)ition to 5 5 5 5 5

T78e o8tion), 8*e)) Ente*5
#=Edit 1=Co87 9=Delete 0=Di)8la7 /=P*int 2=Rename
3=Di)8la7 de):*i8tion ;=Sa<e %1=C-an.e te=t %9=Com8ile %0=C*eate module555

!8t Membe* T78e Te=t
>No *e:o*d) in ?ile@

Pa*amete*) o* :ommand
===> STRS6
F1=E=it F9=P*om8t F0=Re?*e)- F/=C*eate
F;=Ret*ie<e F%$=Command ent*7 F#1=Mo*e o8tion) F#9=Mo*e +e7)

( Du)eStar Technolo#ies *vt. Ltd.
L/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21

Ente* S6 Statement)

T78e S6 )tatement, 8*e)) Ente*5
> *ollba:+ d%
To+en D% ,a) not <alid5 Valid to+en)A W!RB H!D5
> *ollba:+ d%
To+en D% ,a) not <alid5 Valid to+en)A W!RB H!D5
> *ollba:+ d%
To+en D% ,a) not <alid5 Valid to+en)A W!RB H!D5
> *ollba:+ d%
To+en D% ,a) not <alid5 Valid to+en)A W!RB H!D5
Se))ion ,a) )a<ed and )ta*ted a.ain5
Cu**ent :onne:tion i) to *elational databa)e S/01#2345
> SEECT
P*om8tin. ,a) :an:elled b7 t-e u)e*5
===> SELECT




4ottom
F1=E=it F9=P*om8t F/=Cn)e*t line F;=Ret*ie<e F%$=Co87 line
F%#=Can:el F%1=Se*<i:e) F#9=Mo*e +e7)

*5$SS <=
S8e:i?7 SEECT Statement

T78e SEECT )tatement in?o*mation5 P*e)) F9 ?o* a li)t5

FR!M ?ile) 5 5 5 5 5 5 5 5 5 PB01U01D/EMP
SEECT ?ield) 5 5 5 5 5 5 5 *
WHERE :ondition) 5 5 5 5 5 5
DR!UP 4" ?ield) 5 5 5 5 5 5
HAVCND :ondition) 5 5 5 5 5
!RDER 4" ?ield) 5 5 5 5 5 5
F!R UPDATE !F ?ield) 5 5 5 5

4ottom
T78e :-oi:e), 8*e)) Ente*5

DCSTCNCT *e:o*d) in *e)ult ?ile 5 5 5 5 5 5 5 5 N "="e), N=No
UNC!N ,it- anot-e* SEECT 5 5 5 5 5 5 5 5 5 5 5 N "="e), N=No
S8e:i?7 additional o8tion) 5 5 5 5 5 5 5 5 5 5 5 N "="e), N=No



F1=E=it F9=P*om8t F0=Re?*e)- F/=Cn)e*t line F;=S8e:i?7 )ubEue*7
F%$=Co87 line F%#=Can:el F%9=Delete line F%0=S8lit line F#9=Mo*e +e7)

90T $@T$5, <hich sho7s messa#e =Query 5unnin#= and e2ecutes all records in *hysical
1ile.
( Du)eStar Technolo#ies *vt. Ltd.
+/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Di)8la7 Data
Data ,idt- 5 5 5 5 5 5 A %3
Po)ition to line 5 5 5 5 5 S-i?t to :olumn 5 5 5 5 5 5
5555F5555%5555F555
EN! ENM ADDR
%$% A4C NNN
%$# DDD HHH
%$9 BBB RRR
'''''''' End o? data ''''''''












4ottom
F1=E=it F%#=Can:el F%;=e?t F#$=Ri.-t F#%=S8lit


S8e:i?7 SEECT Statement

T78e SEECT )tatement in?o*mation5 P*e)) F9 ?o* a li)t5

FR!M ?ile) 5 5 5 5 5 5 5 5 5 P4$%U$%D (EMP
SEECT ?ield) 5 5 5 5 5 5 5 EN!,ENM
WHERE :ondition) 5 5 5 5 5 5 EN!=%$%
DR!UP 4" ?ield) 5 5 5 5 5 5
HAVCND :ondition) 5 5 5 5 5
!RDER 4" ?ield) 5 5 5 5 5 5
F!R UPDATE !F ?ield) 5 5 5 5

4ottom
T78e :-oi:e), 8*e)) Ente*5

DCSTCNCT *e:o*d) in *e)ult ?ile 5 5 5 5 5 5 5 5 N "="e), N=No
UNC!N ,it- anot-e* SEECT 5 5 5 5 5 5 5 5 5 5 5 N "="e), N=No
S8e:i?7 additional o8tion) 5 5 5 5 5 5 5 5 5 5 5 N "="e), N=No



F1=E=it F9=P*om8t F0=Re?*e)- F/=Cn)e*t line F;=S8e:i?7 )ubEue*7
F%$=Co87 line F%#=Can:el F%9=Delete line F%0=S8lit line F#9=Mo*e +e7)

90T $@T$5 <909 Q0:$S D>" 1>LL><0@Q >"T*"T
( Du)eStar Technolo#ies *vt. Ltd.
K/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Di)8la7 Data
Data ,idt- 5 5 5 5 5 5 A %%
Po)ition to line 5 5 5 5 5 S-i?t to :olumn 5 5 5 5 5 5
5555F5555%5
EN! ENM
%$% A4C
'''''''' End o? data ''''''''














4ottom
F1=E=it F%#=Can:el F%;=e?t F#$=Ri.-t F#%=S8lit

@o7 ho7 to delete record !rom table,
S8e:i?7 DEETE Statement

T78e :-oi:e), 8*e)) Ente*5

File 5 5 5 5 5 5 5 5 5 5 5 EMP Name, F9 ?o* li)t
ib*a*7 5 5 5 5 5 5 5 5 PB01U01D Name, F9 ?o* li)t
Co**elation 5 5 5 5 5 5 5 Name

T78e WHERE :ondition), 8*e)) Ente*5
ENO = 104


4ottom
T78e :-oi:e), 8*e)) Ente*5

WCTH i)olation le<el 5 5 5 1 %=Cu**ent le<el, #=NC >N!NE@
1=UR >CHD@, 9=CS, 0=RS >A@
/=RR



F1=E=it F9=P*om8t F0=Re?*e)- F/=Cn)e*t line F;=S8e:i?7 )ubEue*7
F%$=Co87 line F%#=Can:el F%9=Delete line F%0=S8lit line F#9=Mo*e +e7)

<hich sho7s result as #iven belo7,
( Du)eStar Technolo#ies *vt. Ltd.
10/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Ente* S6 Statement)

T78e S6 )tatement, 8*e)) Ente*5
> SEECT ' FR!M EMP
SEECT )tatement *un :om8lete5
> SEECT ' FR!M EMP(P4$%U$%D
P4$%U$%D in EMP t78e 'FCE not ?ound5
> SEECT ' FR!M P4$%U$%D(EMP
SEECT )tatement *un :om8lete5
> SEECT EN!,ENM FR!M P4$%U$%D(EMP
SEECT )tatement *un :om8lete5
> SEECT EN!,ENM FR!M P4$%U$%D(EMP WHERE EN!=%$%
SEECT )tatement *un :om8lete5
> DELETE FROM PB01U01D/EMP WERE ENO=104
1 !"#$ %e&ete% '!"( EMP )n PB01U01D .
===>




4ottom
F1=E=it F9=P*om8t F/=Cn)e*t line F;=Ret*ie<e F%$=Co87 line
F%#=Can:el F%1=Se*<i:e) F#9=Mo*e +e7)

9o7 to insert a record in *hysical 1ile&
Ente* S6 Statement)

T78e S6 )tatement, 8*e)) Ente*5
> SEECT ' FR!M P4$%U$%D(EMP
SEECT )tatement *un :om8lete5
> SEECT EN!,ENM FR!M P4$%U$%D(EMP
SEECT )tatement *un :om8lete5
> SEECT EN!,ENM FR!M P4$%U$%D(EMP WHERE EN!=%$%
SEECT )tatement *un :om8lete5
> DEETE FR!M P4$%U$%D(EMP WHERE EN!=%$9
% *o,) deleted ?*om EMP in P4$%U$%D 5
Se))ion ,a) )a<ed and )ta*ted a.ain5
Cu**ent :onne:tion i) to *elational databa)e S/01#2345
> DEETE FR!M P4$%U$%D(EMP WHERE eno < %$$ and eno >%$#
Ro, not ?ound ?o* DEETE5
===> CNSERT




4ottom
F1=E=it F9=P*om8t F/=Cn)e*t line F;=Ret*ie<e F%$=Co87 line
F%#=Can:el F%1=Se*<i:e) F#9=Mo*e +e7)

*ress 14&
( Du)eStar Technolo#ies *vt. Ltd.
11/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
S8e:i?7 CNSERT Statement

T78e :-oi:e), 8*e)) Ente*5

CNT! ?ile 5 5 5 5 5 5 5 5 EMP Name, F9 ?o* li)t
ib*a*7 5 5 5 5 5 5 5 5 P4$%U$%D Name, F9 ?o* li)t

Sele:t ?ield) to in)e*t
CNT! 5 5 5 5 5 5 5 5 5 N "="e), N=No

Cn)e*tion met-od 5 5 5 5 % %=Cn8ut VAUES
#=Sub)ele:t

T78e :-oi:e), 8*e)) Ente*5

WCTH i)olation le<el 5 5 % %=Cu**ent le<el, #=NC >N!NE@
1=UR >CHD@, 9=CS, 0=RS >A@
/=RR



F1=E=it F9=P*om8t F0=Re?*e)- F%#=Can:el F#$=Di)8la7 enti*e name
F#%=Di)8la7 )tatement

*5$SS $@T$5
S8e:i?7 CNSERT Statement

T78e <alue) to in)e*t, 8*e)) Ente*5

Field Value
EN! %$1
ENM GDUBEH
ADDR GPUNEH












4ottom
F1=E=it F0=Re?*e)- F/=Cn)e*t line F%$=Co87 line F%%=Di)8la7 t78e
F%#=Can:el F%9=Delete line F%0=S8lit line F#9=Mo*e +e7)

90T $@T$5
( Du)eStar Technolo#ies *vt. Ltd.
12/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Ente* S6 Statement)

T78e S6 )tatement, 8*e)) Ente*5
> CNSERT CNT! P4$%U$%D(EMP VAUES>%$1, WWW, RRR@
Column RRR not in )8e:i?ied table)5
> CNSERT CNT! P4$%U$%D(EMP VAUES>%$1, WWW, RRR@
Column RRR not in )8e:i?ied table)5
> CNSERT CNT! P4$%U$%D(EMP VAUES>%$1, WWW@
Column WWW not in )8e:i?ied table)5
> CNSERT CNT! P4$%U$%D(EMP VAUES>%$1@
Statement :ontain) ,*on. numbe* o? <alue)5
> *NSERT *NTO PB01U01D/EMP +,LUES-10./ 0RR0/ 0PP01
1 !"#$ )n$e!te% )n EMP )n PB01U01D .
2 *NSERT *NTO PB01U01D/EMP +,LUES-104/ 0DU3E0/ 0PUNE01
1 !"#$ )n$e!te% )n EMP )n PB01U01D .
===2




4ottom
F1=E=it F9=P*om8t F/=Cn)e*t line F;=Ret*ie<e F%$=Co87 line
F%#=Can:el F%1=Se*<i:e) F#9=Mo*e +e7)

hec) that above record is inserted in $'*, use select statement on command line to
chec) it,
>S#L#C! ? <"O' ($18418D@#'(;
1ollo7in# is the output
Di)8la7 Data
Data ,idt- 5 5 5 5 5 5 A %3
Po)ition to line 5 5 5 5 5 S-i?t to :olumn 5 5 5 5 5 5
5555F5555%5555F555
EN! ENM ADDR
%$% A4C NNN
%$# DDD HHH
%$1 RR PP
104 DU3E PUNE
'''''''' End o? data ''''''''











4ottom
F1=E=it F%#=Can:el F%;=e?t F#$=Ri.-t F#%=S8lit

9o7 to update record !rom *hysical /le,
( Du)eStar Technolo#ies *vt. Ltd.
1E/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Ente* S6 Statement)

T78e S6 )tatement, 8*e)) Ente*5
> RUN6R"
To+en RUN6R" ,a) not <alid5 Valid to+en)A > END SET CA DR!P FREE H
> RUN6UR"
To+en RUN6UR" ,a) not <alid5 Valid to+en)A > END SET CA DR!P FREE
> RUN6R"
To+en RUN6R" ,a) not <alid5 Valid to+en)A > END SET CA DR!P FREE H
> SEECT ' FR!M P4$%U$%D(EMP
SEECT )tatement *un :om8lete5
> UPDATE P4$%U$%D(EMP SET ENM = ISSSI WHERE EN! =%$9
% *o,) u8dated in EMP in P4$%U$%D 5
> UPDATE
P*om8tin. ,a) :an:elled b7 t-e u)e*5
===> UPDATE




4ottom
F1=E=it F9=P*om8t F/=Cn)e*t line F;=Ret*ie<e F%$=Co87 line
F%#=Can:el F%1=Se*<i:e) F#9=Mo*e +e7)


*5$SS 14
S8e:i?7 UPDATE Statement

T78e :-oi:e), 8*e)) Ente*5

File 5 5 5 5 5 5 5 5 5 EMP Name, F9 ?o* li)t
ib*a*7 5 5 5 5 5 5 P4$%U$%D Name, F9 ?o* li)t

Co**elation 5 5 5 5 5 Name













F1=E=it F9=P*om8t F0=Re?*e)- F%#=Can:el F#$=Di)8la7 enti*e name
F#%=Di)8la7 )tatement

*5$SS $@T$5
( Du)eStar Technolo#ies *vt. Ltd.
14/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
S8e:i?7 UPDATE Statement

T78e :-oi:e), 8*e)) Ente*5

File 5 5 5 5 5 5 5 5 5 EMP Name, F9 ?o* li)t
ib*a*7 5 5 5 5 5 5 P4$%U$%D Name, F9 ?o* li)t

Co**elation 5 5 5 5 5 Name


T78e in?o*mation, 8*e)) Ente*5

Field Value
EN!
ENM 0TTT0
ADDR




4ottom
F1=E=it F9=P*om8t F0=Re?*e)- F/=Cn)e*t line F%$=Co87 line
F%%=Di)8la7 t78e F%#=Can:el F%9=Delete line F#9=Mo*e +e7)

90T $@T$5
S8e:i?7 UPDATE Statement

T78e WHERE :ondition), 8*e)) Ente*5 P*e)) F9 ?o* a li)t5
ENO = 104




4ottom
T78e :-oi:e), 8*e)) Ente*5

WCTH i)olation le<el 5 5 5 % %=Cu**ent le<el, #=NC >N!NE@
1=UR >CHD@, 9=CS, 0=RS >A@
/=RR







F1=E=it F9=P*om8t F0=Re?*e)- F/=Cn)e*t line F;=S8e:i?7 )ubEue*7
F%$=Co87 line F%#=Can:el F%9=Delete line F%0=S8lit line F#9=Mo*e +e7)

*5$SS $@T$5
( Du)eStar Technolo#ies *vt. Ltd.
1J/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Ente* S6 Statement)

T78e S6 )tatement, 8*e)) Ente*5
> RUN6R"
To+en RUN6R" ,a) not <alid5 Valid to+en)A > END SET CA DR!P FREE H
> SEECT ' FR!M P4$%U$%D(EMP
SEECT )tatement *un :om8lete5
> UPDATE P4$%U$%D(EMP SET ENM = ISSSI WHERE EN! =%$9
% *o,) u8dated in EMP in P4$%U$%D 5
2 UPD,TE PB01U01D/EMP SET ENM = 0TTT0 WERE ENO = 104
1 !"#$ 45%ate% )n EMP )n PB01U01D .
===>





4ottom
F1=E=it F9=P*om8t F/=Cn)e*t line F;=Ret*ie<e F%$=Co87 line
F%#=Can:el F%1=Se*<i:e) F#9=Mo*e +e7)

S$$ T9$ >"T*"T "S0@Q S$L$T ST;T$'$@T.
,n last section how to create a table with constraints in SQL.
!y3es of constraints:
o Do5ain integrity constraints: These constraints set a ran#e and any
violation that ta)es place 7ill prevent the user per!ormin# the
manipulation. There are t7o types o! Domain inte#rity constraints.
+ot +ull: This constraint does not allo7 null values in a /eld or set o!
/elds.
Check: These constraints allo7 only particular set o! values. $very
record should satis!y the condition de/ned in hec) constraint.
o #ntity integrity constraints: $ntity is any data stored in a database.
$ach entity represents a table in a database and each record in that table
represents instance o! that entity. %y usin# entity inte#rity constraints,
each record in the table can be uni3uely de/ned. There are t7o types o!
$ntity inte#rity constraints.
( Du)eStar Technolo#ies *vt. Ltd.
1O/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Syntax: CREATE TA4E <Table Name>><Field><Data T78e>N!T NU, <Field
#><Data T78e> N!T NU@
e.g. CREATE TA4E EMP!"EE >EMPN! CNTEDER N!T NU, EMPNM CHAR>#0@
N!T NU@
Syntax: CREATE TA4E <Table Name>><Field><Data T78e>N!T NU, <Field
#><Data T78e> C!NSTRACNT<Con)t*aint Name>CHECB >Cond5@@
e.g. CREATE TA4E EMP!"EE >EMPN! CNTEDER N!T NU, EMPNM CHAR>#0@
N!T NU, SAAR" CNTEDER C!NSTRACNT CSA CHECB>SAAR" 5 %0$$$@@
Work With Structured Query Language
Day 21
4niAue: This constraint does not allo7 duplicate values in a column or
set o! columns. %ut it allo7s null values. This constraint can be de/ned
at both.column as 7ell as ro7 level.
(ri5ary 9ey: This constrain does not allo7 duplicate as 7ell as null values. ; table
an have only one primary )ey. 0! primary )ey is assi#ned to more than one column
then it is said to be composite )ey. *rimary )ey can be de/ned at both.column as
7ell as ro7 level.
o "eferential integrity constraints: These constraints are used to establish
*arent.hild relationship bet7een t7o tables havin# common column. To establish
this relationship, a column in the parent table should be de/ned as primary )ey.
and same column in the child table should be !orei#n )ey. The !orei#n )ey column in
the child should re!er to correspondin# column in the parent table.
( Du)eStar Technolo#ies *vt. Ltd.
1L/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Syntax: CREATE TA4E <Table Name>><Field><Data T78e>N!T NU, <Field
#><Data T78e> C!NSTRACNT<Con)t*aint Name> UNC6UE@
!* CREATE TA4E <Table Name>><Field><Data T78e>N!T NU, <Field
#><Data T78e> C!NSTRACNT<Con)t*aint Name> UNC6UE@><Field%>@@
e.g. CREATE TA4E EMP!"EE >EMPN! CNTEDER C!NSTRACNT CEMP UNC6UE,
EMPNM CHAR>#0@ N!T NU,
!* CREATE TA4E EMP!"EE >EMPN! CNTEDER EMPNM CHAR>#0@ N!T NU
C!NSTRACNT CEMP UNC6UE>EMPN!@@
Syntax: CREATE TA4E <Table Name> ><Field%><Data T78e> N!T NU,
<Field #> <Data T78e> C!NSTRACNT <Con)t*aint Name> PRCMAR" BE"@
!* CREATE TA4E <Table Name>><Field%><Data T78e>N!T NU, <Field
#><Data T78e> C!NSTRACNT<Con)t*aint Name> PRCMAR" BE"@ ><Field%>@@
e.g. CREATE TA4E EMP!"EE >EMPN! CNTEDER C!NSTRACNT CEMP PRCMAR"
BE", EMPNM CHAR>#0@ N!T NU,
!* CREATE TA4E EMP!"EE >EMPN! CNTEDER EMPNM CHAR>#0@ N!T NU
C!NSTRACNT CEMP PRCMAR" BE">EMPN!@@
Syntax: CREATE TA4E <C-ild Table Name> ><Field%><Data T78e> N!T
NU, <Field #> <Data T78e> C!NSTRACNT <Con)t*aint Name>
REFERENCES<Pa*ent Table Name><Field>@>@
!* CREATE TA4E <C-ild Table Name>><Field%><Data T78e>N!T NU,
F!RECDN BE" <Field #><Data T78e> C!NSTRACNT<Con)t*aint Name>
REFERENCES<Pa*ent Table Name ><Field>@>@
e.g. CREATE TA4E EMP!"EE >EMPN! CNTEDER DEPTCD EMPNM CHAR>#0@
C!NSTRACNT CDCD REFERENCES DEPT JMAST >DEPTCD@@
!* CREATE TA4E EMP!"EE >EMPN! CNTEDER F!RECDN BE" DEPTCD CHAR>#0@
C!NSTRACNT CDCD REFERENCES DEPTKMAST >DEPTCD@@
Work With Structured Query Language
Day 21
Dro3 Constraints: onstraints de/ned on a table can be dropped 7ith the help o!
alter table command 7ith Drop onstraint )ey7ord.
Syntax:ATER TA4E <Table Name> DR!P C!NSTRACNT <Con)t*aint Name>
e5.5 ATER TA4E EMP!"EE DR!P C!NSTRACNT CDCD
Ente* S6 Statement)

T78e S6 )tatement, 8*e)) Ente*5
> SEECT ' FR!M P4$%U$%D(EMP
SEECT )tatement *un :om8lete5
> UPDATE P4$%U$%D(EMP SET ENM = ISSSI WHERE EN! =%$9
% *o,) u8dated in EMP in P4$%U$%D 5
> UPDATE
P*om8tin. ,a) :an:elled b7 t-e u)e*5
> UPDATE
P*om8tin. ,a) :an:elled b7 t-e u)e*5
> UPDATE P4$%U$%D(EMP SET ENM = ITTTI WHERE EN! = %$9
% *o,) u8dated in EMP in P4$%U$%D 5

===> CRE,TE




4ottom
F1=E=it F9=P*om8t F/=Cn)e*t line F;=Ret*ie<e F%$=Co87 line
F%#=Can:el F%1=Se*<i:e) F#9=Mo*e +e7)

("#SS <= and select o3tion B&
( Du)eStar Technolo#ies *vt. Ltd.
1+/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Sele:t CREATE Statement

Sele:t one o? t-e ?ollo,in.A

%5 CREATE ACAS
#5 CREATE C!ECTC!N
15 CREATE CNDEL
95 CREATE PR!CEDURE
6. CRE,TE T,BLE
/5 CREATE VCEW









Sele:tion
6

F1=E=it F%#=Can:el

*5$SS $@T$5 ;@D 10LL T9$ 1>LL><0@Q 0@1>5';T0>@.
S8e:i?7 CREATE TA4E Statement

T78e in?o*mation, 8*e)) Ente*5

File 5 5 5 5 5 5 5 5 5 5 DEPT Name
ib*a*7 5 5 5 5 5 5 5 P4$%U$%D Name, F9 ?o* li)t

Null)A %=NU, #=N!T NU, 1=N!T NU WCTH DEFAUT

Field F!R Field T78e en.t- S:ale Null)
DCD NUMERCC 1 $ 1
DNM CHAR 0 1
H!D CHAR 2 1
1
1
1
1
4ottom
File C!NSTRACNT 5 5 5 5 5 5 5 5 5 5 5 5 5 7 "="e), N=No
Di)t*ibuted File 5 5 5 5 5 5 5 5 5 5 5 5 5 N "="e), N=No

F1=E=it F9=P*om8t F0=Re?*e)- F/=Cn)e*t line F%$=Co87 line
F%%=Di)8la7 mo*e att*ibute) F%#=Can:el F%9=Delete line F#9=Mo*e +e7)

*5$SS $@T$5
( Du)eStar Technolo#ies *vt. Ltd.
1K/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
S8e:i?7 CREATE TA4E Statement

T78e :on)t*aint o8tion, 8*e)) Ente*5
%=Add #=C-an.e 9=D*o8

!8t T78e Con)t*aint Field)
%













4ottom
F1=E=it F0=Re?*e)- F%#=Can:el F#$=Di)8la7 enti*e name
F#%=Di)8la7 )tatement
No :on)t*aint) de?ined, 8*e)) Ente* to add a :on)t*aint
*5$SS $@T$5
S8e:i?7 CREATE TA4E Statement

T78e :on)t*aint in?o*mation, 8*e)) Ente*5

T78e 5 5 5 5 5 5 5 5 % %=PRCMAR" BE", #=UNC6UE
1=F!RECDN BE", 9=CHECB
Con)t*aint 5 5 5 5 5 PRCMAR" BE" Name, 4lan+ to .ene*ate














F1=E=it F9=P*om8t F0=Re?*e)- F%#=Can:el F#$=Di)8la7 enti*e name
F#%=Di)8la7 )tatement

*5$SS $@T$5
( Du)eStar Technolo#ies *vt. Ltd.
20/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
S8e:i?7 CREATE TA4E Statement

T78e :on)t*aint in?o*mation, 8*e)) Ente*5

T78e 5 5 5 5 5 5 5 5 % %=PRCMAR" BE", #=UNC6UE
1=F!RECDN BE", 9=CHECB
Con)t*aint 5 5 5 5 5 Name, 4lan+ to .ene*ate
Field) 5 5 5 5 5 5 5 DCD Name, F9 ?o* li)t




4ottom








F1=E=it F9=P*om8t F0=Re?*e)- F/=Cn)e*t line F%$=Co87 line
F%#=Can:el F%9=Delete line F#$=Di)8la7 enti*e name F#9=Mo*e +e7)

*5$SS $@T$5 T> Q$T 1>LL><0@Q S5$$@
S8e:i?7 CREATE TA4E Statement

T78e :on)t*aint o8tion, 8*e)) Ente*5
%=Add #=C-an.e 9=D*o8

!8t T78e Con)t*aint Field)

PR*M,R7 3E7 D*D












4ottom
F1=E=it F0=Re?*e)- F%#=Can:el F#$=Di)8la7 enti*e name
F#%=Di)8la7 )tatement

90T $@T$5
( Du)eStar Technolo#ies *vt. Ltd.
21/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Ente* S6 Statement)

T78e S6 )tatement, 8*e)) Ente*5
> UPDATE P4$%U$%D(EMP SET ENM = ITTTI WHERE EN! = %$9
% *o,) u8dated in EMP in P4$%U$%D5
> CREATE
P*om8tin. ,a) :an:elled b7 t-e u)e*5
2 CRE,TE T,BLE PB01U01D /DEPT -D*D NUMER*C -. / 01 NOT NULL W*T DEF,ULT/
DNM C,R -6 1 NOT NULL W*T DEF,ULT/ OD C,R -8 1 NOT NULL W*T
DEF,ULT/ PR*M,R7 3E7 -D*D11
Ta9&e DEPT )n PB01U01D :!eate% 94t :"4&% n"t 9e ;"4!na&e%5
===>





4ottom
F1=E=it F9=P*om8t F/=Cn)e*t line F;=Ret*ie<e F%$=Co87 line
F%#=Can:el F%1=Se*<i:e) F#9=Mo*e +e7)

@>< 9$M T9$ T;%L$ "S0@Q S$L$T Q"$5D.
0n this 7ay try insert, update and delete command on D$*T1 table.
<unctions
; !unction is an operation denoted by a !unction name !ollo7ed by one or more
operands, 7hich are enclosed in parentheses. The operands o! !unctions are called
ar#uments. 'ost !unctions have a sin#le ar#ument that is speci/ed by an e2pression.
The result o! a !unction is a sin#le value derived by applyin# the !unction to the result
o! the e2pression.
-eneral rules
o 1or ;:Q and S"' !unctions, ar#uments must be o! type numeric.
o $2cept >"@T ,I- the ar#ument may be preceded by the )ey 7ord
D0ST0@T to eliminate duplicate records be!ore the !unction is actually
applied to the selected /eld. The de!ault value is ;LL.
o The !unction >"@T ,I- is used in all records 7ithout any duplicate
elimination and hence D0ST0@T is not applicable to this !unction.
o ;ny @ulls in the selected /eld are eliminated be!ore the !unction is applied
re#ardless o! 7hether D0ST0@T is used or not. %ut in case o! >"@T ,I- all
the @ulls are treated as normal values.
o 0n case o! computers usin# ;S00 character set, the order is Di#its,
"ppercase letters, Lo7ercase letters, 7hile in case o! computers, usin#
$%D0 3uery usin# >5D$5 %D clause, 7ith ';R and '0@ !unctions, can
produce di?erent results in the t7o systems.
Co55on functions
( Du)eStar Technolo#ies *vt. Ltd.
22/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
o CO4+!*2 and CO4+!*?2:>"@T ,- !unction is used to count number o!
values in a /eld, 7hile >"@T ,I- !unction is used to count number o!
records o! the 3uery result.
o S4': S"' !unction is used to calculate sum o! values in a /eld.
o A6-: ;:Q !unction is used to calculate
o 'AC:';R !unction is used to /nd minimum o! value in a /eld.

;bove assi#nment completes the simple command o! SQL/400. 1ollo7in# section
includes the embedded SQL i.e. SQL in 5*Q pro#rammin#.
#5bedded SQL:
"nrestricted dynamic e2ecution o! 0nsert, "pdate or Delete is dan#erous. ;d hoc entry o!
statements can be error prone. 'ore over situations in an application may arise 7here a
sin#le SQL command or a set o! SQL commands alone 7ill not be able to meet the
re3uirements. Some such situations are&
5eadin# data !rom a /le and conditionally usin# it to insert/update ro7s.
*rocessin# data !rom multiple /les to produce a report.
"tili6in# user input to 3uery.
( Du)eStar Technolo#ies *vt. Ltd.
2E/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Syntax: SEECT C!UNT>'(Field Name@ FR!M <Table Name> WHERE <Condition>
DR!UP 4" <Field Name>
e.g. SEECT C!UNT>'@ FR!M EMP!"EE
o* SEECT C!UNT >EMPN!@FR!M EMP!"EE WHERE DEPT=HACCH
o* SEECT C!UNT >EMPN!@ FR!M EMP!"EE DR!UP 4" DEPT5
Syntax: SEECT SUM>Field Name@ FR!M <Table Name> WHERE <Condition> DR!UP 4"
<Field Name>
e.g. SEECT C!UNT >SAAR"@FR!M EMP!"EE WHERE DEPT=HACCH
"! SEECT C!UNT >SAAR"@ FR!M EMP!"EE DR!UP 4" DEPT5
Syntax: SEECT AVD ><Field Name>@ FR!M <Table Name> WHERE <Condition> DR!UP
4" <Field Name>
e.g. SEECT AVD >SAAR"@FR!M EMP!"EE WHERE DEPT=HACCH
"! SEECT AVD >SAAR"@ FR!M EMP!"EE DR!UP 4" DEPT5
Syntax: SEECT MAL ><Field Name>@ FR!M <Table Name> WHERE <Condition> DR!UP
4" <Field Name>
e.g. SEECT MCN >SAAR"@FR!M EMP!"EE WHERE DEPT=HACCH
"! SEECT MCN >SAAR"@ FR!M EMP!"EE DR!UP 4" DEPT5
Work With Structured Query Language
Day 21
Traditional pro#rammin# lan#ua#es are better e3uipped to handle such re3uirements as
they support constructs !or control 4o7. 5eport !ormattin# and screen handlin# etc. that
are not supported by SQL.
G$mbedded SQL= in conventional pro#rammin# lan#ua#es provides SQL statements !rom
9LL& Dou code all embedded SQL statement in spec and be!ore each SQL statementS
you code /$R$ SQL in positions L throu#h 1J. ;!ter each SQL statement you code /$@D.
$R$ in delimiters must have a T in position L.
0n SQL, a pro#ram variable coded in an embedded SQL statement is re!erred to as host
variable. 9ost variables should be declared be!ore the /rst use in SQL and al7ays should
start 7ith a G&= 7ithin a SQL The synta2 o! an SQL statement, 7hen usin# interactive SQL,
di?ers sli#htly !rom the !ormat o! the same statement 7hen it is embedded in a pro#ram.
SQL statements can be embedded in hi#h.level lan#ua#es, dynamically prepared and
run, or run interactively.
1or a static S$L$T statement ,one embedded in an SQL pro#ram-, an 0@T> clause must
be speci/ed be!ore the 15>' clause. The 0@T> clause names the host variables
,variables in your pro#ram used to contain retrieved column values-. The value o! the
/rst result column speci/ed in the S$L$T clause is put into the /rst host variable named
in the 0@T> clauseS the second value is put into the second host variable, and so on.
The select.statement embedded in the 0@S$5T statement is no di?erent !rom the select.
statement you use to retrieve data.
Synta2 !or $mbedded SQL is
/$R$ SQL
................
................
/$@D.$R$
1ollo7in# is the e2ample !or select statement in embedded SQL
(hysical le na5e : $'*
"(- (rogra5 na5e : *'1
Source !y3e : SQL5*Q
Descri3tion : GSimple SQL pro#ram=
( Du)eStar Technolo#ies *vt. Ltd.
24/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
Ste3 8: #'( is (hysical le
Column) 5 5 5 A % 2% Edit P4$%U$%D(M"S6
SEU==> EMP
''''''''''''''' 4e.innin. o? data '''''''''''''''''''''''''''''''''''''
$$$$5$% A R EMPR
$$$%5$$ A EN! 1S $
$$$15$$ A ENM 0A
$$$15$% A ADDR 0A
$$$95$$ A B EN!
'''''''''''''''''' End o? data ''''''''''''''''''''''''''''''''''''''''














Step 2& 0n embedded SQL 5*Q pro#ram !or select statement is #iven belo7.
Column) 5 5 5 A % 2% Edit P4$%U$%D(M"S6
SEU==> PM%
''''''''''''''' 4e.innin. o? data '''''''''''''''''''''''''''''''''''''
$$$%5$$ C MKADD0$ N% 1$
$$$#5$$ C M!VE'4ANB NM 0
$$$#5$% C M!VE'4ANB AD 0
$$$15$$ '
$$$95$$ C/E<EC S=L
$$$05$$ C> SELECT ENM/,DDR *NTO :NM/:,D FROM EMP WERE ENO = 10?
$$$/5$$ C/END@E<EC
$$$25$% C NM DSP"
$$$25$# C AD DSP"
$$$35$$ C SET!N R
'''''''''''''''''' End o? data ''''''''''''''''''''''''''''''''''''''''








0n above e2ample 7e are de/nin# variables @1, @', ;D to move or select values !rom
physical /le and ma)e it displayed usin# simple 5*Q.
4sing a Cursor:
<hen SQL runs a select statement, the resultin# ro7s comprise the result table. ; cursor
provides a 7ay access a result table. 0t is used 7ithin an SQL pro#ram to maintain a
position in the result table. SQL uses a cursor to 7or) 7ith the ro7s in the result table
althou#h each must have a uni3ue name.
( Du)eStar Technolo#ies *vt. Ltd.
2J/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
; D$L;5$ "5S>5 statement to open and close the cursor !or use 7ithin the pro#ram.
The cursor must be opened be!ore any ro7s can be retrieved.
; 1$T9 statement to retrieve ro7s !rom the cursor=s result table or to position the
cursor on another ro7. The step is iterated till all ro7s are !etched and processed.
;n "*D;T$F <9$5$ "55$@T >1 statement to update the current ro7 o! a cursor.
; D$L$T$ F <9$5$ "55$@T >1 statement to delete the current ro7 o! a cursor.
Ste3s for using a cursor:
De/ne the cursor& The D$L;5$ "5S>5 statement names a cursor and speci/es a
select.statement. The select.statement de/nes a set o! ro7s that, conceptually, ma)e up
the result table.
O3en the Cursor: To be#in processin# the ro7s o! the result table, issue the
>*$@ statement. <hen your pro#ram issues the >*$@ statement, SQL processes the
select 8 statement 7ithin the D$L;5$ "5S>5 statement to identi!y a set o! ro7s,
called a result table ,1-, usin# the current value o! any host variables speci/ed in the
select statement.
5etrieve a 5o7 "sin# a ursor& To move the contents o! a selected ro7 into your
pro#ram=s host variables use the 1$T9 statement. The S$L$T statement 7ithin the
D$L;5$ "5S>5 statement identi/es ro7s that contain the column values your
pro#ram 7ants. 9o7ever, SQL does not retrieve any data !or your application pro#ram
until the 1$T9 statement is issued.
<hen your pro#ram issues the 1$T9 statement, SQL uses the current cursor position as
a startin# point to locate the re3uested ro7 in the result table. This chan#es that ro7 to
the current ro7. 0! an 0@T> clause 7as speci/ed, SQL moves the current ro7=s contents
into your pro#ram=s host variables. This se3uence is repeated each time the 1$T9
statement is issued. SQL maintains the position o! the current ro7 ,that is, the cursor
points to the current ro7- until the ne2t 1$T9 statement !or the cursor is issued.
Speci!y <hat to Do <hen $nd. o! 8 Data 0s 5eached& To /nd out 7hen the end o! the
result table is reached, test the SQL>D /eld !or a value o! 100 or test the SQLST;T$
/eld !or a value o! G02000= ,that is, end 8 o! 8 data-. This condition and your pro#ram
issues a subse3uent 1$T9. Dour pro#ram should anticipate an end. o! 8 data condition
( Du)eStar Technolo#ies *vt. Ltd.
2O/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
(ELEC S6
F DECARE :u*)o*Kname CURS!R F!R
FSEECT :olumnK%, :olumnK # N
F FR!M tableKname , N
F WHERE N >o8tional@
(ENDKELEC
(ELEC S6
F !PEN :u*)o* J name
(ENDJELEC
(ELEC S6
F FETCH :u*)o* J name
F CNT! A -o)t <a*iable K% &, A Ho)t <a*iable J #O N
(END J ELEC
Work With Structured Query Language
Day 21
7henever a cursor is used to !etch a ro7, and should be prepared to handle this situation
7hen it occurs.
lose the ursor& 0! you processed the ro7s o! a result table !or a serial cursor, and you
7ant to use the cursor a#ain, issue a L>S$ statement to close the cursor 7hen the Cob
ends.
SQLCOD
The database mana#er sets the SQL>D values a!ter each SQL statement is e2ecuted.
;n application can chec) the SQL>D value to determine 7hether the last SQL
statement 7as success!ul. SQL does not communicate directly 7ith end user but rather
returns error codes to the application pro#ram 7hen an error occurs.
There are many other SQL messa#es, but they are not listed here. Detailed descriptions
o! all D%2 !or >S/400 messa#es, includin# SQL>D$ s, are available on.line and can be
displayed and printed !rom the Display 'essa#e Description display. Dou can access this
display by usin# the L command Display 'essa#e Description ,DS*'SQD-.
SQL>D$ are returned in the SQL; ,SQL communication area- structure. SQLST;T$ is
an additional return code that provides application pro#rams 7ith common return codes
!or common error conditions !ound amon# the 0%' relational, database systems. $very
SQL>D has a correspondin# messa#e in messa#e /le QSQL'SQ in library QSDS.
Status o! an embedded SQL e2ecution is returned in a variable SQL>D.
SQL>D value 0 N success!ul e2ecution
SQL>D value 100 N not !ound
@e#ative SQL>D value N error.
Code related to C4"SO":
Column) 5 5 5 A % 2% Edit P4$%U$%S(M"S6
SEU==> PM1
FMT FL 55555FFilenameCPEAF5555555555C55555555De<i:eF555555BE=itFFEnt*7FA5555U
''''''''''''''' 4e.innin. o? data '''''''''''''''''''''''''''''''''''''
$$$$5$# FDSP% CF E W!RBSTN
$$$$5$1 '
$$$$5$9 C(ELEC S6
$$$$5$0 CF DECARE C% CURS!R F!R
$$$$5$/ CF SEECT EN!,ENM,ADDR FR!M EMP
$$$$5$2 C(ENDKELEC
$$$$5$3 '
$$$$5$; C(ELEC S6
$$$$5%$ CF !PEN C%
$$$$5%% C(ENDKELEC
$$$$5%# '
$$$$5%1 C S6C!D D!WE6$
$$$$5%9 '
$$$$5%0 C(ELEC S6
$$$$5%/ CF FETCH C% CNT! AN!, ANM, AAD
$$$$5%2 C(ENDKELEC
$$$$5%3 '
$$$$5%; C ELFMTREC1
$$$$5#$ C S6C!D CFE6 %$$
$$$$5#% C D!T! A
$$$$5## C ENDCF
$$$$5#1 C ENDD!
$$$$5#9 '
$$$$5#0 C(ELEC S6
$$$$5#/ CF C!SE C%
IIIIIIIIIIIIIIIIII $nd o! data IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
( Du)eStar Technolo#ies *vt. Ltd.
2L/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.
Work With Structured Query Language
Day 21
F1=E=it F9=P*om8t F0=Re?*e)- F%#=Can:el F%1=Ho, to u)e t-i) di)8la7
F#9=Mo*e +e7)

( Du)eStar Technolo#ies *vt. Ltd.
2+/2+
No part of this document can be copied, reproduced in any form, complete or partial without written consent from DukeStar Technologies
Pvt. Ltd.

Das könnte Ihnen auch gefallen