You are on page 1of 13

www.jntuworld.

com

www.jwjobs.net

Introduction to SQL SQL is a standard language for accessing and manipulating databases. 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 execute queries against a database 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 can create stored procedures in a database SQL can create views in a database SQL can set permissions on tables procedures and views SQL is a Standard - BUT.... Alt!oug! SQL is an ANSI (American National Standards Institute) standard t!ere are many different versions of t!e SQL language" #owever to be compliant wit! t!e ANSI standard t!ey all support at least t!e ma$or commands (suc! as S%L%&' ()*A'% *%L%'% INS%+' ,#%+%) in a similar manner" Using SQL in Your Web Site 'o build a web site t!at s!ows some data from a database you will need t!e followingAn +*./S database program (i"e" /S Access SQL Server /ySQL) A server0side scripting language li1e )#) or AS) SQL #'/L 2 &SS RDBMS +*./S stands for +elational *atabase /anagement System" +*./S is t!e basis for SQL and for all modern database systems li1e /S SQL Server I./ *.3 4racle /ySQL and /icrosoft Access" '!e data in +*./S is stored in database ob$ects called tables" A table is a collections of related data entries and it consists of columns and rows"

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

Database Tab es A database most often contains one or more tables" %ac! table is identified by a name (e"g" 5&ustomers5 or 54rders5)" 'ables contain records (rows) wit! data" .elow is an example of a table called 5)ersons5)6Id LastName 7irstName Address &ity 8 #ansen 4la 'imoteivn 89 Sandnes 3 Svendson 'ove.orgvn 3: Sandnes : )ettersen ;ariStorgt 39 Stavanger '!e table above contains t!ree records (one for eac! person) and five columns ()6Id LastName 7irstName Address and &ity)" SQL State!ents /ost of t!e actions you need to perform on a database are done wit! SQL statements" '!e following SQL statement will select all t!e records in t!e 5)ersons5 tableSELECT * FROM Persons "ee# in Mind That... SQL is not case sensitive Se!ico on a$ter SQL State!ents? Some database systems require a semicolon at t!e end of eac! SQL statement" Semicolon is t!e standard way to separate eac! SQL statement in database systems t!at allow more t!an one SQL statement to be executed in t!e same call to t!e server" ,e are using /S Access and SQL Server 3999 and we do not !ave to put a semicolon after eac! SQL statement but some database programs force you to use it" SQL DML and DDL SQL can be divided into two parts- '!e *ata /anipulation Language (*/L) and t!e *ata *efinition Language (**L)" '!e query and update commands form t!e */L part of SQLSELECT 0 extracts data from a database P!"TE 0 updates data in a database !ELETE 0 deletes data from a database #$SERT #$TO 0 inserts new data into a database '!e **L part of SQL permits database tables to be created or deleted" It also define indexes (1eys) specify lin1s between tables and impose constraints between tables" '!e most important **L statements in SQL are-

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

CRE"TE !"T"%"SE 0 creates a new database "LTER !"T"%"SE 0 modifies a database CRE"TE T"%LE 0 creates a new table "LTER T"%LE 0 modifies a table !ROP T"%LE 0 deletes a table CRE"TE #$!E& 0 creates an index (searc! 1ey) !ROP #$!E& 0 deletes an index B%SICS &. The SQL S'L'CT State!ent '!e SELECT statement is used to select data from a database" '!e result is stored in a result table called t!e result0set" SQL S'L'CT S(nta) (Select from) S'L'CT co u!n*na!e+s, -R.M tab e*na!e %nd S'L'CT / -R.M tab e*na!e $ote- SQL is not case sensitive" S%L%&' is t!e same as select" %n SQL S'L'CT ')a!# e '!e 5)ersons5 table)6Id LastName 7irstName Address &ity 8 #ansen 4la 'imoteivn 89 Sandnes 3 Svendson 'ove.orgvn 3: Sandnes : )ettersen ;ariStorgt 39 Stavanger Now we want to select t!e content of t!e columns named 5LastName5 and 57irstName5 from t!e table above" ,e use t!e following S%L%&' statementS'L'CT Last0a!e1-irst0a!e -R.M 2ersons '!e result0set will loo1 li1e t!isLastName 7irstName #ansen 4la Svendson 'ove )ettersen ;ari S'L'CT / ')a!# e Now we want to select all t!e columns from t!e 5)ersons5 table"

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

