Sei sulla pagina 1di 107

TUTORIAL SOURCE : W3SCHOOLS

Introduction to SQL

SQL is a standard language for accessing and mani ulating data!ases"

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

What Can SQL do?


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

SQL is a Standard - BUT....


Alt,oug, SQL is an A$SI %American $ational Standards Institute& standard+ t,ere are man# different )ersions of t,e SQL language"

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

Using SQL in Your Web Site


To !uild a *e! site t,at s,o*s some data from a data!ase+ #ou *ill need t,e follo*ing:

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"

(ee) in Mind That...

SQL is not case sensiti)e

Se$i*o on a+ter SQL State$ents?


Some data!ase s#stems re(uire a semicolon at t,e end of eac, SQL statement" Semicolon is t,e standard *a# to se arate eac, SQL statement in data!ase s#stems t,at allo* more t,an one SQL statement to !e e'ecuted in t,e same call to t,e ser)er" We are using 0S Access and SQL Ser)er 6999 and *e do not ,a)e to ut a semicolon after eac, SQL statement+ !ut some data!ase rograms force #ou to use it"

SQL DML and DDL


SQL can !e di)ided into t*o arts: T,e /ata 0ani ulation Language %/0L& and t,e /ata /efinition Language %//L&" T,e (uer# and u date commands form t,e /0L art of SQL:

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 &LT,R D&T&B&S, 3 modifies a data!ase CR,&T, T&BL, 3 creates a ne* ta!le &LT,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'

SQL SELECT Statement

T,is c,a ter *ill e' lain t,e SELECT and t,e SELECT = statements"

The SQL S,L,CT State$ent


T,e SELECT statement is used to select data from a data!ase" T,e result is stored in a result ta!le+ called t,e result3set"

SQL SELECT Syntax


SELECT column_name(s) FROM table_name

and
SELECT * FROM table_name

Note: SQL is not case sensiti)e" SELECT is t,e same as select"

&n SQL S,L,CT ,/a$) e


T,e 7.ersons7 ta!le:

P_Id 1

LastName Hansen Svendson

FirstName Ola Tove &a"i

Address Timoteivn 10 !o"#vn $ Sto"#t 0

City Sandnes Sandnes Stavan#e"

%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

T,e result3set *ill loo4 li4e t,is:


LastName Hansen Svendson %ette"sen FirstName Ola Tove &a"i

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

Ola Tove &a"i

Timoteivn 10 !o"#vn $ Sto"#t 0

Sandnes Sandnes Stavan#e"

%ette"sen

SQL SELECT DISTINCT Statement

T,is c,a ter *ill e' lain t,e SELECT /ISTI$CT statement"

The SQL S,L,CT D#ST#NCT State$ent


In a ta!le+ some of t,e columns ma# contain du licate )alues" T,is is not a ro!lem+ ,o*e)er+ sometimes #ou *ill *ant to list onl# t,e different %distinct& )alues in a ta!le" T,e /ISTI$CT 4e#*ord can !e used to return onl# distinct %different& )alues"

SQL SELECT DISTINCT Syntax


SELECT )*ST*'CT column_name(s) FROM table_name

S,L,CT D#ST#NCT ,/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 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:

SELECT )*ST*'CT Cit+ FROM %e"sons

T,e result3set *ill loo4 li4e t,is:


City Sandnes Stavan#e"

SQL WHERE Clause

T,e WHERE clause is used to filter records"

The W1,R, C ause


T,e WHERE clause is used to e'tract onl# t,ose records t,at fulfill a s ecified criterion"

SQL WHERE Syntax


SELECT column_name(s) FROM table_name ,HERE column_name o-e"ato" value

W1,R, C ause ,/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 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/

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 !o"#vn $ City Sandnes Sandnes

Quotes &round Te/t %ie ds


SQL uses single (uotes around te't )alues %most data!ase s#stems *ill also acce t dou!le (uotes&" Alt,oug,+ numeric )alues s,ould not !e enclosed in (uotes" <or te't )alues:
T0is is co""ect1 SELECT * FROM %e"sons ,HERE Fi"st'ame./Tove/ T0is is 2"on#1 SELECT * FROM %e"sons ,HERE Fi"st'ame.Tove

<or numeric )alues:


T0is is co""ect1 SELECT * FROM %e"sons ,HERE 3ea".1456 T0is is 2"on#1

SELECT * FROM %e"sons ,HERE 3ea"./1456/

-)erators & o2ed in the W1,R, C ause


Wit, t,e WHERE clause+ t,e follo*ing o erators can !e used:
Operator . 89 9 8 9. 8. E7ual 'ot e7ual :"eate" t0an Less t0an :"eate" t0an o" e7ual Less t0an o" e7ual Description

!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

Note: In some )ersions of SQL t,e >? o erator ma# !e *ritten as 1@

SQL AND & OR O erators

T,e A$/ A OR o erators are used to filter records !ased on more t,an one condition"

The &ND 3 -R -)erators


T,e A$/ o erator dis la#s a record if !ot, t,e first condition and t,e second condition is true"

T,e OR o erator dis la#s a record if eit,er t,e first condition or t,e second condition is true"

&ND -)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 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/

T,e result3set *ill loo4 li4e t,is:


P_Id LastName Svendson FirstName Tove Address !o"#vn $ City Sandnes

-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/

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 !o"#vn $ City Sandnes Sandnes

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/)

T,e result3set *ill loo4 li4e t,is:


P_Id LastName Svendson FirstName Tove Address !o"#vn $ City Sandnes

SQL ORDER !" #ey$ord

T,e OR/ER 2B 4e#*ord is used to sort t,e result3set"

The -RD,R BY (e'2ord


T,e OR/ER 2B 4e#*ord is used to sort t,e result3set !# a s ecified column" T,e OR/ER 2B 4e#*ord sort t,e records in ascending order !# default" If #ou *ant to sort t,e records in a descending order+ #ou can use t,e /ESC 4e#*ord"

SQL ORDER !" Syntax


SELECT column_name(s) FROM table_name OR)ER !3 column_name(s) >SC?)ESC

-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

T,e result3set *ill loo4 li4e t,is:


P_Id 1 @ $ LastName Hansen 'ilsen %ette"sen Svendson FirstName Ola Tom &a"i Tove Address Timoteivn 10 Ain#vn $ Sto"#t 0 !o"#vn $ City Sandnes Stavan#e" Stavan#e" Sandnes

-RD,R BY D,SC ,/a$) 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 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 result3set *ill loo4 li4e t,is:


P_Id LastName Svendson $ @ 1 %ette"sen 'ilsen Hansen FirstName Tove &a"i Tom Ola Address !o"#vn $ Sto"#t 0 Ain#vn $ Timoteivn 10 City Sandnes Stavan#e" Stavan#e" Sandnes

SQL INSERT INTO Statement

T,e I$SERT I$TO statement is used to insert ne* records in a ta!le"

The #NS,RT #NT- State$ent


T,e I$SERT I$TO statement is used to insert a ne* ro* in a ta!le"

SQL INSERT INTO Syntax


It is ossi!le to *rite t,e I$SERT I$TO statement in t*o forms" T,e first form doesnCt s ecif# t,e column names *,ere t,e data *ill !e inserted+ onl# t,eir )alues:
*'SERT *'TO table_name A>LBES (value1( value ( value$(CCC)

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)

SQL #NS,RT #NT- ,/a$) e


We ,a)e t,e follo*ing 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 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 7.ersons7 ta!le *ill no* loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson $ @ %ette"sen 'ilsen FirstName Ola Tove &a"i Do0an Address Timoteivn 10 !o"#vn $ Sto"#t 0 !a<<en City Sandnes Sandnes Stavan#e" Stavan#e"

#nsert Data -n ' in S)e*i+ied Co u$ns


It is also ossi!le to onl# add data in s ecific columns"

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/)

T,e 7.ersons7 ta!le *ill no* loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson $ @ 6 %ette"sen 'ilsen TEessem FirstName Ola Tove &a"i Do0an Da<ob Address Timoteivn 10 !o"#vn $ Sto"#t 0 !a<<en City Sandnes Sandnes Stavan#e" Stavan#e"

SQL %&DATE Statement

T,e U./ATE statement is used to u date records in a ta!le"

The U!D&T, State$ent


T,e U./ATE statement is used to u date e'isting records in a ta!le"

SQL %&DATE Syntax


