Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction to SQL
What is SQL?
SQL stands for Structured Quer# Language SQL lets #ou access and mani ulate data!ases SQL is an A$SI %American $ational Standards Institute& standard
SQL can e'ecute (ueries against a data!ase SQL can retrie)e data from a data!ase SQL can insert records in a data!ase SQL can u date records in a data!ase SQL can delete records from a data!ase SQL can create ne* data!ases SQL can create ne* ta!les in a data!ase SQL can create stored rocedures in a data!ase SQL can create )ie*s in a data!ase SQL can set ermissions on ta!les+ rocedures+ and )ie*s
Ho*e)er+ to !e com liant *it, t,e A$SI standard+ t,e# all su ort at least t,e ma-or commands %suc, as SELECT+ U./ATE+ /ELETE+ I$SERT+ WHERE& in a similar manner" Note: 0ost of t,e SQL data!ase rograms also ,a)e t,eir o*n ro rietar# e'tensions in addition to t,e SQL standard1
An R/20S data!ase rogram %i"e" 0S Access+ SQL Ser)er+ 0#SQL& A ser)er3side scri ting language+ li4e .H. or AS. SQL HT0L 5 CSS
RDBMS
R/20S stands for Relational /ata!ase 0anagement S#stem" R/20S is t,e !asis for SQL+ and for all modern data!ase s#stems li4e 0S SQL Ser)er+ I20 /26+ Oracle+ 0#SQL+ and 0icrosoft Access" T,e data in R/20S is stored in data!ase o!-ects called ta!les" A ta!le is a collections of related data entries and it consists of columns and ro*s"
Database Tab es
A data!ase most often contains one or more ta!les" Eac, ta!le is identified !# a name %e"g" 7Customers7 or 7Orders7&" Ta!les contain records %ro*s& *it, data" 2elo* is an e'am le of a ta!le called 7.ersons7: !"#d 8 6 3 LastNa$e Hansen S)endson .ettersen %irstNa$e Ola To)e :ari &ddress Timotei)n 89 2org)n 63 Storgt 69 Cit' Sandnes Sandnes Sta)anger
T,e ta!le a!o)e contains t,ree records %one for eac, erson& and fi)e columns %.;Id+ Last$ame+ <irst$ame+ Address+ and Cit#&"
SQL State$ents
0ost of t,e actions #ou need to erform on a data!ase are done *it, SQL statements" T,e follo*ing SQL statement *ill select all t,e records in t,e 7.ersons7 ta!le: SELECT = <RO0 .ersons In t,is tutorial *e *ill teac, #ou all a!out t,e different SQL statements"
S,L,CT 3 e'tracts data from a data!ase U!D&T, 3 u dates data in a data!ase D,L,T, 3 deletes data from a data!ase #NS,RT #NT- 3 inserts ne* data into a data!ase
T,e //L art of SQL ermits data!ase ta!les to !e created or deleted" It also define inde'es %4e#s&+ s ecif# lin4s !et*een ta!les+ and im ose constraints !et*een ta!les" T,e most im ortant //L statements in SQL are:
CR,&T, D&T&B&S, 3 creates a ne* data!ase <,R D&T&B&S, 3 modifies a data!ase CR,&T, T&BL, 3 creates a ne* ta!le <,R T&BL, 3 modifies a ta!le DR-! T&BL, 3 deletes a ta!le CR,&T, #ND,. 3 creates an inde' %searc, 4e#& DR-! #ND,. 3 deletes an inde'
T,is c,a ter *ill e' lain t,e SELECT and t,e SELECT = statements"
and
SELECT * FROM table_name
P_Id 1
%ette"sen
$o* *e *ant to select t,e content of t,e columns named 7Last$ame7 and 7<irst$ame7 from t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
SELECT Last'ame(Fi"st'ame FROM %e"sons
S,L,CT 0 ,/a$) e
$o* *e *ant to select all t,e columns from t,e 7.ersons7 ta!le" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons
Ti): T,e asteris4 %=& is a (uic4 *a# of selecting all columns1 T,e result3set *ill loo4 li4e t,is:
P_Id LastName FirstName Address City
Hansen Svendson
%ette"sen
T,is c,a ter *ill e' lain t,e SELECT /ISTI$CT statement"
$o* *e *ant to select onl# t,e distinct )alues from t,e column named 7Cit#7 from t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
$o* *e *ant to select onl# t,e ersons li)ing in t,e cit# 7Sandnes7 from t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Cit+./Sandnes/
!ET,EE !et2een an inclusive "an#e ' L*&E *' Sea"c0 ;o" a -atte"n *; +ou <no2 t0e e=act value +ou 2ant to "etu"n ;o" at least one o; t0e columns
T,e A$/ A OR o erators are used to filter records !ased on more t,an one condition"
T,e OR o erator dis la#s a record if eit,er t,e first condition or t,e second condition is true"
$o* *e *ant to select onl# t,e ersons *it, t,e first name e(ual to 7To)e7 A$/ t,e last name e(ual to 7S)endson7: We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Fi"st'ame./Tove/ >') Last'ame./Svendson/
-R -)erator ,/a$) e
$o* *e *ant to select onl# t,e ersons *it, t,e first name e(ual to 7To)e7 OR t,e first name e(ual to 7Ola7: We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Fi"st'ame./Tove/ OR Fi"st'ame./Ola/
Co$bining &ND 3 -R
Bou can also com!ine A$/ and OR %use arent,esis to form com le' e' ressions&" $o* *e *ant to select onl# t,e ersons *it, t,e last name e(ual to 7S)endson7 A$/ t,e first name e(ual to 7To)e7 OR to 7Ola7: We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Last'ame./Svendson/ >') (Fi"st'ame./Tove/ OR Fi"st'ame./Ola/)
-RD,R BY ,/a$) e
T,e 7.ersons7 ta!le:
P_Id 1 LastName Hansen Svendson $ @ %ette"sen 'ilsen FirstName Ola Tove &a"i Tom Address Timoteivn 10 !o"#vn $ Sto"#t 0 Ain#vn $ City Sandnes Sandnes Stavan#e" Stavan#e"
$o* *e *ant to select all t,e ersons from t,e ta!le a!o)e+ ,o*e)er+ *e *ant to sort t,e ersons !# t,eir last name" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons OR)ER !3 Last'ame
$o* *e *ant to select all t,e ersons from t,e ta!le a!o)e+ ,o*e)er+ *e *ant to sort t,e ersons descending !# t,eir last name" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons OR)ER !3 Last'ame )ESC
T,e second form s ecifies !ot, t,e column names and t,e )alues to !e inserted:
*'SERT *'TO table_name (column1( column ( column$(CCC) A>LBES (value1( value ( value$(CCC)
$o* *e *ant to insert a ne* ro* in t,e 7.ersons7 ta!le" We use t,e follo*ing SQL statement:
*'SERT *'TO %e"sons A>LBES (@(/'ilsen/( /Do0an/( /!a<<en /( /Stavan#e"/)
T,e follo*ing SQL statement *ill add a ne* ro*+ !ut onl# add data in t,e 7.;Id7+ 7Last$ame7 and t,e 7<irst$ame7 columns:
*'SERT *'TO %e"sons (%_*d( Last'ame( Fi"st'ame) A>LBES (6( /TEessem/( /Da<ob/)
Note: $otice t,e WHERE clause in t,e U./ATE s#nta'" T,e WHERE clause s ecifies *,ic, record or records t,at s,ould !e u dated" If #ou omit t,e WHERE clause+ all records *ill !e u dated1
$o* *e *ant to u date t,e erson 7T-essem+ Da4o!7 in t,e 7.ersons7 ta!le" We use t,e follo*ing SQL statement:
B%)>TE %e"sons SET >dd"ess./'issestien 5F/( Cit+./Sandnes/ ,HERE Last'ame./TEessem/ >') Fi"st'ame./Da<ob/
Note: $otice t,e WHERE clause in t,e /ELETE s#nta'" T,e WHERE clause s ecifies *,ic, record or records t,at s,ould !e deleted" If #ou omit t,e WHERE clause+ all records *ill !e deleted1
P_Id 1
$ @ 6
$o* *e *ant to delete t,e erson 7T-essem+ Da4o!7 in t,e 7.ersons7 ta!le" We use t,e follo*ing SQL statement:
)ELETE FROM %e"sons ,HERE Last'ame./TEessem/ >') Fi"st'ame./Da<ob/
Note: 2e )er# careful *,en deleting records" Bou cannot undo t,is statement1
Exam le
SELECT * FROM %e"sons L*M*T 6
Oracle Syntax
SELECT column_name(s) FROM table_name ,HERE RO,'BM 8. numbe"
Exam le
SELECT * FROM %e"sons ,HERE RO,'BM 8.6
$o* *e *ant to select onl# t,e t*o first records in t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
SELECT TO% * FROM %e"sons
$ @
%ette"sen 'ilsen
&a"i Tom
Sto"#t 0 Ain#vn $
Stavan#e" Stavan#e"
$o* *e *ant to select onl# E9F of t,e records in t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
SELECT TO% 60 %ERCE'T * FROM %e"sons
T,e LI:E o erator is used in a WHERE clause to searc, for a s ecified attern in a column"
P_Id 1
%ette"sen
$o* *e *ant to select t,e ersons li)ing in a cit# t,at starts *it, 7s7 from t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Cit+ L*&E /sG/
T,e 7F7 sign can !e used to define *ildcards %missing letters in t,e attern& !ot, !efore and after t,e attern" T,e result3set *ill loo4 li4e t,is:
P_Id 1 LastName Hansen Svendson $ %ette"sen FirstName Ola Tove &a"i Address Timoteivn 10 !o"#vn $ Sto"#t 0 City Sandnes Sandnes Stavan#e"
$e't+ *e *ant to select t,e ersons li)ing in a cit# t,at ends *it, an 7s7 from t,e 7.ersons7 ta!le" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Cit+ L*&E /Gs/
$e't+ *e *ant to select t,e ersons li)ing in a cit# t,at contains t,e attern 7ta)7 from t,e 7.ersons7 ta!le" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Cit+ L*&E /GtavG/
It is also ossi!le to select t,e ersons li)ing in a cit# t,at $OT contains t,e attern 7ta)7 from t,e 7.ersons7 ta!le+ !# using t,e $OT 4e#*ord" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Cit+ 'OT L*&E /GtavG/
SQL Wildcards
SQL Wi d*ards
SQL *ildcards can su!stitute for one or more c,aracters *,en searc,ing for data in a data!ase"
SQL *ildcards must !e used *it, t,e SQL LI:E o erator" Wit, SQL+ t,e follo*ing *ildcards can !e used:
Wildcard G _ Ic0a"listJ IKc0a"listJ Description > substitute ;o" He"o o" mo"e c0a"acte"s > substitute ;o" e=actl+ one c0a"acte" >n+ sin#le c0a"acte" in c0a"list >n+ sin#le c0a"acte" not in c0a"list
or G1c,arlistH
P_Id 1
$e't+ *e *ant to select t,e ersons li)ing in a cit# t,at contains t,e attern 7nes7 from t,e 7.ersons7 ta!le" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Cit+ L*&E /GnesG/
$e't+ *e *ant to select t,e ersons *it, a last name t,at starts *it, 7S7+ follo*ed !# an# c,aracter+ follo*ed !# 7end7+ follo*ed !# an# c,aracter+ follo*ed !# 7on7 from t,e 7.ersons7 ta!le" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Last'ame L*&E /S_end_on/
$e't+ *e *ant to select t,e ersons *it, a last name t,at do not start *it, 7!7 or 7s7 or 7 7 from t,e 7.ersons7 ta!le" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Last'ame L*&E /ILbs-JG/
SQL IN O erator
The #N -)erator
T,e I$ o erator allo*s #ou to s ecif# multi le )alues in a WHERE clause"
SQL IN Syntax
SELECT column_name(s) FROM table_name ,HERE column_name *' (value1(value (CCC)
#N -)erator ,/a$) e
T,e 7.ersons7 ta!le:
P_Id 1 LastName Hansen Svendson $ %ette"sen FirstName Ola Tove &a"i Address Timoteivn 10 !o"#vn $ Sto"#t 0 City Sandnes Sandnes Stavan#e"
$o* *e *ant to select t,e ersons *it, a last name e(ual to 7Hansen7 or 7.ettersen7 from t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Last'ame *' (/Hansen/(/%ette"sen/)
T,e 2ETWEE$ o erator is used in a WHERE clause to select a range of data !et*een t*o )alues"
$o* *e *ant to select t,e ersons *it, a last name al ,a!eticall# !et*een 7Hansen7 and 7.ettersen7 from t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Last'ame !ET,EE' /Hansen/ >') /%ette"sen/
Note: T,e 2ETWEE$ o erator is treated differentl# in different data!ases" In some data!ases+ ersons *it, t,e Last$ame of 7Hansen7 or 7.ettersen7 *ill not !e listed+ !ecause t,e 2ETWEE$ o erator onl# selects fields t,at are !et*een and e'cluding t,e test )alues&" In ot,er data!ases+ ersons *it, t,e Last$ame of 7Hansen7 or 7.ettersen7 *ill !e listed+ !ecause t,e 2ETWEE$ o erator selects fields t,at are !et*een and including t,e test )alues&" And in ot,er data!ases+ ersons *it, t,e Last$ame of 7Hansen7 *ill !e listed+ !ut 7.ettersen7 *ill not !e listed %li4e t,e e'am le a!o)e&+ !ecause t,e 2ETWEE$ o erator selects fields !et*een t,e test )alues+ including t,e first test )alue and e'cluding t,e last test )alue" T,erefore: C,ec4 ,o* #our data!ase treats t,e 2ETWEE$ o erator"
,/a$) e 9
To dis la# t,e ersons outside t,e range in t,e re)ious e'am le+ use $OT 2ETWEE$:
SELECT * FROM %e"sons ,HERE Last'ame 'OT !ET,EE' /Hansen/ >') /%ette"sen/
Svendson $ %ette"sen
Tove &a"i
!o"#vn $ Sto"#t 0
Sandnes Stavan#e"
SQL Alias
As #ouCll see from t,e t*o SELECT statements a!o)eI aliases can ma4e (ueries easier to !ot, *rite and to read"
SQL +oins
SQL -oins are used to (uer# data from t*o or more ta!les+ !ased on a relations,i !et*een certain columns in t,ese ta!les"
SQL :-#N
T,e DOI$ 4e#*ord is used in an SQL statement to (uer# data from t*o or more ta!les+ !ased on a relations,i !et*een certain columns in t,ese ta!les" Ta!les in a data!ase are often related to eac, ot,er *it, 4e#s" A rimar# 4e# is a column %or a com!ination of columns& *it, a uni(ue )alue for eac, ro*" Eac, rimar# 4e# )alue must !e uni(ue *it,in t,e ta!le" T,e ur ose is to !ind data toget,er+ across ta!les+ *it,out re eating all of t,e data in e)er# ta!le" Loo4 at t,e 7.ersons7 ta!le:
P_Id 1 LastName Hansen Svendson $ %ette"sen FirstName Ola Tove &a"i Address Timoteivn 10 !o"#vn $ Sto"#t 0 City Sandnes Sandnes Stavan#e"
$ote t,at t,e 7.;Id7 column is t,e rimar# 4e# in t,e 7.ersons7 ta!le" T,is means t,at no t*o ro*s can ,a)e t,e same .;Id" T,e .;Id distinguis,es t*o ersons e)en if t,e# ,a)e t,e same name" $e't+ *e ,a)e t,e 7Orders7 ta!le:
O_Id 1 OrderNo FFM46 @@5FM $ @ 6 @65 @65 $@F5@ P_Id $ $ 1 1 16
$ote t,at t,e 7O;Id7 column is t,e rimar# 4e# in t,e 7Orders7 ta!le and t,at t,e 7.;Id7 column refers to t,e ersons in t,e 7.ersons7 ta!le *it,out using t,eir names" $otice t,at t,e relations,i !et*een t,e t*o ta!les a!o)e is t,e 7.;Id7 column"
JOIN1 Retu"n "o2s 20en t0e"e is at least one matc0 in bot0 tables LEFT JOIN1 Retu"n all "o2s ;"om t0e le;t table( even i; t0e"e a"e no matc0es in t0e "i#0t table RIG T JOIN1 Retu"n all "o2s ;"om t0e "i#0t table( even i; t0e"e a"e no matc0es in t0e le;t table F!LL JOIN1 Retu"n "o2s 20en t0e"e is a matc0 in one o; t0e tables
$o* *e *ant to list all t,e ersons *it, an# orders" We use t,e follo*ing SELECT statement:
SELECT %e"sonsCLast'ame( %e"sonsCFi"st'ame( O"de"sCO"de"'o FROM %e"sons *''ER DO*' O"de"s O' %e"sonsC%_*d.O"de"sC%_*d
OR)ER !3 %e"sonsCLast'ame
T,e I$$ER DOI$ 4e#*ord return ro*s *,en t,ere is at least one matc, in !ot, ta!les" If t,ere are ro*s in 7.ersons7 t,at do not ,a)e matc,es in 7Orders7+ t,ose ro*s *ill $OT !e listed"
Svendson $ %ette"sen
Tove &a"i
!o"#vn $ Sto"#t 0
Sandnes Stavan#e"
$o* *e *ant to list all t,e ersons and t,eir orders 3 if an#+ from t,e ta!les a!o)e" We use t,e follo*ing SELECT statement:
SELECT %e"sonsCLast'ame( %e"sonsCFi"st'ame( O"de"sCO"de"'o FROM %e"sons LEFT DO*' O"de"s O' %e"sonsC%_*d.O"de"sC%_*d OR)ER !3 %e"sonsCLast'ame
T,e LE<T DOI$ 4e#*ord returns all t,e ro*s from t,e left ta!le %.ersons&+ e)en if t,ere are no matc,es in t,e rig,t ta!le %Orders&"
@ 6
@65 $@F5@
1 16
$o* *e *ant to list all t,e orders *it, containing ersons 3 if an#+ from t,e ta!les a!o)e" We use t,e follo*ing SELECT statement:
SELECT %e"sonsCLast'ame( %e"sonsCFi"st'ame( O"de"sCO"de"'o FROM %e"sons R*:HT DO*' O"de"s O' %e"sonsC%_*d.O"de"sC%_*d OR)ER !3 %e"sonsCLast'ame
T,e RIJHT DOI$ 4e#*ord returns all t,e ro*s from t,e rig,t ta!le %Orders&+ e)en if t,ere are no matc,es in t,e left ta!le %.ersons&"
$o* *e *ant to list all t,e ersons and t,eir orders+ and all t,e orders *it, t,eir ersons" We use t,e follo*ing SELECT statement:
SELECT %e"sonsCLast'ame( %e"sonsCFi"st'ame( O"de"sCO"de"'o FROM %e"sons FBLL DO*' O"de"s O' %e"sonsC%_*d.O"de"sC%_*d OR)ER !3 %e"sonsCLast'ame
$@F5@
T,e <ULL DOI$ 4e#*ord returns all t,e ro*s from t,e left ta!le %.ersons&+ and all t,e ro*s from t,e rig,t ta!le %Orders&" If t,ere are ro*s in 7.ersons7 t,at do not ,a)e matc,es in 7Orders7+ or if t,ere are ro*s in 7Orders7 t,at do not ,a)e matc,es in 7.ersons7+ t,ose ro*s *ill !e listed as *ell"
Note: T,e U$IO$ o erator selects onl# distinct )alues !# default" To allo* du licate )alues+ use U$IO$ ALL"
!S: T,e column names in t,e result3set of a U$IO$ are al*a#s e(ual to t,e column names in t,e first SELECT statement in t,e U$IO$"
<,$) o'ees"US&<:
E_ID 01 0 0$ 0@ E_Name Tu"ne"( Sall+ &ent( Cla"< Svendson( Ste-0en Scott( Ste-0en
$o* *e *ant to list a the di++erent em lo#ees in $or*a# and USA" We use t,e follo*ing SELECT statement:
SELECT E_'ame FROM Em-lo+ees_'o"2a+ B'*O'
Note: T,is command cannot !e used to list all em lo#ees in $or*a# and USA" In t,e e'am le a!o)e *e ,a)e t*o em lo#ees *it, e(ual names+ and onl# one of t,em *ill !e listed" T,e U$IO$ command selects onl# distinct )alues"
Resu t
E_Name Hansen( Ola Svendson( Tove Svendson( Ste-0en %ette"sen( &a"i
T,e SQL SELECT I$TO statement can !e used to create !ac4u co ies of ta!les"
Or *e can select onl# t,e columns *e *ant into t,e ne* ta!le:
SELECT column_name(s) *'TO ne2_table_name I*' e=te"naldatabaseJ FROM old_tablename
Ma=e a Ba*=u) Co)' 3 $o* *e *ant to ma4e an e'act co # of t,e data in our 7.ersons7 ta!le" We use t,e follo*ing SQL statement:
SELECT * *'TO %e"sons_!ac<uFROM %e"sons
We can also use t,e I$ clause to co # t,e ta!le into anot,er data!ase:
SELECT * *'TO %e"sons_!ac<u- *' /!ac<u-Cmdb/ FROM %e"sons
SELECT %e"sonsCLast'ame(O"de"sCO"de"'o *'TO %e"sons_O"de"_!ac<uFROM %e"sons *''ER DO*' O"de"s O' %e"sonsC%_*d.O"de"sC%_*d
T,e data t# e s ecifies *,at t# e of data t,e column can ,old" <or a com lete reference of all t,e data t# es a)aila!le in 0S Access+ 0#SQL+ and SQL Ser)er+ go to our com lete /ata T# es reference"
T,e .;Id column is of t# e int and *ill ,old a num!er" T,e Last$ame+ <irst$ame+ Address+ and Cit# columns are of t# e )arc,ar *it, a ma'imum lengt, of 6EE c,aracters" T,e em t# 7.ersons7 ta!le *ill no* loo4 li4e t,is:
P_Id LastName FirstName Address City
T,e em t# ta!le can !e filled *it, data *it, t,e I$SERT I$TO statement"
SQL Constraints
SQL Constraints
Constraints are used to limit t,e t# e of data t,at can go into a ta!le" Constraints can !e s ecified *,en a ta!le is created %*it, t,e CREATE TA2LE statement& or after t,e ta!le is created %*it, t,e ALTER TA2LE statement&" We *ill focus on t,e follo*ing constraints:
To allo* naming of a U$IQUE constraint+ and for defining a U$IQUE constraint on multi le columns+ use t,e follo*ing SQL s#nta': M'SQL > SQL Ser5er > -ra* e > MS &**ess:
CRE>TE T>!LE %e"sons ( %_*d int 'OT 'BLL( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66)( CO'STR>*'T uc_%e"son*) B'*NBE (%_*d(Last'ame) )
To allo* naming of a U$IQUE constraint+ and for defining a U$IQUE constraint on multi le columns+ use t,e follo*ing SQL s#nta': M'SQL > SQL Ser5er > -ra* e > MS &**ess:
>LTER T>!LE %e"sons >)) CO'STR>*'T uc_%e"son*) B'*NBE (%_*d(Last'ame)
M'SQL:
>LTER T>!LE %e"sons )RO% *')EO uc_%e"son*)
To allo* naming of a .RI0ARB :EB constraint+ and for defining a .RI0ARB :EB constraint on multi le columns+ use t,e follo*ing SQL s#nta': M'SQL > SQL Ser5er > -ra* e > MS &**ess:
CRE>TE T>!LE %e"sons ( %_*d int 'OT 'BLL( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66)( CO'STR>*'T -<_%e"son*) %R*M>R3 &E3 (%_*d(Last'ame) )
To allo* naming of a .RI0ARB :EB constraint+ and for defining a .RI0ARB :EB constraint on multi le columns+ use t,e follo*ing SQL s#nta': M'SQL > SQL Ser5er > -ra* e > MS &**ess:
Note: If #ou use t,e ALTER TA2LE statement to add a rimar# 4e#+ t,e rimar# 4e# column%s& must alread# ,a)e !een declared to not contain $ULL )alues %*,en t,e ta!le *as first created&"
O_Id 1
P_Id $ $
$ @
@65 @65 1
$ote t,at t,e 7.;Id7 column in t,e 7Orders7 ta!le oints to t,e 7.;Id7 column in t,e 7.ersons7 ta!le" T,e 7.;Id7 column in t,e 7.ersons7 ta!le is t,e .RI0ARB :EB in t,e 7.ersons7 ta!le" T,e 7.;Id7 column in t,e 7Orders7 ta!le is a <OREIJ$ :EB in t,e 7Orders7 ta!le" T,e <OREIJ$ :EB constraint is used to re)ent actions t,at *ould destro# lin4s !et*een ta!les" T,e <OREIJ$ :EB constraint also re)ents t,at in)alid data form !eing inserted into t,e foreign 4e# column+ !ecause it ,as to !e one of t,e )alues contained in t,e ta!le it oints to"
O_*d int 'OT 'BLL %R*M>R3 &E3( O"de"'o int 'OT 'BLL( %_*d int FORE*:' &E3 REFERE'CES %e"sons(%_*d) )
To allo* naming of a <OREIJ$ :EB constraint+ and for defining a <OREIJ$ :EB constraint on multi le columns+ use t,e follo*ing SQL s#nta': M'SQL > SQL Ser5er > -ra* e > MS &**ess:
CRE>TE T>!LE O"de"s ( O_*d int 'OT 'BLL( O"de"'o int 'OT 'BLL( %_*d int( %R*M>R3 &E3 (O_*d)( CO'STR>*'T ;<_%e"O"de"s FORE*:' &E3 (%_*d) REFERE'CES %e"sons(%_*d) )
To allo* naming of a <OREIJ$ :EB constraint+ and for defining a <OREIJ$ :EB constraint on multi le columns+ use t,e follo*ing SQL s#nta': M'SQL > SQL Ser5er > -ra* e > MS &**ess:
>LTER T>!LE O"de"s >)) CO'STR>*'T ;<_%e"O"de"s FORE*:' &E3 (%_*d) REFERE'CES %e"sons(%_*d)
Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66)( CHEC& (%_*d90) )
To allo* naming of a CHEC: constraint+ and for defining a CHEC: constraint on multi le columns+ use t,e follo*ing SQL s#nta': M'SQL > SQL Ser5er > -ra* e > MS &**ess:
CRE>TE T>!LE %e"sons ( %_*d int 'OT 'BLL( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66)( CO'STR>*'T c0<_%e"son CHEC& (%_*d90 >') Cit+./Sandnes/) )
To allo* naming of a CHEC: constraint+ and for defining a CHEC: constraint on multi le columns+ use t,e follo*ing SQL s#nta': M'SQL > SQL Ser5er > -ra* e > MS &**ess:
>LTER T>!LE %e"sons >)) CO'STR>*'T c0<_%e"son CHEC& (%_*d90 >') Cit+./Sandnes/)
Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66) )EF>BLT /Sandnes/ )
T,e /E<AULT constraint can also !e used to insert s#stem )alues+ !# using functions li4e JET/ATE%&:
CRE>TE T>!LE O"de"s ( O_*d int 'OT 'BLL( O"de"'o int 'OT 'BLL( %_*d int( O"de")ate date )EF>BLT :ET)>TE() )
T,e CREATE I$/EK statement is used to create inde'es in ta!les" Inde'es allo* t,e data!ase a lication to find data fastI *it,out reading t,e *,ole ta!le"
#nde/es
An inde' can !e created in a ta!le to find data more (uic4l# and efficientl#" T,e users cannot see t,e inde'es+ t,e# are -ust used to s eed u searc,es5(ueries" Note: U dating a ta!le *it, inde'es ta4es more time t,an u dating a ta!le *it,out %!ecause t,e inde'es also need an u date&" So #ou s,ould onl# create inde'es on columns %and ta!les& t,at *ill !e fre(uentl# searc,ed against"
Note: T,e s#nta' for creating inde'es )aries amongst different data!ases" T,erefore: C,ec4 t,e s#nta' for creating inde'es in #our data!ase"
If #ou *ant to create an inde' on a com!ination of columns+ #ou can list t,e column names *it,in t,e arent,eses+ se arated !# commas:
CRE>TE *')EO %*nde= O' %e"sons (Last'ame( Fi"st'ame)
Inde'es+ ta!les+ and data!ases can easil# !e deleted5remo)ed *it, t,e /RO. statement"
To delete a column in a ta!le+ use t,e follo*ing s#nta' %notice t,at some data!ase s#stems donCt allo* deleting a column&:
>LTER T>!LE table_name )RO% COLBM' column_name
$o* *e *ant to add a column named 7/ateOf2irt,7 in t,e 7.ersons7 ta!le" We use t,e follo*ing SQL statement:
>LTER T>!LE %e"sons >)) )ateO;!i"t0 date
$otice t,at t,e ne* column+ 7/ateOf2irt,7+ is of t# e date and is going to ,old a date" T,e data t# e s ecifies *,at t# e of data t,e column can ,old" <or a com lete reference of all t,e data t# es a)aila!le in 0S Access+ 0#SQL+ and SQL Ser)er+ go to our com lete /ata T# es reference" T,e 7.ersons7 ta!le *ill no* li4e t,is:
P_Id LastName FirstName Address City DateO"#irt$
Hansen Svendson
%ette"sen
$otice t,at t,e 7/ateOf2irt,7 column is no* of t# e #ear and is going to ,old a #ear in a t*o3 digit or four3digit format"
Auto3increment allo*s a uni(ue num!er to !e generated *,en a ne* record is inserted into a ta!le"
0#SQL uses t,e AUTO;I$CRE0E$T 4e#*ord to erform an auto3increment feature" 2# default+ t,e starting )alue for AUTO;I$CRE0E$T is 8+ and it *ill increment !# 8 for eac, ne* record" To let t,e AUTO;I$CRE0E$T se(uence start *it, anot,er )alue+ use t,e follo*ing SQL statement:
>LTER T>!LE %e"sons >BTO_*'CREME'T.100
To insert a ne* record into t,e 7.ersons7 ta!le+ *e *ill not ,a)e to s ecif# a )alue for t,e 7.;Id7 column %a uni(ue )alue *ill !e added automaticall#&:
*'SERT *'TO %e"sons (Fi"st'ame(Last'ame) A>LBES (/La"s/(/Monsen/)
T,e SQL statement a!o)e *ould insert a ne* record into t,e 7.ersons7 ta!le" T,e 7.;Id7 column *ould !e assigned a uni(ue )alue" T,e 7<irst$ame7 column *ould !e set to 7Lars7 and t,e 7Last$ame7 column *ould !e set to 70onsen7"
T,e 0S SQL Ser)er uses t,e I/E$TITB 4e#*ord to erform an auto3increment feature" 2# default+ t,e starting )alue for I/E$TITB is 8+ and it *ill increment !# 8 for eac, ne* record" To s ecif# t,at t,e 7.;Id7 column s,ould start at )alue 89 and increment !# E+ c,ange t,e identit# to I/E$TITB%89+E&" To insert a ne* record into t,e 7.ersons7 ta!le+ *e *ill not ,a)e to s ecif# a )alue for t,e 7.;Id7 column %a uni(ue )alue *ill !e added automaticall#&:
*'SERT *'TO %e"sons (Fi"st'ame(Last'ame) A>LBES (/La"s/(/Monsen/)
T,e SQL statement a!o)e *ould insert a ne* record into t,e 7.ersons7 ta!le" T,e 7.;Id7 column *ould !e assigned a uni(ue )alue" T,e 7<irst$ame7 column *ould !e set to 7Lars7 and t,e 7Last$ame7 column *ould !e set to 70onsen7"
T,e 0S Access uses t,e AUTOI$CRE0E$T 4e#*ord to erform an auto3increment feature" 2# default+ t,e starting )alue for AUTOI$CRE0E$T is 8+ and it *ill increment !# 8 for eac, ne* record" To s ecif# t,at t,e 7.;Id7 column s,ould start at )alue 89 and increment !# E+ c,ange t,e autoincrement to AUTOI$CRE0E$T%89+E&" To insert a ne* record into t,e 7.ersons7 ta!le+ *e *ill not ,a)e to s ecif# a )alue for t,e 7.;Id7 column %a uni(ue )alue *ill !e added automaticall#&:
*'SERT *'TO %e"sons (Fi"st'ame(Last'ame) A>LBES (/La"s/(/Monsen/)
T,e SQL statement a!o)e *ould insert a ne* record into t,e 7.ersons7 ta!le" T,e 7.;Id7 column *ould !e assigned a uni(ue )alue" T,e 7<irst$ame7 column *ould !e set to 7Lars7 and t,e 7Last$ame7 column *ould !e set to 70onsen7"
T,e code a!o)e creates a se(uence o!-ect called se(; erson+ t,at starts *it, 8 and *ill increment !# 8" It *ill also cac,e u to 89 )alues for erformance" T,e cac,e o tion s ecifies ,o* man# se(uence )alues *ill !e stored in memor# for faster access" To insert a ne* record into t,e 7.ersons7 ta!le+ *e *ill ,a)e to use t,e ne't)al function %t,is function retrie)es t,e ne't )alue from se(; erson se(uence&:
*'SERT *'TO %e"sons (%_*d(Fi"st'ame(Last'ame) A>LBES (se7_-e"sonCne=tval(/La"s/(/Monsen/)
T,e SQL statement a!o)e *ould insert a ne* record into t,e 7.ersons7 ta!le" T,e 7.;Id7 column *ould !e assigned t,e ne't num!er from t,e se(; erson se(uence" T,e 7<irst$ame7 column *ould !e set to 7Lars7 and t,e 7Last$ame7 column *ould !e set to 70onsen7"
SQL 3ie$s
A )ie* is a )irtual ta!le" T,is c,a ter s,o*s ,o* to create+ u date+ and delete a )ie*"
,HERE condition
Note: A )ie* al*a#s s,o*s u 3to3date data1 T,e data!ase engine recreates t,e data+ using t,e )ie*Cs SQL statement+ e)er# time a user (ueries a )ie*"
Anot,er )ie* in t,e $ort,*ind sam le data!ase selects e)er# roduct in t,e 7.roducts7 ta!le *it, a unit rice ,ig,er t,an t,e a)erage unit rice:
CRE>TE A*E, I%"oducts >bove >ve"a#e %"iceJ >S SELECT %"oduct'ame(Bnit%"ice FROM %"oducts ,HERE Bnit%"ice9(SELECT >A:(Bnit%"ice) FROM %"oducts)
Anot,er )ie* in t,e $ort,*ind data!ase calculates t,e total sale for eac, categor# in 8NNO" $ote t,at t,is )ie* selects its data from anot,er )ie* called 7.roduct Sales for 8NNO7:
CRE>TE A*E, ICate#o"+ Sales Fo" 144FJ >S SELECT )*ST*'CT Cate#o"+'ame(Sum(%"oductSales) >S Cate#o"+Sales FROM I%"oduct Sales ;o" 144FJ :ROB% !3 Cate#o"+'ame
We can also add a condition to t,e (uer#" $o* *e *ant to see t,e total sale onl# for t,e categor# 72e)erages7:
SELECT * FROM ICate#o"+ Sales Fo" 144FJ ,HERE Cate#o"+'ame./!eve"a#es/
$o* *e *ant to add t,e 7Categor#7 column to t,e 7Current .roduct List7 )ie*" We *ill u date t,e )ie* *it, t,e follo*ing SQL:
CRE>TE A*E, ICu""ent %"oduct ListJ >S SELECT %"oduct*)(%"oduct'ame(Cate#o"+ FROM %"oducts ,HERE )iscontinued.'o
SQL Dates
T,e most difficult art *,en *or4ing *it, dates is to !e sure t,at t,e format of t,e date #ou are tr#ing to insert+ matc,es t,e format of t,e date column in t,e data!ase" As long as #our data contains onl# t,e date ortion+ #our (ueries *ill *or4 as e' ected" Ho*e)er+ if a time ortion is in)ol)ed+ it gets com licated" 2efore tal4ing a!out t,e com lications of (uer#ing for dates+ *e *ill loo4 at t,e most im ortant !uilt3in functions for *or4ing *it, dates"
T,e follo*ing ta!le lists t,e most im ortant !uilt3in date functions in SQL Ser)er:
F%nction :ET)>TE() )>TE%>RT() )>TE>))() )>TE)*FF() CO'AERT() Description Retu"ns t0e cu""ent date and time Retu"ns a sin#le -a"t o; a datePtime >dds o" subt"acts a s-eci;ied time inte"val ;"om a date Retu"ns t0e time bet2een t2o dates )is-la+s datePtime data in di;;e"ent ;o"mats
)>TE Q ;o"mat 3333QMMQ)) )>TET*ME Q ;o"mat1 3333QMMQ)) HH1MM1SS T*MEST>M% Q ;o"mat1 3333QMMQ)) HH1MM1SS 3E>R Q ;o"mat 3333 o" 33
SQL Ser5er comes *it, t,e follo*ing data t# es for storing a date or a date5time )alue in t,e data!ase:
)>TE Q ;o"mat 3333QMMQ)) )>TET*ME Q ;o"mat1 3333QMMQ)) HH1MM1SS SM>LL)>TET*ME Q ;o"mat1 3333QMMQ)) HH1MM1SS T*MEST>M% Q ;o"mat1 a uni7ue numbe"
Note: T,e date t# es are c,osen for a column *,en #ou create a ne* ta!le in #our data!ase1 <or an o)er)ie* of all data t# es a)aila!le+ go to our com lete /ata T# es reference"
$o* *e *ant to select t,e records *it, an Order/ate of 7699P3883887 from t,e ta!le a!o)e" We use t,e follo*ing SELECT statement:
SELECT * FROM O"de"s ,HERE O"de")ate./ 00MQ11Q11/
$o*+ assume t,at t,e 7Orders7 ta!le loo4s li4e t,is %notice t,e time com onent in t,e 7Order/ate7 column&:
OrderId 1 Prod%ctName :eitost Camembe"t %ie""ot $ @ MoHHa"ella di :iovanni Masca"-one Fabioli OrderDate 00MQ11Q11 1$1 $1@@ 00MQ11Q04 161@61 1 00MQ11Q11 1111 101 00MQ10Q 4 1@165164
*e *ill get no result1 T,is is !ecause t,e (uer# is loo4ing onl# for dates *it, no time ortion" Ti): If #ou *ant to 4ee #our (ueries sim le and eas# to maintain+ do not allo* time com onents in #our dates1
$ULL )alues re resent missing un4no*n data" 2# default+ a ta!le column can ,old $ULL )alues" T,is c,a ter *ill e' lain t,e IS $ULL and IS $OT $ULL o erators"
Su ose t,at t,e 7Address7 column in t,e 7.ersons7 ta!le is o tional" T,is means t,at if *e insert a record *it, no )alue for t,e 7Address7 column+ t,e 7Address7 column *ill !e sa)ed *it, a $ULL )alue"
Ho* can *e test for $ULL )aluesL It is not ossi!le to test for $ULL )alues *it, com arison o erators+ suc, as @+ >+ or >?" We *ill ,a)e to use t,e IS $ULL and IS $OT $ULL o erators instead"
SQL #S NULL
Ho* do *e select onl# t,e records *it, $ULL )alues in t,e 7Address7 columnL We *ill ,a)e to use t,e IS $ULL o erator:
SELECT Last'ame(Fi"st'ame(>dd"ess FROM %e"sons ,HERE >dd"ess *S 'BLL
In t,e ne't c,a ter *e *ill loo4 at t,e IS$ULL%&+ $ML%&+ I<$ULL%& and COALESCE%& functions"
Su
ose t,at t,e 7UnitsOnOrder7 column is o tional+ and ma# contain $ULL )alues"
In t,e e'am le a!o)e+ if an# of t,e 7UnitsOnOrder7 )alues are $ULL+ t,e result is $ULL" 0icrosoftCs IS$ULL%& function is used to s ecif# ,o* *e *ant to treat $ULL )alues" T,e $ML%&+ I<$ULL%&+ and COALESCE%& functions can also !e used to ac,ie)e t,e same result" In t,is case *e *ant $ULL )alues to !e Qero" 2elo*+ if 7UnitsOnOrder7 is $ULL it *ill not ,arm t,e calculation+ !ecause IS$ULL%& returns a Qero if t,e )alue is $ULL: SQL Ser5er > MS &**ess
SELECT %"oduct'ame(Bnit%"ice*(Bnits*nStoc<R*S'BLL(BnitsOnO"de"(0)) FROM %"oducts
-ra* e Oracle does not ,a)e an IS$ULL%& function" Ho*e)er+ *e can use t,e $ML%& function to ac,ie)e t,e same result:
SELECT %"oduct'ame(Bnit%"ice*(Bnits*nStoc<R'AL(BnitsOnO"de"(0)) FROM %"oducts
M'SQL 0#SQL does ,a)e an IS$ULL%& function" Ho*e)er+ it *or4s a little !it different from 0icrosoftCs IS$ULL%& function" In 0#SQL *e can use t,e I<$ULL%& function+ li4e t,is:
SELECT %"oduct'ame(Bnit%"ice*(Bnits*nStoc<R*F'BLL(BnitsOnO"de"(0)) FROM %"oducts
SQL Data Ty es
/ata t# es and ranges for 0icrosoft Access+ 0#SQL and SQL Ser)er"
Memo
!+te
b+tes
>llo2s 20ole numbe"s bet2een Q (1@F(@M$(5@M and (1@F(@M$(5@F @ b+tes Sin#le -"ecision ;loatin#Q-ointC ,ill 0andle most decimals )ouble -"ecision ;loatin#Q-ointC ,ill 0andle most decimals Bse ;o" cu""enc+C Holds u- to 16 di#its o; 20ole dolla"s( -lus @ decimal -lacesC Tip) 3ou can c0oose 20ic0 count"+/s cu""enc+ to use @ b+tes M b+tes M b+tes
>uto'umbe"
>uto'umbe" ;ields automaticall+ #ive eac0 "eco"d its o2n numbe"( @ b+tes usuall+ sta"tin# at 1 Bse ;o" dates and times M b+tes
)atePTime 3esP'o
> lo#ical ;ield can be dis-la+ed as 3esP'o( T"uePFalse( o" OnPO;;C 1 bit *n code( use t0e constants T"ue and False (e7uivalent to Q1 and 0)C Note) 'ull values a"e not allo2ed in 3esP'o ;ields Can sto"e -ictu"es( audio( video( o" ot0e" !LO!s (!ina"+ La"#e O!Eects) Contain lin<s to ot0e" ;iles( includin# 2eb -a#es Let +ou t+-e a list o; o-tions( 20ic0 can t0en be c0osen ;"om a d"o-Qdo2n list @ b+tes u- to 1:!
Ole ObEect
A>RCH>R(siHe)
c0a"acte"s)C T0e ma=imum siHe is s-eci;ied in -a"ent0esisC Can sto"e u- to 66 c0a"acte"sC Note) *; +ou -ut a #"eate" value t0an 66 it 2ill be conve"ted to a TEOT t+-e T*'3TEOT TEOT !LO! ME)*BMTEOT ME)*BM!LO! LO':TEOT LO':!LO! Holds a st"in# 2it0 a ma=imum len#t0 o; 66 c0a"acte"s Holds a st"in# 2it0 a ma=imum len#t0 o; 56(6$6 c0a"acte"s Fo" !LO!s (!ina"+ La"#e O!Eects)C Holds u- to 56(6$6 b+tes o; data Holds a st"in# 2it0 a ma=imum len#t0 o; 15(FFF( 16 c0a"acte"s Fo" !LO!s (!ina"+ La"#e O!Eects)C Holds u- to 15(FFF( 16 b+tes o; data Holds a st"in# 2it0 a ma=imum len#t0 o; @( 4@(45F( 46 c0a"acte"s Fo" !LO!s (!ina"+ La"#e O!Eects)C Holds u- to @( 4@(45F( 46 b+tes o; data
E'BM(=(+(H(etcC) Let +ou ente" a list o; -ossible valuesC 3ou can list u- to 566$6 values in an E'BM listC *; a value is inse"ted t0at is not in t0e list( a blan< value 2ill be inse"tedC
Note: T,e )alues are sorted in t,e order #ou enter t,em" Bou enter t,e ossi!le )alues in t,is format: E$U0%CKC+CBC+CRC&
SET Simila" to E'BM e=ce-t t0at SET ma+ contain u- to 5@ list items and can sto"e mo"e t0an one c0oice
Nu$ber t')es:
Data type T*'3*'T(siHe) Description Q1 M to 1 F no"malC 0 to 66 B'S*:'E)*C T0e ma=imum numbe" o; di#its ma+ be s-eci;ied in -a"ent0esis Q$ F5M to $ F5F no"malC 0 to 566$6 B'S*:'E)*C T0e ma=imum numbe" o; di#its ma+ be s-eci;ied in -a"ent0esis
SM>LL*'T(siHe)
ME)*BM*'T(siHe) QM$MM50M to M$MM50F no"malC 0 to 15FFF 16 B'S*:'E)*C T0e ma=imum numbe" o; di#its ma+ be s-eci;ied in -a"ent0esis *'T(siHe) Q 1@F@M$5@M to 1@F@M$5@F no"malC 0 to @ 4@45F 46 B'S*:'E)*C T0e ma=imum numbe" o; di#its ma+ be s-eci;ied in -a"ent0esis Q4 $$F 0$5M6@FF6M0M to 4 $$F 0$5M6@FF6M0F no"malC 0 to 1M@@5F@@0F$F04661516 B'S*:'E)*C T0e ma=imum numbe" o; di#its ma+
!*:*'T(siHe)
be s-eci;ied in -a"ent0esis FLO>T(siHe(d) > small numbe" 2it0 a ;loatin# decimal -ointC T0e ma=imum numbe" o; di#its ma+ be s-eci;ied in t0e siHe -a"amete"C T0e ma=imum numbe" o; di#its to t0e "i#0t o; t0e decimal -oint is s-eci;ied in t0e d -a"amete" > la"#e numbe" 2it0 a ;loatin# decimal -ointC T0e ma=imum numbe" o; di#its ma+ be s-eci;ied in t0e siHe -a"amete"C T0e ma=imum numbe" o; di#its to t0e "i#0t o; t0e decimal -oint is s-eci;ied in t0e d -a"amete"
)OB!LE(siHe(d)
)EC*M>L(siHe(d) > )OB!LE sto"ed as a st"in# ( allo2in# ;o" a ;i=ed decimal -ointC T0e ma=imum numbe" o; di#its ma+ be s-eci;ied in t0e siHe -a"amete"C T0e ma=imum numbe" o; di#its to t0e "i#0t o; t0e decimal -oint is s-eci;ied in t0e d -a"amete"
=T,e integer t# es ,a)e an e'tra o tion called U$SIJ$E/" $ormall#+ t,e integer goes from an negati)e to ositi)e )alue" Adding t,e U$SIJ$E/ attri!ute *ill mo)e t,at range u so it starts at Qero instead of a negati)e num!er" Date t')es:
Data type )>TE() Description > dateC Fo"mat1 3333QMMQ))
Note: T,e su
)>TET*ME()
*> timestam-C T*MEST>M% values a"e sto"ed as t0e numbe" o; seconds since t0e Bni= e-oc0 (/14F0Q01Q01 00100100/ BTC)C Fo"mat1 3333QMMQ)) HH1MM1SS
Note: T,e su orted range is from C8NO9398398 99:99:98C UTC to C693P3983 9N 93:8S:9OC UTC
T*ME() > timeC Fo"mat1 HH1MM1SS
Note: T,e su
3E>R()
Note: Malues allo*ed in four3digit format: 8N98 to 68EE" Malues allo*ed in t*o3digit format: O9 to TN+ re resenting #ears from 8NO9 to 69TN
=E)en if /ATETI0E and TI0ESTA0. return t,e same format+ t,e# *or4 )er# differentl#" In an I$SERT or U./ATE (uer#+ t,e TI0ESTA0. automaticall# set itself to t,e current date and time" TI0ESTA0. also acce ts )arious formats+ li4e BBBB00//HH00SS+ BB00//HH00SS+ BBBB00//+ or BB00//"
te=t
Uni*ode strings:
Data type nc0a"(n) nva"c0a"(n) nva"c0a"(ma=) nte=t Description Fi=edQlen#t0 Bnicode dataC Ma=imum @(000 c0a"acte"s Aa"iableQlen#t0 Bnicode dataC Ma=imum @(000 c0a"acte"s Aa"iableQlen#t0 Bnicode dataC Ma=imum 6$5(MF0(41 c0a"acte"s Aa"iableQlen#t0 Bnicode dataC Ma=imum :! o; te=t data &tora(e
Binar' t')es:
Data type bit bina"+(n) va"bina"+(n) va"bina"+(ma=) Description >llo2s 0( 1( o" 'BLL Fi=edQlen#t0 bina"+ dataC Ma=imum M(000 b+tes Aa"iableQlen#t0 bina"+ dataC Ma=imum M(000 b+tes Aa"iableQlen#t0 bina"+ dataC Ma=imum :! &tora(e
ima#e
Nu$ber t')es:
Data type tin+int smallint int bi#int Description >llo2s 20ole numbe"s ;"om 0 to 66 >llo2s 20ole numbe"s bet2een Q$ (F5M and $ (F5F &tora(e 1 b+te b+tes
>llo2s 20ole numbe"s bet2een Q (1@F(@M$(5@M and (1@F(@M$(5@F @ b+tes >llo2s 20ole numbe"s bet2een Q4( 4( $($F (0$5(M6@(FF6(M0F Fi=ed -"ecision and scale numbe"sC $($F (0$5(M6@(FF6(M0M and M b+tes
decimal(-(s)
6Q1F b+tes
Allo*s num!ers from 389U3P V8 to 89U3P W8" T,e arameter indicates t,e ma'imum total num!er of digits t,at can !e stored %!ot, to t,e left and to t,e rig,t of t,e decimal oint&" must !e a )alue from 8 to 3P" /efault is 8P" T,e s arameter indicates t,e ma'imum num!er of digits stored to t,e rig,t of t,e decimal oint" s must !e a )alue from 9 to " /efault )alue is 9
nume"ic(-(s) Fi=ed -"ecision and scale numbe"sC 6Q1F b+tes
Allo*s num!ers from 389U3P V8 to 89U3P W8" T,e arameter indicates t,e ma'imum total num!er of digits t,at can !e stored %!ot, to t,e left and to t,e rig,t of t,e decimal oint&" must !e a )alue from 8 to 3P" /efault is 8P" T,e s arameter indicates t,e ma'imum num!er of digits stored to t,e rig,t of t,e decimal oint" s must !e a )alue from 9 to " /efault )alue is 9
smallmone+ mone+ Moneta"+ data ;"om Q 1@(F@MC$5@M to 1@(F@MC$5@F Moneta"+ data ;"om Q4 ($$F( 0$(5M6(@FFC6M0M to 4 ($$F( 0$(5M6(@FFC6M0F @ b+tes M b+tes
;loat(n)
Floatin# -"ecision numbe" data ;"om Q1CF4E R $0M to 1CF4E R $0MC @ o" M b+tes
!#tes" float%6S& ,olds a S3!#te field and float%E3& ,olds an P3!#te field" /efault )alue of n is E3"
"eal Floatin# -"ecision numbe" data ;"om Q$C@0E R $M to $C@0E R $M @ b+tes
Date t')es:
Data type datetime Description F"om Danua"+ 1( 1F6$ to )ecembe" $1( 4444 2it0 an accu"ac+ o; $C$$ milliseconds F"om Danua"+ 1( 0001 to )ecembe" $1( 4444 2it0 an accu"ac+ o; 100 nanoseconds F"om Danua"+ 1( 1400 to Dune 5( 0F4 2it0 an accu"ac+ o; 1 minute Sto"e a date onl+C F"om Danua"+ 1( 0001 to )ecembe" $1( 4444 Sto"e a time onl+ to an accu"ac+ o; 100 nanoseconds T0e same as datetime 2it0 t0e addition o; a time Hone o;;set &tora(e M b+tes
datetime
5QM b+tes
smalldatetime
@ b+tes
timestam-
Sto"es a uni7ue numbe" t0at #ets u-dated eve"+ time a "o2 #ets c"eated o" modi;iedC T0e timestam- value is based u-on an inte"nal cloc< and does not co""es-ond to "eal timeC Eac0 table ma+ 0ave onl+ one timestam- va"iable
SQL ,unctions
>A:() Q Retu"ns t0e ave"a#e value COB'T() Q Retu"ns t0e numbe" o; "o2s F*RST() Q Retu"ns t0e ;i"st value L>ST() Q Retu"ns t0e last value M>O() Q Retu"ns t0e la"#est value M*'() Q Retu"ns t0e smallest value SBM() Q Retu"ns t0e sum
BC>SE() Q Conve"ts a ;ield to u--e" case LC>SE() Q Conve"ts a ;ield to lo2e" case M*)() Q E=t"act c0a"acte"s ;"om a te=t ;ield LE'() Q Retu"ns t0e len#t0 o; a te=t ;ield ROB')() Q Rounds a nume"ic ;ield to t0e numbe" o; decimals s-eci;ied 'O,() Q Retu"ns t0e cu""ent s+stem date and time FORM>T() Q Fo"mats 0o2 a ;ield is to be dis-la+ed
Ti): T,e aggregate functions and t,e scalar functions *ill !e e' lained in details in t,e ne't c,a ters"
$o* *e *ant to find t,e a)erage )alue of t,e 7Order.rice7 fields" We use t,e follo*ing SQL statement:
SELECT >A:(O"de"%"ice) >S O"de">ve"a#e FROM O"de"s
OrderA*era(e 460
$o* *e *ant to find t,e customers t,at ,a)e an Order.rice )alue ,ig,er t,an t,e a)erage Order.rice )alue" We use t,e follo*ing SQL statement:
SELECT Custome" FROM O"de"s ,HERE O"de"%"ice9(SELECT >A:(O"de"%"ice) FROM O"de"s)
T,e COU$T%& function returns t,e num!er of ro*s t,at matc,es a s ecified criteria"
Note: COU$T%/ISTI$CT& *or4s *it, ORACLE and 0icrosoft SQL Ser)er+ !ut not *it, 0icrosoft Access"
$o* *e *ant to count t,e num!er of orders from 7Customer $ilsen7" We use t,e follo*ing SQL statement:
SELECT COB'T(Custome") >S Custome"'ilsen FROM O"de"s ,HERE Custome"./'ilsen/
T,e result of t,e SQL statement a!o)e *ill !e 6+ !ecause t,e customer $ilsen ,as made 6 orders in total:
C%stomerNilsen
*,ic, is t,e num!er of uni(ue customers %Hansen+ $ilsen+ and Densen& in t,e 7Orders7 ta!le"
$o* *e *ant to find t,e first )alue of t,e 7Order.rice7 column" We use t,e follo*ing SQL statement:
SELECT F*RST(O"de"%"ice) >S Fi"stO"de"%"ice FROM O"de"s
orted:
$o* *e *ant to find t,e last )alue of t,e 7Order.rice7 column" We use t,e follo*ing SQL statement:
SELECT L>ST(O"de"%"ice) >S LastO"de"%"ice FROM O"de"s
orted:
LastOrderPrice 100
$o* *e *ant to find t,e largest )alue of t,e 7Order.rice7 column" We use t,e follo*ing SQL statement:
SELECT M>O(O"de"%"ice) >S La"#estO"de"%"ice FROM O"de"s
$o* *e *ant to find t,e smallest )alue of t,e 7Order.rice7 column" We use t,e follo*ing SQL statement:
@ 6 5
$o* *e *ant to find t,e total sum %total order& of eac, customer" We *ill ,a)e to use t,e JROU. 2B statement to grou t,e customers" We use t,e follo*ing SQL statement:
SELECT Custome"(SBM(O"de"%"ice) FROM O"de"s :ROB% !3 Custome"
$ice1 IsnCt itL :& LetCs see *,at ,a ens if *e omit t,e JROU. 2B statement:
SELECT Custome"(SBM(O"de"%"ice) FROM O"de"s
Densen 'ilsen
6F00 6F00
T,e result3set a!o)e is not *,at *e *anted" ,/) anation o+ 2h' the abo5e S,L,CT state$ent *annot be used: T,e SELECT statement a!o)e ,as t*o columns s ecified %Customer and SU0%Order.rice&" T,e 7SU0%Order.rice&7 returns a single )alue %t,at is t,e total sum of t,e 7Order.rice7 column&+ *,ile 7Customer7 returns T )alues %one )alue for eac, ro* in t,e 7Orders7 ta!le&" T,is *ill t,erefore not gi)e us t,e correct result" Ho*e)er+ #ou ,a)e seen t,at t,e JROU. 2B statement sol)es t,is ro!lem"
$o* *e *ant to find if an# of t,e customers ,a)e a total order of less t,an 6999" We use t,e follo*ing SQL statement:
SELECT Custome"(SBM(O"de"%"ice) FROM O"de"s :ROB% !3 Custome" H>A*': SBM(O"de"%"ice)8 000
$o* *e *ant to find if t,e customers 7Hansen7 or 7Densen7 ,a)e a total order of more t,an 8E99" We add an ordinar# WHERE clause to t,e SQL statement:
SELECT Custome"(SBM(O"de"%"ice) FROM O"de"s ,HERE Custome"./Hansen/ OR Custome"./Densen/ :ROB% !3 Custome" H>A*': SBM(O"de"%"ice)91600
Hansen Densen
000 000
$o* *e *ant to select t,e content of t,e 7Last$ame7 and 7<irst$ame7 columns a!o)e+ and con)ert t,e 7Last$ame7 column to u ercase" We use t,e follo*ing SELECT statement:
SELECT BC>SE(Last'ame) as Last'ame(Fi"st'ame FROM %e"sons
$o* *e *ant to select t,e content of t,e 7Last$ame7 and 7<irst$ame7 columns a!o)e+ and con)ert t,e 7Last$ame7 column to lo*ercase" We use t,e follo*ing SELECT statement:
SELECT LC>SE(Last'ame) as Last'ame(Fi"st'ame FROM %e"sons
Description Re7ui"edC T0e ;ield to e=t"act c0a"acte"s ;"om Re7ui"edC S-eci;ies t0e sta"tin# -osition (sta"ts at 1) O-tionalC T0e numbe" o; c0a"acte"s to "etu"nC *; omitted( t0e M*)() ;unction "etu"ns t0e "est o; t0e te=t
$o* *e *ant to e'tract t,e first four c,aracters of t,e 7Cit#7 column a!o)e" We use t,e follo*ing SELECT statement:
SELECT M*)(Cit+(1(@) as SmallCit+ FROM %e"sons
$o* *e *ant to select t,e lengt, of t,e )alues in t,e 7Address7 column a!o)e" We use t,e follo*ing SELECT statement:
SELECT LE'(>dd"ess) as Len#t0O;>dd"ess FROM %e"sons
Parameter column_name
decimals
$o* *e *ant to dis la# t,e roduct name and t,e rice rounded to t,e nearest integer" We use t,e follo*ing SELECT statement:
SELECT %"oduct'ame( ROB')(Bnit%"ice(0) as Bnit%"ice FROM %"oducts
$o* *e *ant to dis la# t,e roducts and rices er toda#Cs date" We use t,e follo*ing SELECT statement:
SELECT %"oduct'ame( Bnit%"ice( 'o2() as %e")ate FROM %"oducts
$o* *e *ant to dis la# t,e roducts and rices er toda#Cs date %*it, toda#Cs date dis la#ed in t,e follo*ing format 7BBBB3003//7&" We use t,e follo*ing SELECT statement:
SELECT %"oduct'ame( Bnit%"ice( FORM>T('o2()(/3333QMMQ))/) as %e")ate FROM %"oducts
&ynta0 SELECT column_name(s) FROM table_name ,HERE condition >')?OR condition >LTER T>!LE table_name >)) column_name datat+-e
>LTER T>!LE
CRE>TE *')EO
or
)ELETE
or /ELETE <RO0 ta!le;name %Note: /eletes t,e entire ta!le11& /ELETE = <RO0 ta!le;name %Note: /eletes t,e entire ta!le11&
)RO% )>T>!>SE )RO% *')EO )RO% )>T>!>SE database_name )RO% *')EO table_nameCinde=_name (SNL Se"ve") )RO% *')EO inde=_name O' table_name (MS >ccess) )RO% *')EO inde=_name ()! PO"acle) >LTER T>!LE table_name )RO% *')EO inde=_name (M+SNL) )RO% T>!LE table_name SELECT column_name( a##"e#ate_;unction(column_name) FROM table_name ,HERE column_name o-e"ato" value :ROB% !3 column_name SELECT column_name( a##"e#ate_;unction(column_name) FROM table_name ,HERE column_name o-e"ato" value :ROB% !3 column_name H>A*': a##"e#ate_;unction(column_name) o-e"ato" value SELECT column_name(s) FROM table_name ,HERE column_name *' (value1(value (CC) *'SERT *'TO table_name
H>A*':
*'
*'SERT *'TO
or I$SERT I$TO ta!le;name %column8+ column6+ column3+"""& MALUES %)alue8+ )alue6+ )alue3+""""&
*''ER DO*' SELECT column_name(s) FROM table_name1 *''ER DO*' table_name O' table_name1Ccolumn_name.table_name Ccolumn_name SELECT column_name(s) FROM table_name1 LEFT DO*' table_name O' table_name1Ccolumn_name.table_name Ccolumn_name SELECT column_name(s) FROM table_name1 R*:HT DO*' table_name O' table_name1Ccolumn_name.table_name Ccolumn_name SELECT column_name(s) FROM table_name1 FBLL DO*' table_name O' table_name1Ccolumn_name.table_name Ccolumn_name SELECT column_name(s) FROM table_name ,HERE column_name L*&E -atte"n SELECT column_name(s) FROM table_name OR)ER !3 column_name I>SC?)ESCJ SELECT column_name(s) FROM table_name SELECT * FROM table_name SELECT )*ST*'CT column_name(s) FROM table_name SELECT * *'TO ne2_table_name I*' e=te"naldatabaseJ
LEFT DO*'
R*:HT DO*'
FBLL DO*'
L*&E
OR)ER !3
SELECT
SELECT *
SELECT )*ST*'CT
SELECT *'TO
FROM old_table_name
B'*O' >LL
B%)>TE
,HERE