Sie sind auf Seite 1von 17

KVSeContents.

in
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards
Institute) standard
What Can SQL do?
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL is a Standard
Although SQL is an ANSI (American National Standards
Institute) standard, there are many different versions of
the SQL language
Semicolon after SQL Statements?
Semicolon is the standard way to separate each SQL
statement in database systems that allow more than one
SQL statement to be e!ecuted in the same call to the
server
SQL DML and DDL
SQL can be divided into two parts" #he $ata %anipulation
Language ($%L) and the $ata $efinition Language ($$L)
#he &uery and update commands form the $%L part of SQL"
SELECT ' e!tracts data from a database
UPDATE ' updates data in a database
DELETE ' deletes data from a database
!SE"T !T# ' inserts new data into a database
#he $$L part of SQL permits database tables to be created
or deleted It also defines inde!es ((eys), specifies
lin(s between tables, and imposes constraints between
tables #he most important $$L statements in SQL are"
C"EATE DATA$ASE ' creates a new database
ALTE" DATA$ASE ' modifies a database
C"EATE TA$LE ' creates a new table
ALTE" TA$LE ' modifies a table
D"#P TA$LE ' deletes a table
KVSeContents.in
C"EATE !DE% ' creates an inde! (search (ey)
D"#P !DE% ' deletes an inde!
Data T&'es
Datat&'e Descri'tion
Ma( Si)e*
PL+SQL
)A*+,A*-(size)
)ariable length
character string having
ma!imum length size
bytes
.ou must specify si/e
0-121 bytes
minimum is 3
)A*+,A* Now deprecated
+,A*(size)
4i!ed length character
data of length si/e
bytes
0-121
N5%67*(p,s)
Number having precision
p and scale s
%agnitude
37'308 3873-9
L:N;
+haracter data of
variable length
$A#7 )alid date range
from <anuary 3,
=13- 6+ to
$ecember 03, ,,,,
A$
The C"EATE TA$LE Statement
#he +*7A#7 #A6L7 statement is used to create a table in a
database
SQL C"EATE TA$LE S&nta(
+*7A#7 #A6L7 table>name
(
column>name3 data>type,
column>name- data>type,

)
#he data type specifies what type of data the column can
hold
KVSeContents.in
C"EATE TA$LE E(am'le
Now we want to create a table called ?@ersons? that
contains five columns" @>Id, LastName, 4irstName,
Address, and +ity
Ae use the following +*7A#7 #A6L7 statement"
+*7A#7 #A6L7 @ersons
(
@>Id int,
LastName varchar(-99),
4irstName varchar(-99),
Address varchar(-99),
+ity varchar(-99)
)
#he @>Id column is of type int and will hold a number
#he LastName, 4irstName, Address, and +ity columns are of
type varchar with a ma!imum length of -99 characters
#he empty ?@ersons? table will now loo( li(e this"
P-dLast!ame.irst!ameAddressCit&
The D"#P TA$LE Statement
#he $*:@ #A6L7 statement is used to delete a table
$*:@ #A6L7 table>name
The ALTE" TA$LE Statement
#he AL#7* #A6L7 statement is used to add, delete, or
modify columns in an e!isting table
SQL ALTE" TA$LE S&nta(
#o add a column in a table, use the following synta!"
AL#7* #A6L7 table>name A$$ column>name datatype
#o delete a column in a table, use the following synta!
AL#7* #A6L7 table>name $*:@ +:L5%N column>name
#o change the data type of a column in a table, use the
following synta!"
AL#7* #A6L7 table>name %:$I4. column>name datatype
KVSeContents.in
SQL ALTE" TA$LE E(am'le
Loo( at the ?@ersons? table"
P-dLast!ame.irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
Now we want to add a column named ?$ate:f6irth? in the
?@ersons? table
Ae use the following SQL statement"
AL#7* #A6L7 @ersons A$$ $ate:f6irth date
#he ?@ersons? table will now li(e this"
P-dLast!ame.irst!ameAddress Cit& Date#f$irth
3 ,ansen :la #imoteivn 38Sandnes
Chan/e Data T&'e E(am'le
Now we want to change the data type of the column named
?$ate:f6irth? in the ?@ersons? table
Ae use the following SQL statement"
AL#7* #A6L7 @ersons AL#7* +:L5%N $ate:f6irth year
Notice that the ?$ate:f6irth? column is now of type year
and is going to hold a year in a two'digit or four'digit
format
D"#P C#LUM! E(am'le
Ne!t, we want to delete the column named ?$ate:f6irth? in
the ?@ersons? table
Ae use the following SQL statement"
AL#7* #A6L7 @ersons $*:@ +:L5%N $ate:f6irth
#he ?@ersons? table will now li(e this"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
The UPDATE Statement
KVSeContents.in
#he 5@$A#7 statement is used to update e!isting records
in a table
SQL UPDATE S&nta(
5@$A#7 table>name S7# column3Cvalue, column-Cvalue-,
A,7*7 some>columnCsome>value
!ote* If you omit the A,7*7 clause, all records will be
updatedD
SQL UPDATE E(am'le
#he ?@ersons? table"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
= Nilsen <ohan 6a((en - Stavanger
9 #Eessem <a(ob
Now we want to update the person ?#Eessem, <a(ob? in the
?@ersons? table
Ae use the following SQL statement"
5@$A#7 @ersons S7# AddressCFNissestien 21F,
+ityCFSandnesF A,7*7 LastNameCF#EessemF AN$
4irstNameCF<a(obF
#he ?@ersons? table will now loo( li(e this"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38 Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
= Nilsen <ohan 6a((en - Stavanger
9 #Eessem <a(ob Nissestien 21Sandnes
The !SE"T !T# Statement
#he INS7*# IN#: statement is used to insert a new row in
a table
SQL !SE"T !T# S&nta(
It is possible to write the INS7*# IN#: statement in two
forms
KVSeContents.in
#he first form doesnFt specify the column names where the
data will be inserted, only their values"
INS7*# IN#: table>name )AL57S (value3, value-,
value0,)
#he second form specifies both the column names and the
values to be inserted"
INS7*# IN#: table>name (column3, column-, column0,)
)AL57S (value3, value-, value0,)
SQL !SE"T !T# E(am'le
Ae have the following ?@ersons? table"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
Now we want to insert a new row in the ?@ersons? table
Ae use the following SQL statement"
INS7*# IN#: @ersons )AL57S (=,FNilsenF, F<ohanF, F6a((en
-F, FStavangerF)
#he ?@ersons? table will now loo( li(e this"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
= Nilsen <ohan 6a((en - Stavanger
nsert Data #nl& in S'ecified Col0mns
It is also possible to only add data in specific columns
#he following SQL statement will add a new row, but only
add data in the ?@>Id?, ?LastName? and the ?4irstName?
columns"
INS7*# IN#: @ersons (@>Id, LastName, 4irstName)
)AL57S (9, F#EessemF, F<a(obF)
#he ?@ersons? table will now loo( li(e this"
KVSeContents.in
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
= Nilsen <ohan 6a((en - Stavanger
9 #Eessem <a(ob
The DELETE Statement
#he $7L7#7 statement is used to delete rows in a table
SQL DELETE S&nta(
$7L7#7 4*:% table>name A,7*7 some>columnCsome>value
!ote* Notice the A,7*7 clause in the $7L7#7 synta! #he
A,7*7 clause specifies which record or records that
should be deleted If you omit the A,7*7 clause, all
records will be deletedD
!ote* 6e very careful when deleting records .ou cannot
undo this statementD
The SQL SELECT Statement
#he S7L7+# statement is used to select data from a
database
#he result is stored in a result table, called the
result'set
SQL SELECT S&nta(
S7L7+# column>name(s) 4*:% table>name
and
S7L7+# G 4*:% table>name
!ote* SQL is not case sensitive S7L7+# is the same as
select
An SQL SELECT E(am'le
#he ?@ersons? table"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
KVSeContents.in
0 @ettersenBari Storgt -8 Stavanger
Now we want to select the content of the columns named
?LastName? and ?4irstName? from the table above
Ae use the following S7L7+# statement"
S7L7+# LastName,4irstName 4*:% @ersons
#he result'set will loo( li(e this"
Last!ame .irst!ame
,ansen :la
Svendson #ove
@ettersenBari
SELECT 1 E(am'le
Now we want to select all the columns from the ?@ersons?
table
S7L7+# G 4*:% @ersons
Ti'* #he asteris( (G) is a &uic( way of selecting all
columnsD
#he result'set will loo( li(e this"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
The SQL SELECT DST!CT Statement
In a table, some of the columns may contain duplicate
values #his is not a problem, however, sometimes you
will want to list only the different (distinct) values in
a table
#he $IS#IN+# (eyword can be used to return only distinct
(different) values
SQL SELECT DST!CT S&nta(
S7L7+# $IS#IN+# column>name(s)4*:% table>name
#he ?@ersons? table"
KVSeContents.in
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
Now we want to select only the distinct values from the
column named ?+ity? from the table above
Ae use the following S7L7+# statement"
S7L7+# $IS#IN+# +ity 4*:% @ersons
#he result'set will loo( li(e this"
#he A,7*7 clause is used to filter records
The W2E"E Cla0se
#he A,7*7 clause is used to e!tract only those records
that fulfill a specified criterion
SQL W2E"E S&nta(
S7L7+# column>name(s)4*:% table>name A,7*7 column>name
operator value
W2E"E Cla0se E(am'le
#he ?@ersons? table"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
Now we want to select only the persons living in the city
?Sandnes? from the table above
Ae use the following S7L7+# statement"
S7L7+# G 4*:% @ersons A,7*7 +ityCFSandnesF
P-dLast!ame.irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson#ove 6orgvn -0 Sandnes
Q0otes Aro0nd Te(t .ields
KVSeContents.in
SQL uses single &uotes around te!t values (most database
systems will also accept double &uotes)
,owever, numeric values should not be enclosed in &uotes
4or te!t values"
#his is correct"
S7L7+# G 4*:% @ersons A,7*7 4irstNameCF#oveF
#his is wrong"
S7L7+# G 4*:% @ersons A,7*7 4irstNameC#ove
4or numeric values"
#his is correct"
S7L7+# G 4*:% @ersons A,7*7 .earC3H29
#his is wrong"
S7L7+# G 4*:% @ersons A,7*7 .earCF3H29F
#'erators Allo3ed in the W2E"E Cla0se
Aith the A,7*7 clause, the following operators can be
used"
#'erator Descri'tion
C 7&ual
IJ Not e&ual
J ;reater than
I Less than
JC ;reater than or e&ual
IC Less than or e&ual
67#A77N 6etween an inclusive range
LIB7 Search for a pattern
IN #o specify multiple possible values for a column
The A!D 4 #" #'erators
#he AN$ operator displays a record if both the first
condition and the second condition is true
KVSeContents.in
#he :* operator displays a record if either the first
condition or the second condition is true
A!D #'erator E(am'le
#he ?@ersons? table"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
Now we want to select only the persons with the first
name e&ual to ?#ove? AN$ the last name e&ual to
?Svendson?"
Ae use the following S7L7+# statement"
S7L7+# G 4*:% @ersons A,7*7 4irstNameCF#oveF AN$
LastNameCFSvendsonF
#he result'set will loo( li(e this"
P-dLast!ame.irst!ameAddress Cit&
- Svendson#ove 6orgvn -0Sandnes
#" #'erator E(am'le
Now we want to select only the persons with the first
name e&ual to ?#ove? :* the first name e&ual to ?:la?"
Ae use the following S7L7+# statement"
S7L7+# G 4*:% @ersons A,7*7 4irstNameCF#oveF :*
4irstNameCF:laF
#he result'set will loo( li(e this"
P-dLast!ame.irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson#ove 6orgvn -0 Sandnes
The #"DE" $5 Ke&3ord
#he :*$7* 6. (eyword is used to sort the result'set by a
specified column
#he :*$7* 6. (eyword sort the records in ascending order
by default
KVSeContents.in
If you want to sort the records in a descending order,
you can use the $7S+ (eyword
SQL #"DE" $5 S&nta(
S7L7+# column>name(s) 4*:% table>name :*$7* 6.
column>name(s) AS+K$7S+
#"DE" $5 E(am'le
#he ?@ersons? table"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
= Nilsen #om )ingvn -0 Stavanger
Now we want to select all the persons from the table
above, however, we want to sort the persons by their last
name
Ae use the following S7L7+# statement"
S7L7+# G 4*:% @ersons :*$7* 6. LastName
#he result'set will loo( li(e this"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
= Nilsen #om )ingvn -0 Stavanger
0 @ettersenBari Storgt -8 Stavanger
- Svendson #ove 6orgvn -0 Sandnes
The 6"#UP $5 Statement
#he ;*:5@ 6. statement is used in conEunction with the
aggregate functions to group the result'set by one or
more columns
SQL 6"#UP $5 S&nta(
S7L7+# column>name, aggregate>function(column>name) 4*:%
table>name A,7*7 column>name operator value ;*:5@ 6.
column>name
SQL 6"#UP $5 E(am'le
Ae have the following ?:rders? table"
KVSeContents.in
#-d#rderDate #rderPriceC0stomer
3 -88LM33M3-3888 ,ansen
- -88LM38M-03288 Nilsen
0 -88LM8HM8-188 ,ansen
= -88LM8HM80088 ,ansen
9 -88LM8LM08-888 <ensen
2 -88LM38M8=388 Nilsen
Now we want to find the total sum (total order) of each
customerAe will have to use the ;*:5@ 6. statement to
group the customersAe use the following SQL statement"
S7L7+# +ustomer,S5%(:rder@rice) 4*:% :rders ;*:5@ 6.
+ustomer
#he result'set will loo( li(e this"
C0stomer SUM7#rderPrice8
,ansen -888
Nilsen 3188
<ensen -888
The 2AV!6 Cla0se
#he ,A)IN; clause was added to SQL because the A,7*7
(eyword could not be used with aggregate functions
SQL 2AV!6 S&nta(
S7L7+# column>name, aggregate>function(column>name) 4*:%
table>name A,7*7 column>name operator value ;*:5@ 6.
column>name ,A)IN; aggregate>function(column>name)
operator value
SQL 2AV!6 E(am'le
Ae have the following ?:rders? table"
#-d#rderDate #rderPriceC0stomer
3 -88LM33M3-3888 ,ansen
- -88LM38M-03288 Nilsen
0 -88LM8HM8-188 ,ansen
= -88LM8HM80088 ,ansen
9 -88LM8LM08-888 <ensen
2 -88LM38M8=388 Nilsen
Now we want to find if any of the customers have a total
order of less than -888
KVSeContents.in
Ae use the following SQL statement"
S7L7+# +ustomer,S5%(:rder@rice) 4*:% :rders ;*:5@ 6.
+ustomer ,A)IN; S5%(:rder@rice)I-888
#he result'set will loo( li(e this"
C0stomer SUM7#rderPrice8
Nilsen 3188
Now we want to find if the customers ?,ansen? or ?<ensen?
have a total order of more than 3988
Ae add an ordinary A,7*7 clause to the SQL statement"
S7L7+# +ustomer,S5%(:rder@rice) 4*:% :rders A,7*7
+ustomerCF,ansenF :* +ustomerCF<ensenF ;*:5@ 6. +ustomer
,A)IN; S5%(:rder@rice)J3988
#he result'set will loo( li(e this"
C0stomer SUM7#rderPrice8
,ansen -888
<ensen -888
The ! #'erator
#he IN operator allows you to specify multiple values in
a A,7*7 clause
SQL ! S&nta(
S7L7+# column>name(s)4*:% table>name A,7*7 column>name IN
(value3,value-,)
! #'erator E(am'le
#he ?@ersons? table"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
Now we want to select the persons with a last name e&ual
to ?,ansen? or ?@ettersen? from the table above
Ae use the following S7L7+# statement"
KVSeContents.in
S7L7+# G 4*:% @ersons A,7*7 LastName IN
(F,ansenF,F@ettersenF)
#he result'set will loo( li(e this"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
0 @ettersenBari Storgt -8 Stavanger
#he 67#A77N operator is used in a A,7*7 clause to select
a range of data between two values
The $ETWEE! #'erator
#he 67#A77N operator selects a range of data between two
values #he values can be numbers, te!t, or dates
SQL $ETWEE! S&nta(
S7L7+# column>name(s) 4*:% table>name A,7*7 column>name
67#A77N value3 AN$ value-
$ETWEE! #'erator E(am'le
#he ?@ersons? table"
P-dLast!ame .irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
- Svendson #ove 6orgvn -0 Sandnes
0 @ettersenBari Storgt -8 Stavanger
Now we want to select the persons with a last name
alphabetically between ?,ansen? and ?@ettersen? from the
table above
Ae use the following S7L7+# statement"
S7L7+# G 4*:% @ersons A,7*7 LastName 67#A77N F,ansenF AN$
F@ettersenF
#he result'set will loo( li(e this"
P-dLast!ame.irst!ameAddress Cit&
3 ,ansen :la #imoteivn 38Sandnes
E(am'le 9
#o display the persons outside the range in the previous
e!ample, use N:# 67#A77N"
KVSeContents.in
S7L7+# G 4*:% @ersons A,7*7 LastNameN:# 67#A77N F,ansenF
AN$ F@ettersenF
#he result'set will loo( li(e this"
P-dLast!ame .irst!ameAddress Cit&
- Svendson #ove 6orgvn -0Sandnes
0 @ettersenBari Storgt -8Stavanger
SQL Dates
#he most difficult part when wor(ing with dates is to be
sure that the format of the date you are trying to
insert, matches the format of the date column in the
database
SQL Date Data T&'es
M&SQL comes with the following data types for storing a
date or a dateMtime value in the database"
$A#7 ' format ....'%%'$$
$A#7#I%7 ' format" ....'%%'$$ ,,"%%"SS
#I%7S#A%@ ' format" ....'%%'$$ ,,"%%"SS
.7A* ' format .... or ..
!ote* #he date types are chosen for a column when you
create a new table in your databaseD
SQL Wor:in/ 3ith Dates
.ou can compare two dates easily if there is no time
component involvedD
Assume we have the following ?:rders? table"
#rderdProd0ct!ame #rderDate
3 ;eitost -88L'33'33
- +amembert @ierrot -88L'33'8H
0 %o//arella di ;iovanni-88L'33'33
= %ascarpone 4abioli -88L'38'-H
Now we want to select the records with an :rder$ate of
?-88L'33'33? from the table above
Ae use the following S7L7+# statement"
S7L7+# G 4*:% :rders A,7*7 :rder$ateCF-88L'33'33F
#he result'set will loo( li(e this"
KVSeContents.in
#rderdProd0ct!ame #rderDate
3 ;eitost -88L'33'33
0 %o//arella di ;iovanni-88L'33'33

Das könnte Ihnen auch gefallen