B%)>TE table_name SET column1.value( column .value (CCC ,HERE some_column.some_value

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

SQL U!D&T, ,/a$) e


T,e 7.ersons7 ta!le:
P_Id 1 LastName Hansen Svendson $ @ 6 %ette"sen 'ilsen TEessem FirstName Ola Tove &a"i Do0an Da<ob Address Timoteivn 10 !o"#vn $ Sto"#t 0 !a<<en City Sandnes Sandnes Stavan#e" Stavan#e"

$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/

T,e 7.ersons7 ta!le *ill no* loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson $ @ 6 %ette"sen 'ilsen TEessem FirstName Ola Tove &a"i Do0an Da<ob Address Timoteivn 10 !o"#vn $ Sto"#t 0 !a<<en 'issestien 5F City Sandnes Sandnes Stavan#e" Stavan#e" Sandnes

SQL U!D&T, Warning


2e careful *,en u dating records" If *e ,ad omitted t,e WHERE clause in t,e e'am le a!o)e+ li4e t,is:

B%)>TE %e"sons SET >dd"ess./'issestien 5F/( Cit+./Sandnes/

T,e 7.ersons7 ta!le *ould ,a)e loo4ed li4e t,is:


P_Id 1 LastName Hansen Svendson $ @ 6 %ette"sen 'ilsen TEessem FirstName Ola Tove &a"i Do0an Da<ob Address 'issestien 5F 'issestien 5F 'issestien 5F 'issestien 5F 'issestien 5F City Sandnes Sandnes Sandnes Sandnes Sandnes

SQL DELETE Statement

T,e /ELETE statement is used to delete records in a ta!le"

The D,L,T, State$ent


T,e /ELETE statement is used to delete ro*s in a ta!le"

SQL DELETE Syntax


)ELETE FROM table_name ,HERE some_column.some_value

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

SQL D,L,T, ,/a$) e


T,e 7.ersons7 ta!le:

P_Id 1

LastName Hansen Svendson

FirstName Ola Tove &a"i Do0an Da<ob

Address Timoteivn 10 !o"#vn $ Sto"#t 0 !a<<en 'issestien 5F

City Sandnes Sandnes Stavan#e" Stavan#e" Sandnes

$ @ 6

%ette"sen 'ilsen TEessem

$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/

T,e 7.ersons7 ta!le *ill no* loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson $ @ %ette"sen 'ilsen FirstName Ola Tove &a"i Do0an Address Timoteivn 10 !o"#vn $ Sto"#t 0 !a<<en City Sandnes Sandnes Stavan#e" Stavan#e"

De ete & Ro2s


It is ossi!le to delete all ro*s in a ta!le *it,out deleting t,e ta!le" T,is means t,at t,e ta!le structure+ attri!utes+ and inde'es *ill !e intact:
)ELETE FROM table_name o" )ELETE * FROM table_name

Note: 2e )er# careful *,en deleting records" Bou cannot undo t,is statement1

SQL TO& Clause

The T-! C ause


T,e TO. clause is used to s ecif# t,e num!er of records to return" T,e TO. clause can !e )er# useful on large ta!les *it, t,ousands of records" Returning a large num!er of records can im act on erformance" Note: $ot all data!ase s#stems su ort t,e TO. clause"

SQL Ser'er Syntax


SELECT TO% numbe"?-e"cent column_name(s) FROM table_name

SQL S,L,CT T-! ,4ui5a ent in M'SQL and -ra* e


(ySQL Syntax
SELECT column_name(s) FROM table_name L*M*T numbe"

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

SQL T-! ,/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 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

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 !o"#vn $ City Sandnes Sandnes

SQL T-! !,RC,NT ,/a$) e


T,e 7.ersons7 ta!le:
P_Id 1 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 !o"#vn $ City Sandnes Sandnes

$ @

%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 result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 !o"#vn $ City Sandnes Sandnes

SQL LI#E O erator

T,e LI:E o erator is used in a WHERE clause to searc, for a s ecified attern in a column"

The L#(, -)erator


T,e LI:E o erator is used to searc, for a s ecified attern in a column"

SQL LI#E Syntax


SELECT column_name(s) FROM table_name ,HERE column_name L*&E -atte"n

L#(, -)erator ,/a$) e


T,e 7.ersons7 ta!le:

P_Id 1

LastName Hansen Svendson

FirstName Ola Tove &a"i

Address Timoteivn 10 !o"#vn $ Sto"#t 0

City Sandnes Sandnes Stavan#e"

%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/

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 !o"#vn $ City Sandnes Sandnes

$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/

T,e result3set *ill loo4 li4e t,is:


P_Id $ LastName %ette"sen FirstName &a"i Address Sto"#t 0 City Stavan#e"

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/

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 !o"#vn $ City Sandnes Sandnes

SQL Wildcards

SQL *ildcards can !e used *,en searc,ing for data in a data!ase"

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

SQL Wi d*ard ,/a$) es


We ,a)e t,e follo*ing 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"

Using the 6 Wi d*ard


$o* *e *ant to select t,e ersons li)ing in a cit# t,at starts *it, 7sa7 from t,e 7.ersons7 ta!le" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Cit+ L*&E /saG/

T,e result3set *ill loo4 li4e t,is:

P_Id 1

LastName Hansen Svendson

FirstName Ola Tove

Address Timoteivn 10 !o"#vn $

City Sandnes Sandnes

$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/

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 !o"#vn $ City Sandnes Sandnes

Using the " Wi d*ard


$o* *e *ant to select t,e ersons *it, a first name t,at starts *it, an# c,aracter+ follo*ed !# 7la7 from t,e 7.ersons7 ta!le" We use t,e follo*ing SELECT statement:
SELECT * FROM %e"sons ,HERE Fi"st'ame L*&E /_la/

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen FirstName Ola Address Timoteivn 10 City Sandnes

$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/

T,e result3set *ill loo4 li4e t,is:


P_Id LastName Svendson FirstName Tove Address !o"#vn $ City Sandnes

Using the 7*har ist8 Wi d*ard


$o* *e *ant to select t,e ersons *it, a last name t,at starts *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 /Ibs-JG/

T,e result3set *ill loo4 li4e t,is:


P_Id LastName Svendson $ %ette"sen FirstName Tove &a"i Address !o"#vn $ Sto"#t 0 City Sandnes Stavan#e"

$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/

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen FirstName Ola Address Timoteivn 10 City Sandnes

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 result3set *ill loo4 li4e t,is:


P_Id 1 $ LastName Hansen %ette"sen FirstName Ola &a"i Address Timoteivn 10 Sto"#t 0 City Sandnes Stavan#e"

SQL !ETWEEN O erator

T,e 2ETWEE$ o erator is used in a WHERE clause to select a range of data !et*een t*o )alues"

The B,TW,,N -)erator


T,e 2ETWEE$ o erator selects a range of data !et*een t*o )alues" T,e )alues can !e num!ers+ te't+ or dates"

SQL !ETWEEN Syntax


SELECT column_name(s) FROM table_name ,HERE column_name !ET,EE' value1 >') value

B,TW,,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 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/

T,e result3set *ill loo4 li4e t,is:


P_Id 1 LastName Hansen FirstName Ola Address Timoteivn 10 City Sandnes

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/

T,e result3set *ill loo4 li4e t,is:


P_Id LastName FirstName Address City

Svendson $ %ette"sen

Tove &a"i

!o"#vn $ Sto"#t 0

Sandnes Stavan#e"

SQL Alias

Wit, SQL+ an alias name can !e gi)en to a ta!le or to a column"

SQL & ias


Bou can gi)e a ta!le or a column anot,er name !# using an alias" T,is can !e a good t,ing to do if #ou ,a)e )er# long or com le' ta!le names or column names" An alias name could !e an#t,ing+ !ut usuall# it is s,ort"

SQL Alias Syntax )or Ta*les


SELECT column_name(s) FROM table_name >S alias_name

SQL Alias Syntax )or Columns


SELECT column_name >S alias_name FROM table_name

& ias ,/a$) e


