7271029 SQL Tutorial

  • View
    217

  • Download
    0

Embed Size (px)

Text of 7271029 SQL Tutorial

  • 8/13/2019 7271029 SQL Tutorial

    1/31

    SQL Tutorial

    SQL TutorialSQL is a standard computer language for accessing andmanipulating databases.

    In this tutorial you will learn how to use SQL to access andmanipulate data in Oracle, Sybase, SQL Server, D !, "ccess, andother database systems.

    Introduction to SQL

    SQL is a standard computer language for accessing and manipulating databases.

    #hat is SQL$

    SQL stands for S tructured Q uery Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against a database SQL can retrieve data from a database SQL can insert new records in a database SQL can delete records from a database SQL can update records in a database SQL is easy to learn

    SQL is a Standard % &T....

    SQL is an ANSI (American National Standards Institute) standard computer language for accessingand manipulating database systems SQL statements are used to retrieve and update data in adatabase SQL wor!s wit" database programs li!e #S Access$ %&'$ Informix$ #S SQL Server$

    racle$ Sybase$ etc

    nfortunately$ t"ere are many different versions of t"e SQL language$ but to be in compliance wit"t"e ANSI standard$ t"ey must support t"e same ma*or !eywords in a similar manner (suc" asS+L+,-$ .%A-+$ %+L+-+$ INS+/-$ 01+/+$ and ot"ers)

    'ote( #ost of t"e SQL database programs also "ave t"eir own proprietary extensions in addition tot"e SQL standard2

    SQL Database Tables

    A database most often contains one or more tables +ac" table is identified by a name (e g3,ustomers3 or 3 rders3) -ables contain records (rows) wit" data

    &elow is an example of a table called 3.ersons34

  • 8/13/2019 7271029 SQL Tutorial

    2/31

    Last'ame )irst'ame "ddress *ity1ansen la -imoteivn 56 SandnesSvendson -ove &orgvn '7 Sandnes.ettersen 8ari Storgt '6 Stavanger

    -"e table above contains t"ree records (one for eac" person) and four columns (LastName$9irstName$ Address$ and ,ity)

    SQL Queries

    0it" SQL$ we can query a database and "ave a result set returned

    A query li!e t"is4

    SELECT LastName FROM Persons

    :ives a result set li!e t"is4

    Last'ame1ansenSvendson.ettersen

    'ote 4 Some database systems require a semicolon at t"e end of t"e SQL statement 0e don;t uset"e semicolon in our tutorials

    SQL Data +anipulation Language D+L-

    SQL (Structured Query Language) is a syntax for executing queries &ut t"e SQL language alsoincludes a syntax to update$ insert$ and delete records

    -"ese query and update commands toget"er form t"e %ata #anipulation Language (%#L) part ofSQL4

    S L *T < extracts data from a database table &/D"T < updates data in a database table D L T < deletes data from a database table I'S 0T I'TO < inserts new data into a database table

    SQL Data Definition Language DDL-

    -"e %ata %efinition Language (%%L) part of SQL permits database tables to be created or deleted0e can also define indexes (!eys)$ specify lin!s between tables$ and impose constraints betweendatabase tables

    -"e most important %%L statements in SQL are4

    *0 "T T" L < creates a new database table "LT 0 T" L < alters (c"anges) a database table

  • 8/13/2019 7271029 SQL Tutorial

    3/31

    D0O/ T" L < deletes a database table *0 "T I'D 1 < creates an index (searc" !ey) D0O/ I'D 1 < deletes an index

    Introduction to SQL

    SQL is a standard computer language for accessing and manipulating databases.

    #hat is SQL$

    SQL stands for S tructured Q uery Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against a database SQL can retrieve data from a database SQL can insert new records in a database SQL can delete records from a database SQL can update records in a database SQL is easy to learn

    SQL is a Standard % &T....

    SQL is an ANSI (American National Standards Institute) standard computer language for accessingand manipulating database systems SQL statements are used to retrieve and update data in adatabase SQL wor!s wit" database programs li!e #S Access$ %&'$ Informix$ #S SQL Server$

    racle$ Sybase$ etc

    nfortunately$ t"ere are many different versions of t"e SQL language$ but to be in compliance wit"t"e ANSI standard$ t"ey must support t"e same ma*or !eywords in a similar manner (suc" asS+L+,-$ .%A-+$ %+L+-+$ INS+/-$ 01+/+$ and ot"ers)

    'ote( #ost of t"e SQL database programs also "ave t"eir own proprietary extensions in addition tot"e SQL standard2

    SQL Database Tables

    A database most often contains one or more tables +ac" table is identified by a name (e g3,ustomers3 or 3 rders3) -ables contain records (rows) wit" data

    &elow is an example of a table called 3.ersons34

    Last'ame )irst'ame "ddress *ity1ansen la -imoteivn 56 SandnesSvendson -ove &orgvn '7 Sandnes.ettersen 8ari Storgt '6 Stavanger

  • 8/13/2019 7271029 SQL Tutorial

    4/31

    -"e table above contains t"ree records (one for eac" person) and four columns (LastName$9irstName$ Address$ and ,ity)

    SQL Queries

    0it" SQL$ we can query a database and "ave a result set returned

    A query li!e t"is4

    SELECT LastName FROM Persons

    :ives a result set li!e t"is4

    Last'ame1ansenSvendson.ettersen

    'ote 4 Some database systems require a semicolon at t"e end of t"e SQL statement 0e don;t uset"e semicolon in our tutorials

    SQL Data +anipulation Language D+L-

    SQL (Structured Query Language) is a syntax for executing queries &ut t"e SQL language alsoincludes a syntax to update$ insert$ and delete records

    -"ese query and update commands toget"er form t"e %ata #anipulation Language (%#L) part ofSQL4

    S L *T < extracts data from a database table &/D"T < updates data in a database table D L T < deletes data from a database table I'S 0T I'TO < inserts new data into a database table

    SQL Data Definition Language DDL-

    -"e %ata %efinition Language (%%L) part of SQL permits database tables to be created or deleted0e can also define indexes (!eys)$ specify lin!s between tables$ and impose constraints betweendatabase tables

    -"e most important %%L statements in SQL are4

    *0 "T T" L < creates a new database table "LT 0 T" L < alters (c"anges) a database table D0O/ T" L < deletes a database table *0 "T I'D 1 < creates an index (searc" !ey) D0O/ I'D 1 < deletes an index

    SQL #2 0 *lause

  • 8/13/2019 7271029 SQL Tutorial

    5/31

    The #2 0 clause is used to specify a selection criterion.

    The #2 0 *lause

    -o conditionally select data from a table$ a 01+/+ clause can be added to t"e S+L+,- statement

    Synta3

    SELECT column FROM tableWHERE column operator value

    0it" t"e 01+/+ clause$ t"e following operators can be used4

    Operator Description= +qual>? Not equal

    ? :reater t"an> Less t"an?= :reater t"an or equal>= Less t"an or equal&+-0++N &etween an inclusive rangeLI8+ Searc" for a pattern

    'ote( In some versions of SQL t"e >? operator may be written as 2=

    &sing the #2 0 *lause

    -o select only t"e persons living in t"e city 3Sandnes3$ we add a 01+/+ clause to t"e S+L+,-statement4

    SELECT * FROM PersonsWHERE City !San"nes!

    4/ersons4 table

    Last'ame )irst'ame "ddress *ity 5ear1ansen la -imoteivn 56 Sandnes [email protected] 5Svendson -ove &orgvn '7 Sandnes [email protected] Stale 8aivn 5C Sandnes [email protected]

    .ettersen 8ari Storgt '6 Stavanger [email protected]

    0esult

    Last'ame )irst'ame "ddress *ity 5ear1ansen la -imoteivn 56 Sandnes [email protected] 5Svendson -ove &orgvn '7 Sandnes [email protected] Stale 8aivn 5C Sandnes [email protected]

  • 8/13/2019 7271029 SQL Tutorial

    6/31

    &sing Quotes

    Note t"at we "ave used single quotes around t"e conditional values in t"e examples

    SQL uses single quotes around text values (most database systems will also accept double quotes)Numeric values s"ould not be enclosed in quotes

    9or text values4

    T#is is correct$SELECT * FROM Persons WHERE FirstName !Tove!T#is is %ron&$SELECT * FROM Persons WHERE FirstName Tove

    9or numeric values4

    T#is is correct$SELECT * FROM Persons WHERE 'ear() +,T#is is %ron&$SELECT * FROM Persons WHERE 'ear(!) +,!

    The LI6 *ondition

    -"e LI8+ condition is used to specify a searc" for a pattern in a column

    Synta3

    SELECT column FROM tableWHERE column L-.E pattern

    A 3E3 sign can be used to define wildcards (missing letters in t"e pattern) bot" before and after t"epattern

    &sing LI6

    -"e following SQL statement will return persons wit" first names t"at start wit" an ; ;4

    SELECT * FROM PersonsWHERE FirstName L-.E !O/!

    -"e following SQL statement will return persons wit" first names t"at end wit" an ;a;4

    SELECT * FROM PersonsWHERE FirstName L-.E !/a!

    -"e following SQL statement will return persons wit" first names t"at contain t"e pattern ;la;4

    SELECT * FROM PersonsWHERE FirstName L-.E !/la/!

  • 8/13/2019 7271029 SQL Tutorial

    7/31

    SQL I'S 0T I'TO Statement

    The I'S 0T I'TO Statement

    -"e INS+/- IN- statement is used to insert new rows into a table

    Synta3

    -NSERT -NTO table0name12L3ES 4value)5 value6577778

    Fou can also specify t"e columns for w"ic" you want to insert data4

    -NSERT -NTO table0name 4column)5 column65777812L3ES 4value)5 value6577778

    Insert a 'ew 0ow

    -"is 3.ersons3 table4

    Last'ame )irst'ame "ddress *ity.ettersen 8ari Storgt '6 Stavanger

    And t"is SQL statement4

    -NSERT -NTO Persons12L3ES 4!Hetlan"!5 !Camilla!5 !Ha&aba99a 6:!5 !San"nes!8

    0ill give t"is result4

    Last'ame )irst'ame "ddress *ity.ettersen 8ari Storgt '6 Stavanger1etland ,amilla 1agaba!!a 'G Sandnes

    Insert Data in Specified *olumns

    -"is 3.ersons3 table4

    Last'ame )irst'ame "ddress *ity.ettersen 8ari Storgt '6 Stavanger1etland ,amilla 1agaba!!a 'G Sandnes

    And -"is SQL statement4

    -NSERT -NTO Persons 4LastName5 2""ress8

  • 8/13/2019 7271029 SQL Tutorial

    8/31

    12L3ES 4!Rasmussen!5 !Stor&t +;!8

    0ill give t"is result4

    Last'ame )irst'ame "ddress *ity.ettersen 8ari Storgt '6 Stavanger1etland ,amilla 1agaba!!a 'G Sandnes/asmussen Storgt DB

    SQL &/D"T Statement

    The &pdate Statement

    -"e .%A-+ statement is used to modify t"e data in a table

    Synta3

    3P

  • 8/13/2019 7271029 SQL Tutorial

    9/31

    SET 2""ress !Stien )6!5 City !Stavan&er!WHERE LastName !Rasmussen!

    0esult(

    Last'ame )irst'ame "ddress *ityNilsen 9red 8ir!egt D Stavanger/asmussen Nina Stien 5' Stavanger

    SQL D L T Statement

    The D L T Statement

    -"e %+L+-+ statement is used to delete rows in a table

    Synta3

  • 8/13/2019 7271029 SQL Tutorial

    10/31

  • 8/13/2019 7271029 SQL Tutorial

    11/31

  • 8/13/2019 7271029 SQL Tutorial

    12/31

    Svendson Step"en 8aivn 5C Sandnes

    3ample

    se AN% to display eac" person wit" t"e first name equal to 3-ove3$ and t"e last name equal to

    3Svendson34

    SELECT * FROM PersonsWHERE FirstName !Tove!2N< LastName !Sven"son!

    0esult(

    Last'ame )irst'ame "ddress *itySvendson -ove &orgvn '7 Sandnes

    3ample

    se / to display eac" person wit" t"e first name equal to 3-ove3$ or t"e last name equal to3Svendson34

    SELECT * FROM PersonsWHERE >irstname !Tove!OR lastname !Sven"son!

    0esult(

    Last'ame )irst'ame "ddress *itySvendson -ove &orgvn '7 SandnesSvendson Step"en 8aivn 5C Sandnes

    3ample

    Fou can also combine AN% and / (use parent"eses to form complex expressions)4

    SELECT * FROM Persons WHERE4FirstName !Tove! OR FirstName !Step#en!82N< LastName !Sven"son!

    0esult(

    Last'ame )irst'ame "ddress *itySvendson -ove &orgvn '7 SandnesSvendson Step"en 8aivn 5C Sandnes

    SQL I'

  • 8/13/2019 7271029 SQL Tutorial

    13/31

    I'

    -"e IN operator may be used if you !now t"e exact value you want to return for at least one of t"ecolumns

    SELECT column0name FROM table0name

    WHERE column0name -N 4value) , value65778

    Original Table used in the e3amples-

    Last'ame )irst'ame "ddress *ity1ansen la -imoteivn 56 SandnesNordmann Anna Neset 5C Sandnes.ettersen 8ari Storgt '6 StavangerSvendson -ove &orgvn '7 Sandnes

    3ample 9

    -o display t"e persons wit" LastName equal to 31ansen3 or 3.ettersen3$ use t"e following SQL4

    SELECT * FROM PersonsWHERE LastName -N 4!Hansen!5!Pettersen!8

    0esult(

    Last'ame )irst'ame "ddress *ity1ansen la -imoteivn 56 Sandnes.ettersen 8ari Storgt '6 Stavanger

    SQL T# '

    T# ' ... "'D

    -"e &+-0++N AN% operator selects a range of data between two values -"ese values can benumbers$ text$ or dates

    SELECT column0name FROM table0nameWHERE column0name=ETWEEN value) 2N< value6

    Original Table used in the e3amples-

    Last'ame )irst'ame "ddress *ity1ansen la -imoteivn 56 SandnesNordmann Anna Neset 5C Sandnes.ettersen 8ari Storgt '6 StavangerSvendson -ove &orgvn '7 Sandnes

  • 8/13/2019 7271029 SQL Tutorial

    14/31

    SQL "lias

    #ith SQL, aliases can be used for column names and table names.

    *olumn 'ame "lias

    -"e syntax is4

    SELECT column 2S column0alias FROM table

    Table 'ame "lias

    -"e syntax is4

    SELECT column FROM table 2S table0alias

    3ample( &sing a *olumn "lias

    -"is table (.ersons)4

    Last'ame )irst'ame "ddress *ity1ansen la -imoteivn 56 SandnesSvendson -ove &orgvn '7 Sandnes

    .ettersen 8ari Storgt '6 Stavanger

    And t"is SQL4

    SELECT LastName 2S Family5 FirstName 2S NameFROM Persons

    /eturns t"is result4

    )amily 'ame1ansen laSvendson -ove

    .ettersen 8ari

    3ample( &sing a Table "lias

    -"is table (.ersons)4

    Last'ame )irst'ame "ddress *ity

  • 8/13/2019 7271029 SQL Tutorial

    15/31

    1ansen la -imoteivn 56 SandnesSvendson -ove &orgvn '7 Sandnes.ettersen 8ari Storgt '6 Stavanger

    And t"is SQL4

    SELECT LastName5 FirstNameFROM Persons 2S Employees

    /eturns t"is result4

    -able +mployees4

    Last'ame )irst'ame1ansen laSvendson -ove.ettersen 8ari

    SQL :OI'

    :oins and 6eys

    Sometimes we "ave to select data from two or more tables to ma!e our result complete 0e "aveto perform a *oin

    -ables in a database can be related to eac" ot"er wit" !eys A primary !ey is a column wit" aunique value for eac" row +ac" primary !ey value must be unique wit"in t"e table -"e purpose isto bind data toget"er$ across tables$ wit"out repeating all of t"e data in every table

    In t"e 3+mployees3 table below$ t"e 3+mployeeHI%3 column is t"e primary !ey$ meaning t"at notwo rows can "ave t"e same +mployeeHI% -"e +mployeeHI% distinguis"es two persons even if t"ey"ave t"e same name

    0"en you loo! at t"e example tables below$ notice t"at4

    -"e 3+mployeeHI%3 column is t"e primary !ey of t"e 3+mployees3 table -"e 3.rodHI%3 column is t"e primary !ey of t"e 3 rders3 table -"e 3+mployeeHI%3 column in t"e 3 rders3 table is used to refer to t"e persons in t"e

    3+mployees3 table wit"out using t"eir names

    mployees 4

    mployee;ID 'ame65 1ansen$ la6' Svendson$ -ove67 Svendson$ Step"en

  • 8/13/2019 7271029 SQL Tutorial

    16/31

    6G .ettersen$ 8ari

    Orders(

    /rod;ID /roduct mployee;ID'7G .rinter 65D B -able 67CD ,"air 67

    0eferring to Two Tables

    0e can select data from two tables by referring to two tables$ li!e t"is4

    3ample

    0"o "as ordered a product$ and w"at did t"ey order

    SELECT Employees7Name5 Or"ers7Pro"uctFROM Employees5 Or"ersWHERE Employees7Employee0-< Or"ers7Employee0-iel"65 >iel"?FROM >irst0table-NNER @O-N secon"0tableON >irst0table79ey>iel" secon"0table7>orei&n09ey>iel"

    0"o "as ordered a product$ and w"at did t"ey order

    SELECT Employees7Name5 Or"ers7Pro"uctFROM Employees-NNER @O-N Or"ersON Employees7Employee0-< Or"ers7Employee0-iel")5 >iel"65 >iel"?FROM >irst0tableLEFT @O-N secon"0tableON >irst0table79ey>iel" secon"0table7>orei&n09ey>iel"

    List all employees$ and t"eir orders < if any

    SELECT Employees7Name5 Or"ers7Pro"uctFROM EmployeesLEFT @O-N Or"ersON Employees7Employee0-< Or"ers7Employee0-