0% found this document useful (0 votes)
90 views

Usman - Malik Leave A Comment

This document provides an overview of ADO.NET and discusses some common interview questions about ADO.NET. ADO.NET is Microsoft's technology for manipulating and accessing data objects from data sources like SQL Server, Oracle, or text files. The document contains questions and answers on topics like what ADO.NET is, important namespaces in ADO.NET, the DataSet object, stored procedures, LINQ, and object pooling. It recommends a course to learn more about using ADO.NET to develop data-centric applications.

Uploaded by

9944843275
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Usman - Malik Leave A Comment

This document provides an overview of ADO.NET and discusses some common interview questions about ADO.NET. ADO.NET is Microsoft's technology for manipulating and accessing data objects from data sources like SQL Server, Oracle, or text files. The document contains questions and answers on topics like what ADO.NET is, important namespaces in ADO.NET, the DataSet object, stored procedures, LINQ, and object pooling. It recommends a course to learn more about using ADO.NET to develop data-centric applications.

Uploaded by

9944843275
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Basic ADO.

NET Interview Questions


June 5, 2014 by usman_malik Leave a Comment
ADO.NE is !i"#oso$t%s latest te"&nolo'y
$o# mani(ulatin' an) a""ess )ata ob*e"ts eit&e# in t&e $o#m o$ a )atabases su"& +,L
+e#ve#, O#a"le o# A""ess, o# 'ene#al )ata sou#"es su"& as te-t $iles, .!L $iles an) ot&e#
$ile st#eams. Anyone lookin' to (u#sue a "a#ee# in !i"#oso$t%s a((li"ation )evelo(ment
te"&nolo'ies, mainly .NE, must &ave some e-(e#ien"e an) kno/le)'e in ADO.NE.
&is a#ti"le "ontains some o$ t&e most $un)amental an) $#e0uently aske) 0uestions in any
.NE1#elate) *ob.
2o# mo#e )etaile) C3 ADO.NE tuto#ials, look at t&is 4)emy."om "ou#se.
Q1: What is ADO.NET?
Answer: ADO.NE stan)s $o# A"tive. Data Ob*e"ts an) is use) to a""ess an)
mani(ulate )ata se#vi"es in !i"#oso$t a((li"ations. ADO.NE inte#a"ts /it& )ata usin'
a )is"onne"te) )ata set mo)el an) is mostly use) $o# a""essin' #elational )atabases.
Q2: What are some of the namesaces in ADO.NET that rovi!e access to !ata?
Answer: &e#e a#e t&#ee ma*o# names(a"es use) $o# inte#a"tin' /it& )ata in ADO.NE
+ystem.Data
+ystem.Data.+,LClient
+ystem.Data.OleDb
Q": What is Data#et O$%ect in ADO.NET?
Answer: &e Data+et ob*e"t is an in1memo#y #e(#esentation o$ a )ata sou#"es su"& as
table #e"o#)s $#om a )atabase. &e Data+et ob*e"t sto#es )ata in t&e $o#m o$ #o/s an)
"olumns /&e#e ea"& #o/ #e(#esents a #e"o#) an) ea"& "olumn #e(#esent a (a#ti"ula# value
o# (#o(e#ty $#om t&at #e"o#).
Q&: What is an ADO.NET Data'ea!er?
Answer: &e Data5ea)e# ob*e"t in ADO.NE is use) to se0uentially #ea) )ata $#om an
un)e#lyin' )ata sou#"e. &e Data5ea)e# ob*e"t (#ovi)es #ea)1only a""ess to )ata an)
"annot be use) $o# DDL an) D!L o(e#ations.
Q(: What is )INQ?
Answer: L6N, stan)s $o# 7Lan'ua'e 6nte'#ate) ,ue#y8 use) $o# 0ue#yin' "olle"tions o$
)ata. L6N, 0ue#ies "an be e-e"ute) on lo"al "olle"tions su"& as ty(es $#om
+ystem.Colle"tions names(a"e an) +ystem.Colle"tions.9ene#i" names(a"e. L6N,
0ue#ies "an also be e-e"ute) ove# L6N, to +,L an) Entity $#ame/o#k "lasses t&at a#e
use) to inte#a"t /it& )atabases an) ot&e# )ata sou#"es t&at )o not e-ist /it&in t&e
a((li"ation )omain, su"& as .!L.
Q*: What is O$%ect +oo,in- in ADO.NET?
Answer: 6n .NE, /&eneve# $un"tionality &as to be (e#$o#me), t&e Class t&at "ontains
t&at $un"tionality is instantiate). &e ob*e"t o$ t&e "lass is "#eate), )esi#e) $un"tionality is
(e#$o#me) an) t&en t&at ob*e"t is )est#oye). &is (#o"ess o$ "#eatin' an) )est#oyin'
ob*e"ts is e-(ensive. 6n ADO.NE ob*e"ts a#e "onsistently "#eate) an) )est#oye) as t&e
a((li"ation is "onne"te) to )atabase, $et"&es #e"o#)s $#om )atabase an) )is"onne"ts $#om
)atabase. &e#e$o#e, t&e ob*e"ts t&at a#e $#e0uently use) a#e (la"e) in a (ool $#om /&e#e
t&ey "an be a""esse) /&eneve# a #elate) $un"tionality nee)s to be (e#$o#me). &is
(&enomenon is kno/n as ob*e"t (oolin'.
6nte#este) in lea#nin' mo#e about ADO.NE /it& C3: ake t&is 4)emy."om "ou#se
Q.: What is ADO.NET DataA!ater?
Answer; o (e#$o#m "ommuni"ation bet/een ADO.NE )ata sou#"es su"& as )atabases
an) .!L $iles an) a Data+et, t&e DataA)a(te# ob*e"t is use). All C54D 7"#eate, #ea),
u()ate an) )elete8 0ue#ies a#e (e#$o#me) usin' t&is DataA)a(te#.
Q/: Brief,0 mention some of the $enefits of ADO.NET !ata access techno,o-0.
Answer: &e $ollo/in' a#e some o$ t&e im(o#tant bene$its o$ usin' ADO.NE $o# )ata
a""ess;
ADO.NE ob*e"ts a#e &i'&ly (#o'#ammable an) a#e $le-ible enou'& to be
t/eake) an) t/iste) to a"&ieve t&e )esi#e) $un"tionality.
Co)e /#itten in ADO.NE is &i'&ly maintainable an) ne/ )ata a""ess $eatu#es
"an be a))e) to e-istin' a((li"ation im(e#"e(tibly.
ADO.NE ob*e"ts a#e sim(le .NE ob*e"ts t&at t#anslate into !+6L "o)e.
&e#e$o#e, ADO.NE "o)e /#itten in one lan'ua'e "an be seamlessly use) /it&
ot&e# .NE lan'ua'es as /ell.
ADO.NE su((o#ts )ata s"alability an) is in)e(en)ent o$ t&e amount o$ )ata
sto#e) in t&e un)e#lyin' )atabase. &e#e$o#e, i$ )ata sou#"e e-(an)s o# s&#inks, t&e
im(a"t on t&e ADO.NE "lasses is minimal.
Q1: Differentiate $etween Bo2in- an! 3n$o2in-
Answer: &e (#o"ess o$ sto#in' a value ty(e into a #e$e#en"e ty(e va#iable o# ob*e"t is
kno/n as bo-in'. 4nbo-in' is t&e o((osite o$ bo-in' an) is #e$e##e) to as t&e (#o"ess o$
sto#in' a #e$e#en"e ty(e into a value ty(e va#iable. 6n .NE, bo-in' is )one im(li"itly,
/&ile a "astin' me"&anism is #e0ui#e) to a"&ieve unbo-in'.
Q14: Do ADO.NET suorts store! roce!ures?
Answer: <es, ADO.NE "ontains a "om(lete $#ame/o#k $o# e-e"utin' sto#e) (#o"e)u#es
on a )ata set. +to#e) (#o"e)u#es in ADO.NE a#e use) mostly $o# "ommon #e(etitive
$un"tions.
Q11: What are the $asic ,a0ers of an0 ADO.NET $ase! !ata5centric a,ication?
Answer: Eve#y )ata1"ent#i" a((li"ation t&at uses ADO.NE s&oul) i)eally be st#u"tu#e)
into t&#ee laye#s.
=#esentation laye#; =#ovi)es all t&e 946 an) inte#a"tion se#vi"es to en) use#.
>usiness Lo'i" Laye#; 4se) to im(lement all t&e business lo'i" o$ an a((li"ation.
Data A""ess Laye#; =#ovi)es ty(es t&at a#e use) to inte#a"t /it& t&e un)e#lyin'
)atabases.
Q12: E,a$orate on some of the a!vanta-es of usin- store! roce!ures in ADO.NET.
Answer: &e $ollo/in' a#e some o$ t&e bene$its o$ im(lementin' ADO.NE sto#e)
(#o"e)u#es;
+to#e) (#o"e)u#es #esi)e in )atabases in "om(ile) $o#m. &is #esults in e-t#emely
$ast (e#$o#man"e.
+to#e) (#o"e)u#es #e0ui#e less net/o#k t#a$$i" an) ban)/i)t& to e-e"ute on
#emote )atabases sin"e t&ey )o not "ontain any e-t#a in$o#mation but only
"om(ile) )ata.
+to#e) (#o"e)u#es a#e se"u#e an) invulne#able to any outsi)e t&#eat )ata mi'&t
#esult in loss o# leaka'e o$ )ata.
Q1": 6an !ata $e e!ite! in a reeater contro,?
Answer: No, t&e #e(eate# )ata "ont#ol is use) to s&o/ )ata. Data "annot be e)ite) in a
#e(eate# "ont#ol. &e Data9#i)?ie/ "ont#ols "an be use) $o# e)itin' (u#(oses.
Q1&: Brief,0 e2,ain some of the comonents of ADO.NET Data rovi!ers:
Answer: &e $ollo/in' a#e some o$ t&e most im(o#tant "om(onents o$ ADO.NE )ata
(#ovi)e#s;
6onnection; &is is t&e ob*e"t #es(onsible $o# establis&in' a "onne"tion to t&e
)atabase.
6omman!: &e ob*e"t #es(onsible $o# e-e"utin' "omman)s an) sto#e)
(#o"e)u#es.
E2ecute'ea!er: 5etu#ns a "om(lete set o$ #esults
E2ecute#ca,er: 5etu#ns only a sin'le value
E2ecuteNonQuer0: E-e"ute a "omman) on a )atabase but it )oesn%t #etu#n any
value $#om t&e )atabase. 4se) mostly $o# inse#tion an) u()ate.
Data'ea!er: =#ovi)es #ea)1only an) $o#/a#)1only a""ess to a )atabase.
&is a#ti"le "ontains some o$ t&e most $un)amental an) basi" ADO.NE 0uestions t&at
"an be aske) in any .NE *ob inte#vie/. o stu)y &o/ ADO.NE "an be use) to )evelo(
)ata1"ent#i" a((li"ations, see t&is "ou#se.

ADO.NET Tutoria, for Be-inners
June 2, 2014 by usman_malik Leave a Comment
ADO.NE, an im(o#tant $eatu#e o$ t&e
.NE $#ame/o#k, is a set o$ ob*e"t1o#iente) lib#a#ies use) to (#ovi)e "onsistent a""ess to
)ata sou#"es su"& !i"#oso$t +,L se#ve#, A""ess, te-t $iles, E-"el s(#ea)s&eets an) .!L.
ADO.NE "lasses #esi)e in a names(a"e @+ystem.Data% an) its im(lementation /it&
.!L is in @+ystem..ml%. !ost o$ t&e )ata1"ent#i" a((li"ations use t&e ADO.NE to
"onne"t to t&ei# )ata sou#"es an) #et#ieve, mana'e an) u()ate t&e )ata. &is ADO.NE
tuto#ial "ove#s a basi" int#o)u"tion o$ t/o main "om(onents o$ ADO.NE "lasses, /&i"&
a#e .NE $#ame/o#k @Data =#ovi)e#s% 7$o# "onne"tin' to )atabase, e-e"utin' "omman)s,
#et#ieve #esults, et".8 an) @Data+et% 7)is"onne"te) "a"&e) )ata8.
Ne/ to C3: C&e"k out a 4)emy."om "ou#se.
ADO.NET .NET 7ramewor8 Data +rovi!ers
2o# "onne"tin' to t&e )i$$e#ent )ata sou#"es an) inte#a"tin' /it& )ata, a #elatively
"ommon mana'e) inte#$a"e is available in ADO.NE )ata (#ovi)e# mo)el. A sin'le set o$
"lasses is not able to a""om(lis& a""ess to all o$ t&e )ata sou#"es sin"e ea"& o$ a )ata
sou#"e &as s(e"ial inte#$a"e, e-(ose) to a se(a#ate (#oto"ol an) #e0ui#e a )i$$e#ent set o$
#ules to "onne"t an) inte#a"t.
2ollo/in' a#e t&e $ou# "o#e ob*e"ts o$ ADO.NE )ata (#ovi)e#s.
&e @Conne"tion% ob*e"t, /&i"& establis&es t&e "onne"tion to a )ata sou#"e.
&e @Comman)% ob*e"t, /&i"& inte#a"ts /it& t&e )ata sou#"e by e-e"utin'
"omman)s. 6t uses t&e @Conne"tion% ob*e"t to i)enti$y t&e )ata sou#"e.
&e @Data5ea)e#% ob*e"t #ea)s a st#eam o$ )ata $#om )ata sou#"e /it& a #ea)1only
att#ibute. &is ob*e"t #et#ieves t&e #esult o$ @+ELEC% statement $#om a
@Comman)% ob*e"t.
&e @DataA)a(te#% ob*e"t $ills t&e @Data+et% /it& #e0ueste) (a#t o$ )ata sou#"e as
a )is"ontinue) )ata an) mana'es t&e u()ates /it& t&e )ata sou#"e.
&e $ollo/in' a#e t&e main )ata (#ovi)e#s available in .NE.
+,L )ata (#ovi)e# is $o# a""essin' t&e !i"#oso$t +,L +e#ve# /&e#e no
inte#me)iate laye#s o$ se#vi"e "om(onents a#e involve). &ese "lasses a#e lo"ate)
in @+ystem.Data.+0lClient% names(a"e.
OLE D> )ata (#ovi)e# is $o# A""ess an) E-"el as t&ey e-(ose an OLE D>
inte#$a"e. Classes a#e lo"ate) in @+ystem.Data.OleDb% names(a"e.
OD>C )ata (#ovi)e# "lasses #esi)e in @+ystem.Data.O)b"% names(a"e use) $o#
ol) )atabases e-(osin' t&e OD>C inte#$a"e.
O#a"le )ata (#ovi)e# is use) $o# inte#a"tin' /it& O#a"le )ata sou#"es t&#ou'&
O#a"le "lient "onne"tivity so$t/a#e. Classes a#e en"a(sulate) in
@+ystem.Data.O#a"leClient% names(a"e.
&e $ollo/in' C3 "o)e sni((et )emonst#ates t&e use o$ OLE D> )ata (#ovi)e# "lasses to
#ea) E-"el s(#ea)s&eet.
string connectionString = string.Format
("Provider=Microsoft.ACE.OLEDB.1.!" Data So#rce = $!%" E&tended
Pro'erties=("E&ce) 1.! *m)" +D,=-o" .ME*=1" MA*SCA-,O/S=!"
,EADO-L0=1,2E("""3 strE&ce)Fi)ePat45"
O)eD6Connection connection = ne7 O)eD6Connection (connectionString5"
tr8 $
connection.O'en (5"
O)eD6Command command = ne7 O)eD6Command ("se)ect 9 from :S4eet-ame;<"3
connection5"
O)eD6DataAda'ter dataAda'ter = ne7 O)eD6DataAda'ter (command5"
DataSet dataSet = ne7 DataSet (5"
dataAda'ter.Fi)) (dataSet5"
con.C)ose (5"
%
catc4 (E&ce'tion e5 $
%
fina))8 $
con.C)ose (5"
%
6n t&e above "o)e, @ACE OLED>% )#ive# is use) to "onne"t to an E-"el s(#ea)s&eet usin'
@Ole)bConne"tion% ob*e"t /it& a @"onne"tion+t#in'% 7t&e (at& o$ a )ata sou#"e, (#o(e#ties
$o# $ile a""ess an) name o$ a (#ovi)e#8 as an a#'ument. &e @Ole)bComman)% ob*e"t uses
t&at "onne"tion ob*e"t an) a 0ue#y st#in' to set a "omman) $o# a )ata sou#"e. 2inally, t&e
@Ole)bDataA)a(te#% ob*e"t takes t&at "omman) ob*e"t as an a#'ument an) #et#ieves all
t&e )ata o$ a @s&eetName% in 0ue#y st#in' an) $ille) a Data+et /it& t&at )ata.
6nte#este) in lea#nin' mo#e about ADO.NE: Look at t&is online "ou#se at 4)emy."om.
ADO.NET Data#et
&e ADO.NE @Data+et% ob*e"t su((o#ts t&e )is"onne"te) )ata a""ess an) o(e#ations on
t&e )ata. 6t is "a"&e) )ata t&at (#ovi)es a #eliable #elational (#o'#ammin' mo)el
#e'a#)less o$ t&e )ata sou#"e. &e#e$o#e, it "an be use) /it& )i$$e#ent )ata sou#"es su"& as
.!L )ata o# $o# mana'in' t&e (a#t o$ )ata )o/nloa)e) in an a((li"ation to in"#ease t&e
s"alability ability o$ an a((li"ation.
ADO.NE @Data+et% "ontains @DataableColle"tion% an) @Data5elationColle"tion% ob*e"t.
A @DataableColle"tion% &ave none o# multi(le @Dataable%, /&i"& #e(#esents a sin'le
table, #esi)in' in t&e memo#y. A @Dataable% "ontains @DataColumnColle"tion%, a
"olle"tion o$ "olumns, an) @Const#aintColle"tion%, a "olle"tion o$ "onst#aints, /&i"&
"olle"tively )es"#ibes t&e s"&ema o$ t&e table. 6t also "ontains @Data5o/Colle"tion%, a
"olle"tion o$ #o/s, /&i"& &ol)s t&e )ata in t&e table in t&e $o#m o$ @Data5o/% ob*e"ts, a
#o/ o$ )ata in a table. A @Data5elationColle"tion% &ave none o# multi(le @Data5elation%
ob*e"ts, /&i"& #e(#esents a #elations&i( bet/een #o/s o$ t/o @Dataable% ob*e"t by
i)enti$yin' t&e mat"&e) "olumns in t&ese t/o tables.
&e $ollo/in' C3 "o)e sni((et illust#ates t&e basi" use o$ ADO.NE @Data+et% an)
!i"#oso$t +,L +e#ve# )ata (#ovi)e# "lasses, to a""ess t&e +,L se#ve#, #et#ieve an) $ilte#
t&e @Custome#% )ata.
string connectionString = ="Data So#rce=S>LSE,?E,****" .nitia)
Cata)og=DB@-AME" 2ser .D=2SE,@-AME" Pass7ord=2SE,@PASS/O,D""
SA)Connection conenction = ne7 SA)Connection (connectionString5"
DataSet dataSet = ne7 DataSet (5"
SA)Command command = ne7 SA)Command (5"
command.Connection = conenction"
SA)DataAda'ter dataAda'ter = ne7 SA)DataAda'ter (5"
command.Command1e&t = "Se)ect 9 From C#stomerOrder""
dataAda'ter.Se)ectCommand = command"
dataAda'ter.Fi)) (dataSet3 "C#stomerOrder"5"
Data1a6)e data1a6)e = ne7 Data1a6)e (5"
data1a6)e = dataSet.1a6)es:!<"
if (dataSet.1a6)es.Co#nt B !5
$
foreac4 (Data,o7 data,o7 in data1a6)e.,o7s5
$CC1raverse a)) ro7s and o#t'#t 74en a order stat#s is on)8 'rod#ction
if (data,o7:"Order@Stat#s"<.1oString (5 == "Prod#ction"5
$
Conso)e./riteLine (".DD " E Convert.1oString (data,o7:"C#[email protected]"<55"
Conso)e./riteLine ("-ameD " E Convert.1oString
(data,o7:"C#stomer@-ame"<55"
%
%
%
Data,o7 ne7Order"
ne7Order = data1a6)e.-e7,o7 (5"
ne7Order:"C#[email protected]"< = !F"
ne7Order:"C#stomer@-ame"< = "Mr. George""
ne7Order:"Order@Stat#s"< = "De)ivered""
CCAdd a ro7
data1a6)e.,o7s.Add (ne7Order5"

ne7Order = data1a6)e.,o7s.Find (15"
CCEdit a ro7
ne7Order.BeginEdit (5"
ne7Order:"Order@Stat#s"< = "De)ivered""
ne7Order.EndEdit (5"

ne7Order = data1a6)e.,o7s.Find (15"
CCDe)ete a ro7
ne7Order.De)ete (5"

SA)CommandB#i)der CommandB#i)der = ne7 SA)CommandB#i)der (dataAda'ter5"
dataAda'ter.2'date (dataSet3 "C#stomerOrder"5"
6n t&e above "o)e sni((et, an ADO.NE Data+et "onne"tion is establis&e) /it& t&e +,L
se#ve# )atabase. 4sin' @+0lComman)% an) @+0lDataA)a(te#%, all )ata s(e"i$ie) in
@+ELEC% 0ue#y is #et#ieve) an) @Data+et% ob*e"t is $ille) /it& it. A @Dataable% at in)e-
Ae#o o$ @DataableColle"tion% &ol)s t&e enti#e "ustome#%s )ata. A Dataable ob*e"t is t&en
enume#ate) usin' a @$o#ea"&% loo( to 'et a #o/ o$ a table in @Data5o/% ob*e"t in
ite#ations. o inse#t a ne/ #o/, $i#st a @Ne/5o/% met&o) o$ Dataable is "alle) to #etu#n
a Data5o/ ob*e"t /it& t&e same $iel)s as in t&e ot&e# Data5o/ ob*e"ts o$ Dataable.
&en t&e @A))% met&o) o$ @Dataable.5o/% a))s a ne/ #o/ in Dataable. o u()ate an)
)elete a #o/, t&e @2in)% met&o) o$ @Dataable.5o/% is "alle) t&at sea#"&es $o# a #e"o#),
(#ovi)e) t&e value mat"&es t&e (#ima#y key o$ a table. B&en a mat"& is $oun),
@>e'inE)it% an) @En)E)it% met&o)s a#e "alle) to u()ate t&e #et#ieve) #e"o#). &e @Delete%
met&o) is use) to )elete t&at sea#"&e) #e"o#) $#om a Dataable. 2inally, all "&an'es a#e
ma)e to a )atabase table in +,L se#ve# by "allin' t&e @4()ate% met&o) o$ @DataA)a(te#%.
#Q) Tutoria,: 3n!erstan!in- 9our
Data$ase )an-ua-e
!ay 14, 2014 by Jenni$e# !a#s& Leave a Comment
!ost )atabase use +t#u"tu#e) ,ue#y
Lan'ua'e 7+,L8. &is lan'ua'e inte#$a"es bet/een you an) you# )atabase tables. Even i$
you kno/ anot&e# /eb1base) s"#i(tin' lan'ua'e 7C3, =C= o# ?>.NE8, you%ll nee) to
kno/ &o/ to 0ue#y )ata $#om you# )atabase. !y+,L, !i"#oso$t +,L +e#ve# an) O#a"le
use simila# lan'ua'e st#u"tu#es. B&ile t&ey a#e simila#, t&ey &ave some )i$$e#en"es, but
lea#nin' one lan'ua'e /ill &el( you un)e#stan) t&e #est o$ t&em.
Ne/ to +,L: Lea#n &o/ to /o#k /it& t&e lan'ua'e at 4)emy."om
A Basic Quer0 to 'etrieve Data
&e Dsele"tE statement is t&e main (&#ase $o# #et#ieve )ata. No )ata is "&an'e) o# )elete).
6t%s *ust #et#ieve) $o# you# use. <ou "an )is(lay t&e )ata on a /eb (a'e o# use it to u()ate
)ata late# on in you# (#o'#am. &e $ollo/in' "o)e is an e-am(le o$ a sele"t statement;
sele"t F $#om use#s
&e above statement is (#obably t&e most basi" +,L statement you "an /#ite. &e
statement basi"ally says Dsele"t all #e"o#)s $#om t&e use#s table.E &e aste#isk tells t&e
)atabase to #etu#n all "olumns. 2o# instan"e, t&e Duse#sE table "oul) &ave $i#st an) last
name, a))#ess an) si'nu( )ate "olumns. Bit& t&e above statement, you%) see t&em all.
Obviously, you /ant to $ilte# you# #e"o#)s. <ou usually /ant to #et#ieve only a "e#tain
#e"o#) set. 2o# instan"e, a "ustome# lo's in to you# /ebsite. <ou t&en #et#ieve t&e
"ustome#%s in$o#mation base) on t&e "ustome#%s 6D 7use#i) in t&is e-am(le8. &e
$ollo/in' "o)e #et#ieves a use# /it& an 6D o$ G;
se)ect 9 from #sers 74ere #serid=H
6n a))ition to #et#ievin' a $ilte#e) #e"o#) set, you s&oul) also limit t&e numbe# o$ "olumns
#etu#ne). &e aste#isk #etu#ns all "olumns. &is in"lu)es si'nu( )ates, mo)i$ie) )ates,
$o#ei'n keys an) ot&e# useless in$o#mation $o# a $#ont1en) /eb (a'e. <ou "an s(e"i$y t&e
"olumns #etu#ne). <ou s&oul) only #etu#n "olumns you nee) to /o#k /it& on t&e $#ont1
en). &e $ollo/in' statement sele"ts only t&e $i#st an) last name "olumns;
se)ect first@name3 )ast@name from #sers 74ere #serid=H
&e#e a#e seve#al ot&e# ty(es o$ sele"t statement. o lea#n mo#e about t&e +,L lan'ua'e,
take a "ou#se at 4)emy."om.
The 3!ate #tatement
&e u()ate statement is /&at you use to e)it #e"o#)s. !ost o$ you# tables /on%t stay
stati". <ou%ll nee) to "&an'e t&e )ata. &e u()ate statement is &o/ it%s )one. &e
$ollo/in' "o)e is an e-am(le o$ a basi" u()ate statement;
#'date #sers set first@name=IJaneI 74ere #serid=H
An im(o#tant (a#t o$ t&is statement is t&e /&e#e "lause. 6$ you $o#'et it, you%ll u()ate
eve#y #e"o#) in t&e use#s table to DJane.E <ou must ensu#e you in"lu)e a /&e#e "lause in
you# u()ate statements unless you /ant to "&an'e eve#y #e"o#) 7an) you usually )on%t8.
<ou "an "&an'e multi(le "olumns at a time as /ell. &e $ollo/in' "o)e "&an'es t&e $i#st
an) last names $o# t&e use# /it& an 6D o$ G;
#'date #sers set first@name=IJaneI3 )ast@name=ISmit4I 74ere #serid=H
The De,ete #tatement
&e +,L )elete statement #emoves #e"o#)s $#om a table. A'ain, t&e /&e#e "lause is
im(o#tant in t&is statement as /ell. Bit&out t&e /&e#e "lause, you%ll )elete eve#y #e"o#)
in you# table. As /it& t&e u()ate statement, t&is ty(e o$ mistake is )isast#ous an) it means
you%ll nee) to #esto#e )ata $#om a ba"ku(.
&e $ollo/in' "o)e is an e-am(le o$ a )elete statement;
de)ete from #sers 74ere #serid=H
&e above "o)e *ust )eletes one #e"o#), but you "an use t&e /&e#e "lause to )elete
multi(le #e"o#)s at a time. <ou s&oul) (#obably limit t&e amount o$ )elete statements
you e-e"ute. 6t%s stan)a#) to set #e"o#)s as a"tive o# ina"tive instea) o$ *ust )eletin' t&em.
<ou a)) a DbitE "olumn to t&e table an) )ea"tivate /&en ne"essa#y, but you still &ave a
#e"o#) o$ t&e a""ount, o#)e# o# ot&e# ty(e o$ #e"o#).
!ost +,L )atabases a#e #elational. &is means t&at tables a#e linke) to'et&e# usin'
(#ima#y an) $o#ei'n keys. 2o# instan"e, a "ustome# 6D is sto#e) to ea"& o#)e# #e"o#) to
link "ustome#s /it& o#)e#s. &is means t&at you nee) to )elete t&e o#)e# be$o#e you
)elete t&e "ustome#. 6$ you )on%t, it leaves /&at is "alle) Do#(&ane) #e"o#)s.E Asi)e $#om
t&e (#oblems /it& )eletin' #e"o#)s, o#(&ane) #e"o#)s "an "ause bu's in you# system.
The Insert #tatement
&e inse#t statement is &o/ you a)) a #e"o#) to t&e )atabase table. &e inse#t statement%s
len't& is )e(en)ent on t&e )ata #e0ui#e) to "#eate a #e"o#) an) any )e$ault values set in
t&e table%s "olumn )e"la#ations. 2o# instan"e, i$ you )on%t allo/ nulls $o# a use#%s $i#st an)
last name, t&en you must in"lu)e t&e $i#st an) last name statement in you# inse#t
statement. &e alte#native to t&is limitation is settin' a )e$ault value in t&e table
)e$inition. <ou%ll set t&ese values /&en you )e$ine you# tables.
&e $ollo/in' "o)e is an e-am(le o$ an inse#t statement;
insert into #sers (first@name3 )ast@name5 va)#es (KJaneI3 KSmit4I5
&e above statement a))s one #e"o#). &e value DJaneE an) D+mit&E a#e use) $o# t&e $i#st
an) last name.
&is statement /ill t&#o/ an e##o# i$ you )on%t in"lu)e all ne"essa#y )ata. 2o# instan"e,
most use# tables &ave an 6D "olumn. <ou )on%t "al"ulate t&is uni0ue 6D, but t&e )atabase
"an be set to auto1in"#ement t&e "olumn by 1 ea"& time a ne/ #e"o#) is inse#te). 6$ t&e
"olumn )oes not in"#ement, an e##o# #etu#ns. &ese "olumns a#e also set as (#ima#y keys
by some )atabase a)minist#ato#s. =#ima#y keys must be uni0ue an) "annot be null, so you
must a""ount $o# t&is issue /&en inse#tin' ne/ )ata.
<ou "an also inse#t some )e$ault )ata. 2o# instan"e, you s&oul) &ave a DC#eate DateE in
you# )atabase. 6n !++,L, you "an use t&e $ollo/in' statement to inse#t a #e"o#) /it& t&e
)e$ault system )ate usin' t&e 'etDate78 $un"tion;
insert into #sers (first@name3 )ast@name3 create@date5 va)#es (KJaneI3
KSmit4I3 getdate(55
&ese a#e t&e $ou# basi" +,L statements you%ll nee) as you /o#k /it& a +,L )atabase.
!o#e "om(le- statements e-ist, but you /on%t nee) t&em $o# most o(e#ations.
#Q) 3NION vs 3NION A)) :
#imi,arities an! Differences
A(#il H, 2014 by A#(ita >&atta"&a#*ee Leave a Comment
B&i"& o(e#ato# )o you use i$ you &ave to
"ombine t&e #esult sets o$ t/o o# mo#e 0ue#ies an) )is(lay all t&e #o/s #etu#ne) by ea"&
o$ t&e 0ue#ies as one sin'le #esult set: &e solution is to use t&e 3NION set o(e#ato#.
E-"e(t in !y+,l, t&e set o(e#ato#s 3NION an) 3NION A)) a#e su((o#te) by most
)atabase (lat$o#ms like D>2, O#a"le, +,L se#ve#, an) =ost'#e +,L.
B&en usin' 4N6ON #emembe# t&at t&e o#)e#, numbe#, an) )ataty(e o$ "olumns s&oul)
be t&e same in all 0ue#ies. &e )ataty(es nee) not be i)enti"al, but t&ey s&oul) be
"om(atible. 2o# e-am(le, CCA5 an) ?A5CCA5 a#e "om(atible )ataty(es. 6$ you /ant
to #e"olle"t in$o#mation on tables, +,L )atabase o(e#ations, an) (#o'#ammin' t&en +,L
Database $o# >e'inne#s an) Lea#n +,L in I )ays a#e '#eat #e$#es&e# "ou#ses. 6$ you nee)
$u#t&e# 'ui)an"e on /#itin' +,L 0ue#ies an) lea#nin' &o/ to buil) a((li"ations o#
'ene#ate business #e(o#ts, t&en #e$e# 6nt#o)u"tion to +,L #ainin'.
6omarison Between 3NION an! 3NION A))
3NION
&e 4N6ON "omman) is use) to sele"t #elate) in$o#mation $#om t/o tables, mu"& like
t&e JO6N "omman). Bit& 4N6ON, only )istin"t values a#e sele"te) by )e$ault. A 4N6ON
statement e$$e"tively )oes a +ELEC D6+6NC on t&e #esult set.
3NION #tatement in #Q) #erver
&e 4N6ON o(e#ato# is use) to "ombine t&e #esult set o$ t/o o# mo#e +ELEC
statements.
Ce#e a#e some o$ t&e sim(le #ules o$ usin' 4N6ON in +,L;
Ea"& +ELEC statement /it&in t&e 4N6ON must &ave t&e same numbe# o$
"olumns an) t&e "olumns must &ave simila# o# "om(atible )ata ty(es.
&e "olumns in ea"& +ELEC statement must be in t&e same o#)e#.
6$ t&e "olumns siAes o$ t&e t/o tables va#y, t&en /&ile #etu#nin' )ata, +,L se#ve#
uses t&e la#'e# o$ t&e t/o "olumns. &us i$ a +ELECJ.4N6ON statement &as a
CCA5 758 an) CCA5 7108 "olumn, t&en it /ill )is(lay out(ut )ata o$ bot& t&e
"olumns as a CCA5 7108 "olumn.
6$ t&e "olumns a"#oss t&e table &ave )i$$e#ent "olumn names t&en in 'ene#al, t&e
"olumn names o$ t&e $i#st 0ue#y a#e use).
#Q) 3NION #0nta2
+ELEC "olumn_name7s8 25O! table1
4N6ON
+ELEC "olumn_name7s8 25O! table2K
3NION A))
&e 4N6ON ALL "omman) is simila# to t&e 4N6ON "omman), e-"e(t t&at 4N6ON ALL
sele"ts all values. +o /it& 4N6ON ALL )u(li"ate #o/s a#e not eliminate), #at&e# t&ey a#e
in"lu)e). &is o(e#ato# *ust (ulls all #o/s $#om all tables /&i"& satis$y t&e 0ue#y an)
"ombines t&em into a table. 6$ you a#e su#e t&at all t&e #e"o#)s #etu#ne) $#om a 4N6ON
o(e#ation a#e uni0ue, t&en usin' 4N6ON ALL is a bette# o(tion as it 'ives $aste# #esults.
&e #esults $#om a 4N6ON ALL a#e unso#te). +o a$te# 'ettin' t&e #esults, you "an so#t
t&em by usin' t&e O5DE5 >< "lause. A O5DE5 >< s&oul) be inse#te) /it& t&e last
+ELEC statement.
#Q) 3NION A)) #0nta2
+ELEC "olumn_name7s8 25O! table1
4N6ON ALL
+ELEC "olumn_name7s8 25O! table2K
E2am,e
o "om(a#e 4N6ON an) 4N6ON ALL let us take t&e $ollo/in' table @Conta"ts%
"ontainin' "olumns $o# City, +tate, an) Li(.
6it0 #tate ;i
Nas&ville N GM2G5
La/#en"e N+ II04H
Co#vallis O5 HMGGG
3NION A)) #Q) #tatement
6$ /e a((ly 4N6ON ALL +,L statement, t&en /e /ill "ombine t&e t/o 0ue#ies /&i"&
/ill #et#ieve an) "ombine #e"o#)s $#om ennessee 7@N%8 t/i"e.
+ynta-;
+ELEC City, +tate, Li( 25O! Conta"ts BCE5E +tate 6N 7@N+%, @N%8
4N6ON ALL
+ELEC City, +tate, Li( 25O! Conta"ts BCE5E 6N 7@O5% @N%8
5esult o$ 4N6ON ALL synta-;
6it0 #tate ;i
Nas&ville N GM2G5
La/#en"e N+ II04H
Nas&ville N GM2G5
Co#vallis O5 HMGGG
Bit& 4N6ON ALL synta- t&e N #e"o#) a((ea#s t/i"e, sin"e bot& t&e +ELEC
statements #et#ieve N #e"o#)s.
3NION #Q) #tatement
Be no/ use t&e +,L 4N6ON "omman) by /#itin' t&e synta-;
+ELEC City, +tate, Li( 25O! Conta"ts BCE5E +tate 6N 7@N+%, @N%8
4N6ON
+ELEC City, +tate, Li( 25O! Conta"ts BCE5E 6N 7@O5% @N%8
&e #esult o$ t&e 4N6ON 0ue#y /ill be as $ollo/s;
6it0 #tate ;i
Co#vallis O5 HMGGG
La/#en"e N+ II04H
Nas&ville N GM2G5
Noti"e t&at t&e N #e"o#) a((ea#s only on"e, even t&ou'& bot& t&e +ELEC statements
#et#ieve N #e"o#)s. &e 4N6ON synta- automati"ally eliminates t&e )u(li"ate #e"o#)s
bet/een t&e t/o +,L statements an) also so#ts t&e #esults. &e #esultant )is(laye)
#e"o#)s a#e so#te) al(&abeti"ally an) so t&e Co#vallis #e"o#) a((ea#s $i#st even t&ou'& it
is $#om t&e se"on) +ELEC statement. A 95O4= >< "lause "an be a))e) at t&e en) to
so#t t&e list.
A 4N6ON 0ue#y, by )e$inition, eliminates all )u(li"ate #o/s an) "om(a#e) to 4N6ON
ALL it is slo/e# as it )oes a so#tin' o(e#ation. o )o t&is in +,L +e#ve#, a 4N6ON
statement must buil) a tem(o#a#y in)e- on all t&e "olumns #etu#ne) by bot& 0ue#ies. 6$
t&e in)e- "annot be buil) $o# t&e 0ue#ies, t&en you /ill 'et a +,L e##o#. 6n su"& "ases it is
best to use t&e 4N6ON ALL statement.
A,ication in Other Data$ase #evers
In Orac,e
O#a"le )oes not su((o#t 4N6ON o# 4N6ON ALL on 0ue#ies un)e# t&ese "i#"umstan"es;
,ue#ies "ontainin' "olumns o$ LON9, >LO>, CLO>, >26LE, o# ?A55A<
)ataty(e.
,ue#ies "ontainin' a 2O5 4=DAE "lause o# a A>LE "olle"tion e-(#ession.
In DB2
6n D>2 )atabase system, you "an use bot& 4N6ON an) 4N6ON ALL alon' /it& t&e
?AL4E+ "lause.
6onc,usion
6$ you #emembe# t&e sim(le #ules to use 4N6ON an) 4N6ON ALL an) t&e synta- $o# t&e
same, t&en "ombinin' tables as (e# you# #e0ui#ement be"omes an easy task. 6$ you /ant
to e-(lo#e basi" +,L 0ue#ies an) ot&e# "o)e statements, t&en it is a 'oo) i)ea to "&e"k
out +,L ,ue#ies 101. 6$ you /ant to 'at&e# kno/le)'e on O#a"le +,L an) O#a"le
=LO+,L =#o'#ammin' t&en it may be /o#t& to (eek into 6nt#o)u"tion to O#a"le +,L, o#

You might also like