Assume *e ,a)e a ta!le called 7.ersons7 and anot,er ta!le called 7.roduct;Orders7" We *ill gi)e t,e ta!le aliases of 7 7 and 7 o7 res ecti)el#" $o* *e *ant to list all t,e orders t,at 7Ola Hansen7 is res onsi!le for" We use t,e follo*ing SELECT statement:
SELECT -oCO"de"*)( -CLast'ame( -CFi"st'ame FROM %e"sons >S -(

%"oduct_O"de"s >S -o ,HERE -CLast'ame./Hansen/ >') -CFi"st'ame./Ola/

T,e same SELECT statement *it,out aliases:


SELECT %"oduct_O"de"sCO"de"*)( %e"sonsCLast'ame( %e"sonsCFi"st'ame FROM %e"sons( %"oduct_O"de"s ,HERE %e"sonsCLast'ame./Hansen/ >') %e"sonsCFi"st'ame./Ola/

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"

Di++erent SQL :-#Ns


2efore *e continue *it, e'am les+ *e *ill list t,e t# es of DOI$ #ou can use+ and t,e differences !et*een t,em"

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

SQL INNER +OIN #ey$ord

SQL #NN,R :-#N (e'2ord


T,e I$$ER DOI$ 4e#*ord return ro*s *,en t,ere is at least one matc, in !ot, ta!les"

SQL INNER +OIN Syntax


SELECT column_name(s) FROM table_name1 *''ER DO*' table_name O' table_name1Ccolumn_name.table_name Ccolumn_name

!S: I$$ER DOI$ is t,e same as DOI$"

SQL #NN,R :-#N ,/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"

T,e 7Orders7 ta!le:


O_Id 1 OrderNo FFM46 @@5FM $ @ 6 @65 @65 $@F5@ P_Id $ $ 1 1 16

$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 result3set *ill loo4 li4e t,is:


LastName Hansen Hansen %ette"sen %ette"sen FirstName Ola Ola &a"i &a"i OrderNo @65 @65 FFM46 @@5FM

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"

SQL LE,T +OIN #ey$ord

SQL L,%T :-#N (e'2ord


T,e LE<T DOI$ 4e#*ord returns all ro*s from t,e left ta!le %ta!le;name8&+ e)en if t,ere are no matc,es in t,e rig,t ta!le %ta!le;name6&"

SQL LE,T +OIN Syntax


SELECT column_name(s) FROM table_name1 LEFT DO*' table_name O' table_name1Ccolumn_name.table_name Ccolumn_name

!S: In some data!ases LE<T DOI$ is called LE<T OUTER DOI$"

SQL L,%T :-#N ,/a$) e


T,e 7.ersons7 ta!le:
P_Id 1 LastName Hansen FirstName Ola Address Timoteivn 10 City Sandnes

Svendson $ %ette"sen

Tove &a"i

!o"#vn $ Sto"#t 0

Sandnes Stavan#e"

T,e 7Orders7 ta!le:


O_Id 1 OrderNo FFM46 @@5FM $ @ 6 @65 @65 $@F5@ P_Id $ $ 1 1 16

$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 result3set *ill loo4 li4e t,is:


LastName Hansen Hansen %ette"sen %ette"sen Svendson FirstName Ola Ola &a"i &a"i Tove OrderNo @65 @65 FFM46 @@5FM

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&"

SQL RI-HT +OIN #ey$ord

SQL R#;1T :-#N (e'2ord


T,e RIJHT DOI$ 4e#*ord Return all ro*s from t,e rig,t ta!le %ta!le;name6&+ e)en if t,ere are no matc,es in t,e left ta!le %ta!le;name8&"

SQL RI-HT +OIN Syntax


SELECT column_name(s) FROM table_name1 R*:HT DO*' table_name O' table_name1Ccolumn_name.table_name Ccolumn_name

!S: In some data!ases RIJHT DOI$ is called RIJHT OUTER DOI$"

SQL R#;1T :-#N ,/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"

T,e 7Orders7 ta!le:


O_Id 1 OrderNo FFM46 @@5FM $ @65 P_Id $ $ 1

@ 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 result3set *ill loo4 li4e t,is:


LastName Hansen Hansen %ette"sen %ette"sen FirstName Ola Ola &a"i &a"i OrderNo @65 @65 FFM46 @@5FM $@F5@

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&"

SQL ,%LL +OIN #ey$ord

SQL %ULL :-#N (e'2ord


T,e <ULL DOI$ 4e#*ord return ro*s *,en t,ere is a matc, in one of t,e ta!les"

SQL ,%LL +OIN Syntax


SELECT column_name(s) FROM table_name1

FBLL DO*' table_name O' table_name1Ccolumn_name.table_name Ccolumn_name

SQL %ULL :-#N ,/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"

T,e 7Orders7 ta!le:


O_Id 1 OrderNo FFM46 @@5FM $ @ 6 @65 @65 $@F5@ P_Id $ $ 1 1 16

$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

T,e result3set *ill loo4 li4e t,is:

LastName Hansen Hansen %ette"sen %ette"sen Svendson

FirstName Ola Ola &a"i &a"i Tove

OrderNo @65 @65 FFM46 @@5FM

$@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"

SQL %NION O erator

T,e SQL U$IO$ o erator com!ines t*o or more SELECT statements"

The SQL UN#-N -)erator


T,e U$IO$ o erator is used to com!ine t,e result3set of t*o or more SELECT statements" $otice t,at eac, SELECT statement *it,in t,e U$IO$ must ,a)e t,e same num!er of columns" T,e columns must also ,a)e similar data t# es" Also+ t,e columns in eac, SELECT statement must !e in t,e same order"

SQL %NION Syntax


SELECT column_name(s) FROM table_name1 B'*O' SELECT column_name(s) FROM table_name

Note: T,e U$IO$ o erator selects onl# distinct )alues !# default" To allo* du licate )alues+ use U$IO$ ALL"

SQL %NION ALL Syntax


SELECT column_name(s) FROM table_name1 B'*O' >LL SELECT column_name(s) FROM table_name

!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$"

SQL UN#-N ,/a$) e