,e use t!e following S%L%&' statementS'L'CT / -R.M 2ersons Tip' '!e asteris1 (<) is a quic1 way of selecting all columns( '!e result0set will loo1 li1e t!is)6Id 8 3 : LastName #ansen Svendson )ettersen 7irstName Address &ity 4la 'imoteivn 89 Sandnes 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger

3.The SQL S'L'CT DISTI0CT State!ent (Select from) In a table some of t!e columns may contain duplicate values" '!is is not a problem !owever sometimes you will want to list only t!e different (distinct) values in a table" '!e *IS'IN&' 1eyword can be used to return only distinct (different) values" SQL S'L'CT DISTI0CT S(nta)4 S%L%&' *IS'IN&' column6name(s) 7+4/ table6name SELECT !#ST#$CT E)ample '!e 5)ersons5 table)6Id LastName 8 #ansen 3 Svendson : )ettersen 7irstName 4la'imoteivn 'ove.orgvn ;ariStorgt Address 89 3: 39 &ity Sandnes Sandnes Stavanger

Now we want to select only t!e distinct values from t!e column named 5&ity5 from t!e table above" ,e use t!e following S%L%&' statementSELECT !#ST#$CT Cit* FROM Persons T+e result,set -ill loo. li.e t+is &ity Sandnes Stavanger

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

/" The W5'R' C ause The W5'R' c ause is used to $i ter records. '!e ,#%+% clause is used to extract only t!ose records t!at fulfill a specified criterion" SQL 01ERE S*nta)' S'L'CT co u!n*na!e+s, -R.M tab e*na!e W5'R' co u!n*na!e o#erator 6a ue W5'R' C ause ')a!# e '!e 5)ersons5 table)6Id LastName 7irstName Address &ity 8 #ansen 4la 'imoteivn 89 Sandnes 3 Svendson 'ove.orgvn 3: Sandnes : )ettersen ;ariStorgt 39 Stavanger Now we want to select only t!e persons living in t!e city 5Sandnes5 from t!e table above" ,e use t!e following S%L%&' statementS'L'CT / -R.M 2ersons W5'R' Cit(78Sandnes8 '!e result0set will loo1 li1e t!is)6Id LastName 7irstName Address &ity 8 #ansen 4la 'imoteivn 89 Sandnes 3 Svendson 'ove.orgvn 3: Sandnes Quotes %round Te)t -ie ds4 SQL uses single quotes around text values (most database systems will also accept double quotes)" Alt!oug! numeric values s!ould not be enclosed in quotes"

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

For te)t 2alues' T+is is correct' S'L'CT / -R.M 2ersons W5'R' -irst0a!e78To6e8 T+is is -rong' S'L'CT / -R.M 2ersons W5'R' -irst0a!e7To6e For numeric 2alues' T+is is correct' S'L'CT / -R.M 2ersons W5'R' Year7&9:; T+is is -rong' S'L'CT / -R.M 2ersons W5'R' Year78&9:;8 Operators "llo-ed in t+e 01ERE Clause 0it+ t+e 01ERE clause3 t+e follo-ing operators can be used' Operator 4 67 7 6 74 64 %ET0EE$ L#9E #$ columns !escription E5ual $ot e5ual 8reater t+an Less t+an 8reater t+an or e5ual Less t+an or e5ual %et-een an inclusi2e range Searc+ for a pattern #f *ou .no- t+e e)act 2alue *ou -ant to return for at least one of t+e

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

:. The %0D < .R .#erators T+e "$! ; OR operators are used to filter records based on more t+an one condition. '!e AN* operator displays a record if bot! t!e first condition and t!e second condition is true" '!e 4+ operator displays a record if eit!er t!e first condition or t!e second condition is true" "$! Operator E)ample '!e 5)ersons5 table)6Id LastName 8 #ansen 3 Svendson : )ettersen 7irstName Address &ity 4la 'imoteivn 89 Sandnes 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger

Now we want to select only t!e persons wit! t!e first name equal to 5'ove5 AN* t!e last name equal to 5Svendson50e use t+e follo-ing SELECT statement' S'L'CT / -R.M 2ersons W5'R' -irst0a!e78To6e8 %0D Last0a!e78S6endson8 '!e result0set will loo1 li1e t!is)6Id LastName 7irstName 3 Svendson 'ov.orgvn OR Operator E)ample Now we want to select only t!e persons wit! t!e first name equal to 5'ove5 4+ t!e first name equal to 54la5,e use t!e following S%L%&' statementS'L'CT / -R.M 2ersons W5'R' -irst0a!e78To6e8 .R -irst0a!e78. a8 '!e result0set will loo1 li1e t!is)6Id LastName 7irstName Address &ity 8 #ansen 4la 'imoteivn 89 Sandnes 3 Svendson 'ove.orgvn 3: Sandnes Address 3: &ity Sandnes

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

