sqljoinsviewsindexes-110110013829-phpapp01

Upload: salai-sundar-ramanujulu

Post on 24-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    1/13

    JOINS VIEWS INDEXES

    What Is The Difference Between SQL and PL/SQL?

    There are so many variants of SQL that it is hard sometimes to figure out what to use !ere

    is a short arti"#e that $rief#y e%ains the differen"e $etween SQL and 'L(SQL En)oy*

    What is SQL?

    SQL +&ronoun"ed ,se-ua#./ stands for Stru"tured Query Language Withe SQL0 you "an view

    data 1 "a##ed Data Definiton Language or DDL and mani&u#ate data 1 "a##ed Data

    2ani&u#ation Languate or D2L 3## of the a$ove are )ust a fan"y way to say that with SQL0the user "an $oth view and a#ter re"ords in the data$ase To he#& 0 here are a "oue of

    -ueries4

    What is PL/SQL?

    The offi"ia# answer is from the 'L(SQL 5ser 6uide4'L(SQL0 Ora"#e7s &ro"edura# e%tension of SQL0 is an advan"ed fourth8generation&rogramming #anguage +96L/ It offers software8engineering features su"h as data

    en"a&su#ation0 over#oading0 "o##e"tion ty&es0 e%"e&tions0 and information hiding 'L(SQLa#so su&&orts ra&id &rototy&ing and deve#o&ment through tight integration with SQL and the

    Ora"#e data$ase!uh: That is what I thought at the $eginning ;ut at a high #eve#0 a## this means is that it

    "an do a## of the things that regu#ar SQL "an do0 $ut a#so0 it is &ro"edura# and "an $e used#i>0 Java0 et"/ ?or instan"e0 you "an use #oo&s and If

    Then statements in your 'L(SQL statements +'rograms/

    !ere is a definition of 'L(SQL from Lewis =unningham +an Ora"#e data$ase e%&ert/4

    ,If I wanted to "reate my own0 very short0 definition of 'L(SQL it wou#d $e this4 'L(SQL isthe Ora"#e native &rogramming #anguage that &rovides data$ase8"entri" a&i"ation

    deve#o&ment It "an native#y "a## stati" SQL and &rovides mu#tie methods of "a##ingdynami" SQL

    2r =unningham a#so does a very good )o$ of s&e##ing out the differen"es $etween SQL and'L(SQL

    SQL is a data oriented #anguage for se#e"ting and mani&u#ating sets of data 'L(SQL is a&ro"edura# #anguage to "reate a&i"ations @ou don7t norma##y have a ,SQL a&i"ation. @ou

    norma##y have an a&i"ation that uses SQL and a re#ationa# data$ase on the $a"

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    2/13

    SQL JOINS

    The JOIN

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    3/13

    The FOrdersF ta$#e4

    O*Id OrderN$ P*Id

    C KM H

    99K H

    H 9M C

    9 9M C

    M H99 CM

    Now we want to #ist a## the &ersons with any orders

    We use the fo##owing SELE=T statement4

    SELE=T 'ersonsLastName0 'ersons?irstName0 OrdersOrderNo

    ?AO2 'ersons

    INNEA JOIN Orders

    ON 'ersons'BIdOrders'BId

    OADEA ;@ 'ersonsLastName

    The resu#t8set wi## #oo< #i

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    4/13

    C !ansen O#a Timoteivn CG Sandnes

    Svendson Tove ;orgvn H Sandnes

    H 'ettersen ari Storgt G Stavanger

    The FOrdersF ta$#e4

    O*Id OrderN$ P*Id

    C KM H

    99K H

    H 9M C

    9 9M C

    M H99 CM

    Now we want to #ist a## the &ersons and their orders 8 if any0 from the ta$#es a$ove

    We use the fo##owing SELE=T statement4

    SELE=T 'ersonsLastName0 'ersons?irstName0 OrdersOrderNo

    ?AO2 'ersons

    LE?T JOIN Orders

    ON 'ersons'BIdOrders'BId

    OADEA ;@ 'ersonsLastName

    The resu#t8set wi## #oo< #i

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    5/13

    SQL RIGT JOIN "e#w$rd

    The AI6!T JOIN

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    6/13

    OADEA ;@ 'ersonsLastName

    The resu#t8set wi## #oo< #i

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    7/13

    1 ,,-/

    2 00,-

    220/ 1

    0 20/2 1

    / 0,0 1/

    Now we want to list all the !ersons and their orders and all the orders with their !ersons.

    #e use the followin$ %&L&'T statement

    SELECT *e'sons.LastName *e'sons.F"'stName O'%e's.O'%e'No

    FROM *e'sonsFULL JOIN O'%e's

    ON *e'sons.*_I%O'%e's.*_I%

    OR3ER &4 *e'sons.LastName

    The result(set will look like this

    LastName FirstName OrderNo

    !ansen Ola 220/

    !ansen Ola 20/2

    *ette'sen +a'" ,,-/

    *ette'sen +a'" 00,-

    S#en%son To#e

    0,0

    The FULL JOIN keyword returns all the rows from the left table )Persons* and all the rows fromthe ri$ht table )Orders*. If there are rows in "Persons" that do not ha+e matches in "Orders" or ifthere are rows in "Orders" that do not ha+e matches in "Persons" those rows will be listed as

    well.

    SQL SET OPER+TORS

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    8/13

    !e SQL UNION Operator

    The UNION o!erator is used to combine the result(set of two or more %&L&'T statements.

    Notice that each %&L&'T statement within the UNION must ha+e the same number of columns.The columns must also ha+e similar data ty!es. ,lso the columns in each %&L&'T statement

    must be in the same order.

    SQL UNION Syntax

    SELECT column_name(s) FROM table_name1

    UNION

    SELECT column_name(s) FROM table_name2

    Note:The UNION o!erator selects only distinct +alues by default. To allow du!licate +alues use

    UNION ,LL.

    SQL UNION ALL Syntax

    SELECT column_name(s) FROM table_name1

    UNION 5LL

    SELECT column_name(s) FROM table_name2

    PS:The column names in the result(set of a UNION are always e-ual to the column names in the

    first %&L&'T statement in the UNION.

    SQL UNION Example

    Look at the followin$ tables

    "Employees_Norway"

    E_I" E_Name

    $1 !ansen Ola

    $2 S#en%son To#e

    $ S#en%son Ste67en

    $0 *ette'sen +a'"

    "Employees_USA"

    E_I" E_Name

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    9/13

    $1 Tu'ne' Sall8

    $2 +ent Cla'9

    $ S#en%son Ste67en

    $0 Scott Ste67en

    Now we want to list all the differentem!loyees in Norway and U%,.

    #e use the followin$ %&L&'T statement

    SELECT E_Name FROM Em6lo8ees_No':a8

    UNION

    SELECT E_Name FROM Em6lo8ees_US5

    The result(set will look like this

    E_Name

    !ansen Ola

    S#en%son To#e

    S#en%son Ste67en

    *ette'sen +a'"

    Tu'ne' Sall8

    +ent Cla'9

    Scott Ste67en

    Note:This command cannot be used to list all em!loyees in Norway and U%,. In the eam!le

    abo+e we ha+e two em!loyees with e-ual names and only one of them will be listed. TheUNION command selects only distinct +alues.

    SQL UNION ALL Example

    Now we want to list allem!loyees in Norway and U%,

    SELECT E_Name FROM Em6lo8ees_No':a8

    UNION 5LL

    SELECT E_Name FROM Em6lo8ees_US5

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    10/13

    Result

    E_Name

    !ansen Ola

    S#en%son To#e

    S#en%son Ste67en

    *ette'sen +a'"

    Tu'ne' Sall8

    +ent Cla'9

    S#en%son Ste67en

    Scott Ste67en

    -iews

    SQL C#EAE $IE% Statement

    In %/L a +iew is a +irtual table based on the result(set of an %/L statement.

    , +iew contains rows and columns 0ust like a real table. The fields in a +iew are fields from oneor more real tables in the database.

    1ou can add %/L functions #2&3& and JOIN statements to a +iew and !resent the data as if

    the data were comin$ from one sin$le table.

    SQL C#EAE $IE% Syntax

    CRE5TE ;IE< #"e:_name 5S

    SELECT column_name(s)

    FROM table_name

    Note:, +iew always shows u!(to(date data4 The database en$ine recreates the data usin$ the

    +iew5s %/L statement e+ery time a user -ueries a +iew.

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    11/13

    SQL C#EAE $IE% Examples

    If you ha+e the Northwind database you can see that it has se+eral +iews installed by default.

    The +iew "'urrent Product List" lists all acti+e !roducts )!roducts that are not discontinued*from the "Products" table. The +iew is created with the followin$ %/L

    CRE5TE ;IE< =Cu''ent *'o%uct L"st> 5S

    SELECT *'o%uctI3*'o%uctName

    FROM *'o%ucts

    #e can -uery the +iew abo+e as follows

    SELECT ? FROM =Cu''ent *'o%uct L"st>

    ,nother +iew in the Northwind sam!le database selects e+ery !roduct in the "Products" tablewith a unit !rice hi$her than the a+era$e unit !rice

    CRE5TE ;IE< =*'o%ucts 5bo#e 5#e'ae *'"ce> 5S

    SELECT *'o%uctNameUn"t*'"ce

    FROM *'o%ucts

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    12/13

    SQL Updatin& a $iew

    1ou can u!date a +iew by usin$ the followin$ synta

    SQL C#EAE O# #EPLACE $IE% Syntax

    CRE5TE OR RE*L5CE ;IE< #"e:_name 5S

    SELECT column_name(s)

    FROM table_name

    Now we want to add the "'ate$ory" column to the "'urrent Product List" +iew. #e will u!date

    the +iew with the followin$ %/L

    CRE5TE ;IE< =Cu''ent *'o%uct L"st> 5S

    SELECT *'o%uctI3*'o%uctNameCateo'8

    FROM *'o%ucts

    SQL "roppin& a $iew

    1ou can delete a +iew with the :3OP ;I command.

    SQL "#OP $IE% Syntax

    3RO* ;IE< #"e:_name

    INDE.ES

    Indexes

    ,n inde can be created in a table to find data more -uickly and efficiently.

    The users cannot see the indees they are 0ust used to s!eed u! searches

  • 7/25/2019 sqljoinsviewsindexes-110110013829-phpapp01

    13/13

    ON table_name (column_name)

    SQL C#EAE UNIQUE IN"E' Syntax

    'reates a uni-ue inde on a table. :u!licate +alues are not allowed

    CRE5TE UNIUE IN3ED "n%e_nameON table_name (column_name)

    Note:The synta for creatin$ indees +aries amon$st different databases. Therefore 'heck the

    synta for creatin$ indees in your database.

    C#EAE IN"E' Example

    The %/L statement below creates an inde named "PInde" on the "LastName" column in the"Persons" table

    CRE5TE IN3ED *In%e

    ON *e'sons (LastName)

    If you want to create an inde on a combination of columns you can list the column names

    within the !arentheses se!arated by commas

    CRE5TE IN3ED *In%e

    ON *e'sons (LastName F"'stName)

    T!e "#OP IN"E' Statement

    The :3OP IN:&= statement is used to delete an inde in a table.

    "#OP IN"E' Syntax (or )S A**ess+

    3RO* IN3ED "n%e_name ON table_name

    "#OP IN"E' Syntax (or )S SQL Ser,er+

    3RO* IN3ED table_name."n%e_name

    "#OP IN"E' Syntax (or "-./Ora*le+

    3RO* IN3ED "n%e_name

    "#OP IN"E' Syntax (or )ySQL+

    5LTER T5&LE table_name 3RO* IN3ED "n%e_name