Loo4 at t,e follo*ing ta!les: <,$) o'ees"Nor2a'<:
E_ID 01 0 0$ 0@ E_Name Hansen( Ola Svendson( Tove Svendson( Ste-0en %ette"sen( &a"i

<,$) 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'

SELECT E_'ame FROM Em-lo+ees_BS>

T,e result3set *ill loo4 li4e t,is:


E_Name Hansen( Ola Svendson( Tove Svendson( Ste-0en %ette"sen( &a"i Tu"ne"( Sall+ &ent( Cla"< Scott( Ste-0en

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"

SQL UN#-N &LL ,/a$) e


$o* *e *ant to list a em lo#ees in $or*a# and USA:
SELECT E_'ame FROM Em-lo+ees_'o"2a+ B'*O' >LL SELECT E_'ame FROM Em-lo+ees_BS>

Resu t
E_Name Hansen( Ola Svendson( Tove Svendson( Ste-0en %ette"sen( &a"i

Tu"ne"( Sall+ &ent( Cla"< Svendson( Ste-0en Scott( Ste-0en

SQL SELECT INTO Statement

T,e SQL SELECT I$TO statement can !e used to create !ac4u co ies of ta!les"

The SQL S,L,CT #NT- State$ent


T,e SELECT I$TO statement selects data from one ta!le and inserts it into a different ta!le" T,e SELECT I$TO statement is most often used to create !ac4u co ies of ta!les"

SQL SELECT INTO Syntax


We can select all columns into t,e ne* ta!le:
SELECT * *'TO ne2_table_name I*' e=te"naldatabaseJ FROM old_tablename

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

SQL S,L,CT #NT- ,/a$) e

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

We can also co # onl# a fe* fields into t,e ne* ta!le:


SELECT Last'ame(Fi"st'ame *'TO %e"sons_!ac<uFROM %e"sons

SQL S,L,CT #NT- - With a W1,R, C ause


We can also add a WHERE clause" T,e follo*ing SQL statement creates a 7.ersons;2ac4u 7 ta!le *it, onl# t,e ersons *,o li)es in t,e cit# 7Sandnes7:
SELECT Last'ame(Fi"stname *'TO %e"sons_!ac<uFROM %e"sons ,HERE Cit+./Sandnes/

SQL S,L,CT #NT- - :oined Tab es


Selecting data from more t,an one ta!le is also ossi!le" T,e follo*ing e'am le creates a 7.ersons;Order;2ac4u 7 ta!le contains data from t,e t*o ta!les 7.ersons7 and 7Orders7:

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

SQL CREATE DATA!ASE Statement

The CR,&T, D&T&B&S, State$ent


T,e CREATE /ATA2ASE statement is used to create a data!ase"

SQL CREATE DATA!ASE Syntax


CRE>TE )>T>!>SE database_name

CR,&T, D&T&B&S, ,/a$) e


$o* *e *ant to create a data!ase called 7m#;d!7" We use t,e follo*ing CREATE /ATA2ASE statement:
CRE>TE )>T>!>SE m+_db

/ata!ase ta!les can !e added *it, t,e CREATE TA2LE statement"

SQL CREATE TA!LE Statement

The CR,&T, T&BL, State$ent


T,e CREATE TA2LE statement is used to create a ta!le in a data!ase"

SQL CREATE TA!LE Syntax


CRE>TE T>!LE table_name ( column_name1 data_t+-e(

column_name data_t+-e( column_name$ data_t+-e( CCCC )

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"

CR,&T, T&BL, ,/a$) e


$o* *e *ant to create a ta!le called 7.ersons7 t,at contains fi)e columns: .;Id+ Last$ame+ <irst$ame+ Address+ and Cit#" We use t,e follo*ing CREATE TA2LE statement:
CRE>TE T>!LE %e"sons ( %_*d int( Last'ame va"c0a"( 66)( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66) )

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:

'OT 'BLL B'*NBE %R*M>R3 &E3 FORE*:' &E3 CHEC& )EF>BLT

T,e ne't c,a ters *ill descri!e eac, constraint in details"

SQL NOT N%LL Constraint

2# default+ a ta!le column can ,old $ULL )alues"

SQL N-T NULL Constraint


T,e $OT $ULL constraint enforces a column to $OT acce t $ULL )alues" T,e $OT $ULL constraint enforces a field to al*a#s contain a )alue" T,is means t,at #ou cannot insert a ne* record+ or u date a record *it,out adding a )alue to t,is field" T,e follo*ing SQL enforces t,e 7.;Id7 column and t,e 7Last$ame7 column to not acce t $ULL )alues:
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) )

SQL %NIQ%E Constraint

SQL UN#QU, Constraint


T,e U$IQUE constraint uni(uel# identifies eac, record in a data!ase ta!le" T,e U$IQUE and .RI0ARB :EB constraints !ot, ro)ide a guarantee for uni(ueness for a column or set of columns" A .RI0ARB :EB constraint automaticall# ,as a U$IQUE constraint defined on it" $ote t,at #ou can ,a)e man# U$IQUE constraints er ta!le+ !ut onl# one .RI0ARB :EB constraint er ta!le"

SQL UN#QU, Constraint on CR,&T, T&BL,


T,e follo*ing SQL creates a U$IQUE constraint on t,e 7.;Id7 column *,en t,e 7.ersons7 ta!le is created: M'SQL:
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)( B'*NBE (%_*d) )

SQL Ser5er > -ra* e > MS &**ess:


CRE>TE T>!LE %e"sons ( %_*d int 'OT 'BLL B'*NBE( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66)

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) )

SQL UN#QU, Constraint on &LT,R T&BL,


To create a U$IQUE constraint on t,e 7.;Id7 column *,en t,e ta!le is alread# created+ use t,e follo*ing SQL: M'SQL > SQL Ser5er > -ra* e > MS &**ess:
>LTER T>!LE %e"sons >)) B'*NBE (%_*d)

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)

To DR-! a UN#QU, Constraint


To dro a U$IQUE constraint+ use t,e follo*ing SQL:

M'SQL:
>LTER T>!LE %e"sons )RO% *')EO uc_%e"son*)

SQL Ser5er > -ra* e > MS &**ess:


>LTER T>!LE %e"sons )RO% CO'STR>*'T uc_%e"son*)

SQL &RI(AR" #E" Constraint

SQL !R#M&RY (,Y Constraint


T,e .RI0ARB :EB constraint uni(uel# identifies eac, record in a data!ase ta!le" .rimar# 4e#s must contain uni(ue )alues" A rimar# 4e# column cannot contain $ULL )alues" Eac, ta!le s,ould ,a)e a rimar# 4e#+ and eac, ta!le can ,a)e onl# O$E rimar# 4e#"

SQL !R#M&RY (,Y Constraint on CR,&T, T&BL,


T,e follo*ing SQL creates a .RI0ARB :EB on t,e 7.;Id7 column *,en t,e 7.ersons7 ta!le is created: M'SQL:
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)( %R*M>R3 &E3 (%_*d) )

SQL Ser5er > -ra* e > MS &**ess:


CRE>TE T>!LE %e"sons ( %_*d int 'OT 'BLL %R*M>R3 &E3( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66) )

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) )

SQL !R#M&RY (,Y Constraint on &LT,R T&BL,


To create a .RI0ARB :EB constraint on t,e 7.;Id7 column *,en t,e ta!le is alread# created+ use t,e follo*ing SQL: M'SQL > SQL Ser5er > -ra* e > MS &**ess:
>LTER T>!LE %e"sons >)) %R*M>R3 &E3 (%_*d)

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:

>LTER T>!LE %e"sons >)) CO'STR>*'T -<_%e"son*) %R*M>R3 &E3 (%_*d(Last'ame)

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&"

To DR-! a !R#M&RY (,Y Constraint


To dro a .RI0ARB :EB constraint+ use t,e follo*ing SQL: M'SQL:
>LTER T>!LE %e"sons )RO% %R*M>R3 &E3

SQL Ser5er > -ra* e > MS &**ess:


>LTER T>!LE %e"sons )RO% CO'STR>*'T -<_%e"son*)

SQL ,OREI-N #E" Constraint

SQL %-R,#;N (,Y Constraint


A <OREIJ$ :EB in one ta!le oints to a .RI0ARB :EB in anot,er ta!le" LetCs illustrate t,e foreign 4e# *it, an e'am le" Loo4 at t,e follo*ing t*o ta!les: 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"

T,e 7Orders7 ta!le:

O_Id 1

OrderNo FFM46 @@5FM

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"

SQL %-R,#;N (,Y Constraint on CR,&T, T&BL,


T,e follo*ing SQL creates a <OREIJ$ :EB on t,e 7.;Id7 column *,en t,e 7Orders7 ta!le is created: M'SQL:
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)( FORE*:' &E3 (%_*d) REFERE'CES %e"sons(%_*d) )

SQL Ser5er > -ra* e > MS &**ess:


CRE>TE T>!LE O"de"s (

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) )

SQL %-R,#;N (,Y Constraint on &LT,R T&BL,


To create a <OREIJ$ :EB constraint on t,e 7.;Id7 column *,en t,e 7Orders7 ta!le is alread# created+ use t,e follo*ing SQL: M'SQL > SQL Ser5er > -ra* e > MS &**ess:
>LTER T>!LE 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)

To DR-! a %-R,#;N (,Y Constraint


To dro a <OREIJ$ :EB constraint+ use t,e follo*ing SQL: M'SQL:
>LTER T>!LE O"de"s )RO% FORE*:' &E3 ;<_%e"O"de"s

SQL Ser5er > -ra* e > MS &**ess:


>LTER T>!LE O"de"s )RO% CO'STR>*'T ;<_%e"O"de"s

SQL CHEC# Constraint

SQL C1,C( Constraint


T,e CHEC: constraint is used to limit t,e )alue range t,at can !e laced in a column" If #ou define a CHEC: constraint on a single column it allo*s onl# certain )alues for t,is column" If #ou define a CHEC: constraint on a ta!le it can limit t,e )alues in certain columns !ased on )alues in ot,er columns in t,e ro*"

SQL C1,C( Constraint on CR,&T, T&BL,


T,e follo*ing SQL creates a CHEC: constraint on t,e 7.;Id7 column *,en t,e 7.ersons7 ta!le is created" T,e CHEC: constraint s ecifies t,at t,e column 7.;Id7 must onl# include integers greater t,an 9" M' SQL:
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)( CHEC& (%_*d90) )

SQL Ser5er > -ra* e > MS &**ess:


CRE>TE T>!LE %e"sons ( %_*d int 'OT 'BLL CHEC& (%_*d90)( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66) )

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/) )