Combining "$! ; OR =ou can also combine AN* and 4+ (use parent!esis to form complex expressions)" Now we want to select only t!e persons wit! t!e last name equal to 5Svendson5 AN* t!e first name equal to 5'ove5 4+ to 54la50e use t+e follo-ing SELECT statement' S'L'CT / -R.M 2ersons W5'R' Last0a!e78S6endson8 %0D +-irst0a!e78To6e8 .R -irst0a!e78. a8, T+e result,set -ill loo. li.e t+is' )6Id LastName 7irstName 3 Svendson 'ove.orgvn <. T+e OR!ER %= 9e*-ord T+e OR!ER %= .e*-ord is used to sort t+e result,set. '!e 4+*%+ .= 1eyword is used to sort t!e result0set by a specified column" '!e 4+*%+ .= 1eyword sort t!e records in ascending order by default" If you want to sort t!e records in a descending order you can use t!e *%S& 1eyword" SQL OR!ER %= S*nta) S'L'CT co u!n*na!e+s, -R.M tab e*na!e .RD'R BY co u!n*na!e+s, %SC=D'SC .RD'R BY ')a!# e '!e 5)ersons5 table)6Id LastName 8 #ansen 3 Svendson : )ettersen > Nilsen'om 7irstName Address &ity 4la 'imoteivn 89 Sandnes 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger ?ingvn 3: Stavanger

Address &ity 3: Sandnes

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

Now we want to select all t!e persons from t!e table above !owever we want to sort t!e persons by t!eir last name" 0e use t+e follo-ing SELECT statement' S'L'CT / -R.M 2ersons .RD'R BY Last0a!e '!e result0set will loo1 li1e t!is)6Id LastName 7irstName Address &ity 8 #ansen 4la 'imoteivn 89 Sandnes > Nilsen'om ?ingvn 3: Stavanger : )ettersen ;ariStorgt 39 Stavanger 3 Svendson 'ove.orgvn 3: Sandnes OR!ER %= !ESC E)ample Now we want to select all t!e persons from t!e table above !owever we want to sort t!e persons descending by t!eir last name" 0e use t+e follo-ing SELECT statement' S'L'CT / -R.M 2ersons .RD'R BY Last0a!e D'SC T+e result,set -ill loo. li.e t+is' )6Id 3 : > 8 LastName Svendson )ettersen Nilsen 'om #ansen 7irstName Address &ity 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger ?ingvn 3: Stavanger 4la 'imoteivn 89 Sandnes

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

>. T+e #$SERT #$TO Statement The I0S'RT I0T. state!ent is used to insert ne> records in a tab e. '!e INS%+' IN'4 statement is used to insert a new row in a table" SQL #$SERT #$TO S*nta) It is possible to write t!e INS%+' IN'4 statement in two forms" T+e first form doesn?t specif* t+e column names -+ere t+e data -ill be inserted3 onl* t+eir 2alues' I0S'RT I0T. tab e*na!e ?%LU'S +6a ue&1 6a ue31 6a ue@1..., T+e second form specifies bot+ t+e column names and t+e 2alues to be inserted' I0S'RT I0T. tab e*na!e +co u!n&1 co u!n31 co u!n@1..., ?%LU'S +6a ue&1 6a ue31 6a ue@1..., SQL #$SERT #$TO E)ample ,e !ave t!e following 5)ersons5 table)6Id 8 3 : LastName #ansen Svendson )ettersen 7irstName Address &ity 4l 'imoteivn 89 Sandnes 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger

Now we want to insert a new row in t!e 5)ersons5 table" 0e use t+e follo-ing SQL statement' I0S'RT I0T. 2ersons ?%LU'S +A180i sen81 8Bohan81 8BaCCen 381 8Sta6anger8, '!e 5)ersons5 table will now loo1 li1e t!is)6Id 8 3 : > LastName #ansen Svendson )ettersen Nilsen@o!an 7irstName Address &ity 4la 'imoteivn 89 Sandnes 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger .a11en 3 Stavanger

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

