Introduction to SQL

31
Introduction to SQL

description

SQL, BCS, Computer

Transcript of Introduction to SQL

Introduction toSQLSQLSQL is a standard language for accessing and manipulating databases.What is SQL?SQL stands for Structured Query LanguageSQL lets you access and manipulate databasesSQL is an ANSI (American National Standards Institute) standardWhat Can SQL do?SQL can execute ueries against a databaseSQL can retrie!e data from a databaseSQL can insert records in a databaseSQL can update records in a databaseSQL can delete records from a databaseSQL can create ne" databasesSQL can create ne" tables in a databaseSQL can create stored procedures in a databaseSQL can create !ie"s in a databaseSQL can set permissions on tables# procedures# and !ie"sUsing SQL in Your Web Site$o build a "eb site t%at s%o"s data from a database# you "ill need&An '()*S database program (i.e. *S Access# SQL Ser!er# *ySQL)$o use a ser!er+side scripting language# li,e -.- or AS-$o use SQL to get t%e data you "ant$o use .$*L / 0SSSQLSyntax(atabase $ablesA database most often contains one or more tables. 1ac% table is identified by a name (e.g. 20ustomers2 or 23rders2). $ables contain records (ro"s) "it% data.Semicolon after SQL Statements?Some database systems reuire a semicolon at t%e end of eac% SQL statement.Semicolon is t%e standard "ay to separate eac% SQL statement in database systems t%at allo" more t%an one SQL statement to be executed in t%e same call to t%e ser!er.The Most Important SQL Commands SELECT + extracts data from a database UPDATE + updates data in a database DELETE + deletes data from a database !SE"T !T# + inserts ne" data into a database C"EATE DATA$ASE + creates a ne" database ALTE" DATA$ASE + modifies a database C"EATE TA$LE + creates a ne" table ALTE" TA$LE + modifies a table D"#P TA$LE + deletes a table C"EATE !DE% + creates an index (searc% ,ey) D"#P !DE% + deletes an indexSQLSELECTStatement$%e S1L10$ statement is used to select data from a database.SQL S1L10$ Syntax4S1L10$ column_name#column_name5'3* table_name6and4S1L10$ 7 5'3* table_name6AccountID Name Address Contact Age2014001 Florence Torino New York 235-12209 212014002 Grace More Miami 243-22104 232014003 Joan Black New York 235-12209 432014004 Adam Smi! New York 235-12209 342014005 Br"an Gi#$on Miami 243-22104 54201400% &aler Smi! Bo$on 23%-13254 23SELECT Column ExampleCreate SQL statement that selects the Name" and Address" columns from the Tbl_Customers" table:$bl80ustomersThe SQL SELECT DISTINCT Statement In a table# a column may contain many duplicate !alues6 and sometimes you only "ant to list t%e different (distinct) !alues. $%e (IS$IN0$ ,ey"ord can be used to return only distinct (different) !alues.SQL S1L10$ (IS$IN0$ Syntax S1L10$ (IS$IN0$ column_name#column_name5'3* table_name6AccountID Name Address Contact Age2014001 Florence Torino New York 235-12209 212014002 Grace More Miami 243-22104 232014003 Joan Black New York 235-12209 432014004 Adam Smi! New York 235-12209 342014005 Br"an Gi#$on Miami 243-22104 54201400% &aler Smi! Bo$on 23%-13254 23$bl80ustomersThe follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table: The follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table:SELECT Column ExampleCreate SQL statement that selects onl" the dstnct #alues from the Address" columns from the "Customers" table:SQLWHEREClause$%e 9.1'1 clause is used to extract only t%ose records t%at fulfill a specified criterion.SQL 9.1'1 SyntaxS1L10$ column_name#column_name5'3* table_name9.1'1 column_name operator value6AccountID Name Address Contact Age2014001 Florence Torino New York 235-12209 212014002 Grace More Miami 243-22104 232014003 Joan Black New York 235-12209 432014004 Adam Smi! New York 235-12209 342014005 Br"an Gi#$on Miami 243-22104 54201400% &aler Smi! Bo$on 23%-13254 23$bl80ustomersThe follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table: The follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table:SELECT Column ExampleCreate SQL statement that selects all the customers from the Addressfeld and select $oston"% n the Tbl_Customers" table:Text ields !s" Numeric ieldsSQL reuires single uotes around text !alues (most database systems "ill also allo" double uotes)..o"e!er# numeric fields s%ould not be enclosed in uotes&1xampleS1L10$ 7 5'3* 0ustomers9.1'1 0ustomerI(:;6The #ollo$in% operators can &e used in the WHERE clause'Operator Description& E'ual() Not e'ual* Note: +n some #ersons of SQL ths operator ma" be wrtten as ,&) -reater than( Less than)& -reater than or e'ual(& Less than or e'ual$ET.EEN $etween an nclus#e ran!eL+/E Search for a pattern+N To specf" multple possble #alues for a columnSQL(ND ) *R*perators $%e AN( < 3' operators are used to filter records based on more t%an one condition.The SQL A!D & #" #'erators $%e AN( operator displays a record if bot% t%e first condition AN( t%e second condition are true. $%e 3' operator displays a record if eit%er t%e first condition 3' t%e second condition is true.A!D #'erator E(am'le$%e follo"ing SQL statement selects all customers from t%e country 2=ermany2 AN( t%e city 2)erlin2# in t%e 20ustomers2 table&1xampleS1L10$ 7 5'3* 0ustomers9.1'1 0ountry:>=ermany>AN( 0ity:>)erlin>6#" #'erator E(am'le$%e follo"ing SQL statement selects all customers from t%e city 2)erlin2 3' 2*?nc%en2# in t%e 20ustomers2 table& 1xampleS1L10$ 7 5'3* 0ustomers9.1'1 0ity:>)erlin>3' 0ity:>*?nc%en>6AccountID Name Address Contact Age2014001 Florence Torino New York 235-12209 212014002 Grace More Miami 243-22104 232014003 Joan Black New York 235-12209 432014004 Adam Smi! New York 235-12209 342014005 Br"an Gi#$on Miami 243-22104 54201400% &aler Smi! Bo$on 23%-13254 23$bl80ustomersThe follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table: The follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table:SELECT Column ExampleCreate SQL statement that selects the countr" 0am"%name -race 0ore1 n the "Customers" table:AccountID Name Address Contact Age2014001 Florence Torino New York 235-12209 212014002 Grace More Miami 243-22104 232014003 Joan Black New York 235-12209 432014004 Adam Smi! New York 235-12209 342014005 Br"an Gi#$on Miami 243-22104 54201400% &aler Smi! Bo$on 23%-13254 23$bl80ustomersThe follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table: The follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table:SELECT Column ExampleCreate SQL statement that selects the contact234563278"% 23756229:1 n the "Customers" table:Combining A!D & #" @ou can also combine AN( and 3' (use parent%esis to form complex expressions). $%e follo"ing SQL statement selects all customers from t%e country 2=ermany2 AN( t%e city must be eual to 2)erlin2 3' 2*?nc%en2# in t%e 20ustomers2 table&1xample S1L10$ 7 5'3* 0ustomers9.1'1 0ountry:>=ermany>AN( (0ity:>)erlin> 3' 0ity:>*?nc%en>)6AccountID Name Address Contact Age2014001 Florence Torino New York 235-12209 212014002 Grace More Miami 243-22104 232014003 Joan Black New York 235-12209 432014004 Adam Smi! New York 235-12209 342014005 Br"an Gi#$on Miami 243-22104 54201400% &aler Smi! Bo$on 23%-13254 23$bl80ustomersThe follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table: The follown! SQL statement selects onl" the dstnct #alues from the "Ct"" columns from the "Customers" table:SELECT Column ExampleCreate SQL statement that selects the Address 1$oston1 and contact234563278"% 23756229:1 n the "Customers" table:SQL*RDER +,-e.$ord$%e 3'(1' )@ ,ey"ord is used to sort t%e result+set by one or more columns.$%e 3'(1' )@ ,ey"ord sorts t%e records in ascending order by default. $o sort t%e records in a descending order# you can use t%e (1S0 ,ey"ord.SQL *RDER +, S.ntaxS1L10$ column_name#column_name5'3* table_name3'(1' )@ column_name#column_name AS0A(1S06CustomerID CustomerNameContactName Address City PostalCode Country6 Alfreds ;utter $erln 6229: -erman"2 Ana Tru?llo Emparedados " heladosAna Tru?llo A#da* de la Consttuc@n 22220Axco B*;* 97926 0exco3 Antono 0oreno Ta'uerCaAntono 0oreno0ataderos 23620Axco B*;* 97923 0exco8 Around the DornThomas Dard"629 Dano#er S'*London .A6 6BE F/7 $er!lunds snabb