SQL C1,C( Constraint on &LT,R T&BL,


To create a CHEC: constraint on t,e 7.;Id7 column *,en t,e ta!le is alread# created+ use t,e follo*ing SQL: M'SQL > SQL Ser5er > -ra* e > MS &**ess:
>LTER T>!LE %e"sons

>)) 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:
>LTER T>!LE %e"sons >)) CO'STR>*'T c0<_%e"son CHEC& (%_*d90 >') Cit+./Sandnes/)

To DR-! a C1,C( Constraint


To dro a CHEC: constraint+ use t,e follo*ing SQL: SQL Ser5er > -ra* e > MS &**ess:
>LTER T>!LE %e"sons )RO% CO'STR>*'T c0<_%e"son

SQL DE,A%LT Constraint

SQL D,%&ULT Constraint


T,e /E<AULT constraint is used to insert a default )alue into a column" T,e default )alue *ill !e added to all ne* records+ if no ot,er )alue is s ecified"

SQL D,%&ULT Constraint on CR,&T, T&BL,


T,e follo*ing SQL creates a /E<AULT constraint on t,e 7Cit#7 column *,en t,e 7.ersons7 ta!le is created: 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) )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() )

SQL D,%&ULT Constraint on &LT,R T&BL,


To create a /E<AULT constraint on t,e 7Cit#7 column *,en t,e ta!le is alread# created+ use t,e follo*ing SQL: M'SQL:
>LTER T>!LE %e"sons >LTER Cit+ SET )EF>BLT /S>')'ES/

SQL Ser5er > -ra* e > MS &**ess:


>LTER T>!LE %e"sons >LTER COLBM' Cit+ SET )EF>BLT /S>')'ES/

To DR-! a D,%&ULT Constraint


To dro a /E<AULT constraint+ use t,e follo*ing SQL: M'SQL:

>LTER T>!LE %e"sons >LTER Cit+ )RO% )EF>BLT

SQL Ser5er > -ra* e > MS &**ess:


>LTER T>!LE %e"sons >LTER COLBM' Cit+ )RO% )EF>BLT

SQL CREATE INDE. Statement

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"

SQL CREATE INDE. Syntax


Creates an inde' on a ta!le" /u licate )alues are allo*ed:
CRE>TE *')EO inde=_name O' table_name (column_name)

SQL CREATE %NIQ%E INDE. Syntax


Creates a uni(ue inde' on a ta!le" /u licate )alues are not allo*ed:
CRE>TE B'*NBE *')EO inde=_name O' table_name (column_name)

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"

CR,&T, #ND,. ,/a$) e


T,e SQL statement !elo* creates an inde' named 7.Inde'7 on t,e 7Last$ame7 column in t,e 7.ersons7 ta!le:
CRE>TE *')EO %*nde= O' %e"sons (Last'ame)

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)

SQL DRO& INDE./ DRO& TA!LE/ and DRO& DATA!ASE

Inde'es+ ta!les+ and data!ases can easil# !e deleted5remo)ed *it, t,e /RO. statement"

The DR-! #ND,. State$ent


T,e /RO. I$/EK statement is used to delete an inde' in a ta!le"

DRO& INDE. Syntax )or (S Access0


)RO% *')EO inde=_name O' table_name

DRO& INDE. Syntax )or (S SQL Ser'er0


)RO% *')EO table_nameCinde=_name

DRO& INDE. Syntax )or D!12Oracle0


)RO% *')EO inde=_name

DRO& INDE. Syntax )or (ySQL0


>LTER T>!LE table_name )RO% *')EO inde=_name

The DR-! T&BL, State$ent


T,e /RO. TA2LE statement is used to delete a ta!le"
)RO% T>!LE table_name

The DR-! D&T&B&S, State$ent


T,e /RO. /ATA2ASE statement is used to delete a data!ase"
)RO% )>T>!>SE database_name

The TRUNC&T, T&BL, State$ent


W,at if *e onl# *ant to delete t,e data inside t,e ta!le+ and not t,e ta!le itselfL T,en+ use t,e TRU$CATE TA2LE statement:
TRB'C>TE T>!LE table_name

SQL ALTER TA!LE Statement

The &LT,R T&BL, State$ent


T,e ALTER TA2LE statement is used to add+ delete+ or modif# columns in an e'isting ta!le"

SQL ALTER TA!LE Syntax


To add a column in a ta!le+ use t,e follo*ing s#nta':
>LTER T>!LE table_name

>)) column_name datat+-e

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

To c,ange t,e data t# e of a column in a ta!le+ use t,e follo*ing s#nta':


>LTER T>!LE table_name >LTER COLBM' column_name datat+-e

SQL &LT,R T&BL, ,/a$) e


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"

$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

Ola Tove &a"i

Timoteivn 10 !o"#vn $ Sto"#t 0

Sandnes Sandnes Stavan#e"

%ette"sen

Change Data T')e ,/a$) e


$o* *e *ant to c,ange t,e data t# e of t,e 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 >LTER COLBM' )ateO;!i"t0 +ea"

$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"

DR-! C-LUMN ,/a$) e


$e't+ *e *ant to delete t,e 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 )RO% COLBM' )ateO;!i"t0

T,e 7.ersons7 ta!le *ill no* 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"

SQL A%TO INCRE(ENT ,ield

Auto3increment allo*s a uni(ue num!er to !e generated *,en a ne* record is inserted into a ta!le"

&UT- #NCR,M,NT a %ie d


Mer# often *e *ould li4e t,e )alue of t,e rimar# 4e# field to !e created automaticall# e)er# time a ne* record is inserted" We *ould li4e to create an auto3increment field in a ta!le"

S'nta/ +or M'SQL


T,e follo*ing SQL statement defines t,e 7.;Id7 column to !e an auto3increment rimar# 4e# field in t,e 7.ersons7 ta!le:
CRE>TE T>!LE %e"sons ( %_*d int 'OT 'BLL >BTO_*'CREME'T( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66)( %R*M>R3 &E3 (%_*d) )

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"

S'nta/ +or SQL Ser5er


T,e follo*ing SQL statement defines t,e 7.;Id7 column to !e an auto3increment rimar# 4e# field in t,e 7.ersons7 ta!le:
CRE>TE T>!LE %e"sons ( %_*d int %R*M>R3 &E3 *)E'T*T3( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66) )

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"

S'nta/ +or &**ess


T,e follo*ing SQL statement defines t,e 7.;Id7 column to !e an auto3increment rimar# 4e# field in t,e 7.ersons7 ta!le:
CRE>TE T>!LE %e"sons ( %_*d %R*M>R3 &E3 >BTO*'CREME'T( Last'ame va"c0a"( 66) 'OT 'BLL( Fi"st'ame va"c0a"( 66)( >dd"ess va"c0a"( 66)( Cit+ va"c0a"( 66) )

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"

S'nta/ +or -ra* e


In Oracle t,e code is a little !it more tric4#" Bou *ill ,a)e to create an auto3increment field *it, t,e se(uence o!-ect %t,is o!-ect generates a num!er se(uence&" Use t,e follo*ing CREATE SEQUE$CE s#nta':
CRE>TE SENBE'CE se7_-e"son

M*'A>LBE 1 ST>RT ,*TH 1 *'CREME'T !3 1 C>CHE 10

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*"

SQL CR,&T, ?#,W State$ent


In SQL+ a )ie* is a )irtual ta!le !ased on t,e result3set of an SQL statement" A )ie* contains ro*s and columns+ -ust li4e a real ta!le" T,e fields in a )ie* are fields from one or more real ta!les in t,e data!ase" Bou can add SQL functions+ WHERE+ and DOI$ statements to a )ie* and resent t,e data as if t,e data *ere coming from one single ta!le"

SQL CREATE 3IEW Syntax


CRE>TE A*E, vie2_name >S SELECT column_name(s) FROM table_name

,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*"

SQL CR,&T, ?#,W ,/a$) es


If #ou ,a)e t,e $ort,*ind data!ase #ou can see t,at it ,as se)eral )ie*s installed !# default" T,e )ie* 7Current .roduct List7 lists all acti)e roducts % roducts t,at are not discontinued& from t,e 7.roducts7 ta!le" T,e )ie* is created *it, t,e follo*ing SQL:
CRE>TE A*E, ICu""ent %"oduct ListJ >S SELECT %"oduct*)(%"oduct'ame FROM %"oducts ,HERE )iscontinued.'o

We can (uer# t,e )ie* a!o)e as follo*s:


SELECT * FROM ICu""ent %"oduct ListJ

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)

