Sie sind auf Seite 1von 11

SQL Syntax Database Tables A database most often contains one or more tables.

Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data. Below is an e am!le of a table called ""ersons"# P_Id $ ) * LastName %ansen (&endson "ettersen FirstName Ola To&e +ari Address Timotei&n $' Borg&n )* (torgt )' City (andnes (andnes (ta&anger

The table abo&e contains three records (one for each !erson) and fi&e columns (",-d. /ast0ame. 1irst0ame. Address. and City). (2/ (tatements 3ost of the actions you need to !erform on a database are done with (2/ statements. The following (2/ statement will select all the records in the ""ersons" table# (E/ECT 4 15O3 "ersons -n this tutorial we will teach you all about the different (2/ statements. +ee! in 3ind That...

(2/ is not case sensiti&e

(emicolon after (2/ (tatements6 (ome database systems re7uire a semicolon at the end of each (2/ statement. (emicolon is the standard way to se!arate each (2/ statement in database systems that allow more than one (2/ statement to be e ecuted in the same call to the ser&er. 8e are using 3( Access and (2/ (er&er )''' and we do not ha&e to !ut a semicolon after each (2/ statement. but some database !rograms force you to use it. (2/ D3/ and DD/ (2/ can be di&ided into two !arts# The Data 3ani!ulation /anguage (D3/) and the Data Definition /anguage (DD/). The 7uery and u!date commands form the D3/ !art of (2/#

SELECT 9 e tracts data from a database UPDATE 9 u!dates data in a database DELETE 9 deletes data from a database INSERT INTO 9 inserts new data into a database

The DD/ !art of (2/ !ermits database tables to be created or deleted. -t also define inde es (:eys). s!ecify lin:s between tables. and im!ose constraints between tables. The most im!ortant DD/ statements in (2/ are#

CREATE DATABASE 9 creates a new database ALTER DATABASE 9 modifies a database CREATE TABLE 9 creates a new table

ALTER TABLE 9 modifies a table DROP TABLE 9 deletes a table CREATE INDEX 9 creates an inde (search :ey) DROP INDEX 9 deletes an inde

SQL SELECT Statement The (2/ (E/ECT (tatement The (E/ECT statement is used to select data from a database. The result is stored in a result table. called the result9set. SQL SELECT Syntax (E/ECT column,name(s) 15O3 table,name and (E/ECT 4 15O3 table,name Note (2/ is not case sensiti&e. (E/ECT is the same as select. An (2/ (E/ECT E am!le The ""ersons" table# P_Id $ ) * LastName %ansen (&endson "ettersen FirstName Ola To&e +ari Address Timotei&n $' Borg&n )* (torgt )' City (andnes (andnes (ta&anger

0ow we want to select the content of the columns named "/ast0ame" and "1irst0ame" from the table abo&e. 8e use the following (E/ECT statement# (E/ECT /ast0ame.1irst0ame 15O3 "ersons The result9set will loo: li:e this# LastName %ansen (&endson "ettersen (E/ECT 4 E am!le 0ow we want to select all the columns from the ""ersons" table. 8e use the following (E/ECT statement# (E/ECT 4 15O3 "ersons Ti! The asteris: (4) is a 7uic: way of selecting all columns; FirstName Ola To&e +ari

The result9set will loo: li:e this# P_Id $ ) * LastName %ansen (&endson "ettersen FirstName Ola To&e +ari Address Timotei&n $' Borg&n )* (torgt )' City (andnes (andnes (ta&anger

SQL SELECT DISTINCT Statement The (2/ (E/ECT D-(T-0CT (tatement -n a table. some of the columns may contain du!licate &alues. This is not a !roblem. howe&er. sometimes you will want to list only the different (distinct) &alues in a table. The D-(T-0CT :eyword can be used to return only distinct (different) &alues. SQL SELECT DISTINCT Syntax (E/ECT D-(T-0CT column,name(s) 15O3 table,name (E/ECT D-(T-0CT E am!le The ""ersons" table# P_Id $ ) * LastName %ansen (&endson "ettersen FirstName Ola To&e +ari Address Timotei&n $' Borg&n )* (torgt )' City (andnes (andnes (ta&anger

0ow we want to select only the distinct &alues from the column named "City" from the table abo&e. 8e use the following (E/ECT statement# (E/ECT D-(T-0CT City 15O3 "ersons The result9set will loo: li:e this# City (andnes (ta&anger SQL "#ERE C$a%se The 8%E5E clause is used to filter records. The 8%E5E Clause The 8%E5E clause is used to e tract only those records that fulfill a s!ecified criterion. SQL "#ERE Syntax (E/ECT column,name(s) 15O3 table,name

8%E5E column,name o!erator &alue 8%E5E Clause E am!le The ""ersons" table# P_Id $ ) * LastName %ansen (&endson "ettersen FirstName Ola To&e +ari Address Timotei&n $' Borg&n )* (torgt )' City (andnes (andnes (ta&anger

0ow we want to select only the !ersons li&ing in the city "(andnes" from the table abo&e. 8e use the following (E/ECT statement# (E/ECT 4 15O3 "ersons 8%E5E City<=(andnes= The result9set will loo: li:e this# P_Id $ ) LastName %ansen (&endson FirstName Ola To&e Address Timotei&n $' Borg&n )* City (andnes (andnes

2uotes Around Te t 1ields (2/ uses single 7uotes around te t &alues (most database systems will also acce!t double 7uotes). Although. numeric &alues should not be enclosed in 7uotes. 1or te t &alues# This is correct# (E/ECT 4 15O3 "ersons 8%E5E 1irst0ame<=To&e= This is wrong# (E/ECT 4 15O3 "ersons 8%E5E 1irst0ame<To&e 1or numeric &alues# This is correct# (E/ECT 4 15O3 "ersons 8%E5E >ear<$?@A This is wrong# (E/ECT 4 15O3 "ersons 8%E5E >ear<=$?@A=

O!erators Allowed in the 8%E5E Clause 8ith the 8%E5E clause. the following o!erators can be used#

O!erator < BC C B C< B< E7ual 0ot e7ual Dreater than /ess than Dreater than or e7ual /ess than or e7ual

Des&ri!tion

BET8EE0 Between an inclusi&e range /-+E -0 (earch for a !attern -f you :now the e act &alue you want to return for at least one of the columns

Note -n some &ersions of (2/ the BC o!erator may be written as ;< SQL AND ' OR O!erators The A0D E O5 o!erators are used to filter records based on more than one condition. The A0D E O5 O!erators The A0D o!erator dis!lays a record if both the first condition and the second condition is true. The O5 o!erator dis!lays a record if either the first condition or the second condition is true. A0D O!erator E am!le The ""ersons" table# P_Id $ ) * LastName %ansen (&endson "ettersen FirstName Ola To&e +ari Address Timotei&n $' Borg&n )* (torgt )' City (andnes (andnes (ta&anger

0ow we want to select only the !ersons with the first name e7ual to "To&e" A0D the last name e7ual to "(&endson"# 8e use the following (E/ECT statement# (E/ECT 4 15O3 "ersons 8%E5E 1irst0ame<=To&e= A0D /ast0ame<=(&endson= The result9set will loo: li:e this# P_Id ) LastName (&endson FirstName To&e Address Borg&n )* City (andnes

O5 O!erator E am!le

0ow we want to select only the !ersons with the first name e7ual to "To&e" O5 the first name e7ual to "Ola"# 8e use the following (E/ECT statement# (E/ECT 4 15O3 "ersons 8%E5E 1irst0ame<=To&e= O5 1irst0ame<=Ola= The result9set will loo: li:e this# P_Id $ ) LastName %ansen (&endson FirstName Ola To&e Address Timotei&n $' Borg&n )* City (andnes (andnes

Combining A0D E O5 >ou can also combine A0D and O5 (use !arenthesis to form com!le e !ressions). 0ow we want to select only the !ersons with the last name e7ual to "(&endson" A0D the first name e7ual to "To&e" O5 to "Ola"# 8e use the following (E/ECT statement# (E/ECT 4 15O3 "ersons 8%E5E /ast0ame<=(&endson= A0D (1irst0ame<=To&e= O5 1irst0ame<=Ola=) The result9set will loo: li:e this# P_Id ) LastName (&endson FirstName To&e Address Borg&n )* City (andnes

SQL ORDER B( )ey*ord The O5DE5 B> :eyword is used to sort the result9set. The O5DE5 B> +eyword The O5DE5 B> :eyword is used to sort the result9set by a s!ecified column. The O5DE5 B> :eyword sort the records in ascending order by default. -f you want to sort the records in a descending order. you can use the DE(C :eyword. SQL ORDER B( Syntax (E/ECT column,name(s) 15O3 table,name O5DE5 B> column,name(s) A(CFDE(C O5DE5 B> E am!le The ""ersons" table# P_Id $ LastName %ansen FirstName Ola Address Timotei&n $' City (andnes

) * G

(&endson "ettersen 0ilsen

To&e +ari Tom

Borg&n )* (torgt )' Hing&n )*

(andnes (ta&anger (ta&anger

0ow we want to select all the !ersons from the table abo&e. howe&er. we want to sort the !ersons by their last name. 8e use the following (E/ECT statement# (E/ECT 4 15O3 "ersons O5DE5 B> /ast0ame The result9set will loo: li:e this# P_Id $ G * ) LastName %ansen 0ilsen "ettersen (&endson FirstName Ola Tom +ari To&e Address Timotei&n $' Hing&n )* (torgt )' Borg&n )* City (andnes (ta&anger (ta&anger (andnes

O5DE5 B> DE(C E am!le 0ow we want to select all the !ersons from the table abo&e. howe&er. we want to sort the !ersons descending by their last name. 8e use the following (E/ECT statement# (E/ECT 4 15O3 "ersons O5DE5 B> /ast0ame DE(C The result9set will loo: li:e this# P_Id ) * G $ LastName (&endson "ettersen 0ilsen %ansen FirstName To&e +ari Tom Ola Address Borg&n )* (torgt )' Hing&n )* Timotei&n $' City (andnes (ta&anger (ta&anger (andnes

SQL INSERT INTO Statement The -0(E5T -0TO statement is used to insert new records in a table. The -0(E5T -0TO (tatement The -0(E5T -0TO statement is used to insert a new row in a table. SQL INSERT INTO Syntax -t is !ossible to write the -0(E5T -0TO statement in two forms. The first form doesn=t s!ecify the column names where the data will be inserted. only their &alues#

-0(E5T -0TO table,name HA/IE( (&alue$. &alue). &alue*....) The second form s!ecifies both the column names and the &alues to be inserted# -0(E5T -0TO table,name (column$. column). column*....) HA/IE( (&alue$. &alue). &alue*....) (2/ -0(E5T -0TO E am!le 8e ha&e the following ""ersons" table# P_Id $ ) * LastName %ansen (&endson "ettersen FirstName Ola To&e +ari Address Timotei&n $' Borg&n )* (torgt )' City (andnes (andnes (ta&anger

0ow we want to insert a new row in the ""ersons" table. 8e use the following (2/ statement# -0(E5T -0TO "ersons HA/IE( (G.=0ilsen=. =Johan=. =Ba::en )=. =(ta&anger=) The ""ersons" table will now loo: li:e this# P_Id $ ) * G LastName %ansen (&endson "ettersen 0ilsen FirstName Ola To&e +ari Johan Address Timotei&n $' Borg&n )* (torgt )' Ba::en ) City (andnes (andnes (ta&anger (ta&anger

-nsert Data Only in (!ecified Columns -t is also !ossible to only add data in s!ecific columns. The following (2/ statement will add a new row. but only add data in the "",-d". "/ast0ame" and the "1irst0ame" columns# -0(E5T -0TO "ersons (",-d. /ast0ame. 1irst0ame) HA/IE( (A. =TKessem=. =Ja:ob=) The ""ersons" table will now loo: li:e this# P_Id $ ) * LastName %ansen (&endson "ettersen FirstName Ola To&e +ari Address Timotei&n $' Borg&n )* (torgt )' City (andnes (andnes (ta&anger

G A

0ilsen TKessem

Johan Ja:ob

Ba::en )

(ta&anger

SQL UPDATE Statement The I"DATE statement is used to u!date records in a table. The I"DATE (tatement The I"DATE statement is used to u!date e isting records in a table. SQL UPDATE Syntax I"DATE table,name (ET column$<&alue. column)<&alue).... 8%E5E some,column<some,&alue Note 0otice the 8%E5E clause in the I"DATE synta . The 8%E5E clause s!ecifies which record or records that should be u!dated. -f you omit the 8%E5E clause. all records will be u!dated; (2/ I"DATE E am!le The ""ersons" table# P_Id $ ) * G A LastName %ansen (&endson "ettersen 0ilsen TKessem FirstName Ola To&e +ari Johan Ja:ob Address Timotei&n $' Borg&n )* (torgt )' Ba::en ) City (andnes (andnes (ta&anger (ta&anger

0ow we want to u!date the !erson "TKessem. Ja:ob" in the ""ersons" table. 8e use the following (2/ statement# I"DATE "ersons (ET Address<=0issestien @L=. City<=(andnes= 8%E5E /ast0ame<=TKessem= A0D 1irst0ame<=Ja:ob= The ""ersons" table will now loo: li:e this# P_Id $ ) * G A LastName %ansen (&endson "ettersen 0ilsen TKessem FirstName Ola To&e +ari Johan Ja:ob Address Timotei&n $' Borg&n )* (torgt )' Ba::en ) 0issestien @L City (andnes (andnes (ta&anger (ta&anger (andnes

(2/ I"DATE 8arning

Be careful when u!dating records. -f we had omitted the 8%E5E clause in the e am!le abo&e. li:e this# I"DATE "ersons (ET Address<=0issestien @L=. City<=(andnes= The ""ersons" table would ha&e loo:ed li:e this# P_Id $ ) * G A LastName %ansen (&endson "ettersen 0ilsen TKessem FirstName Ola To&e +ari Johan Ja:ob Address 0issestien @L 0issestien @L 0issestien @L 0issestien @L 0issestien @L City (andnes (andnes (andnes (andnes (andnes

SQL DELETE Statement The DE/ETE statement is used to delete records in a table. The DE/ETE (tatement The DE/ETE statement is used to delete rows in a table. SQL DELETE Syntax DE/ETE 15O3 table,name 8%E5E some,column<some,&alue Note 0otice the 8%E5E clause in the DE/ETE synta . The 8%E5E clause s!ecifies which record or records that should be deleted. -f you omit the 8%E5E clause. all records will be deleted; (2/ DE/ETE E am!le The ""ersons" table# P_Id $ ) * G A LastName %ansen (&endson "ettersen 0ilsen TKessem FirstName Ola To&e +ari Johan Ja:ob Address Timotei&n $' Borg&n )* (torgt )' Ba::en ) 0issestien @L City (andnes (andnes (ta&anger (ta&anger (andnes

0ow we want to delete the !erson "TKessem. Ja:ob" in the ""ersons" table. 8e use the following (2/ statement# DE/ETE 15O3 "ersons 8%E5E /ast0ame<=TKessem= A0D 1irst0ame<=Ja:ob= The ""ersons" table will now loo: li:e this#

P_Id $ ) * G

LastName %ansen (&endson "ettersen 0ilsen

FirstName Ola To&e +ari Johan

Address Timotei&n $' Borg&n )* (torgt )' Ba::en )

City (andnes (andnes (ta&anger (ta&anger

Delete All 5ows -t is !ossible to delete all rows in a table without deleting the table. This means that the table structure. attributes. and inde es will be intact# DE/ETE 15O3 table,name or DE/ETE 4 15O3 table,name Note Be &ery careful when deleting records. >ou cannot undo this statement;