Sunteți pe pagina 1din 32

Introduction to Structured Query

Language
Version 4.71
This page is a introductory tutorial of the Structured Query Language (also known as
SQL) and is a pioneering effort on the World Wide Web, as this is the first
comprehensive SQL tutorial available on the nternet! SQL allows users to access data in
relational database management systems, such as "racle, Sybase, nformi#, $icrosoft
SQL Server, %ccess, and others, by allowing users to describe the data the user wishes to
see! SQL also allows users to define the data in a database, and manipulate that data! This
page will describe how to use SQL, and give e#amples! The SQL used in this document is
&%'S&, or standard SQL, and no SQL features of specific database management systems
will be discussed until the &'onstandard SQL& section! t is recommended that you print
this page, so that you can easily refer back to previous e#amples!
%lso, you may be interested in (oining the new SQL )lub on *ahoo+, where you can read
or enter messages in a SQL forum!
Table of Contents
,asics of the S-L-)T Statement
)onditional Selection
.elational "perators
)ompound )onditions
' / ,-TW--'
0sing L1-
2oins
1eys
3erforming a 2oin
-liminating 4uplicates
%liases / n5Sub6ueries
%ggregate 7unctions
8iews
)reating 'ew Tables
%ltering Tables
%dding 4ata
4eleting 4ata
0pdating 4ata
nde#es
9."03 ,* / :%8'9
$ore Sub6ueries
-;STS / %LL
0'"' / "uter 2oins
-mbedded SQL
)ommon SQL Questions
'onstandard SQL
Synta# Summary
-#ercises
mportant Links
Basics of the SELECT Statement
n a relational database, data is stored in tables! %n e#ample table would relate Social
Security 'umber, 'ame, and %ddress<
EmployeeAddressTable
SSN FirstName LastName Address City State
=>?@ABC=A 2oe Smith AD 7irst Street :oward "hio
B=AC?EE>? $ary Scott AC? 8ine %ve! Losantiville "hio
>E??=CAF@ Sam 2ones DD -lm St! 3aris 'ew *ork
AB@=>?=@D Sarah %ckerman CCE 0!S! >>E 0pton $ichigan
'ow, letGs say you want to see the address of each employee! 0se the S-L-)T statement,
like so<
SELECT FirstName, LastName, Address, City, State
FROM EmployeeAddressTable;
The following is the results of your query of the database<
First Name Last Name Address City State
2oe Smith AD 7irst Street :oward "hio
$ary Scott AC? 8ine %ve! Losantiville "hio
Sam 2ones DD -lm St! 3aris 'ew *ork
Sarah %ckerman CCE 0!S! >>E 0pton $ichigan
To e#plain what you (ust did, you asked for the all of data in the -mployee%ddressTable,
and specifically, you asked for the columns called 7irst'ame, Last'ame, %ddress, )ity,
and State! 'ote that column names and table names do not have spaces!!!they must be
typed as one wordH and that the statement ends with a semicolon (H)! The general form for
a S-L-)T statement, retrieving all of the rows in the table is<
SELECT ColumnName, ColumnName, ...
FROM TableName;
To get all columns of a table without typing all column names, use<
SELECT * FROM TableName;
-ach database management system (4,$S) and database software has different methods
for logging in to the database and entering SQL commandsH see the local computer &guru&
to help you get onto the system, so that you can use SQL!
Conditional Selection
To further discuss the S-L-)T statement, letGs look at a new e#ample table (for
hypothetical purposes only)<
EmployeeStatisticsTable
EmployeeINo Salary Benefits !osition
E>E B=EEE >=EEE $anager
>E= @=EEE >=EEE $anager
>=? @EEEE >=EEE $anager
?>= @EEEE >?=EE $anager
?CC =EEEE >?EEE Staff
DEE C=EEE >EEEE Staff
DD= CEEEE >EEEE Staff
CEE D?EEE B=EE -ntryILevel
CC> ?AEEE B=EE -ntryILevel
"elational #perators
There are si# .elational "perators in SQL, and after introducing them, weGll see how
theyGre used<
J -6ual
K or +J (see
manual)
'ot -6ual
K Less Than
L 9reater Than
KJ Less Than or -6ual To
LJ 9reater Than or -6ual To
The WHERE clause is used to specify that only certain rows of the table are displayed,
based on the criteria described in that WHERE clause! t is most easily understood by
looking at a couple of e#amples!
f you wanted to see the -$3L"*--4'"Gs of those making at or over M=E,EEE, use the
following<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE SALAR! '( )****;
'otice that the LJ (greater than or e6ual to) sign is used, as we wanted to see those who
made greater than M=E,EEE, or e6ual to M=E,EEE, listed together! This displays<
EMLO!EE"#NO
++++++++++++
*,*
,*)
,)-
-,)
-..
The WHERE description, S%L%.* LJ =EEEE, is known as a condition (an operation
which evaluates to True or 7alse). The same can be done for te#t columns<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE OS"T"ON ( /Mana0er/;
This displays the 4 'umbers of all $anagers! 9enerally, with te#t columns, stick to
e6ual to or not e6ual to, and make sure that any te#t that appears in the statement is
surrounded by single 6uotes (G)! Note: 3osition is now an illegal identifier because it is
now an unused, but reserved, keyword in the SQLIF? standard!
$ore )omple# )onditions< )ompound )onditions 5 Logical "perators
The AND operator (oins two or more conditions, and displays a row only if that rowGs
data satisfies ALL conditions listed (i!e! all conditions hold true)! 7or e#ample, to display
all staff making over MCE,EEE, use<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE SALAR! ' .**** AN# OS"T"ON ( /Sta11/;
The OR operator (oins two or more conditions, but returns a row if AN$ of the
conditions listed hold true! To see all those who make less than MCE,EEE or have less than
M>E,EEE in benefits, listed together, use the following 6uery<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE SALAR! 2 .**** OR $ENEF"TS 2 ,****;
%'4 / ". can be combined, for e#ample<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE OS"T"ON ( /Mana0er/ AN# SALAR! ' 3**** OR $ENEF"TS ' ,-***;
7irst, SQL finds the rows where the salary is greater than M@E,EEE and the position
column is e6ual to $anager, then taking this new list of rows, SQL then sees if any of
these rows satisfies the previous %'4 condition or the condition that the ,enefits column
is greater than M>?,EEE! Subse6uently, SQL only displays this second new list of rows,
keeping in mind that anyone with ,enefits over M>?,EEE will be included as the ".
operator includes a row if either resulting condition is True! %lso note that the %'4
operation is done first!
To generaliNe this process, SQL performs the %'4 operation(s) to determine the rows
where the %'4 operation(s) hold true (remember< all of the conditions are true), then
these results are used to compare with the ". conditions, and only display those
remaining rows where any of the conditions (oined by the ". operator hold true (where a
condition or result from an %'4 is paired with another condition or %'4 result to use to
evaluate the "., which evaluates to true if either value is true)! $athematically, SQL
evaluates all of the conditions, then evaluates the %'4 &pairs&, and then evaluates the
".Gs (where both operators evaluate left to right)!
To look at an e#ample, for a given row for which the 4,$S is evaluating the SQL
statement Where clause to determine whether to include the row in the 6uery result (the
whole Where clause evaluates to True), the 4,$S has evaluated all of the conditions,
and is ready to do the logical comparisons on this result<
True AN# False OR True AN# True OR False AN# False
7irst simplify the %'4 pairs<
False OR True OR False
'ow do the ".Gs, left to right<
True OR False
True
The result is True, and the row passes the 6uery conditions! ,e sure to see the ne#t
section on '"TGs, and the order of logical operations! hope that this section has helped
you understand %'4Gs or ".Gs, as itGs a difficult sub(ect to e#plain briefly!
To perform ".Gs before %'4Gs, like if you wanted to see a list of employees making a
large salary (M=E,EEE) or have a large benefit package (M>E,EEE), and that happen to be a
manager, use parentheses<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE OS"T"ON ( /Mana0er/ AN# 4SALAR! ' )**** OR $ENEF"TS ' ,****5;
IN & BETWEEN
%n easier method of using compound conditions uses IN or E!WEEN. 7or e#ample, if
you wanted to list all managers and staff<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE OS"T"ON "N 4/Mana0er/, /Sta11/5;
or to list those making greater than or e6ual to MDE,EEE, but less than or e6ual to M=E,EEE,
use<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE SALAR! $ET%EEN 6**** AN# )****;
To list everyone not in this range, try<
SELECT EMLO!EE"#NO
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE SALAR! NOT $ET%EEN 6**** AN# )****;
Similarly, '"T ' lists all rows e#cluded from the IN list!
%dditionally, '"TGs can be thrown in with %'4Gs / ".Gs, e#cept that '"T is a unary
operator (evaluates one condition, reversing its value, whereas, %'4Gs / ".Gs evaluate
two conditions), and that all '"TGs are performed before any %'4Gs or ".Gs!
SQL Order of Logical Operations (each operates from left to right)
>! NOT
?! AN#
D! OR
%sing LIE
Look at the -mployeeStatisticsTable, and say you wanted to see all people whose last
names started with &S&H try<
SELECT EMLO!EE"#NO
FROM EMLO!EEA##RESSTA$LE
%&ERE LASTNAME L"7E /S8/;
The percent sign (O) is used to represent any possible character (number, letter, or
punctuation) or set of characters that might appear after the &S&! To find those people with
Last'ameGs ending in &S&, use GOSG, or if you wanted the &S& in the middle of the word,
try GOSOG! The GOG can be used for any characters in the same position relative to the
given characters! '"T L1- displays rows not fitting the given description! "ther
possibilities of using L1-, or any of these discussed conditionals, are available, though
it depends on what 4,$S you are usingH as usual, consult a manual or your system
manager or administrator for the available features on your system, or (ust to make sure
that what you are trying to do is available and allowed! This disclaimer holds for the
features of SQL that will be discussed below! This section is (ust to give you an idea of
the possibilities of 6ueries that can be written in SQL!
&oins
n this section, we will only discuss inner (oins, and equi"oins, as in general, they are the
most useful! 7or more information, try the SQL links at the bottom of the page!
9ood database design suggests that each table lists data only about a single entity, and
detailed information can be obtained in a relational database, by using additional tables,
and by using a "oin!
7irst, take a look at these e#ample tables<
Anti'ue#(ners
#(nerI #(nerLastName #(nerFirstName
E> 2ones ,ill
E? Smith ,ob
>= Lawson 3atricia
?> %kins 2ane
=E 7owler Sam
#rders
#(nerI Itemesired
E? Table
E? 4esk
?> )hair
>= $irror
Anti'ues
SellerI BuyerI Item
E> =E ,ed
E? >= Table
>= E? )hair
?> =E $irror
=E E> 4esk
E> ?> )abinet
E? ?> )offee Table
>= =E )hair
E> >= 2ewelry ,o#
E? ?> 3ottery
?> E? ,ookcase
=E E> 3lant Stand
)eys
7irst, letGs discuss the concept of #eys! % $rimary #ey is a column or set of columns that
uni6uely identifies the rest of the data in any given row! 7or e#ample, in the
%nti6ue"wners table, the "wner4 column uni6uely identifies that row! This means two
things< no two rows can have the same "wner4, and, even if two owners have the same
first and last names, the "wner4 column ensures that the two owners will not be
confused with each other, because the uni6ue "wner4 column will be used throughout
the database to track the owners, rather than the names!
% %oreign #ey is a column in a table where that column is a primary key of another table,
which means that any data in a foreign key column must have corresponding data in the
other table where that column is the primary key! n 4,$SIspeak, this correspondence is
known as re%erential integrity! 7or e#ample, in the %nti6ues table, both the ,uyer4 and
Seller4 are foreign keys to the primary key of the %nti6ue"wners table ("wner4H for
purposes of argument, one has to be an %nti6ue "wner before one can buy or sell any
items), as, in both tables, the 4 rows are used to identify the owners or buyers and
sellers, and that the "wner4 is the primary key of the %nti6ue"wners table! n other
words, all of this &4& data is used to refer to the owners, buyers, or sellers of anti6ues,
themselves, without having to use the actual names!
!erforming a &oin
The purpose of these #eys is so that data can be related across tables, without having to
repeat data in every tableIIthis is the power of relational databases! 7or e#ample, you can
find the names of those who bought a chair without having to list the full name of the
buyer in the %nti6ues table!!!you can get the name by relating those who bought a chair
with the names in the %nti6ue"wners table through the use of the "wner4, which
relates the data in the two tables! To find the names of those who bought a chair, use the
following 6uery<
SELECT O%NERLASTNAME, O%NERF"RSTNAME
FROM ANT"9:EO%NERS, ANT"9:ES
%&ERE $:!ER"# ( O%NER"# AN# "TEM ( /C;air/;
'ote the following about this 6uery!!!notice that both tables involved in the relation are
listed in the 7."$ clause of the statement! n the W:-.- clause, first notice that the
T-$ J G)hairG part restricts the listing to those who have bought (and in this e#ample,
thereby own) a chair! Secondly, notice how the 4 columns are related from one table to
the ne#t by use of the ,0*-.4 J "W'-.4 clause! "nly where 4Gs match across
tables and the item purchased is a chair (because of the %'4), will the names from the
%nti6ue"wners table be listed! ,ecause the (oining condition used an e6ual sign, this (oin
is called an equi"oin! The result of this 6uery is two names< Smith, ,ob / 7owler, Sam!
Dot notation refers to prefi#ing the table names to column names, to avoid ambiguity, as
follows<
SELECT ANT"9:EO%NERS.O%NERLASTNAME, ANT"9:EO%NERS.O%NERF"RSTNAME
FROM ANT"9:EO%NERS, ANT"9:ES
%&ERE ANT"9:ES.$:!ER"# ( ANT"9:EO%NERS.O%NER"# AN# ANT"9:ES."TEM (
/C;air/;
%s the column names are different in each table, however, this wasnGt necessary!
!ISTIN"T and Eliminating uplicates
LetGs say that you want to list the 4 and names of only those people who have sold an
anti6ue! "bviously, you want a list where each seller is only listed onceIIyou donGt want
to know how many anti6ues a person sold, (ust the fact that this person sold one (for
counts, see the %ggregate 7unction section below)! This means that you will need to tell
SQL to eliminate duplicate sales rows, and (ust list each person only once! To do this, use
the DIS!IN&! keyword!
7irst, we will need an e6ui(oin to the %nti6ue"wners table to get the detail data of the
personGs Last'ame and 7irst'ame! :owever, keep in mind that since the Seller4
column in the %nti6ues table is a foreign key to the %nti6ue"wners table, a seller will
only be listed if there is a row in the %nti6ue"wners table listing the 4 and names! We
also want to eliminate multiple occurrences of the Seller4 in our listing, so we use
DIS!IN&! on the column where the repeats may occur (however, it is generally not
necessary to strictly put the 4istinct in front of the column name)!
To throw in one more twist, we will also want the list alphabetiNed by Last'ame, then by
7irst'ame (on a Last'ame tie)! Thus, we will use the ORDER ' clause<
SELECT #"ST"NCT SELLER"#, O%NERLASTNAME, O%NERF"RSTNAME
FROM ANT"9:ES, ANT"9:EO%NERS
%&ERE SELLER"# ( O%NER"#
OR#ER $! O%NERLASTNAME, O%NERF"RSTNAME;
n this e#ample, since everyone has sold an item, we will get a listing of all of the
owners, in alphabetical order by last name! 7or future reference (and in case anyone
asks), this type of (oin is considered to be in the category of inner "oins.
Aliases * In+Sub'ueries
n this section, we will talk about Aliases, In and the use of sub6ueries, and how these can
be used in a DItable e#ample! 7irst, look at this 6uery which prints the last name of those
owners who have placed an order and what the order is, only listing those orders which
can be filled (that is, there is a buyer who owns that ordered item)<
SELECT O%N.O%NERLASTNAME Last Name, OR#."TEM#ES"RE# "tem Ordered
FROM OR#ERS OR#, ANT"9:EO%NERS O%N
%&ERE OR#.O%NER"# ( O%N.O%NER"#
AN# OR#."TEM#ES"RE# "N
4SELECT "TEM
FROM ANT"9:ES5;
This gives<
Last Name "tem Ordered
+++++++++ ++++++++++++
Smit; Table
Smit; #es<
A<ins C;air
La=son Mirror
There are several things to note about this 6uery<
>! 7irst, the &Last 'ame& and &tem "rdered& in the Select lines gives the headers on
the report!
?! The "W' / ".4 are aliasesH these are new names for the two tables listed in the
7."$ clause that are used as prefi#es for all dot notations of column names in
the 6uery (see above)! This eliminates ambiguity, especially in the e6ui(oin
W:-.- clause where both tables have the column named "wner4, and the dot
notation tells SQL that we are talking about two different "wner4Gs from the two
different tables!
D! 'ote that the "rders table is listed first in the 7."$ clauseH this makes sure
listing is done off of that table, and the %nti6ue"wners table is only used for the
detail information (Last 'ame)!
C! $ost importantly, the %'4 in the W:-.- clause forces the n Sub6uery to be
invoked (&J %'*& or &J S"$-& are two e6uivalent uses of ')! What this does
is, the sub6uery is performed, returning all of the tems owned from the %nti6ues
table, as there is no W:-.- clause! Then, for a row from the "rders table to be
listed, the tem4esired must be in that returned list of tems owned from the
%nti6ues table, thus listing an item only if the order can be filled from another
owner! *ou can think of it this way< the sub6uery returns a set of tems from
which each tem4esired in the "rders table is comparedH the n condition is true
only if the tem4esired is in that returned set from the %nti6ues table!
=! %lso notice, that in this case, that there happened to be an anti6ue available for
each one desired!!!obviously, that wonGt always be the case! n addition, notice that
when the ', &J %'*&, or &J S"$-& is used, that these keywords refer to any
possible row matches, not column matches!!!that is, you cannot put multiple
columns in the sub6uery Select clause, in an attempt to match the column in the
outer Where clause to one of multiple possible column values in the sub6ueryH
only one column can be listed in the sub6uery, and the possible match comes from
multiple row values in that one column, not viceIversa!
Whew+ ThatGs enough on the topic of comple# S-L-)T 6ueries for now! 'ow on to other
SQL statements!
,iscellaneous SQL Statements
Aggregate Functions
will discuss five important aggregate %unctions< S0$, %89, $%;, $', and )"0'T!
They are called aggregate functions because they summariNe the results of a 6uery, rather
than listing all of the rows!
S0$ () gives the total of all the rows, satisfying any conditions, of the given
column, where the given column is numeric!
%89 () gives the average of the given column!
$%; () gives the largest figure in the given column!
$' () gives the smallest figure in the given column!
)"0'T(P) gives the number of rows satisfying the conditions!
Looking at the tables at the top of the document, letGs look at three e#amples<
SELECT S:M4SALAR!5, A>?4SALAR!5
FROM EMLO!EESTAT"ST"CSTA$LE;
This 6uery shows the total of all salaries in the table, and the average salary of all of the
entries in the table!
SELECT M"N4$ENEF"TS5
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE OS"T"ON ( /Mana0er/;
This 6uery gives the smallest figure of the ,enefits column, of the employees who are
$anagers, which is >?=EE!
SELECT CO:NT4*5
FROM EMLO!EESTAT"ST"CSTA$LE
%&ERE OS"T"ON ( /Sta11/;
This 6uery tells you how many employees have Staff status (D)!
-ie(s
n SQL, you might (check your 4,%) have access to create views for yourself! What a
view does is to allow you to assign the results of a 6uery to a new, personal table, that
you can use in other 6ueries, where this new table is given the view name in your 7."$
clause! When you access a view, the 6uery that is defined in your view creation statement
is performed (generally), and the results of that 6uery look (ust like another table in the
6uery that you wrote invoking the view! 7or e#ample, to create a view<
CREATE >"E% ANT>"E% AS SELECT "TEM#ES"RE# FROM OR#ERS;
'ow, write a 6uery using this view as a table, where the table is (ust a listing of all tems
4esired from the "rders table<
SELECT SELLER"#
FROM ANT"9:ES, ANT>"E%
%&ERE "TEM#ES"RE# ( "TEM;
This 6uery shows all Seller4Gs from the %nti6ues table where the tem in that table
happens to appear in the %ntview view, which is (ust all of the tems 4esired in the
"rders table! The listing is generated by going through the %nti6ue tems oneIbyIone
until thereGs a match with the %ntview view! 8iews can be used to restrict database access,
as well as, in this case, simplify a comple# 6uery!
Creating Ne( Tables
%ll tables within a database must be created at some point in time!!!letGs see how we
would create the "rders table<
CREATE TA$LE OR#ERS
4O%NER"# "NTE?ER NOT N:LL,
"TEM#ES"RE# C&AR4.*5 NOT N:LL5;
This statement gives the table name and tells the 4,$S about each column in the table!
#lease note that this statement uses generic data types, and that the data types might be
different, depending on what 4,$S you are using! %s usual, check local listings! Some
common generic data types are<
)har(#) I % column of characters, where # is a number designating the ma#imum
number of characters allowed (ma#imum length) in the column!
nteger I % column of whole numbers, positive or negative!
4ecimal(#, y) I % column of decimal numbers, where # is the ma#imum length in
digits of the decimal numbers in this column, and y is the ma#imum number of
digits allowed after the decimal point! The ma#imum (C,?) number would be
FF!FF!
4ate I % date column in a 4,$SIspecific format!
Logical I % column that can hold only two values< T.0- or 7%LS-!
"ne other note, the '"T '0LL means that the column must have a value in each row! f
'0LL was used, that column may be left empty in a given row!
Altering Tables
LetGs add a column to the %nti6ues table to allow the entry of the price of a given tem
(3arentheses optional)<
ALTER TA$LE ANT"9:ES A## 4R"CE #EC"MAL4@,-5 N:LL5;
The data for this new column can be updated or inserted as shown later!
Adding ata
To insert rows into a table, do the following<
"NSERT "NTO ANT"9:ES >AL:ES 4-,, *,, /Ottoman/, -**.**5;
This inserts the data into the table, as a new row, columnIbyIcolumn, in the preIdefined
order! nstead, letGs change the order and leave 3rice blank<
"NSERT "NTO ANT"9:ES 4$:!ER"#, SELLER"#, "TEM5
>AL:ES 4*,, -,, /Ottoman/5;
eleting ata
LetGs delete this new row back out of the database<
#ELETE FROM ANT"9:ES
%&ERE "TEM ( /Ottoman/;
,ut if there is another row that contains G"ttomanG, that row will be deleted also! LetGs
delete all rows (one, in this case) that contain the specific data we added before<
#ELETE FROM ANT"9:ES
%&ERE "TEM ( /Ottoman/ AN# $:!ER"# ( *, AN# SELLER"# ( -,;
%pdating ata
LetGs update a 3rice into a row that doesnGt have a price listed yet<
:#ATE ANT"9:ES SET R"CE ( )**.** %&ERE "TEM ( /C;air/;
This sets all )hairGs 3rices to =EE!EE! %s shown above, more W:-.- conditionals, using
%'4, must be used to limit the updating to more specific rows! %lso, additional columns
may be set by separating e6ual statements with commas!
,iscellaneous Topics
Inde$es
nde#es allow a 4,$S to access data 6uicker ($lease note( this feature is
nonstandard5not available on all systems)! The system creates this internal data structure
(the inde#) which causes selection of rows, when the selection is based on inde#ed
columns, to occur faster! This inde# tells the 4,$S where a certain row is in the table
given an inde#edIcolumn value, much like a book inde# tells you what page a given word
appears! LetGs create an inde# for the "wner4 in the %nti6ue"wners table<
CREATE "N#EA O"#B"#A ON ANT"9:EO%NERS 4O%NER"#5;
'ow on the names<
CREATE "N#EA NAMEB"#A ON ANT"9:EO%NERS 4O%NERLASTNAME, O%NERF"RSTNAME5;
To get rid of an inde#, drop it<
#RO "N#EA O"#B"#A;
,y the way, you can also &drop& a table, as well (careful+IIthat means that your table is
deleted)! n the second e#ample, the inde# is kept on the two columns, aggregated
togetherIIstrange behavior might occur in this situation!!!check the manual before
performing such an operation!
Some 4,$SGs do not enforce primary keysH in other words, the uni6ueness of a column
is not enforced automatically! What that means is, if, for e#ample, tried to insert another
row into the %nti6ue"wners table with an "wner4 of E?, some systems will allow me to
do that, even though we do not, as that column is supposed to be uni6ue to that table
(every row value is supposed to be different)! "ne way to get around that is to create a
uni6ue inde# on the column that we want to be a primary key, to force the system to
enforce prohibition of duplicates<
CREATE :N"9:E "N#EA O"#B"#A ON ANT"9:EO%NERS 4O%NER"#5;
%&O'# B( & )*+IN%
"ne special use of 9."03 ,* is to associate an aggregate function (especially )"0'TH
counting the number of rows in each group) with groups of rows! 7irst, assume that the
%nti6ues table has the 3rice column, and each row has a value for that column! We want
to see the price of the most e#pensive item bought by each owner! We have to tell SQL to
grou$ each ownerGs purchases, and tell us the ma#imum purchase price<
SELECT $:!ER"#, MAA4R"CE5
FROM ANT"9:ES
?RO: $! $:!ER"#;
'ow, say we only want to see the ma#imum purchase price if the purchase is over M>EEE,
so we use the :%8'9 clause<
SELECT $:!ER"#, MAA4R"CE5
FROM ANT"9:ES
?RO: $! $:!ER"#
&A>"N? R"CE ' ,***;
,ore S-./-eries
%nother common usage of sub6ueries involves the use of operators to allow a Where
condition to include the Select output of a sub6uery! 7irst, list the buyers who purchased
an e#pensive item (the 3rice of the item is M>EE greater than the average price of all items
purchased)<
SELECT $:!ER"#
FROM ANT"9:ES
%&ERE R"CE '
4SELECT A>?4R"CE5 C ,**
FROM ANT"9:ES5;
The sub6uery calculates the average 3rice, plus M>EE, and using that figure, an "wner4
is printed for every item costing over that figure! "ne could use 4ST')T ,0*-.4,
to eliminate duplicates!
List the Last 'ames of those in the %nti6ue"wners table, "'L* if they have bought an
item<
SELECT O%NERLASTNAME
FROM ANT"9:EO%NERS
%&ERE O%NER"# "N
4SELECT #"ST"NCT $:!ER"#
FROM ANT"9:ES5;
The sub6uery returns a list of buyers, and the Last 'ame is printed for an %nti6ue "wner
if and only if the "wnerGs 4 appears in the sub6uery list (sometimes called a candidate
list)! Note( on some 4,$SGs, e6uals can be used instead of ', but for clarityGs sake,
since a set is returned from the sub6uery, ' is the better choice!
7or an 0pdate e#ample, we know that the gentleman who bought the bookcase has the
wrong 7irst 'ame in the database!!!it should be 2ohn<
:#ATE ANT"9:EO%NERS
SET O%NERF"RSTNAME ( /Do;n/
%&ERE O%NER"# (
4SELECT $:!ER"#
FROM ANT"9:ES
%&ERE "TEM ( /$oo<Ease/5;
7irst, the sub6uery finds the ,uyer4 for the person(s) who bought the ,ookcase, then
the outer 6uery updates his 7irst 'ame!
"emember this rule about sub'ueries. when you have a sub6uery as part of a W:-.-
condition, the Select clause in the sub6uery must have columns that match in number and
type to those in the Where clause of the outer 6uery! n other words, if you have &%&ERE
ColumnName ( 4SELECT...5;&, the Select must have only one column in it, to match the
)olumn'ame in the outer Where clause, and they must match in type (both being
integers, both being character strings, etc!)!
E0ISTS & *LL
-;STS uses a sub6uery as a condition, where the condition is True if the sub6uery
returns any rows, and 7alse if the sub6uery does not return any rowsH this is a
nonintuitive feature with few uni6ue uses! :owever, if a prospective customer wanted to
see the list of "wners only if the shop dealt in )hairs, try<
SELECT O%NERF"RSTNAME, O%NERLASTNAME
FROM ANT"9:EO%NERS
%&ERE EA"STS
4SELECT *
FROM ANT"9:ES
%&ERE "TEM ( /C;air/5;
f there are any )hairs in the %nti6ues column, the sub6uery would return a row or rows,
making the -;STS clause true, causing SQL to list the %nti6ue "wners! f there had
been no )hairs, no rows would have been returned by the outside 6uery!
%LL is another unusual feature, as %LL 6ueries can usually be done with different, and
possibly simpler methodsH letGs take a look at an e#ample 6uery<
SELECT $:!ER"#, "TEM
FROM ANT"9:ES
%&ERE R"CE '( ALL
4SELECT R"CE
FROM ANT"9:ES5;
This will return the largest priced item (or more than one item if there is a tie), and its
buyer! The sub6uery returns a list of all 3rices in the %nti6ues table, and the outer 6uery
goes through each row of the %nti6ues table, and if its 3rice is greater than or e6ual to
every (or %LL) 3rices in the list, it is listed, giving the highest priced tem! The reason
&J& must be used is that the highest priced item will be e6ual to the highest price on the
list, because this tem is in the 3rice list!
'NION & O-ter 1oins (.riefl2 e$plained)
There are occasions where you might want to see the results of multiple 6ueries together,
combining their outputH use 0'"'! To merge the output of the following two 6ueries,
displaying the 4Gs of all ,uyers, plus all those who have an "rder placed<
SELECT $:!ER"#
FROM ANT"9:ES
:N"ON
SELECT O%NER"#
FROM OR#ERS;
'otice that SQL re6uires that the Select list (of columns) must match, columnIbyI
column, in data type! n this case ,uyer4 and "wner4 are of the same data type
(integer)! %lso notice that SQL does automatic duplicate elimination when using 0'"'
(as if they were two &sets&)H in single 6ueries, you have to use 4ST')T!
The outer "oin is used when a (oin 6uery is &united& with the rows not included in the
(oin, and are especially useful if constant te#t &flags& are included! 7irst, look at the
6uery<
SELECT O%NER"#, /is in bot; Orders F AntiGues/
FROM OR#ERS, ANT"9:ES
%&ERE O%NER"# ( $:!ER"#
:N"ON
SELECT $:!ER"#, /is in AntiGues only/
FROM ANT"9:ES
%&ERE $:!ER"# NOT "N
4SELECT O%NER"#
FROM OR#ERS5;
The first 6uery does a (oin to list any owners who are in both tables, and putting a tag line
after the 4 repeating the 6uote! The 0'"' merges this list with the ne#t list! The
second list is generated by first listing those 4Gs not in the "rders table, thus generating a
list of 4Gs e#cluded from the (oin 6uery! Then, each row in the %nti6ues table is scanned,
and if the ,uyer4 is not in this e#clusion list, it is listed with its 6uoted tag! There might
be an easier way to make this list, but itGs difficult to generate the informational 6uoted
strings of te#t!
This concept is useful in situations where a primary key is related to a foreign key, but the
foreign key value for some primary keys is '0LL! 7or e#ample, in one table, the primary
key is a salesperson, and in another table is customers, with their salesperson listed in the
same row! :owever, if a salesperson has no customers, that personGs name wonGt appear in
the customer table! The outer (oin is used if the listing of all salespersons is to be printed,
listed with their customers, whether the salesperson has a customer or notIIthat is, no
customer is printed (a logical '0LL value) if the salesperson has no customers, but is in
the salespersons table! "therwise, the salesperson will be listed with each customer!
%nother important related point about 'ulls having to do with (oins< the order of tables
listed in the 7rom clause is very important! The rule states that SQL &adds& the second
table to the firstH the first table listed has any rows where there is a null on the (oin
column displayedH if the second table has a row with a null on the (oin column, that row
from the table listed second does not get (oined, and thus included with the first tableGs
row data! This is another occasion (should you wish that data included in the result)
where an outer (oin is commonly used! The concept of nulls is important, and it may be
worth your time to investigate them further!
-'"09: Q0-.-S+++ you sayQ!!!now on to something completely different!!!
Em.edded SQL33an -gl2 e$ample (do not 4rite a program li5e this666for
p-rposes of arg-ment ONL()
H* +To 0et ri0;t to it, ;ere is an eIample pro0ram t;at uses Embedded
S9L. Embedded S9L allo=s pro0rammers to EonneEt to a database and
inElude S9L Eode ri0;t in t;e pro0ram, so t;at t;eir pro0rams Ean
use, manipulate, and proEess data 1rom a database.
+T;is eIample C ro0ram 4usin0 Embedded S9L5 =ill print a report.
+T;is pro0ram =ill ;aJe to be preEompiled 1or t;e S9L statements,
be1ore re0ular Eompilation.
+T;e EAEC S9L parts are t;e same 4standard5, but t;e surroundin0 C
Eode =ill need to be E;an0ed, inEludin0 t;e ;ost Jariable
deElarations, i1 you are usin0 a di11erent lan0ua0e.
+Embedded S9L E;an0es 1rom system to system, so, onEe a0ain, E;eE<
loEal doEumentation, espeEially Jariable deElarations and lo00in0
in proEedures, in =;iE; net=or<, #$MS, and operatin0 system
Eonsiderations are EruEial. *H
H************************************************H
H* T&"S RO?RAM "S NOT COM"LA$LE OR EAEC:TA$LE *H
H* "T "S FOR EAAMLE :ROSES ONL! *H
H************************************************H
KinElude 2stdio.;'
H* T;is seEtion deElares t;e ;ost Jariables; t;ese =ill be t;e
Jariables your pro0ram uses, but also t;e Jariable S9L =ill put
Jalues in or ta<e Jalues out. *H
EAEC S9L $E?"N #ECLARE SECT"ON;
int $uyer"#;
E;ar FirstNameL,**M, LastNameL,**M, "temL,**M;
EAEC S9L EN# #ECLARE SECT"ON;
H* T;is inEludes t;e S9LCA Jariable, so t;at some error E;eE<in0 Ean be
done. *H
EAEC S9L "NCL:#E S9LCA;
main45 N
H* T;is is a possible =ay to lo0 into t;e database *H
EAEC S9L CONNECT :ser"#Hass=ord;
H* T;is Eode eit;er says t;at you are EonneEted or E;eE<s i1 an error
Eode =as 0enerated, meanin0 lo0 in =as inEorreEt or not possible. *H
i14sGlEa.sGlEode5 N
print14rinter, OError EonneEtin0 to database serJer.PnO5;
eIit45;
Q
print14OConneEted to database serJer.PnO5;
H* T;is deElares a OCursorO. T;is is used =;en a Guery returns more
t;an one ro=, and an operation is to be per1ormed on eaE; ro=
resultin0 1rom t;e Guery. %it; eaE; ro= establis;ed by t;is Guery,
"/m 0oin0 to use it in t;e report. Later, OFetE;O =ill be used to
piE< o11 eaE; ro=, one at a time, but 1or t;e Guery to aEtually
be eIeEuted, t;e OOpenO statement is used. T;e O#eElareO Rust
establis;es t;e Guery. *H
EAEC S9L #ECLARE "temCursor C:RSOR FOR
SELECT "TEM, $:!ER"#
FROM ANT"9:ES
OR#ER $! "TEM;
EAEC S9L OEN "temCursor;
H* C++ !ou may =is; to put a similar error E;eE<in0 bloE< ;ere ++C *H
H* FetE; puts t;e Jalues o1 t;e OneItO ro= o1 t;e Guery in t;e ;ost
Jariables, respeEtiJely. &o=eJer, a Oprimin0 1etE;O 4pro0rammin0
teE;niGue5 must 1irst be done. %;en t;e Eursor is out o1 data, a
sGlEode =ill be 0enerated allo=in0 us to leaJe t;e loop. NotiEe
t;at, 1or simpliEity/s sa<e, t;e loop =ill leaJe on any sGlEode,
eJen i1 it is an error Eode. Ot;er=ise, speEi1iE Eode E;eE<in0 must
be per1ormed. *H
EAEC S9L FETC& "temCursor "NTO S"tem, S$uyer"#;
=;ile4TsGlEa.sGlEode5 N
H* %it; eaE; ro=, =e =ill also do a Eouple o1 t;in0s. First, bump t;e
priEe up by U) 4dealer/s 1ee5 and 0et t;e buyer/s name to put in
t;e report. To do t;is, "/ll use an :pdate and a SeleEt, be1ore
printin0 t;e line on t;e sEreen. T;e update assumes ;o=eJer, t;at
a 0iJen buyer ;as only bou0;t one o1 any 0iJen item, or else t;e
priEe =ill be inEreased too many times. Ot;er=ise, a ORo="#O lo0iE
=ould ;aJe to be used 4see doEumentation5. Also notiEe t;e Eolon
be1ore ;ost Jariable names =;en used inside o1 S9L statements. *H
EAEC S9L :#ATE ANT"9:ES
SET R"CE ( R"CE C )
%&ERE "TEM ( S"tem AN# $:!ER"# ( S$uyer"#;
EAEC S9L SELECT O%NERF"RSTNAME, O%NERLASTNAME
"NTO SFirstName, SLastName
FROM ANT"9:EO%NERS
%&ERE $:!ER"# ( S$uyer"#;
print14O8-)s 8-)s 8-)sO, FirstName, LastName, "tem5;
H* :0ly report++1or eIample purposes onlyT ?et t;e neIt ro=. *H
EAEC S9L FETC& "temCursor "NTO S"tem, S$uyer"#;
Q
H* Close t;e Eursor, Eommit t;e E;an0es 4see belo=5, and eIit t;e
pro0ram. *H
EAEC S9L CLOSE "temCursor;
EAEC S9L COMM"T RELEASE;
eIit45;
Q
"ommon SQL Q-estions & *d7anced Topics
>! Why canGt (ust ask for the first three rows in a tableQ II,ecause in relational
databases, rows are inserted in no particular order, that is, the system inserts them
in an arbitrary orderH so, you can only re6uest rows using valid SQL features, like
".4-. ,*, etc!
?! What is this 44L and 4$L hear aboutQ II44L (4ata 4efinition Language)
refers to (in SQL) the )reate Table statement!!!4$L (4ata $anipulation
Language) refers to the Select, 0pdate, nsert, and 4elete statements! %lso, Q$L,
referring to Select statements, stands for Query $anipulation Language!
D! %renGt database tables (ust filesQ IIWell, 4,$SGs store data in files declared by
system managers before new tables are created (on large systems), but the system
stores the data in a special format, and may spread data from one table over
several files! n the database world, a set of files created for a database is called a
ta)les$ace! n general, on small systems, everything about a database (definitions
and all table data) is kept in one file!
C! (.elated 6uestion) %renGt database tables (ust like spreadsheetsQ II'o, for two
reasons! 7irst, spreadsheets can have data in a cell, but a cell is more than (ust a
rowIcolumnIintersection! 4epending on your spreadsheet software, a cell might
also contain formulas and formatting, which database tables cannot have
(currently)! Secondly, spreadsheet cells are often dependent on the data in other
cells! n databases, &cells& are independent, e#cept that columns are logically
related (hopefullyH together a row of columns describe an entity), and, other than
primary key and foreign key constraints, each row in a table is independent from
one another!
=! :ow do import a te#t file of data into a databaseQ IIWell, you canGt do it
directly!!!you must use a utility, such as "racleGs SQLPLoader, or write a program
to load the data into the database! % program to do this would simply go through
each record of a te#t file, break it up into columns, and do an nsert into the
database!
@! What web sites and computer books would you recommend for more information
about SQL and databasesQ II7irst, look at the sites at the bottom of this page!
would especially suggest the following< 4, ngredients (more theorical topics),
4,$S Lab5Links (comprehensive academic 4,$S link listing), Tutorial 3age
(listing of other tutorials), and miniSQL (more information about the best known
free 4,$S)!
%lso, if you wish to practice SQL on an interactive site (using 2ava technologies),
highly recommend 7rank TorresG (torresfRuswest!net) site at
http<55s6lcourse!com and its new se6uel (so to speak) site at
http<55s6lcourse?!com! 7rank did an outstanding (ob with his site, and if you have
a recent release browser, itGs definitely worth a visit! n addition, point your
browser to www!topica!com, and subscribe to their SQL eImail Tips of the
4ay!!!they are outstandingH Tim Quinlan goes into topics that canGt even begin to
go into here, such inde# data structures (,Itrees and ,SItrees) and (oin
algorithms, so advanced T .4,$S pros will get a daily insight into these data
management tools!
0nfortunately, there is not a great deal of information on the web about SQLH the
list have below is fairly comprehensive (definitely representative)! %s far as
books are concerned, would suggest (for beginners to intermediateIlevel)
&"racle< The )omplete .eference& (multiple versions) from "racle and
&0nderstanding SQL& from Sybe# for general SQL information! %lso, would
recommend "G.eilly 3ublishingGs books, and 2oe )elkoGs writings for advanced
users! 7or specific 4,$S info (especially in the %ccess area), recommend QueGs
&0sing& series, and the books of %lison ,alter!
B! What is a sc*emaQ II% schema is a logical set of tables, such as the %nti6ues
database above!!!usually, it is thought of as simply &the database&, but a database
can hold more than one schema! 7or e#ample, a star sc*ema is a set of tables
where one large, central table holds all of the important information, and is linked,
via foreign keys, to dimension tables which hold detail information, and can be
used in a (oin to create detailed reports!
A! understand that "racle offers a special keyword, 4ecode, that allows for some
&ifIthen& logic! :ow does that workQ II Technically, 4ecode allows for
conditional output based on the value of a column or function! The synta# looks
like this (from the "racle< )omplete .eference series)<
SeleEt ...#ECO#E 4>alue, "1,, T;en,, L"1 -, T;en -, ...,M
Else5 ...From ...;
The 8alue is the name of a column, or a function (conceivably based on a column
or columns), and for each f included in the statement, the corresponding Then
clause is the output if the condition is true! f none of the conditions are true, then
the -lse value is output! LetGs look at an e#ample<
SeleEt #istinEt City,
#ECO#E 4City, /CinEinnati/, /9ueen City/, /Ne= !or</, /$i0 Apple/,
/C;iEa0o/,
/City o1 $road S;oulders/, City5 AS NiE<name
From Cities;
The output might look like this<
City NiE<name
++++++++++++ ++++++++++++++++++++++++++++++
$oston $oston
CinEinnati 9ueen City
CleJeland CleJeland
Ne= !or< $i0 Apple
G)ityG in the first argument denotes the column name used for the test! The second,
fourth, etc! arguments are the individual e6uality tests (taken in the order given)
against each value in the )ity column! The third, fifth, etc! arguments are the
corresponding outputs if the corresponding test is true! The final parameter is the
default output if none of the tests are trueH in this case, (ust print out the column
value!
T3< f you want nothing to be output for a given condition, such as the default
&-lse& value, enter the value 'ull for that value, such as<
SeleEt #istinEt City,
#ECO#E 4City, /CinEinnati/, /9ueen City/, /Ne= !or</, /$i0 Apple/,
/C;iEa0o/,
/City o1 $road S;oulders/, Null5 AS NiE<name
From Cities;
f the )ity column value is not one of the ones mentioned, nothing is outputted,
rather than the city name itself!
City NiE<name
++++++++++++ ++++++++++
$oston
CinEinnati 9ueen City
CleJeland
Ne= !or< $i0 Apple

F! *ou mentioned .eferential ntegrity before, but what does that have to do with
this concept Gve heard about, )ascading 0pdates and 4eletesQ IIThis is a difficult
topic to talk about, because itGs covered differently in different 4,$SGs!
7or e#ample, $icrosoft SQL Server (B!E / below) re6uires that you write
&triggers& (see the *ahoo SQL )lub link to find links that discuss this topicII may
include that topic in a future version of this page) to implement this! (% 6uick
definition, thoughH a Trigger is a SQL statement stored in the database that allows
you to perform a given 6uery Tusually an &%ction& QueryII4elete, nsert, 0pdateU
automatically, when a specified event occurs in the database, such as a column
update, but anyway!!!) $icrosoft %ccess (believe it or not) will perform this if you
define it in the .elationships screen, but it will still burden you with a prompt!
"racle does this automatically, if you specify a special &)onstraint& (see reference
at bottom for definition, not synta#) on the keyed column!
So, Gll (ust discuss the concept! 7irst, see the discussion above on 3rimary and
7oreign keys!
)oncept< f a row from the primary key column is deleted5updated, if &)ascading&
is activated, the value of the foreign key in those other tables will be deleted (the
whole row)5updated!
The reverse, a foreign key deletion5update causing a primary key value to be
deleted5changed, may or may not occur< the constraint or trigger may not be
defined, a &oneItoImany& relationship may e#ist, the update might be to another
e#isting primary key value, or the 4,$S itself may or may not have rules
governing this! %s usual, see your 4,$SGs documentation!
7or e#ample, if you set up the %nti6ue"wners table to have a 3rimary 1ey,
"wner4, and you set up the database to delete rows on the 7oreign 1ey,
Seller4, in the %nti6ues table, on a primary key deletion, then if you deleted the
%nti6ue"wners row with "wner4 of GE>G, then the rows in %nti6ues, with the
tem values, ,ed, )abinet, and 2ewelry ,o# (GE>G sold them), will all be deleted!
"f course, assuming the proper 4, definition, if you (ust updated GE>G to another
value, those Seller 4 values would be updated to that new value too!
>E! Show me an e#ample of an outer "oin. IIWell, from the 6uestions receive, this is
an e#tremely common e#ample, and Gll show you both the "racle and %ccess
6ueries!!!
Think of the following -mployee table (the employees are given numbers, for
simplicity)<
'ame 4epartment
> >E
? >E
D ?E
C DE
= DE
'ow think of a department table<
4epartment
>E
?E
DE
CE
'ow suppose you want to (oin the tables, seeing all of the employees and all of
the departments together!!!youGll have to use an outer (oin which includes a null
employee to go with 4ept! CE!
n the book, &"racle B< the )omplete .eference&, about outer (oins, &think of the
(S), which must immediately follow the (oin column of the table, as saying add an
e#tra (null) row anytime thereGs no match&! So, in "racle, try this 6uery (the S
goes on -mployee, which adds the null row on no match)<
SeleEt E.Name, #.#epartment
From #epartment #, Employee E
%;ere E.#epartment4C5 ( #.#epartment;
This is a left (outer) (oin, in %ccess<
SELECT #"ST"NCTRO% Employee.Name, #epartment.#epartment
FROM #epartment LEFT DO"N Employee ON #epartment.#epartment (
Employee.#epartment;
%nd you get this result<
'ame 4epartment
> >E
? >E
D ?E
C DE
= DE
CE
>>! What are some general tips you would give to make my SQL 6ueries and
databases better and faster (o$timi+ed)Q
o *ou should try, if you can, to avoid e#pressions in Selects, such as
S-L-)T )olumn% S )olumn,, etc! The query o$timi+er of the database,
the portion of the 4,$S that determines the best way to get the re6uired
data out of the database itself, handles e#pressions in such a way that
would normally re6uire more time to retrieve the data than if columns
were normally selected, and the e#pression itself handled
programmatically!
o $inimiNe the number of columns included in a 9roup ,y clause!
o f you are using a (oin, try to have the columns (oined on (from both
tables) inde#ed!
o When in doubt, inde#!
o 0nless doing multiple counts or a comple# 6uery, use )"0'T(P) (the
number of rows generated by the 6uery) rather than
)"0'T()olumnV'ame)!
>?! What is a )artesian productQ IISimply, it is a (oin without a Where clause! t gives
you every row in the first table, (oined with every row in the second table! This is
best shown by e#ample<
SELECT *
FROM AntiGueO=ners, Orders;
This gives<

Anti'ue#(ners/
#(nerI
Anti'ue#(ners/
#(nerLastName
Anti'ue#(ners/
#(nerFirstName
#rders/
#(nerI
#rders/
Itemesired
E> 2ones ,ill E? Table
E> 2ones ,ill E? 4esk
E> 2ones ,ill ?> )hair
E> 2ones ,ill >= $irror
E? Smith ,ob E? Table
E? Smith ,ob E? 4esk
E? Smith ,ob ?> )hair
E? Smith ,ob >= $irror
>= Lawson 3atricia E? Table
>= Lawson 3atricia E? 4esk
>= Lawson 3atricia ?> )hair
>= Lawson 3atricia >= $irror
?> %kins 2ane E? Table
?> %kins 2ane E? 4esk
?> %kins 2ane ?> )hair
?> %kins 2ane >= $irror
=E 7owler Sam E? Table
=E 7owler Sam E? 4esk
=E 7owler Sam ?> )hair
=E 7owler Sam >= $irror
The number of rows in the result has the number of rows in the first table times
the number of rows in the second table, and is sometimes called a )rossI2oin!
f you think about it, you can see how (oins work! Look at the )artesian product
results, then look for rows where the "wner4Gs are e6ual, and the result is what
you would get on an e6ui(oin!
"f course, this is not how 4,$SGs actually perform (oins because loading this
result can take too much memoryH instead, comparisons are performed in nested
loops, or by comparing values in inde#es, and then loading result rows!
>D! What is Relational Alge)raQ II.elational %lgebra is the mathematical notation
used to describe relational database theory, and to rigorously define a 6uery! t
suggests an algorithm used to perform a 6uery, but does not necessary correspond
to SQL, or represent optimiNed code needed to actually run the 6uery! t is sort of
a mental approach to solve a 6uery problem, by forcing the programmer to think
in terms of sets of rows! n fact, many of the functions described in the following
relational algebraic operations table are similar to regular set operations!
When first learned relational databases, didnGt start with SQL! nstead,
learned to write all my definitions (Table'ame W)olumn%, )olumn,, !!!X) and
6ueries in relational algebraic notation! t forces one to think of getting the 6uery
results one wants by set inclusion and e#clusion! $any college courses in
databases start with this topic! n the e#amples, consider two new tables,
%nti6ues> and %nti6ues?, defined the same as %nti6ues!
#peration efinition E0ample
.ename
.ename Table %ttribute
()olumn)
ren "wner4IL"wn4 (%nti6ue"wners)
0nion
nclude all rows from two
similarly defined tables into
one table (append operation)
%nti6ues> 0 %nti6ues?
ntersection
List rows where the e#act
same row appears in two
simlarly defined tables (actual
symbol is upsideIdown &0&)
%nti6ues> intersection %nti6ues?
4ifference
9ive all rows in one table that
do not appear in another
similarly defined table
(similar to a left5right outer
(oin)
%nti6ues> I %nti6ues?
)artesian 3roduct See Y>? above %nti6ue"wners ; "rders
3ro(ection
Select certain attributes from a
table or view, showing all
rows
$ro" "wner4, "wnerLast'ame
(%nti6ue"wners)
Selection
Show certain rows specified
by a where condition
sel Seller4 J &E>& ". Seller4 J &E?&
(%nti6ues)
-6uiI2oin
2oin on an e6ual condition (as
previously stated, this concept
can be e#pressed by taking the
)artesian 3roduct and picking
out the rows where the
specified key attributes are
e6ual)
%nti6ue"wners equi,"oin "wner4 J
Seller4 %nti6ues
'atural 2oin
3erform an e6uiI(oin, but do
not return both of the (oin
columnsH the e#ample does
not give the "wner4 column
from the %nti6ue"wners
table!
"rders nat,"oin %nti6ue"wners
Theta 2oin 2oin on any operation (J, KL, %nti6ue"wners t*eta,"oin "wner4 K
K, L, KJ, LJ), though
generally performed on
numeric attributes!
Seller4 %nti6ues
SemiI(oin
3erform a (oin, but (ust show
columns from one table, and
include nulls, like in the outer
(oin from Y>E!
-mployee le%t,semi,"oin
-mployee!4epartment J 4epartment!4epartment
4epartment
% (oin 6uery such as<
Select "wnerLast'ame, tem
7rom %nti6ue"wners, %nti6ues
Where "wner4 J Seller4H
can be e#pressed in the relational algebra by
$ro" %nti6ue"wners!"wnerLast'ame, tem %nti6ues (sel %nti6ue"wners!"wner4 J %nti6ues!Seller4
(%nti6ue"wners ; %nti6ues))
7rom right to left, perform a )artesian 3roduct, perform an e6uiI(oin (select
where e6ual), and then display "wnerLast'ame from %nti6ue"wners and tem
from %nti6ues!
>C! What is normali+ationQ II'ormaliNation is a techni6ue of database design that
suggests that certain criteria be used when constructing a table layout (deciding
what columns each table will have, and creating the key structure), where the idea
is to eliminate redundancy of nonIkey data across tables! 'ormaliNation is usually
referred to in terms of %orms, and will introduce only the first three, even though
it is somewhat common to use other, more advanced forms (fourth, fifth, ,oyceI
)oddH see documentation)!
-irst Normal -orm refers to moving data into separate tables where the data in
each table is of a similar type, and by giving each table a primary key!
3utting data in Second Normal -orm involves removing to other tables data that is
only dependent of a part of the key! 7or e#ample, if had left the names of the
%nti6ue "wners in the items table, that would not be in Second 'ormal 7orm
because that data would be redundantH the name would be repeated for each item
ownedH as such, the names were placed in their own table! The names themselves
donGt have anything to do with the items, only the identities of the buyers and
sellers!
!*ird Normal -orm involves getting rid of anything in the tables that doesnGt
depend solely on the primary key! "nly include information that is dependent on
the key, and move off data to other tables that are independent of the primary key,
and create a primary key for the new tables!
There is some redundancy to each form, and if data is in .N- (shorthand for Drd
normal form), it is already in /N- and 0N-! n terms of data design then, arrange
data so that any nonIprimary key columns are dependent only on the w*ole
$rimary #ey! f you take a look at the sample database, you will see that the way
then to navigate through the database is through (oins using common key
columns!
Two other important points in database design are using good, consistent, logical,
fullIword names for the tables and columns, and the use of full words in the
database itself! "n the last point, my database is lacking, as use numeric codes
for identification! t is usually best, if possible, to come up with keys that are, by
themselves, selfIe#planatoryH for e#ample, a better key would be the first four
letters of the last name and first initial of the owner, like 2"'-, for ,ill 2ones (or
for tiebreaking purposes, add numbers to the end to differentiate two or more
people with similar names, so you could try 2"'-,>, 2"'-,?, etc!)!
>=! What is the difference between a single,row query and a multi$le,row query and
why is it important to know the differenceQ II7irst, to cover the obvious, a singleI
row 6uery is a 6uery that returns one row as its result, and a multipleIrow 6uery is
a 6uery that returns more than one row as its result! Whether a 6uery returns one
row or more than one row is entirely dependent on the design (or sc*ema) of the
tables of the database! %s 6ueryIwriter, you must be aware of the schema, be sure
to include enough conditions, and structure your SQL statement properly, so that
you will get the desired result (either one row or multiple rows)! 7or e#ample, if
you wanted to be sure that a 6uery of the %nti6ue"wners table returned only one
row, consider an e6ual condition of the primary key column, "wner4!
Three reasons immediately come to mind as to why this is important! 7irst,
getting multiple rows when you were e#pecting only one, or viceIversa, may
mean that the 6uery is erroneous, that the database is incomplete, or simply, you
learned something new about your data! Second, if you are using an update or
delete statement, you had better be sure that the statement that you write performs
the operation on the desired row (or rows)!!!or else, you might be deleting or
updating more rows than you intend! Third, any 6ueries written in -mbedded
SQL must be carefully thought out as to the number of rows returned! f you write
a singleIrow 6uery, only one SQL statement may need to be performed to
complete the programming logic re6uired! f your 6uery, on the other hand,
returns multiple rows, you will have to use the 7etch statement, and 6uite
probably, some sort of looping structure in your program will be re6uired to
iterate processing on each returned row of the 6uery!
>@! Tell me about a simple approach to relational database design! IIThis was sent to
me via a news postingH it was submitted by 2ohn 7rame ( (frameR(frame!com )
and .ichard 7reedman ( rfreedmRvoicenet!com )H offer a shortened version as
advice, but Gm not responsible for it, and some of the concepts are readdressed in
the ne#t 6uestion!!!
7irst, create a list of important things (entities) and include those things you may
not initially believe is important! Second, draw a line between any two entities
that have any connection whatsoeverH e#cept that no two entities can connect
without a GruleGH e!g!< families have children, employees work for a department!
Therefore put the GconnectionG in a diamond, the GentitiesG in s6uares! Third, your
picture should now have many s6uares (entities) connected to other entities
through diamonds (a s6uare enclosing an entity, with a line to a diamond
describing the relationship, and then another line to the other entity)! 7ourth, put
descriptors on each s6uare and each diamond, such as customer II airline II trip!
7ifth, give each diamond and s6uare any attributes it may have (a person has a
name, an invoice has a number), but some relationships have none (a parent (ust
owns a child)! Si#th, everything on your page that has attributes is now a table,
whenever two entities have a relationship where the relationship has no attributes,
there is merely a foreign key between the tables! Seventh, in general you want to
make tables not repeat data! So, if a customer has a name and several addresses,
you can see that for every address of a customer, there will be repeated the
customerGs first name, last name, etc! So, record 'ame in one table, and put all his
addresses in another! -ighth, each row (record) should be uni6ue from every other
oneH $r! 7reedman suggests a GautoIincrement numberG primary key, where a new,
uni6ue number is generated for each new inserted row! 'inth, a key is any way to
uni6uely identify a row in a table!!!first and last name together are good as a
GcompositeG key! ThatGs the techni6ue!
>B! What are relations*i$s1 II%nother design 6uestion!!!the term &relationships&
(often termed &relation&) usually refers to the relationships among primary and
foreign keys between tables! This concept is important because when the tables of
a relational database are designed, these relationships must be defined because
they determine which columns are or are not primary or foreign keys! *ou may
have heard of an Entity1"elationship iagram, which is a graphical view of
tables in a database schema, with lines connecting related columns across tables!
See the sample diagram at the end of this section or some of the sites below in
regard to this topic, as there are many different ways of drawing -I. diagrams!
,ut first, letGs look at each kind of relationship!!!
% One,to,one relations*i$ means that you have a primary key column that is
related to a foreign key column, and that for every primary key value, there is one
foreign key value! 7or e#ample, in the first e#ample, the -mployee%ddressTable,
we add an -mployee4'o column! Then, the -mployee%ddressTable is related to
the -mployeeStatisticsTable (second e#ample table) by means of that
-mployee4'o! Specifically, each employee in the -mployee%ddressTable has
statistics (one row of data) in the -mployeeStatisticsTable! -ven though this is a
contrived e#ample, this is a &>I>& relationship! %lso notice the &has& in
bold!!!when e#pressing a relationship, it is important to describe the relationship
with a verb!
The other two kinds of relationships may or may not use logical primary key and
foreign key constraints!!!it is strictly a call of the designer! The first of these is the
one,to,many relations*i$ (&>I$&)! This means that for every column value in one
table, there is one or more related values in another table! 1ey constraints may be
added to the design, or possibly (ust the use of some sort of identifier column may
be used to establish the relationship! %n e#ample would be that for every
"wner4 in the %nti6ue"wners table, there are one or more (Nero is permissible
too) tems bought in the %nti6ues table (verb< buy)!
7inally, the many,to,many relations*i$ (&$I$&) does not involve keys generally,
and usually involves identifying columns! The unusual occurrence of a &$I$&
means that one column in one table is related to another column in another table,
and for every value of one of these two columns, there are one or more related
values in the corresponding column in the other table (and viceIversa), or more a
common possibility, two tables have a >I$ relationship to each other (two
relationships, one >I$ going each way)! % TbadU e#ample of the more common
situation would be if you had a (ob assignment database, where one table held one
row for each employee and a (ob assignment, and another table held one row for
each (ob with one of the assigned employees! :ere, you would have multiple rows
for each employee in the first table, one for each (ob assignment, and multiple
rows for each (ob in the second table, one for each employee assigned to the
pro(ect! These tables have a $I$< each employee in the first table has many (ob
assignments from the second table, and each (ob has many employees assigned to
it from the first table! This is the tip of the iceberg on this topic!!!see the links
below for more information and see the diagram below for a sim$li%ied e#ample
of an -I. diagram!
>A! What are some important nonstandard SQL features (e#tremely common
6uestion)Q IIWell, see the ne#t section!!!
Nonstandard SQL6668chec5 local listings8
'T-.S-)T and $'0S are like the 0'"' statement, e#cept that
'T-.S-)T produces rows that appear in both 6ueries, and $'0S produces
rows that result from the first 6uery, but not the second!
.eport 9eneration 7eatures< the )"$30T- clause is placed at the end of a 6uery
to place the result of an aggregate function at the end of a listing, like COM:TE
S:M 4R"CE5; %nother option is to use break logic< define a break to divide the
6uery results into groups based on a column, like $REA7 ON $:!ER"#! Then, to
produce a result after the listing of a group, use COM:TE S:M OF R"CE ON
$:!ER"#! f, for e#ample, you used all three of these clauses (,.-%1 first,
)"$30T- on break second, )"$30T- overall sum third), you would get a
report that grouped items by their ,uyer4, listing the sum of 3rices after each
group of a ,uyer4Gs items, then, after all groups are listed, the sum of all 3rices
is listed, all with SQLIgenerated headers and lines!
n addition to the above listed aggregate functions, some 4,$SGs allow more
functions to be used in Select lists, e#cept that these functions (some character
functions allow multipleIrow results) are to be used with an individual value (not
groups), on single,row queries. The functions are to be used only on appropriate
data types, also! :ere are some ,athematical Functions<
ABS234
%bsolute valueIconverts negative numbers to positive, or leaves
positive numbers alone
CEIL234 ; is a decimal value that will be rounded up!
FL##"234 ; is a decimal value that will be rounded down!
5"EATEST236$4 .eturns the largest of the two values!
LEAST236$4 .eturns the smallest of the two values!
,#236$4 .eturns the remainder of ; 5 *!
!#7E"236$4 .eturns ; to the power of *!
"#%N236$4
.ounds ; to * decimal places! f * is omitted, ; is rounded to the
nearest integer!
SI5N234 .eturns a minus if ; K E, else a plus!
SQ"T234 .eturns the s6uare root of ;!
Character Functions
LEFT28string9634 .eturns the leftmost ; characters of the string!
"I5:T28string9634 .eturns the rightmost ; characters of the string!
%!!E"28string94 )onverts the string to all uppercase letters!
L#7E"28string94 )onverts the string to all lowercase letters!
INITCA!28string94 )onverts the string to initial caps!
LEN5T:28string94 .eturns the number of characters in the string!
8string9;;8string9
)ombines the two strings of te#t into one, concatenated
string, where the first string is immediately followed by
the second!
L!A28string9636<=<4
3ads the string on the left with the P (or whatever character
is inside the 6uotes), to make the string ; characters long!
"!A28string9636<=<4
3ads the string on the right with the P (or whatever
character is inside the 6uotes), to make the string ;
characters long!
S%BST"28string9636$4 -#tracts * letters from the string beginning at position ;!
N-L28column968>alue94
The 'ull value function will substitute KvalueL for any
'0LLs for in the KcolumnL! f the current value of
KcolumnL is not '0LL, '8L has no effect!
Synta0 Summary11For Ad>anced %sers #nly
:ere are the general forms of the statements discussed in this tutorial, plus some e#tra
important ones (e#planations given)! "E,E,BE" that all of these statements may or
may not be available on your system, so check documentation regarding availability<
ALTER TABLE 2TA$LE NAME' A##V#ROVMO#"F! 4COL:MN SEC"F"CAT"ONLSM...see
Create Table5; IIallows you to add or delete a column or columns from a table, or
change the specification (data type, etc!) on an e#isting columnH this statement is also
used to change the physical specifications of a table (how a table is stored, etc!), but these
definitions are 4,$SIspecific, so read the documentation! %lso, these physical
specifications are used with the )reate Table statement, when a table is first created! n
addition, only one option can be performed per %lter Table statement IIeither add, drop,
#" modify in a single statement!
COMMIT? IImakes changes made to some database systems permanent (since the last
)"$$TH known as a transaction)
CREATE [UNIQUE] INDEX 2"N#EA NAME'
ON 2TA$LE NAME' 42COL:MN L"ST'5; II0'Q0- is optionalH within brackets!
CREATE TABLE 2TA$LE NAME'
42COL:MN NAME' 2#ATA T!E' L42S"WE'5M 2COL:MN CONSTRA"NT',
...ot;er Eolumns5; 4also valid with %LT-. T%,L-)
IIwhere SZ- is only used on certain data types (see above), and constraints include the
following possibilities (automatically enforced by the 4,$SH failure causes an error to
be generated)<
>! '0LL or '"T '0LL (see above)
?! 0'Q0- enforces that no two rows will have the same value for this column
D! 3.$%.* 1-* tells the database that this column is the primary key column
(only used if the key is a one column key, otherwise a 3.$%.* 1-* (column,
column, !!!) statement appears after the last column definition!
C! ):-)1 allows a condition to be checked for when data in that column is updated
or insertedH for e#ample, C&EC7 4R"CE *5 causes the system to check that the
3rice column is greater than Nero before accepting the value!!!sometimes
implemented as the )"'ST.%'T statement!
=! 4-7%0LT inserts the default value into the database if a row is inserted without
that columnGs data being insertedH for e#ample, $ENEF"TS "NTE?ER #EFA:LT (
,****
@! 7".-9' 1-* works the same as 3rimary 1ey, but is followed by< REFERENCES
2TA$LE NAME' 42COL:MN NAME'5, which refers to the referential primary key!
CREATE VIEW 2TA$LE NAME' AS 29:ER!';
DELETE FROM 2TA$LE NAME' %&ERE 2CON#"T"ON';
INSERT "NTO 2TA$LE NAME' L42COL:MN L"ST'5M
>AL:ES 42>AL:E L"ST'5;
ROLLBAC? IITakes back any changes to the database that you have made, back to the last
time you gave a )ommit command!!!beware+ Some software uses automatic committing
on systems that use the transaction features, so the .ollback command may not work!
SELECT L#"ST"NCTVALLM 2L"ST OF COL:MNS, F:NCT"ONS, CONSTANTS, ETC.'
FROM 2L"ST OF TA$LES OR >"E%S'
L%&ERE 2CON#"T"ON4S5'M
L?RO: $! 2?RO:"N? COL:MN4S5'M
L&A>"N? 2CON#"T"ON'M
LOR#ER $! 2OR#ER"N? COL:MN4S5' LASCV#ESCMM; IIwhere %S)[4-S) allows the
ordering to be done in %S)ending or 4-S)ending order
U!DATE 2TA$LE NAME'
SET 2COL:MN NAME' ( 2>AL:E'
L%&ERE 2CON#"T"ON'M; IIif the Where clause is left out, all rows will be updated
according to the Set statement!
E$ercises
Queries
0sing the e#ample tables in the tutorial, write a SQL statement to<
>! Show each %nti6ues order and the last and first names of the person who ordered the
item!
?! Show each column in the -mployeeStatisticsTable in alphabetical order by 3osition,
then by -mployee4'o!
D! Show the annual budget for ,enefits from the -mployeeStatisticsTable!
C! 0sing the ' "perator, show the names of the owners of )hairs!
=! Show the names of all %nti6ues "wners who have do not have an order placed!
@! Show the names of those who have placed %nti6ue orders, with no duplicates (:int<
consider the order of tables in the 7rom clause)!
B! 4elete all of ,ob SmithGs %nti6ue orders (:int< ,obGs 4 'umber is E?)!
A! )reate an %nti6ue order for a .ocking )hair for 2ane %kins (:int< 2aneGs 4 'umber
is ?>)!
F! )reate a table called -mployees, with columns -mployee4'o (donGt worry about
trailing Neroes), 7irst'ame, and Last'ame!
>E! ()hallenger) Show the annual budget for Salary by each position from the
-mployeeStatisticsTable (:int< Try 9."03 ,*)!
atabases
>>! What is the relationship between the %nti6ue"wners table and the "wners tableQ
>?! f you do not have a primary key in a table, the addition of what type of column is
preferred to give the table a primary keyQ
>D! Which function will allow you to substitute a given value for any 'ull values arising
from a Select statementQ
>C! When using -mbedded SQL, what do you need to create in order to iterate through
the results of a multiIrow 6uery, one row at a timeQ
>=! f all of the columns in all of the tables in your schema are dependent solely on the
value of the primary key in each table, in which 'ormal 7orm is your designQ
>@! What term is used to describe the event of a database system automatically updating
the values of foreign keys in other tables, when the value of a primary key is updatedQ
>B! What database ob(ect provides fast access to the data in the rows of a tableQ
>A! What type of SQL statement is used to change the attributes of a columnQ
>F! n a )reate Table statement, when a column is designated as '"T '0LL, what does
this meanQ
?E! f you wish to write a 6uery that is based on other 6ueries, rather than tables, what do
these other 6ueries need to be created asQ
Ans"ers 49ueries may ;aJe more t;an one EorreEt ans=er5S
,. SELECT AntiGueO=ners.O=nerLastName, AntiGueO=ners.O=nerFirstName,
Orders."tem#esired
FROM AntiGueO=ners, Orders
%&ERE AntiGueO=ners.O=ner"# ( Orders.O=ner"#;
or
SELECT AntiGueO=ners.O=nerLastName, AntiGueO=ners.O=nerFirstName,
Orders."tem#esired
FROM AntiGueO=ners R"?&T DO"N Orders ON AntiGueO=ners.O=ner"# (
Orders.O=ner"#;
-. SELECT *
FROM EmployeeStatistiEsTable
OR#ER $! osition, Employee"#No;
6. SELECT Sum4$ene1its5
FROM EmployeeStatistiEsTable;
.. SELECT O=nerLastName, O=nerFirstName
FROM AntiGueO=ners, AntiGues
%&ERE "tem "n 4/C;air/5
AN# AntiGueO=ners.O=ner"# ( AntiGues.$uyer"#;
). SELECT O=nerLastName, O=nerFirstName
FROM AntiGueO=ners
%&ERE O=ner"# NOT "N
4SELECT O=ner"#
FROM Orders5;
3. SELECT #"ST"NCT O=nerLastName, O=nerFirstName
FROM Orders, AntiGueO=ners
%&ERE AntiGueO=ners.O=ner"# ( Orders.O=ner"#;
or to use DO"N notationS
SELECT #"ST"NCT AntiGueO=ners.O=nerLastName,
AntiGueO=ners.O=nerFirstName
FROM AntiGueO=ners R"?&T DO"N Orders ON AntiGueO=ners.O=ner"# (
Orders.O=ner"#;
X. #ELETE FROM OR#ERS
%&ERE O%NER"# ( *-;
@. "NSERT "NTO OR#ERS >AL:ES 4-,, /RoE<in0 C;air/5;
Y. CREATE TA$LE EMLO!EES
4Employee"#No "NTE?ER NOT N:LL,
FirstName C&AR4.*5 NOT N:LL,
LastName C&AR4.*5 NOT N:LL5;
,*. SELECT osition, Sum4Salary5
FROM EmployeeStatistiEsTable
?RO: $! osition;
,,. One+to+Many.
,-. An inte0er identi1iEation number; an auto+inErement "# is pre1erred.
,6. N>L.
,.. A Cursor.
,). T;ird Normal Form.
,3. CasEadin0 update.
,X. An "ndeI.
,@. ALTER TA$LE.
,Y. A Jalue is reGuired in t;is Eolumn 1or eJery ro= in t;e table.
-*. >ie=s.

S-ar putea să vă placă și