We can (uer# t,e )ie* a!o)e as follo*s:


SELECT * FROM I%"oducts >bove >ve"a#e %"iceJ

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 (uer# t,e )ie* a!o)e as follo*s:


SELECT * FROM ICate#o"+ Sales Fo" 144FJ

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/

SQL U)dating a ?ie2


Bou can u date a )ie* !# using t,e follo*ing s#nta':

SQL CREATE OR RE&LACE 3IEW Syntax


CRE>TE OR RE%L>CE A*E, vie2_name >S SELECT column_name(s) FROM table_name ,HERE condition

$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 Dro))ing a ?ie2


Bou can delete a )ie* *it, t,e /RO. MIEW command"

SQL DRO& 3IEW Syntax


)RO% A*E, vie2_name

SQL Date ,unctions

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"

M'SQL Date %un*tions


T,e follo*ing ta!le lists t,e most im ortant !uilt3in date functions in 0#SQL:
F%nction 'O,() CBR)>TE() CBRT*ME() )>TE() EOTR>CT() )>TE_>))() )>TE_SB!() )>TE)*FF() )>TE_FORM>T() Description Retu"ns t0e cu""ent date and time Retu"ns t0e cu""ent date Retu"ns t0e cu""ent time E=t"acts t0e date -a"t o; a date o" datePtime e=-"ession Retu"ns a sin#le -a"t o; a datePtime >dds a s-eci;ied time inte"val to a date Subt"acts a s-eci;ied time inte"val ;"om a date Retu"ns t0e numbe" o; da+s bet2een t2o dates )is-la+s datePtime data in di;;e"ent ;o"mats

SQL Ser5er Date %un*tions

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

SQL Date Data T')es


M'SQL 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 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"

SQL Wor=ing 2ith Dates


Bou can com are t*o dates easil# if t,ere is no time com onent in)ol)ed1

Assume *e ,a)e t,e follo*ing 7Orders7 ta!le:


OrderId 1 Prod%ctName :eitost Camembe"t %ie""ot $ @ MoHHa"ella di :iovanni Masca"-one Fabioli OrderDate 00MQ11Q11 00MQ11Q04 00MQ11Q11 00MQ10Q 4

$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/

T,e result3set *ill loo4 li4e t,is:


OrderId 1 $ Prod%ctName :eitost MoHHa"ella di :iovanni OrderDate 00MQ11Q11 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

If *e use t,e same SELECT statement as a!o)e:


SELECT * FROM O"de"s ,HERE O"de")ate./ 00MQ11Q11/

*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

SQL N%LL 3alues

$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"

SQL NULL ?a ues


If a column in a ta!le is o tional+ *e can insert a ne* record or u date an e'isting record *it,out adding a )alue to t,is column" T,is means t,at t,e field *ill !e sa)ed *it, a $ULL )alue" $ULL )alues are treated differentl# from ot,er )alues" $ULL is used as a lace,older for un4no*n or ina lica!le )alues" Note: It is not ossi!le to com are $ULL and 9I t,e# are not e(ui)alent"

SQL Wor=ing 2ith NULL ?a ues


Loo4 at t,e follo*ing 7.ersons7 ta!le:
P_Id 1 LastName Hansen Svendson $ %ette"sen FirstName Ola Tove &a"i !o"#vn $ Address City Sandnes Sandnes Stavan#e"

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

T,e result3set *ill loo4 li4e t,is:


LastName Hansen %ette"sen FirstName Ola &a"i Address

Ti): Al*a#s use IS $ULL to loo4 for $ULL )alues"

SQL #S N-T NULL


Ho* do *e select onl# t,e records *it, no $ULL )alues in t,e 7Address7 columnL We *ill ,a)e to use t,e IS $OT $ULL o erator:
SELECT Last'ame(Fi"st'ame(>dd"ess FROM %e"sons ,HERE >dd"ess *S 'OT 'BLL

T,e result3set *ill loo4 li4e t,is:


LastName Svendson FirstName Tove Address !o"#vn $

In t,e ne't c,a ter *e *ill loo4 at t,e IS$ULL%&+ $ML%&+ I<$ULL%& and COALESCE%& functions"

SQL N%LL ,unctions

SQL #SNULL@AB N?L@AB #%NULL@A and C-&L,SC,@A %un*tions


Loo4 at t,e follo*ing 7.roducts7 ta!le:
P_Id 1 Prod%ctName Da"lsbe"# Masca"-one $ :o"#onHola !nitPrice 10C@6 $ C65 16C5F !nitsIn&toc' 15 $ 4 0 !nitsOnOrder 16

Su

ose t,at t,e 7UnitsOnOrder7 column is o tional+ and ma# contain $ULL )alues"

We ,a)e t,e follo*ing SELECT statement:


SELECT %"oduct'ame(Bnit%"ice*(Bnits*nStoc<RBnitsOnO"de") FROM %"oducts

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

or *e can use t,e COALESCE%& function+ li4e t,is:


SELECT %"oduct'ame(Bnit%"ice*(Bnits*nStoc<RCO>LESCE(BnitsOnO"de"(0)) FROM %"oducts

SQL Data Ty es

/ata t# es and ranges for 0icrosoft Access+ 0#SQL and SQL Ser)er"

Mi*roso+t &**ess Data T')es


Data type Te=t Description Bse ;o" te=t o" combinations o; te=t and numbe"sC 66 c0a"acte"s ma=imum Memo is used ;o" la"#e" amounts o; te=tC Sto"es u- to 56(6$5 c0a"acte"sC Note) 3ou cannot so"t a memo ;ieldC Ho2eve"( t0e+ a"e sea"c0able >llo2s 20ole numbe"s ;"om 0 to 66 1 b+te &tora(e

Memo

!+te

*nte#e" Lon# Sin#le )ouble Cu""enc+

>llo2s 20ole numbe"s bet2een Q$ (F5M and $ (F5F

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

H+-e"lin< Loo<u- ,iHa"d

M'SQL Data T')es


In 0#SQL t,ere are t,ree main t# es : te't+ num!er+ and /ate5Time t# es" Te/t t')es:
Data type CH>R(siHe) Description Holds a ;i=ed len#t0 st"in# (can contain lette"s( numbe"s( and s-ecial c0a"acte"s)C T0e ;i=ed siHe is s-eci;ied in -a"ent0esisC Can sto"e u- to 66 c0a"acte"s Holds a va"iable len#t0 st"in# (can contain lette"s( numbe"s( and s-ecial

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()

orted range is from C8999398398C to CNNNN386338C

*> date and time combinationC Fo"mat1 3333QMMQ)) HH1MM1SS

Note: T,e su 63:EN:ENC


T*MEST>M%()

orted range is from C8999398398 99:99:99C to CNNNN386338

*> 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()

orted range is from C3P3P:EN:ENC to CP3P:EN:ENC

> +ea" in t2oQdi#it o" ;ou"Qdi#it ;o"matC

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//"

SQL Ser5er Data T')es


Chara*ter strings:
Data type c0a"(n) va"c0a"(n) va"c0a"(ma=) Description Fi=edQlen#t0 c0a"acte" st"in#C Ma=imum M(000 c0a"acte"s Aa"iableQlen#t0 c0a"acte" st"in#C Ma=imum M(000 c0a"acte"s Aa"iableQlen#t0 c0a"acte" st"in#C Ma=imum 1(0F$(F@1(M @ c0a"acte"s Aa"iableQlen#t0 c0a"acte" st"in#C Ma=imum :! o; te=t data &tora(e n

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

Aa"iableQlen#t0 bina"+ dataC Ma=imum :!

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

T,e n arameter indicates *,et,er t,e field s,ould ,old S or P

!#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

date time datetimeo;;set

$ b+tes $Q6 b+tes MQ10 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

-ther data t')es:


Data type s7l_va"iant Description Sto"es u- to M(000 b+tes o; data o; va"ious data t+-es( e=ce-t te=t( nte=t( and timestamSto"es a #loball+ uni7ue identi;ie" (:B*)) Sto"es OML ;o"matted dataC Ma=imum :! Sto"es a "e;e"ence to a cu"so" used ;o" database o-e"ations Sto"es a "esultQset ;o" late" -"ocessin#

uni7ueidenti;ie" =ml cu"so" table

SQL ,unctions

SQL ,as man# !uilt3in functions for erforming calculations on data"

SQL &ggregate %un*tions


SQL aggregate functions return a single )alue+ calculated from )alues in a column" Useful aggregate functions:

>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

SQL S*a ar +un*tions