#nsert !ata Onl* in Specified Columns It is also possible to only add data in specific columns" T+e follo-ing SQL statement -ill add a ne- ro-3 but onl* add data in t+e @PA#d@3 @Last$ame@ and t+e @First$ame@ columns' I0S'RT I0T. 2ersons +2*Id1 Last0a!e1 -irst0a!e, ?%LU'S +;1 8TDesse!81 8BaCob8, '!e 5)ersons5 table will now loo1 li1e t!is)6Id 8 3 : > A LastName #ansen Svendson )ettersen Nilsen @o!an '$essem 7irstName Address &ity 4la 'imoteivn 89 Sandnes 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger .a11en 3 Stavanger @a1ob

B. T+e P!"TE Statement The U2D%T' state!ent is used to u#date records in a tab e. '!e ()*A'% statement is used to update existing records in a table" SQL P!"TE S*nta) U2D%T' tab e*na!e S'T co u!n&76a ue1 co u!n376a ue31... W5'R' so!e*co u!n7so!e*6a ue 0ote4 Notice t!e ,#%+% clause in t!e ()*A'% syntax" '!e ,#%+% clause specifies w!ic! record or records t!at s!ould be updated" If you omit t!e ,#%+% clause all records will be updatedB SQL P!"TE E)ample '!e 5)ersons5 table)6Id LastName 8 #ansen 3 Svendson : )ettersen > Nilsen@o!an A '$essem 7irstName Address &ity 4la 'imoteivn 89 Sandnes 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger .a11en 3 Stavanger @a1ob

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

Now we want to update t!e person 5'$essem @a1ob5 in t!e 5)ersons5 table" 0e use t+e follo-ing SQL statement' U2D%T' 2ersons S'T %ddress780issestien :E81 Cit(78Sandnes8 W5'R' Last0a!e78TDesse!8 %0D -irst0a!e78BaCob8 T+e @Persons@ table -ill no- loo. li.e t+is' )6Id 8 3 : > A LastName #ansen Svendson )ettersen Nilsen@o!an '$essem 7irstName Address &ity 4la 'imoteivn 89 Sandnes 'ove.orgvn 3: Sandnes ;ariStorgt 39 Stavanger .a11en 3 Stavanger @a1obNissestien CD Sandnes

SQL P!"TE 0arning %e careful -+en updating records. #f -e +ad omitted t+e 01ERE clause in t+e e)ample abo2e3 li.e t+is' U2D%T' 2ersons S'T %ddress780issestien :E81 Cit(78Sandnes8 '!e 5)ersons5 table would !ave loo1ed li1e t!is)6Id LastName 7irstName Address &ity 8 #ansen 4la Nissestien CD Sandnes 3 Svendson 'oveNissestien CD Sandnes : )ettersen ;ariNissestien CD Sandnes > Nilsen @o!an Nissestien CD Sandnes A '$essem @a1obNissestien CD Sandnes B. T+e !ELETE Statement The D'L'T' state!ent is used to de ete records in a tab e. '!e *%L%'% statement is used to delete rows in a table" SQL !ELETE S*nta) D'L'T' -R.M tab e*na!e W5'R' so!e*co u!n7so!e*6a ue

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

0ote4 Notice t!e ,#%+% clause in t!e *%L%'% syntax" '!e ,#%+% clause specifies w!ic! record or records t!at s!ould be deleted" If you omit t!e ,#%+% clause all records will be deletedB SQL !ELETE E)ample '!e 5)ersons5 table)6Id 8 3 : > A LastName #ansen Svendson )ettersen Nilsen @o!an '$essem 7irstName Address &ity 4la 'imoteivn 89 Sandnes 'ove .orgvn 3: Sandnes ;ari Storgt 39 Stavanger .a11en 3 Stavanger @a1obNissestien CD Sandnes

Now we want to delete t!e person 5'$essem @a1ob5 in t!e 5)ersons5 table" 0e use t+e follo-ing SQL statement' D'L'T' -R.M 2ersons W5'R' Last0a!e78TDesse!8 %0D -irst0a!e78BaCob8 T+e @Persons@ table -ill no- loo. li.e t+is' )6Id LastName 7irstName Address &ity 8 #ansen 4la 'imoteivn 89 Sandnes 3 Svendson 'ove .orgvn 3: Sandnes : )ettersen ;ari Storgt 39 Stavanger > Nilsen @o!an .a11en 3 Stavanger !elete "ll Ro-s It is possible to delete all rows in a table wit!out deleting t!e table" '!is means t!at t!e table structure attributes and indexes will be intactD'L'T' -R.M tab e*na!e or D'L'T' / -R.M tab e*na!e 0ote4 .e very careful w!en deleting records" =ou cannot undo t!is statementB

www.jntuworld.com

You might also like