SQL scalar functions return a single )alue+ !ased on t,e in ut )alue" Useful scalar functions:

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"

SQL A3-45 ,unction

The &?;@A %un*tion


T,e AMJ%& function returns t,e a)erage )alue of a numeric column"

SQL A3-45 Syntax


SELECT >A:(column_name) FROM table_name

SQL &?;@A ,/a$) e


We ,a)e t,e follo*ing 7Orders7 ta!le:
O_Id 1 OrderDate 00MP11P1 00MP10P $ $ @ 6 5 00MP04P0 00MP04P0$ 00MP0MP$0 00MP10P0@ OrderPrice 1000 1500 F00 $00 000 100 C%stomer Hansen 'ilsen Hansen Hansen Densen 'ilsen

$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

T,e result3set *ill loo4 li4e t,is:

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 result3set *ill loo4 li4e t,is:


C%stomer Hansen 'ilsen Densen

SQL CO%NT45 ,unction

T,e COU$T%& function returns t,e num!er of ro*s t,at matc,es a s ecified criteria"

SQL CO%NT4column6name5 Syntax


T,e COU$T%column;name& function returns t,e num!er of )alues %$ULL )alues *ill not !e counted& of t,e s ecified column:
SELECT COB'T(column_name) FROM table_name

SQL CO%NT475 Syntax


T,e COU$T%=& function returns t,e num!er of records in a ta!le:
SELECT COB'T(*) FROM table_name

SQL CO%NT4DISTINCT column6name5 Syntax


T,e COU$T%/ISTI$CT column;name& function returns t,e num!er of distinct )alues of t,e s ecified column:
SELECT COB'T()*ST*'CT column_name) FROM table_name

Note: COU$T%/ISTI$CT& *or4s *it, ORACLE and 0icrosoft SQL Ser)er+ !ut not *it, 0icrosoft Access"

SQL C-UNT@*o u$n"na$eA ,/a$) e


We ,a)e t,e follo*ing 7Orders7 ta!le:
O_Id 1 OrderDate 00MP11P1 00MP10P $ $ @ 6 5 00MP04P0 00MP04P0$ 00MP0MP$0 00MP10P0@ OrderPrice 1000 1500 F00 $00 000 100 C%stomer Hansen 'ilsen Hansen Hansen Densen 'ilsen

$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

SQL C-UNT@0A ,/a$) e


If *e omit t,e WHERE clause+ li4e t,is:
SELECT COB'T(*) >S 'umbe"O;O"de"s FROM O"de"s

T,e result3set *ill loo4 li4e t,is:


N%m+erO"Orders 5

*,ic, is t,e total num!er of ro*s in t,e ta!le"

SQL C-UNT@D#ST#NCT *o u$n"na$eA ,/a$) e


$o* *e *ant to count t,e num!er of uni(ue customers in t,e 7Orders7 ta!le" We use t,e follo*ing SQL statement:
SELECT COB'T()*ST*'CT Custome") >S 'umbe"O;Custome"s FROM O"de"s

T,e result3set *ill loo4 li4e t,is:


N%m+erO"C%stomers $

*,ic, is t,e num!er of uni(ue customers %Hansen+ $ilsen+ and Densen& in t,e 7Orders7 ta!le"

SQL ,IRST45 ,unction

The %#RST@A %un*tion


T,e <IRST%& function returns t,e first )alue of t,e selected column"

SQL ,IRST45 Syntax


SELECT F*RST(column_name) FROM table_name

SQL %#RST@A ,/a$) e


We ,a)e t,e follo*ing 7Orders7 ta!le:
O_Id 1 OrderDate 00MP11P1 00MP10P $ $ @ 6 5 00MP04P0 00MP04P0$ 00MP0MP$0 00MP10P0@ OrderPrice 1000 1500 F00 $00 000 100 C%stomer Hansen 'ilsen Hansen Hansen Densen 'ilsen

$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

Ti): Wor4around if <IRST%& function is not su

orted:

SELECT O"de"%"ice FROM O"de"s OR)ER !3 O_*d L*M*T 1

T,e result3set *ill loo4 li4e t,is:


FirstOrderPrice 1000

SQL LAST45 ,unction

The L&ST@A %un*tion


T,e LAST%& function returns t,e last )alue of t,e selected column"

SQL LAST45 Syntax


SELECT L>ST(column_name) FROM table_name

SQL L&ST@A ,/a$) e


We ,a)e t,e follo*ing 7Orders7 ta!le:
O_Id 1 OrderDate 00MP11P1 00MP10P $ $ @ 6 5 00MP04P0 00MP04P0$ 00MP0MP$0 00MP10P0@ OrderPrice 1000 1500 F00 $00 000 100 C%stomer Hansen 'ilsen Hansen Hansen Densen 'ilsen

$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

Ti): Wor4around if LAST%& function is not su

orted:

SELECT O"de"%"ice FROM O"de"s OR)ER !3 O_*d )ESC L*M*T 1

T,e result3set *ill loo4 li4e t,is:

LastOrderPrice 100

SQL (A.45 ,unction

The M&.@A %un*tion


T,e 0AK%& function returns t,e largest )alue of t,e selected column"

SQL (A.45 Syntax


SELECT M>O(column_name) FROM table_name

SQL M&.@A ,/a$) e


We ,a)e t,e follo*ing 7Orders7 ta!le:
O_Id 1 OrderDate 00MP11P1 00MP10P $ $ @ 6 5 00MP04P0 00MP04P0$ 00MP0MP$0 00MP10P0@ OrderPrice 1000 1500 F00 $00 000 100 C%stomer Hansen 'ilsen Hansen Hansen Densen 'ilsen

$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

T,e result3set *ill loo4 li4e t,is:


Lar(estOrderPrice 000

SQL (IN45 ,unction

The M#N@A %un*tion


T,e 0I$%& function returns t,e smallest )alue of t,e selected column"

SQL (IN45 Syntax


SELECT M*'(column_name) FROM table_name

SQL M#N@A ,/a$) e


We ,a)e t,e follo*ing 7Orders7 ta!le:
O_Id 1 OrderDate 00MP11P1 00MP10P $ $ @ 6 5 00MP04P0 00MP04P0$ 00MP0MP$0 00MP10P0@ OrderPrice 1000 1500 F00 $00 000 100 C%stomer Hansen 'ilsen Hansen Hansen Densen 'ilsen

$o* *e *ant to find t,e smallest )alue of t,e 7Order.rice7 column" We use t,e follo*ing SQL statement:

SELECT M*'(O"de"%"ice) >S SmallestO"de"%"ice FROM O"de"s

T,e result3set *ill loo4 li4e t,is:


&mallestOrderPrice 100

SQL S%(45 ,unction

The SUM@A %un*tion


T,e SU0%& function returns t,e total sum of a numeric column"

SQL S%(45 Syntax


SELECT SBM(column_name) FROM table_name

SQL SUM@A ,/a$) e


We ,a)e t,e follo*ing 7Orders7 ta!le:
O_Id 1 OrderDate 00MP11P1 00MP10P $ $ @ 6 5 00MP04P0 00MP04P0$ 00MP0MP$0 00MP10P0@ OrderPrice 1000 1500 F00 $00 000 100 C%stomer Hansen 'ilsen Hansen Hansen Densen 'ilsen

$o* *e *ant to find t,e sum of all 7Order.rice7 fields7"

We use t,e follo*ing SQL statement:


SELECT SBM(O"de"%"ice) >S O"de"Total FROM O"de"s

T,e result3set *ill loo4 li4e t,is:


OrderTotal 6F00

SQL -RO%& !" Statement

Aggregate functions often need an added JROU. 2B statement"

The ;R-U! BY State$ent


T,e JROU. 2B statement is used in con-unction *it, t,e aggregate functions to grou t,e result3set !# one or more columns"

SQL -RO%& !" Syntax


SELECT column_name( a##"e#ate_;unction(column_name) FROM table_name ,HERE column_name o-e"ato" value :ROB% !3 column_name

SQL ;R-U! BY ,/a$) e


We ,a)e t,e follo*ing 7Orders7 ta!le:
O_Id 1 OrderDate 00MP11P1 00MP10P $ $ 00MP04P0 OrderPrice 1000 1500 F00 C%stomer Hansen 'ilsen Hansen

@ 6 5

00MP04P0$ 00MP0MP$0 00MP10P0@

$00 000 100

Hansen Densen 'ilsen

$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"

T,e result3set *ill loo4 li4e t,is:


C%stomer Hansen 'ilsen Densen &!,-OrderPrice. 000 1F00 000

$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

T,e result3set *ill loo4 li4e t,is:


C%stomer Hansen 'ilsen Hansen Hansen &!,-OrderPrice. 6F00 6F00 6F00 6F00

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"

;R-U! BY More Than -ne Co u$n


We can also use t,e JROU. 2B statement on more t,an one column+ li4e t,is:
SELECT Custome"(O"de")ate(SBM(O"de"%"ice) FROM O"de"s :ROB% !3 Custome"(O"de")ate

SQL HA3IN- Clause

The 1&?#N; C ause


T,e HAMI$J clause *as added to SQL !ecause t,e WHERE 4e#*ord could not !e used *it, aggregate functions"

SQL HA3IN- Syntax


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

SQL 1&?#N; ,/a$) e

We ,a)e t,e follo*ing 7Orders7 ta!le:


O_Id 1 OrderDate 00MP11P1 00MP10P $ $ @ 6 5 00MP04P0 00MP04P0$ 00MP0MP$0 00MP10P0@ OrderPrice 1000 1500 F00 $00 000 100 C%stomer Hansen 'ilsen Hansen Hansen Densen 'ilsen

$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

T,e result3set *ill loo4 li4e t,is:


C%stomer 'ilsen &!,-OrderPrice. 1F00

$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

T,e result3set *ill loo4 li4e t,is:


C%stomer &!,-OrderPrice.

Hansen Densen

000 000

SQL %CASE45 ,unction

The UC&S,@A %un*tion


T,e UCASE%& function con)erts t,e )alue of a field to u ercase"

SQL %CASE45 Syntax


SELECT BC>SE(column_name) FROM table_name

Syntax )or SQL Ser'er


SELECT B%%ER(column_name) FROM table_name

SQL UC&S,@A ,/a$) e


We ,a)e t,e follo*ing 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 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

T,e result3set *ill loo4 li4e t,is:


LastName H>'SE' SAE')SO' %ETTERSE' FirstName Ola Tove &a"i

SQL LCASE45 ,unction

The LC&S,@A %un*tion


T,e LCASE%& function con)erts t,e )alue of a field to lo*ercase"

SQL LCASE45 Syntax


SELECT LC>SE(column_name) FROM table_name

Syntax )or SQL Ser'er


SELECT LO,ER(column_name) FROM table_name

SQL LC&S,@A ,/a$) e


We ,a)e t,e follo*ing 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 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

T,e result3set *ill loo4 li4e t,is:


LastName 0ansen svendson -ette"sen FirstName Ola Tove &a"i

SQL (ID45 ,unction

The M#D@A %un*tion


T,e 0I/%& function is used to e'tract c,aracters from a te't field"

SQL (ID45 Syntax


SELECT M*)(column_name(sta"tI(len#t0J) FROM table_name

Parameter column_name sta"t len#t0

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

SQL M#D@A ,/a$) e

We ,a)e t,e follo*ing 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 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

T,e result3set *ill loo4 li4e t,is:


&mallCity Sand Sand Stav

SQL LEN45 ,unction

The L,N@A %un*tion


T,e LE$%& function returns t,e lengt, of t,e )alue in a te't field"

SQL LEN45 Syntax


SELECT LE'(column_name) FROM table_name

SQL L,N@A ,/a$) e

We ,a)e t,e follo*ing 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 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

T,e result3set *ill loo4 li4e t,is:


Len(t$O"Address 1 4 4

SQL RO%ND45 ,unction

The R-UND@A %un*tion


T,e ROU$/%& function is used to round a numeric field to t,e num!er of decimals s ecified"

SQL RO%ND45 Syntax


SELECT ROB')(column_name(decimals) FROM table_name

Parameter column_name

Description Re7ui"edC T0e ;ield to "oundC

decimals

Re7ui"edC S-eci;ies t0e numbe" o; decimals to be "etu"nedC

SQL R-UND@A ,/a$) e


We ,a)e t,e follo*ing 7.roducts7 ta!le:
Prod_Id 1 Prod%ctName Da"lsbe"# Masca"-one $ :o"#onHola !nit 1000 # 1000 # 1000 # !nitPrice 10C@6 $ C65 16C5F

$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

T,e result3set *ill loo4 li4e t,is:


Prod%ctName Da"lsbe"# Masca"-one :o"#onHola !nitPrice 10 $$ 15

SQL NOW45 ,unction

The N-W@A %un*tion


T,e $OW%& function returns t,e current s#stem date and time"

SQL NOW45 Syntax


SELECT 'O,() FROM table_name

SQL N-W@A ,/a$) e


We ,a)e t,e follo*ing 7.roducts7 ta!le:
Prod_Id 1 Prod%ctName Da"lsbe"# Masca"-one $ :o"#onHola !nit 1000 # 1000 # 1000 # !nitPrice 10C@6 $ C65 16C5F

$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

T,e result3set *ill loo4 li4e t,is:


Prod%ctName Da"lsbe"# Masca"-one :o"#onHola !nitPrice 10C@6 $ C65 16C5F PerDate 10PFP 00M 111 610 >M 10PFP 00M 111 610 >M 10PFP 00M 111 610 >M

SQL ,OR(AT45 ,unction

The %-RM&T@A %un*tion


T,e <OR0AT%& function is used to format ,o* a field is to !e dis la#ed"

SQL ,OR(AT45 Syntax


SELECT FORM>T(column_name(;o"mat) FROM table_name

Parameter column_name ;o"mat

Description Re7ui"edC T0e ;ield to be ;o"mattedC Re7ui"edC S-eci;ies t0e ;o"matC

SQL %-RM&T@A ,/a$) e


We ,a)e t,e follo*ing 7.roducts7 ta!le:
Prod_Id 1 Prod%ctName Da"lsbe"# Masca"-one $ :o"#onHola !nit 1000 # 1000 # 1000 # !nitPrice 10C@6 $ C65 16C5F

$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

T,e result3set *ill loo4 li4e t,is:


Prod%ctName Da"lsbe"# Masca"-one :o"#onHola !nitPrice 10C@6 $ C65 16C5F PerDate 00MQ10Q0F 00MQ10Q0F 00MQ10Q0F

SQL Quic8 Re)erence ,rom W9Sc:ools

&/L &tatement >') P OR

&ynta0 SELECT column_name(s) FROM table_name ,HERE condition >')?OR condition >LTER T>!LE table_name >)) column_name datat+-e

>LTER T>!LE

or ALTER TA2LE ta!le;name /RO. COLU0$ column;name


>S (alias) SELECT column_name >S column_alias FROM table_name

or SELECT column;name <RO0 ta!le;name AS ta!le;alias


!ET,EE' SELECT column_name(s) FROM table_name ,HERE column_name !ET,EE' value1 >') value CRE>TE )>T>!>SE database_name CRE>TE T>!LE table_name ( column_name1 data_t+-e( column_name data_t+-e( column_name data_t+-e( CCC ) CRE>TE *')EO inde=_name O' table_name (column_name)

CRE>TE )>T>!>SE CRE>TE T>!LE

CRE>TE *')EO

or

CREATE U$IQUE I$/EK inde';name O$ ta!le;name %column;name&


CRE>TE A*E, CRE>TE A*E, vie2_name >S SELECT column_name(s) FROM table_name ,HERE condition )ELETE FROM table_name ,HERE some_column.some_value

)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

)RO% T>!LE :ROB% !3

H>A*':

*'

*'SERT *'TO

A>LBES (value1( value ( value$(CCCC)

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

or SELECT column;name%s& I$TO ne*;ta!le;name GI$ e'ternaldata!aseH <RO0 old;ta!le;name


SELECT TO% SELECT TO% numbe"?-e"cent column_name(s) FROM table_name TRB'C>TE T>!LE table_name SELECT column_name(s) FROM table_name1 B'*O' SELECT column_name(s) FROM table_name SELECT column_name(s) FROM table_name1 B'*O' >LL SELECT column_name(s) FROM table_name B%)>TE table_name SET column1.value( column .value(CCC ,HERE some_column.some_value SELECT column_name(s) FROM table_name ,HERE column_name o-e"ato" value

TRB'C>TE T>!LE B'*O'

B'*O' >LL

B%)>TE

,HERE

Potrebbero piacerti anche