SlideShare uma empresa Scribd logo
1 de 60
INB(N)343
           Advanced Data Mining and Data
                  Warehousing

               Introducing SQL Server 2008




03/20/12                                     1
Objectives
       •   What is SQL Server 2008
       •   The key components of SQL server
       •   SQL server 2008 databases
       •   SQL server 2008 storage architecture




03/20/12                    © Y Li: INB(N)343 week 2   2
What’s SQL Server 2008
   • It is primarily thought of as a Relational Database
     Management System (RDBMS).
   • It can be more accurately described as an Enterprise Data
     Platform.
   • It builds on many of the features that had first been
     incorporated in SQL server 2005.
   • It also provides rich reporting capabilities, powerful data
     analysis, and data mining.
   • It also has features that support asynchronous data
     applications, data-driven event notification and more.




03/20/12                ©Yuefeng Li :INB(N)343 week 2              3
The key components of SQL server
   •   Database Engine
   •   Integration Services
   •   Analysis Services
   •   Reporting Services
   •   Service Broker
   •   Data Tier Web Services
   •   Replication Services
   •   Replication Services
   •   Multiple Instances
   •   Database Mail



03/20/12                 ©Yuefeng Li :INB343 week 2   4
Database Engine
   • It is the primary component of SQL Server 2008 for the
     efficient storage, retrieval, and manipulation of relational
     and XML formatted data.
   • It is highly optimized for transaction processing.
   • The Database Engine is also responsible for the controlled
     access and modification of data through its security
     subsystem.




03/20/12                  ©Yuefeng Li :INB343 week 2                5
Integration Services
   • SQL Server Integration Services (SSIS) is Microsoft’s
     enterprise class data Extract, Transform, and Load (ETL)
     tool,
           – Where the ETL process begins by defining a data sources.
             From the data source, you define which data you are
             interested in copying to a new destination.
   • Integration services is part of a suit of tools included in the
     Business Intelligent Development Studio (BIDS),
           – which includes add-ins for designing solutions for integration
             services, analysis services, and reporting services.




03/20/12                        ©Yuefeng Li :INB343 week 2                    6
Analysis Services
   • It delivers Online Analytical Processing (OLAP) and Data
     Mining functionality for Business Intelligence application.
   • It provides a robust environment for the detailed analysis
     of data, through
           – User-created, multidimensional data structures, such as
             relational databases, spreadsheets, flat files, and other
             multidimensional sources.
   • The Data Mining component allows the analysis of large
     quantities of data by mining hidden relationships and
     patterns that may be of interest to an organization’s data
     analyst.




03/20/12                        ©Yuefeng Li :INB343 week 2               7
Reporting Services
   • It is a Web Service-based solution for designing,
     deploying, and managing flexible, dynamic web-based
     reports, as well as traditional paper reports.
           – These reports can contain information from virtually any data
             source.
   • It also allows for more savvy users to create ad hoc reports
     based report models.
   • Report models provide an abstraction of the data in the
     form of entities so that users are not bothered with having
     to know the intricate details of the actual data sources.




03/20/12                        ©Yuefeng Li :INB343 week 2                   8
Notice: Compatibility Level
   • In the new Database window, we can use change the
     compatibility level using options.
   • Normally, we should have
           – Collation: <server default>
           – Recovery model: Full
           – Compatibility level: SQL Server 2008 (100)
   • SQL Server 2008 allows three compatibility levels: 80, 90,
     and 100 that correlates to SQL Server 2000, SQL Server
     2005, and SQL Server 2008.
   • The following code will set the compatibility level of the
     AdventureWorks2008 database to SQL Server 2000:
           ALTER DATABASE AdventureWorks2008
           SET COMPATIBILITY_LEVEL = 80
03/20/12                      ©Yuefeng Li :INB(N)343 week 2       9
SQL Server 2008 Database Objects
   •   Server
        – The server scope encompasses all the objects that exist on the
          instance of SQL Server, regardless of their respective database of
          namespace.
        – The term server actually refers to the SQL Server 2008 instance.
   •   Database
        – The database scope defines all the objects within a database
          catalog. Schemas exist in the database scope.
   •   Schema
        – A schema is a namespace for database object. Each database can
          contain one or more schemas. All data objects in a database
          reside in a specific schema.




03/20/12                      ©Yuefeng Li :INB343 week 2                       10
SQL Server 2008 Database Objects
                         cont.
   •   Object Names
        – Every object is identified by a 4-part, fully qualified name, with the
          form server.database.schema.object
        – The default schema is dbo.
        – The fully qualified name can be abbreviated
        – E.g., Fred’s query “select * from Person” may resolve to
          FITPA00034343.AdventureWorks.dbo.Person; or
            FITPA00034343.AdventureWorks.Person.Person
        – If we use the user-defined schema Person, the query will have to
          be rewritten to reference the appropriate schema scope: “select
          * from Person.Person”; or Fred’s default schema can be
          changed to the Person schema
            use AdventureWorks;
            GO
            Alter user Fred with DEFAULT_SCHEMA=Person;

03/20/12                        ©Yuefeng Li :INB343 week 2                         11
The most basic object: Table
   • A table can be thought of as equating to an accountant’s
     ledger or an Excel spreadsheet.
   • It is made up of what is called domain data (columns) and
     entity data (rows). The actual data for the database is
     stored in the tables.
   • Each table definition also contains the metadata
     (descriptive information about data) that describes the
     nature of the data it is to contain.
   • Each column has its own set of rules about what can be
     stored in that column.
   • A violation of the rules of any one column can cause the
     system to reject an inserted row, an update to an existing
     row, or the deletion of a row.

03/20/12                 ©Yuefeng Li :INB343 week 2               12
Example of a table




03/20/12       ©Yuefeng Li :INB343 week 1   13
Example of a table cont.
   • The table in Figure 1-1 is made up of five columns of data.
     The number of columns remains constant regardless of
     how much data (even zero) is in the table.
   • Currently, the table has fourteen records. The number of
     records will go up and down as we add or delete data, but
     the nature of the data in each record (or row) is described
     and restricted by the data type of the column.




03/20/12                 ©Yuefeng Li :INB343 week 1                14
Partitioning Tables
   • All data pages in logical units are stored in partitions.
   • Normally, tables are stored in a single partition defined on
     a single filegroup.
   • SQL server also provides the ability to separate large table
     into smaller units by horizontally partitioning the table
     across multiple files managed by filegroup definitions.




03/20/12                 ©Yuefeng Li :INB(N)343 week 2              15
Indexes
   • An index is an object that exists only within the framework
     of a particular table or view.
   • An index works much like the index does in the back of an
     encyclopedia.
   • There is some sort of lookup (or “key”) value that is sorted
     in a particular way and, once you have that, you are
     provided another key with which you can look up the actual
     information.
   • An index provides us ways of speeding the lookup of our
     information.




03/20/12                 ©Yuefeng Li :INB343 week 2                 16
Two Indexes categories
   ❑ Clustered — You can have only one of these per table.
           – If an index is clustered, it means that the table on which the
             clustered index is based is physically sorted according to that index.
           – If you were indexing an encyclopedia, the clustered index would be
             the page numbers (the information in the encyclopedia is stored in
             the order of the page numbers).
   ❑ Non-clustered — You can have many of these for every
     table.
           – This is more along the lines of what you probably think of when you
             hear the word “index.”
           – This kind of index points to some other value that will let you find the
             data.
           – For our encyclopedia, this would be the keyword index at the back of
             the book.


03/20/12                           ©Yuefeng Li :INB343 week 2                         17
Triggers
   • A trigger is an object that exists only within the framework
     of a table.
   • Triggers are pieces of logical code that are automatically
     executed when certain things (such as inserts, updates, or
     deletes) happen to your table.
   • Triggers can be used for a great variety of things, but are
     mainly used for either copying data as it is entered, or
     checking the update to make sure that it meets some
     criteria.




03/20/12                 ©Yuefeng Li :INB(N)343 week 2              18
Triggers: An Example
• E.g. whenever a row is deleted from particular table, we
  might want to execute an INSERT statement:

   CREATE TRIGGER MyTrigger
     AFTER DELETE ON MyTable
     REFERENCING OLD ROW AS MyOldRow
     FOR EACH ROW
       INSERT INTO MyLog VALUES
      (‘Deleted’, MyOldRow.MyColumn1, MyOldRow.MyColumn2,
                                        CURRENT_TIMESTAMP);


(Because a DELETE statement may delete more than one row, we need
   to say FOR EACH ROW here, so that we get one row inserted for
   each row deleted.)


                       ©Yuefeng Li :INB(N)343 week 2
Triggers: Why use triggers?

• People use triggers for:
   – logging/audit-trail (for security and/or statistical analysis);
   – archiving to another table (for keeping historical data);
   – enforce validation-rules/constraints that cannot easily be
     achieved through SQL constraints (CHECK, CREATE
     ASSERTION, etc.).
• Triggers cannot be applied to SELECT statements,
  although this may change in future SQL standards and
  implementations (to enforce better security & privacy
  policies).




                     ©Yuefeng Li :INB(N)343 week 2
What are the parts of a trigger?

•   Every trigger must be created with 5 parts:
    –    a name;
    –    a triggering event, which determines when the trigger is
         activated − this specifies:
         •   the event type (Insert, Update or Delete);
         •   for Updates: which columns are involved;
         •   the table involved;
    3. whether the trigger is fired immediately before or after the
       triggering event (see later slide);




                        ©Yuefeng Li :INB(N)343 week 2
What are the parts of a trigger? cont.

 1. a trigger level, either:
     –   row-level: the trigger is fired once for each row
         inserted/updated/deleted; or
     –   statement-level: the trigger is fired once for each SQL
         Insert/Update/Delete statement (regardless of the number
         of rows affected).
 2. a trigger body, i.e. the statements executed when the
    trigger is activated.




                   ©Yuefeng Li :INB(N)343 week 2
Why BEFORE and AFTER triggers?

• An AFTER trigger fires after the action has completed.
  Both the old and new row values are "visible" to the
  trigger (where appropriate).
• So, an AFTER trigger is good for logging and/or
  archiving, as shown in the earlier example.




                   ©Yuefeng Li :INB(N)343 week 2
Why BEFORE and AFTER triggers?

• On the other hand, a BEFORE trigger fires before the
  action (Insert, Update or Delete) is attempted.
• What use is this? Such a trigger has the ability to:
   – skip the action for one or more rows (i.e. prevent certain
     rows being inserted/updated/deleted); or
   – effectively change the action, (e.g. a Delete action could be
     turned into an Update that sets a “logically-deleted” flag in
     each of the rows affected); or
   – change the row being inserted/updated (for INSERT and
     UPDATE actions only) – as shown by example on next slide.
• So, a BEFORE trigger is good for enforcing
  rules/constraints.


                      ©Yuefeng Li :INB(N)343 week 2
Example of a BEFORE trigger
     CREATE TRIGGER MyInsertTrigger
       BEFORE INSERT ON MyTable
       REFERENCING NEW ROW AS NewRow
       FOR EACH ROW
         SET NewRow.ContactPhoneNumber =
           COALESCE (NewRow.ContactPhoneNumber,
                                  NewRow.HomePhoneNumber);

•   As you can see, this trigger tries to ensure that each newly-inserted
    row has a (non-null) ContactPhoneNumber.
     – If the new row has a non-null ContactPhoneNumber already, that
       value will be used (i.e. unchanged).
     – But if the ContactPhoneNumber is null, then the
       HomePhoneNumber will be used (which won’t be null, we hope).




                          ©Yuefeng Li :INB(N)343 week 2
Constraints
   • A constraint is yet another object that exists only within the
     confines of a table.
   • Constraints are much like they sound; they confine the
     data in your table to meet certain conditions.
   • Constraints, in a way, compete with triggers as possible
     solutions to data integrity issues. They are not, however,
     the same thing: Each has its own distinct advantages.




03/20/12                  ©Yuefeng Li :INB(N)343 week 2               26
Triggers vs. Constraints

• Remember that CHECK constraints can be specified
  within CREATE TABLE (and within CREATE ASSERTION).
• Some DBMSs use (system-generated) triggers for
  handling Referential Integrity constraints, e.g. cascaded
  deletes/updates.
   – PostgreSQL does this.
   – It’s relatively easy to implement, but is not the most
     machine-efficient way to do things.




                     ©Yuefeng Li :INB(N)343 week 2
Triggers vs. Constraints (cont.)

• Triggers are more flexible. This can be an advantage: a
  trigger can execute a complex SQL routine.
• Constraints are more rigid. But this rigidity can be an
  advantage in some situations:
   – It’s easier for humans to see what the constraint does.
   – It’s easier for the DBMS to see what the constraint does,
     and to optimise accordingly.
   – Constraints can be deferred (i.e. checked at the end of a
     transaction, rather than during it) – triggers can’t.




                    ©Yuefeng Li :INB(N)343 week 2
Diagrams
   •   A database diagram is a visual representation of the database design,
       including the various tables, the column names in each table, and the
       relationships between tables.




03/20/12                      ©Yuefeng Li :INB343 week 2                       29
Views
   • A view is, at its core, really nothing more than a stored
     query, to reduce the complexity for the end user, or hiding
     sensitive data.
   • The basic form
           CREATE VIEW <view name>
           AS
           <SELECT statement>




03/20/12                    ©Yuefeng Li :INB343 week 2             30
Example
           USE Oly2000a;
           GO
           CREATE view Name_vw
           AS
              SELECT GivenName, CountryCode
              FROM dbo.Competitors
              WHERE CompetitorNum <=9000

   •   Notice it does not return any rows. It just lets us know that the view
       has been created: Command(s) completed successfully. We can run a
       SELECT statement against this view

           SELECT * FROM dbo.Name_vw




03/20/12                       ©Yuefeng Li :INB343 week 2                       31
SQL Server 2008 Database
   • Two types of databases: system databases and user
     databases.
   • The system databases are used to store system-wide data
     and metadata, including master, model. msdb, and tempdb
     databases , as well as the hidden resources database.
   • User databases are created by users who have the
     appropriate level of permission to store application data.




03/20/12                 ©Yuefeng Li :INB343 week 2               32
SQL Server 2008 Database Storage
   • All system and user databases are stored in files.
   • There is always a minimum of two files: one data file (e.g.,
     “mydb.mdf”) and one transaction log file (e.g.,
     mydb_log.ldf).
   • When a user database is created, it must contain at least a
     data file, or called primary data file, which is a member of
     the default Primary filegroup.
   • More filegroups can aslo be defined.
   • Upon initial creation of database, one transaction log must
     be defined, which is used to record all modifications to the
     database.



03/20/12                 ©Yuefeng Li :INB343 week 2                 33
SQL Server Data Types
   •    We are going to assume that you already understand how data types
        work.
   •    Normal Numerics: Integer, Decimal and Float

       Data Type    Class         Size in      Nature of the data
       Name                       Bytes
       Bit          Integer       1            The first bit data type in a table takes up 1
                                               byte; the next 7 make use of the same byte.
       Bigint       Integer       8            -263 to 263-1
       Int          Integer       4            -2,147,483,648 to 2,147,483,647
       SmallInt     Integer       2            -32,768 to 32,767
       TinyInt      Integer       1            0 to 255
       Decimal or   Decimal/      Varies       Fixed precison and scale from -1038-1 to
       Numeric      Numeric                    1038-1. The two names are synonymous,
       Float        Approximate   Varies       -1.79E+308 to 1.79E+308
                    Numerics

03/20/12                          ©Yuefeng Li :INB343 week 2                                   34
SQL Server Data Types: Money, Date/Time
   Data Type Name   Class       Size     Nature of the data
                                in
                                Bytes
   Money            Money       8        From -263 to 263 plus precision to four decimal places.


   SmallMoney       Money       4        -214, 748.3648 to +214,748.3647

   DateTime         Date/Time   8        from January 1, 1753 to December 31, 9999 with an
                                         accuracy of three hundredths of a second.
   DateTime2        Date/Time   Varies Supports larger date
                                (6-8)  ranges and large time-fraction precision

   SamllDateTime    Date/Time   4        from January 1, 1900, to June 6, 2079, with an accuracy
                                         of one minute
   DateTimeOffset   Date/Time   Varies Time is stored internally as UTC time
                                (8-10)
   Date             Date/Time   3        from January 1, 0001 to December 31, 9999, as defined
                                         by the Gregorian calendar.
   Time             Date/Time   Varies precisions as granular as 100 nano econds
                                (3-5)

03/20/12                        ©Yuefeng Li :INB343 week 2                                         35
SQL Server Data Types: Character, Unicode
                  and Binary
   Data Type   Class       Size in      Nature of the data
   Name                    Bytes
   Char        Character   Varies       Fixed-length character data

   VarChar     Character   Varies       Variable-length character data

   Text        Character   Varies       Legacy support as of SQL Server 2005. Use
                                        varchar(max) instead!
   XML         Character   Varies       Defines a character field as being for XML data

   Nchar       Unicode     Varies       Fixed-length Unicode character data

   NVarChar    Unicode     Varies       Variable-length Unicode character data

   Ntext       Unicode     Varies       Variable-length Unicode character data. It is legacy
                                        support only
   Binary      Binary      Varies       Fixed-length binary data with a maximum length of
                                        8,000 bytes
   VarBinary   Binary      Varies       Variable-length binary data with a maximum specified
                                        length of 8,000 bytes
   Image       Binary      Varies       Legacy support only as of SQL Server 2005. Use
                                        varbinary(max) instead!


03/20/12                    ©Yuefeng Li :INB343 week 2                                         36
T-SQL
   • The most fundamental Transact-SQL (T-SQL) statements.
   • T-SQL is SQL Server’s own dialect of Structured Query
     Language (SQL).
   • The T-SQL statements that we will learn in this lecture are:
           ❑   SELECT
           ❑   INSERT
           ❑   UPDATE
           ❑   DELETE




03/20/12                  ©Yuefeng Li :INB343 week 2                37
The Basic Syntax Rules of a SELECT
                   Statement
   SELECT [ALL|DISTINCT] [TOP (<expression>) [PERCENT]
      [WITH TIES]] <column list>
   [FROM <source table(s)/view(s)>]
   [WHERE <restrictive condition>]
   [GROUP BY <column name or expression using a column in the
      SELECT list>]
   [HAVING <restrictive condition based on the GROUP BY
      results>]
   [ORDER BY <column list>]
   [[FOR XML {RAW | AUTO | EXPLICIT | PATH [(<element>)]}
      [,XMLDATA][,ELEMENTS][,BINARY base 64]]
   [OPTION (<query hint>, [, ...n])]




03/20/12                 ©Yuefeng Li :INB343 week 2             38
Example
      USE Oly2000a
      SELECT GivenName, CountryCode, COUNT(*) AS Count
      FROM dbo.Competitors
      WHERE CompetitorNum <= 9000
      GROUP BY GivenName, CountryCode
      ORDER BY GivenName, CountryCode;




03/20/12                ©Yuefeng Li :INB343 week 2       39
Result




03/20/12   ©Yuefeng Li :INB343 week 2   40
The full syntax for INSERT
   INSERT [TOP ( <expression> ) [PERCENT] ] [INTO] <tabular object>
      [(<column list>)]
      [ OUTPUT <output clause> ]
   { VALUES (<data values>) [,(<data values>)] [, …n]
    | <table source>
    | EXEC <prodecure>
    | DEFAULT VALUES


   The more basic syntax for an INSERT statement looks like this:

   INSERT [INTO] <table>
     [(<column list>)]
   VALUES (<data values>) [,(<data values>)] [, …n]




03/20/12                        ©Yuefeng Li :INB343 week 2            41
Examples of Insert
   INSERT INTO Sales
   (StoreCode, OrderNumber, OrderDate, Quantity, Terms, TitleID)
   VALUES
   (‘TEST’, ‘TESTORDER’, ‘01/01/1999’, 10, ‘NET 30’, 1234567);

   USE AdventureWorks2008;
   DECLARE @MyTable Table
   (
   SalesOrderID int,
   CustomerID char(5)
   );
   INSERT INTO @MyTable
   SELECT SalesOrderID, CustomerID
   FROM AdventureWorks2008.Sales.SalesOrderHeader
   WHERE SalesOrderID BETWEEN 44000 AND 44010;


03/20/12                 ©Yuefeng Li :INB343 week 2                42
Update Statement
   UPDATE [TOP ( <expression> ) [PERCENT] ] <tabular object>
   SET <column> = <value>[.WRITE(<expression>, <offset>, <length>)]
   [,<column> = <value>[.WRITE(<expression>, <offset>, <length>)]]
   [ OUTPUT <output clause> ]
   [FROM <source table(s)>]
   [WHERE <restrictive condition>]

   Let’s look at the more basic syntax:

   UPDATE <table name>
   SET <column> = <value> [,<column> = <value>]
   [FROM <source table(s)>]
   [WHERE <restrictive condition>]




03/20/12                         ©Yuefeng Li :INB343 week 2           43
Delete
   DELETE [TOP ( <expression> ) [PERCENT] ] [FROM] <tabular object>
     [ OUTPUT <output clause> ]
   [FROM <table or join condition>]
   [WHERE <search condition> | CURRENT OF [GLOBAL] <cursor name>]


   The basic syntax couldn’t be much easier:

   DELETE <table name>
   [WHERE <condition>]




03/20/12                     ©Yuefeng Li :INB343 week 2               44
Joins
   • A JOIN does just what it sounds like — it puts the
     information from two tables together into one result set.
   • We can think of a result set as being a “virtual” table. It has
     both columns and rows, and the columns have data types.
   • The various forms of the JOIN clause. These will include:
           ❑ INNER JOIN
           ❑ OUTER JOIN (both LEFT and RIGHT)
           ❑ FULL JOIN
           ❑ CROSS JOIN




03/20/12                    ©Yuefeng Li :INB343 week 2                 45
INNER JOIN
   • The preferred code for an INNER JOIN looks something
     like this:

           SELECT <select list>
           FROM <first_table>
           <join_type> <second_table>
              [ON <join_condition>]




03/20/12                    ©Yuefeng Li :INB343 week 2      46
Examples of INNER JOINS
  SELECT *
  FROM dbo.Events
  INNER JOIN dbo.Results
  ON dbo.Events.EventId = dbo.Results.EventId



  SELECT pbe.EventId, pbe.Style, hre.CompetitorNum,
     hre.ElapsedTime, pp.CountryCode, pp.FamilyName
  FROM dbo.Events pbe
  INNER JOIN dbo.Results hre
  ON pbe.EventId = hre.EventId
  INNER JOIN dbo.Competitors pp
  ON hre.CompetitorNum = pp.CompetitorNum




03/20/12                ©Yuefeng Li :INB343 week 2    47
Results of the first join




03/20/12          ©Y Li :ITB(N)740 week 2   48
The result of the second join




03/20/12            ©Yuefeng Li :INB343 week 2   49
The result of the second join cont.




             (208 row(s) affected)

03/20/12                ©Yuefeng Li :INB343 week 2   50
OUTER JOINs
   SELECT <SELECT list>
   FROM <the table you want to be the “LEFT” table>
   <LEFT|RIGHT> [OUTER] JOIN <table you want to be the “RIGHT” table>
   ON <join condition>




   •   OUTER JOINs are, as I’ve said, inclusive in nature. What specifically
       gets included depends on which side of the join you have emphasized.
   •   A LEFT OUTER JOIN includes all the information from the table on the
       left, and a RIGHT OUTER JOIN includes all the information from the
       table on the right.




03/20/12                      ©Yuefeng Li :INB343 week 2                       51
Examples of OUTER JOINs
   SELECT *
   FROM dbo.Events
   LEFT OUTER JOIN dbo.Results
   ON dbo.Events.EventId = dbo.Results.EventId



   SELECT pbe.EventId, pbe.Style, hre.CompetitorNum,
     hre.ElapsedTime, pp.CountryCode, pp.FamilyName
   FROM dbo.Events pbe
   LEFT OUTER JOIN dbo.Results hre
   ON pbe.EventId = hre.EventId
   LEFT OUTER JOIN dbo.Competitors pp
   ON hre.CompetitorNum = pp.CompetitorNum



03/20/12               ©Yuefeng Li :INB343 week 2      52
The result of the first outer join




       Left                                      Right
03/20/12               ©Y Li :ITB(N)740 week 2           53
The result of the second outer join




           (214 row(s) affected)

03/20/12                    ©Yuefeng Li :INB343 week 2   54
OUTER vs. INNER
   • The outer join yields similar results, but with a rather
     important difference.
   • If we are joining and we don’t have a matching record for
     the table, then what happens?
   • Since it is not on the inclusive side of the JOIN (in this
     case, the LEFT side), SQL Server will fill in a NULL for any
     value that comes from the opposite side of the join if there
     is no match with the inclusive side of the JOIN.
   • In this case, all but 3 of our rows have EventIDs. What we
     can discern from that is that all of our Events are
     associated with at least one result except three (SWM411,
     SWM412, SWM451).


03/20/12                  ©Yuefeng Li :INB343 week 1                55
FULL JOIN
   • Like many things in SQL, a FULL JOIN (also known as a
     FULL OUTER JOIN) is basically what it sounds like — it is
     a matching up of data on both sides of the JOIN with
     everything included, no matter what side of the JOIN it is
     on.
   • A FULL JOIN is perhaps best described as what you would
     get if you could do a LEFT JOIN and a RIGHT JOIN in the
     same JOIN. You get all the records that match, based on
     the JOIN field(s).
   • You also get any records that exist only on the left side,
     with NULLs being returned for columns from the right side.
     Finally, you get any records that exist only on the right
     side, with NULLs being returned for columns from the left
     side.
03/20/12                 ©Yuefeng Li :INB343 week 1               56
Inner Join                                           Full Outer Join
             Competitors   Countries                            Competitors   Countries




Left Outer Join                                      Right Outer Join
             Competitors   Countries                            Competitors   Countries




                                   ©Yuefeng Li :INB343 week 2
Examples
   SELECT ev.EventId, re.CompetitorNum, re.ElapsedTime
   FROM dbo.Events ev
   FULL JOIN dbo.Results re
   ON ev.EventId = re.EventId
      WHERE re.CompetitorNum is NULL

   GO

   SELECT ev.EventId, re.CompetitorNum, re.ElapsedTime
   FROM dbo.Events ev
   LEFT OUTER JOIN dbo.Results re
   ON ev.EventId = re.EventId
      WHERE re.CompetitorNum is NULL

   GO

   SELECT ev.EventId, re.CompetitorNum, re.ElapsedTime
   FROM dbo.Events ev
   RIGHT OUTER JOIN dbo.Results re
   ON ev.EventId = re.EventId


03/20/12                       ©Yuefeng Li :INB343 week 2   58
CROSS JOIN
   • CROSS JOINs are very strange critters indeed.
   • A CROSS JOIN differs from other JOINs in that there is no
     ON operator and that it joins every record on one side of
     the JOIN with every record on the other side of the JOIN.
   • In short, you wind up with a Cartesian product of all the
     records on both sides of the JOIN.
   • The syntax is the same as any other JOIN, except that it
     uses the keyword CROSS (instead of INNER, OUTER, or
     FULL) and that it has no ON operator.




03/20/12                ©Yuefeng Li :INB(N)343 week 2            59
References

           – Vieira, Robert Author, Beginning Microsoft SQL Server 2008
             Programming, (QUT Library E book).
           – Chris Leiter et al., Beginning Microsoft SQL Server 2008
             Administration, Wrox, Hoboken. (QUT Library E book)




03/20/12                         © Y Li: INB(N)343 week 2                 60

Mais conteúdo relacionado

Mais procurados

Oracle Database Introduction
Oracle Database IntroductionOracle Database Introduction
Oracle Database IntroductionChhom Karath
 
9. Document Oriented Databases
9. Document Oriented Databases9. Document Oriented Databases
9. Document Oriented DatabasesFabio Fumarola
 
Overview of oracle database
Overview of oracle databaseOverview of oracle database
Overview of oracle databaseSamar Prasad
 
Introducing ms sql_server_updated
Introducing ms sql_server_updatedIntroducing ms sql_server_updated
Introducing ms sql_server_updatedleetinhf
 
Chapter 7(documnet databse termininology) no sql for mere mortals
Chapter 7(documnet databse termininology) no sql for mere mortalsChapter 7(documnet databse termininology) no sql for mere mortals
Chapter 7(documnet databse termininology) no sql for mere mortalsnehabsairam
 
FileTable and Semantic Search in SQL Server 2012
FileTable and Semantic Search in SQL Server 2012FileTable and Semantic Search in SQL Server 2012
FileTable and Semantic Search in SQL Server 2012Michael Rys
 
Ado.net session04
Ado.net session04Ado.net session04
Ado.net session04Niit Care
 
Chapter 5 design of keyvalue databses from nosql for mere mortals
Chapter 5 design of keyvalue databses from nosql for mere mortalsChapter 5 design of keyvalue databses from nosql for mere mortals
Chapter 5 design of keyvalue databses from nosql for mere mortalsnehabsairam
 
MDF and LDF in SQL Server
MDF and LDF in SQL ServerMDF and LDF in SQL Server
MDF and LDF in SQL ServerMasum Reza
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Databasepuja_dhar
 
ADO.NET difference faqs compiled- 1
ADO.NET difference  faqs compiled- 1ADO.NET difference  faqs compiled- 1
ADO.NET difference faqs compiled- 1Umar Ali
 

Mais procurados (20)

Oracle Database Introduction
Oracle Database IntroductionOracle Database Introduction
Oracle Database Introduction
 
Sql Server Basics
Sql Server BasicsSql Server Basics
Sql Server Basics
 
Sql server basics
Sql server basicsSql server basics
Sql server basics
 
Oracle DB
Oracle DBOracle DB
Oracle DB
 
Oracle
OracleOracle
Oracle
 
Ado.net
Ado.netAdo.net
Ado.net
 
9. Document Oriented Databases
9. Document Oriented Databases9. Document Oriented Databases
9. Document Oriented Databases
 
Ado.net
Ado.netAdo.net
Ado.net
 
Overview of oracle database
Overview of oracle databaseOverview of oracle database
Overview of oracle database
 
Introducing ms sql_server_updated
Introducing ms sql_server_updatedIntroducing ms sql_server_updated
Introducing ms sql_server_updated
 
Chapter 7(documnet databse termininology) no sql for mere mortals
Chapter 7(documnet databse termininology) no sql for mere mortalsChapter 7(documnet databse termininology) no sql for mere mortals
Chapter 7(documnet databse termininology) no sql for mere mortals
 
FileTable and Semantic Search in SQL Server 2012
FileTable and Semantic Search in SQL Server 2012FileTable and Semantic Search in SQL Server 2012
FileTable and Semantic Search in SQL Server 2012
 
Key-Value NoSQL Database
Key-Value NoSQL DatabaseKey-Value NoSQL Database
Key-Value NoSQL Database
 
NoSQL databases
NoSQL databasesNoSQL databases
NoSQL databases
 
Ado.net session04
Ado.net session04Ado.net session04
Ado.net session04
 
Chapter 5 design of keyvalue databses from nosql for mere mortals
Chapter 5 design of keyvalue databses from nosql for mere mortalsChapter 5 design of keyvalue databses from nosql for mere mortals
Chapter 5 design of keyvalue databses from nosql for mere mortals
 
MDF and LDF in SQL Server
MDF and LDF in SQL ServerMDF and LDF in SQL Server
MDF and LDF in SQL Server
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Database
 
Nosql
NosqlNosql
Nosql
 
ADO.NET difference faqs compiled- 1
ADO.NET difference  faqs compiled- 1ADO.NET difference  faqs compiled- 1
ADO.NET difference faqs compiled- 1
 

Destaque

Direccionamiento absoluto y relativo
Direccionamiento absoluto y relativoDireccionamiento absoluto y relativo
Direccionamiento absoluto y relativoLore Valderrama
 
2011 sandra reginamarcelinopinto_v_corr
2011 sandra reginamarcelinopinto_v_corr2011 sandra reginamarcelinopinto_v_corr
2011 sandra reginamarcelinopinto_v_corrGilberto Costa
 
Ag Leader - 2014 Product Catalog
Ag Leader - 2014 Product CatalogAg Leader - 2014 Product Catalog
Ag Leader - 2014 Product Catalogkindecoop
 

Destaque (7)

El derecho.
El derecho.El derecho.
El derecho.
 
Sentencia Diaz
Sentencia DiazSentencia Diaz
Sentencia Diaz
 
Direccionamiento absoluto y relativo
Direccionamiento absoluto y relativoDireccionamiento absoluto y relativo
Direccionamiento absoluto y relativo
 
1101 Fichas segundo periodo
1101 Fichas segundo periodo 1101 Fichas segundo periodo
1101 Fichas segundo periodo
 
Correo electronico
Correo electronicoCorreo electronico
Correo electronico
 
2011 sandra reginamarcelinopinto_v_corr
2011 sandra reginamarcelinopinto_v_corr2011 sandra reginamarcelinopinto_v_corr
2011 sandra reginamarcelinopinto_v_corr
 
Ag Leader - 2014 Product Catalog
Ag Leader - 2014 Product CatalogAg Leader - 2014 Product Catalog
Ag Leader - 2014 Product Catalog
 

Semelhante a Inb343 week2 sql server intro

Introduction to Database Management Systems
Introduction to Database Management SystemsIntroduction to Database Management Systems
Introduction to Database Management SystemsAdri Jovin
 
UNIT 5- Other Databases.pdf
UNIT 5- Other Databases.pdfUNIT 5- Other Databases.pdf
UNIT 5- Other Databases.pdfShitalGhotekar
 
Complete first chapter rdbm 17332
Complete first chapter rdbm 17332Complete first chapter rdbm 17332
Complete first chapter rdbm 17332Tushar Wagh
 
Overview of Data Base Systems Concepts and Architecture
Overview of Data Base Systems Concepts and ArchitectureOverview of Data Base Systems Concepts and Architecture
Overview of Data Base Systems Concepts and ArchitectureRubal Sagwal
 
SQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdfSQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdfNiravPanchal50
 
A STUDY ON GRAPH STORAGE DATABASE OF NOSQL
A STUDY ON GRAPH STORAGE DATABASE OF NOSQLA STUDY ON GRAPH STORAGE DATABASE OF NOSQL
A STUDY ON GRAPH STORAGE DATABASE OF NOSQLijscai
 
A Study on Graph Storage Database of NOSQL
A Study on Graph Storage Database of NOSQLA Study on Graph Storage Database of NOSQL
A Study on Graph Storage Database of NOSQLIJSCAI Journal
 
A STUDY ON GRAPH STORAGE DATABASE OF NOSQL
A STUDY ON GRAPH STORAGE DATABASE OF NOSQLA STUDY ON GRAPH STORAGE DATABASE OF NOSQL
A STUDY ON GRAPH STORAGE DATABASE OF NOSQLijscai
 
A Study on Graph Storage Database of NOSQL
A Study on Graph Storage Database of NOSQLA Study on Graph Storage Database of NOSQL
A Study on Graph Storage Database of NOSQLIJSCAI Journal
 
A Survey And Comparison Of Relational And Non-Relational Database
A Survey And Comparison Of Relational And Non-Relational DatabaseA Survey And Comparison Of Relational And Non-Relational Database
A Survey And Comparison Of Relational And Non-Relational DatabaseKarla Adamson
 
Data Never Lies Presentation for beginners in data field.pptx
Data Never Lies Presentation for beginners in data field.pptxData Never Lies Presentation for beginners in data field.pptx
Data Never Lies Presentation for beginners in data field.pptxTusharAgarwal49094
 
Utsav Mahendra : Introduction to Database and managemnet
Utsav Mahendra : Introduction to Database and managemnetUtsav Mahendra : Introduction to Database and managemnet
Utsav Mahendra : Introduction to Database and managemnetUtsav Mahendra
 
Objectivity/DB: A Multipurpose NoSQL Database
Objectivity/DB: A Multipurpose NoSQL DatabaseObjectivity/DB: A Multipurpose NoSQL Database
Objectivity/DB: A Multipurpose NoSQL DatabaseInfiniteGraph
 
Azure Data Factory
Azure Data FactoryAzure Data Factory
Azure Data FactoryHARIHARAN R
 
Lecture 2 DATABASE LANGUAGE.pptx
Lecture 2 DATABASE LANGUAGE.pptxLecture 2 DATABASE LANGUAGE.pptx
Lecture 2 DATABASE LANGUAGE.pptxsazia7
 
Data massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodesData massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodesUlf Wendel
 
Scalable relational database with SQL Azure
Scalable relational database with SQL AzureScalable relational database with SQL Azure
Scalable relational database with SQL AzureShy Engelberg
 
Relational
RelationalRelational
Relationaldieover
 

Semelhante a Inb343 week2 sql server intro (20)

Introduction to Database Management Systems
Introduction to Database Management SystemsIntroduction to Database Management Systems
Introduction to Database Management Systems
 
UNIT 5- Other Databases.pdf
UNIT 5- Other Databases.pdfUNIT 5- Other Databases.pdf
UNIT 5- Other Databases.pdf
 
Complete first chapter rdbm 17332
Complete first chapter rdbm 17332Complete first chapter rdbm 17332
Complete first chapter rdbm 17332
 
Overview of Data Base Systems Concepts and Architecture
Overview of Data Base Systems Concepts and ArchitectureOverview of Data Base Systems Concepts and Architecture
Overview of Data Base Systems Concepts and Architecture
 
SQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdfSQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdf
 
A STUDY ON GRAPH STORAGE DATABASE OF NOSQL
A STUDY ON GRAPH STORAGE DATABASE OF NOSQLA STUDY ON GRAPH STORAGE DATABASE OF NOSQL
A STUDY ON GRAPH STORAGE DATABASE OF NOSQL
 
A Study on Graph Storage Database of NOSQL
A Study on Graph Storage Database of NOSQLA Study on Graph Storage Database of NOSQL
A Study on Graph Storage Database of NOSQL
 
A STUDY ON GRAPH STORAGE DATABASE OF NOSQL
A STUDY ON GRAPH STORAGE DATABASE OF NOSQLA STUDY ON GRAPH STORAGE DATABASE OF NOSQL
A STUDY ON GRAPH STORAGE DATABASE OF NOSQL
 
A Study on Graph Storage Database of NOSQL
A Study on Graph Storage Database of NOSQLA Study on Graph Storage Database of NOSQL
A Study on Graph Storage Database of NOSQL
 
A Survey And Comparison Of Relational And Non-Relational Database
A Survey And Comparison Of Relational And Non-Relational DatabaseA Survey And Comparison Of Relational And Non-Relational Database
A Survey And Comparison Of Relational And Non-Relational Database
 
MS-ACCESS.pptx
MS-ACCESS.pptxMS-ACCESS.pptx
MS-ACCESS.pptx
 
Data Never Lies Presentation for beginners in data field.pptx
Data Never Lies Presentation for beginners in data field.pptxData Never Lies Presentation for beginners in data field.pptx
Data Never Lies Presentation for beginners in data field.pptx
 
Database systems introduction
Database systems introductionDatabase systems introduction
Database systems introduction
 
Utsav Mahendra : Introduction to Database and managemnet
Utsav Mahendra : Introduction to Database and managemnetUtsav Mahendra : Introduction to Database and managemnet
Utsav Mahendra : Introduction to Database and managemnet
 
Objectivity/DB: A Multipurpose NoSQL Database
Objectivity/DB: A Multipurpose NoSQL DatabaseObjectivity/DB: A Multipurpose NoSQL Database
Objectivity/DB: A Multipurpose NoSQL Database
 
Azure Data Factory
Azure Data FactoryAzure Data Factory
Azure Data Factory
 
Lecture 2 DATABASE LANGUAGE.pptx
Lecture 2 DATABASE LANGUAGE.pptxLecture 2 DATABASE LANGUAGE.pptx
Lecture 2 DATABASE LANGUAGE.pptx
 
Data massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodesData massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodes
 
Scalable relational database with SQL Azure
Scalable relational database with SQL AzureScalable relational database with SQL Azure
Scalable relational database with SQL Azure
 
Relational
RelationalRelational
Relational
 

Último

原版1:1定制(IC大学毕业证)帝国理工学院大学毕业证国外文凭复刻成绩单#电子版制作#留信入库#多年经营绝对保证质量
原版1:1定制(IC大学毕业证)帝国理工学院大学毕业证国外文凭复刻成绩单#电子版制作#留信入库#多年经营绝对保证质量原版1:1定制(IC大学毕业证)帝国理工学院大学毕业证国外文凭复刻成绩单#电子版制作#留信入库#多年经营绝对保证质量
原版1:1定制(IC大学毕业证)帝国理工学院大学毕业证国外文凭复刻成绩单#电子版制作#留信入库#多年经营绝对保证质量208367051
 
原版1:1定制阳光海岸大学毕业证(JCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制阳光海岸大学毕业证(JCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制阳光海岸大学毕业证(JCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制阳光海岸大学毕业证(JCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
call girls in G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in  G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in  G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
suspensionretardersystem-220726153321-060937ae.pptx
suspensionretardersystem-220726153321-060937ae.pptxsuspensionretardersystem-220726153321-060937ae.pptx
suspensionretardersystem-220726153321-060937ae.pptxbasant11731
 
Digamma / CertiCon Company Presentation
Digamma / CertiCon Company  PresentationDigamma / CertiCon Company  Presentation
Digamma / CertiCon Company PresentationMihajloManjak
 
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full NightCall Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
2024 WRC Hyundai World Rally Team’s i20 N Rally1 Hybrid
2024 WRC Hyundai World Rally Team’s i20 N Rally1 Hybrid2024 WRC Hyundai World Rally Team’s i20 N Rally1 Hybrid
2024 WRC Hyundai World Rally Team’s i20 N Rally1 HybridHyundai Motor Group
 
(Griffith毕业证)格里菲斯大学毕业证毕业证成绩单修改留信学历认证原版一比一
(Griffith毕业证)格里菲斯大学毕业证毕业证成绩单修改留信学历认证原版一比一(Griffith毕业证)格里菲斯大学毕业证毕业证成绩单修改留信学历认证原版一比一
(Griffith毕业证)格里菲斯大学毕业证毕业证成绩单修改留信学历认证原版一比一ejgeojhg
 
定制(MSVU毕业证书)圣文森山大学毕业证毕业证成绩单原版一比一定制
定制(MSVU毕业证书)圣文森山大学毕业证毕业证成绩单原版一比一定制定制(MSVU毕业证书)圣文森山大学毕业证毕业证成绩单原版一比一定制
定制(MSVU毕业证书)圣文森山大学毕业证毕业证成绩单原版一比一定制miwhjk
 
办理克莱姆森大学毕业证成绩单|购买美国文凭证书
办理克莱姆森大学毕业证成绩单|购买美国文凭证书办理克莱姆森大学毕业证成绩单|购买美国文凭证书
办理克莱姆森大学毕业证成绩单|购买美国文凭证书zdzoqco
 
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptUNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptDineshKumar4165
 
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
办理阳光海岸大学毕业证成绩单原版一比一
办理阳光海岸大学毕业证成绩单原版一比一办理阳光海岸大学毕业证成绩单原版一比一
办理阳光海岸大学毕业证成绩单原版一比一F La
 
UNIT-III-TRANSMISSION SYSTEMS REAR AXLES
UNIT-III-TRANSMISSION SYSTEMS REAR AXLESUNIT-III-TRANSMISSION SYSTEMS REAR AXLES
UNIT-III-TRANSMISSION SYSTEMS REAR AXLESDineshKumar4165
 
办理(PITT毕业证书)美国匹兹堡大学毕业证成绩单原版一比一
办理(PITT毕业证书)美国匹兹堡大学毕业证成绩单原版一比一办理(PITT毕业证书)美国匹兹堡大学毕业证成绩单原版一比一
办理(PITT毕业证书)美国匹兹堡大学毕业证成绩单原版一比一F La
 
原版1:1复刻俄亥俄州立大学毕业证OSU毕业证留信学历认证
原版1:1复刻俄亥俄州立大学毕业证OSU毕业证留信学历认证原版1:1复刻俄亥俄州立大学毕业证OSU毕业证留信学历认证
原版1:1复刻俄亥俄州立大学毕业证OSU毕业证留信学历认证jdkhjh
 
Equity & Freight Electrification by Jose Miguel Acosta Cordova
Equity & Freight Electrification by Jose Miguel Acosta CordovaEquity & Freight Electrification by Jose Miguel Acosta Cordova
Equity & Freight Electrification by Jose Miguel Acosta CordovaForth
 
如何办理(UQ毕业证书)昆士兰大学毕业证毕业证成绩单原版一比一
如何办理(UQ毕业证书)昆士兰大学毕业证毕业证成绩单原版一比一如何办理(UQ毕业证书)昆士兰大学毕业证毕业证成绩单原版一比一
如何办理(UQ毕业证书)昆士兰大学毕业证毕业证成绩单原版一比一hnfusn
 

Último (20)

原版1:1定制(IC大学毕业证)帝国理工学院大学毕业证国外文凭复刻成绩单#电子版制作#留信入库#多年经营绝对保证质量
原版1:1定制(IC大学毕业证)帝国理工学院大学毕业证国外文凭复刻成绩单#电子版制作#留信入库#多年经营绝对保证质量原版1:1定制(IC大学毕业证)帝国理工学院大学毕业证国外文凭复刻成绩单#电子版制作#留信入库#多年经营绝对保证质量
原版1:1定制(IC大学毕业证)帝国理工学院大学毕业证国外文凭复刻成绩单#电子版制作#留信入库#多年经营绝对保证质量
 
原版1:1定制阳光海岸大学毕业证(JCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制阳光海岸大学毕业证(JCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制阳光海岸大学毕业证(JCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制阳光海岸大学毕业证(JCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
call girls in G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in  G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in  G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
suspensionretardersystem-220726153321-060937ae.pptx
suspensionretardersystem-220726153321-060937ae.pptxsuspensionretardersystem-220726153321-060937ae.pptx
suspensionretardersystem-220726153321-060937ae.pptx
 
Digamma / CertiCon Company Presentation
Digamma / CertiCon Company  PresentationDigamma / CertiCon Company  Presentation
Digamma / CertiCon Company Presentation
 
Indian Downtown Call Girls # 00971528903066 # Indian Call Girls In Downtown D...
Indian Downtown Call Girls # 00971528903066 # Indian Call Girls In Downtown D...Indian Downtown Call Girls # 00971528903066 # Indian Call Girls In Downtown D...
Indian Downtown Call Girls # 00971528903066 # Indian Call Girls In Downtown D...
 
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full NightCall Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
 
2024 WRC Hyundai World Rally Team’s i20 N Rally1 Hybrid
2024 WRC Hyundai World Rally Team’s i20 N Rally1 Hybrid2024 WRC Hyundai World Rally Team’s i20 N Rally1 Hybrid
2024 WRC Hyundai World Rally Team’s i20 N Rally1 Hybrid
 
(Griffith毕业证)格里菲斯大学毕业证毕业证成绩单修改留信学历认证原版一比一
(Griffith毕业证)格里菲斯大学毕业证毕业证成绩单修改留信学历认证原版一比一(Griffith毕业证)格里菲斯大学毕业证毕业证成绩单修改留信学历认证原版一比一
(Griffith毕业证)格里菲斯大学毕业证毕业证成绩单修改留信学历认证原版一比一
 
定制(MSVU毕业证书)圣文森山大学毕业证毕业证成绩单原版一比一定制
定制(MSVU毕业证书)圣文森山大学毕业证毕业证成绩单原版一比一定制定制(MSVU毕业证书)圣文森山大学毕业证毕业证成绩单原版一比一定制
定制(MSVU毕业证书)圣文森山大学毕业证毕业证成绩单原版一比一定制
 
办理克莱姆森大学毕业证成绩单|购买美国文凭证书
办理克莱姆森大学毕业证成绩单|购买美国文凭证书办理克莱姆森大学毕业证成绩单|购买美国文凭证书
办理克莱姆森大学毕业证成绩单|购买美国文凭证书
 
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptUNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
 
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
办理阳光海岸大学毕业证成绩单原版一比一
办理阳光海岸大学毕业证成绩单原版一比一办理阳光海岸大学毕业证成绩单原版一比一
办理阳光海岸大学毕业证成绩单原版一比一
 
UNIT-III-TRANSMISSION SYSTEMS REAR AXLES
UNIT-III-TRANSMISSION SYSTEMS REAR AXLESUNIT-III-TRANSMISSION SYSTEMS REAR AXLES
UNIT-III-TRANSMISSION SYSTEMS REAR AXLES
 
办理(PITT毕业证书)美国匹兹堡大学毕业证成绩单原版一比一
办理(PITT毕业证书)美国匹兹堡大学毕业证成绩单原版一比一办理(PITT毕业证书)美国匹兹堡大学毕业证成绩单原版一比一
办理(PITT毕业证书)美国匹兹堡大学毕业证成绩单原版一比一
 
原版1:1复刻俄亥俄州立大学毕业证OSU毕业证留信学历认证
原版1:1复刻俄亥俄州立大学毕业证OSU毕业证留信学历认证原版1:1复刻俄亥俄州立大学毕业证OSU毕业证留信学历认证
原版1:1复刻俄亥俄州立大学毕业证OSU毕业证留信学历认证
 
Equity & Freight Electrification by Jose Miguel Acosta Cordova
Equity & Freight Electrification by Jose Miguel Acosta CordovaEquity & Freight Electrification by Jose Miguel Acosta Cordova
Equity & Freight Electrification by Jose Miguel Acosta Cordova
 
Hot Sexy call girls in Pira Garhi🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Pira Garhi🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Pira Garhi🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Pira Garhi🔝 9953056974 🔝 escort Service
 
如何办理(UQ毕业证书)昆士兰大学毕业证毕业证成绩单原版一比一
如何办理(UQ毕业证书)昆士兰大学毕业证毕业证成绩单原版一比一如何办理(UQ毕业证书)昆士兰大学毕业证毕业证成绩单原版一比一
如何办理(UQ毕业证书)昆士兰大学毕业证毕业证成绩单原版一比一
 

Inb343 week2 sql server intro

  • 1. INB(N)343 Advanced Data Mining and Data Warehousing Introducing SQL Server 2008 03/20/12 1
  • 2. Objectives • What is SQL Server 2008 • The key components of SQL server • SQL server 2008 databases • SQL server 2008 storage architecture 03/20/12 © Y Li: INB(N)343 week 2 2
  • 3. What’s SQL Server 2008 • It is primarily thought of as a Relational Database Management System (RDBMS). • It can be more accurately described as an Enterprise Data Platform. • It builds on many of the features that had first been incorporated in SQL server 2005. • It also provides rich reporting capabilities, powerful data analysis, and data mining. • It also has features that support asynchronous data applications, data-driven event notification and more. 03/20/12 ©Yuefeng Li :INB(N)343 week 2 3
  • 4. The key components of SQL server • Database Engine • Integration Services • Analysis Services • Reporting Services • Service Broker • Data Tier Web Services • Replication Services • Replication Services • Multiple Instances • Database Mail 03/20/12 ©Yuefeng Li :INB343 week 2 4
  • 5. Database Engine • It is the primary component of SQL Server 2008 for the efficient storage, retrieval, and manipulation of relational and XML formatted data. • It is highly optimized for transaction processing. • The Database Engine is also responsible for the controlled access and modification of data through its security subsystem. 03/20/12 ©Yuefeng Li :INB343 week 2 5
  • 6. Integration Services • SQL Server Integration Services (SSIS) is Microsoft’s enterprise class data Extract, Transform, and Load (ETL) tool, – Where the ETL process begins by defining a data sources. From the data source, you define which data you are interested in copying to a new destination. • Integration services is part of a suit of tools included in the Business Intelligent Development Studio (BIDS), – which includes add-ins for designing solutions for integration services, analysis services, and reporting services. 03/20/12 ©Yuefeng Li :INB343 week 2 6
  • 7. Analysis Services • It delivers Online Analytical Processing (OLAP) and Data Mining functionality for Business Intelligence application. • It provides a robust environment for the detailed analysis of data, through – User-created, multidimensional data structures, such as relational databases, spreadsheets, flat files, and other multidimensional sources. • The Data Mining component allows the analysis of large quantities of data by mining hidden relationships and patterns that may be of interest to an organization’s data analyst. 03/20/12 ©Yuefeng Li :INB343 week 2 7
  • 8. Reporting Services • It is a Web Service-based solution for designing, deploying, and managing flexible, dynamic web-based reports, as well as traditional paper reports. – These reports can contain information from virtually any data source. • It also allows for more savvy users to create ad hoc reports based report models. • Report models provide an abstraction of the data in the form of entities so that users are not bothered with having to know the intricate details of the actual data sources. 03/20/12 ©Yuefeng Li :INB343 week 2 8
  • 9. Notice: Compatibility Level • In the new Database window, we can use change the compatibility level using options. • Normally, we should have – Collation: <server default> – Recovery model: Full – Compatibility level: SQL Server 2008 (100) • SQL Server 2008 allows three compatibility levels: 80, 90, and 100 that correlates to SQL Server 2000, SQL Server 2005, and SQL Server 2008. • The following code will set the compatibility level of the AdventureWorks2008 database to SQL Server 2000: ALTER DATABASE AdventureWorks2008 SET COMPATIBILITY_LEVEL = 80 03/20/12 ©Yuefeng Li :INB(N)343 week 2 9
  • 10. SQL Server 2008 Database Objects • Server – The server scope encompasses all the objects that exist on the instance of SQL Server, regardless of their respective database of namespace. – The term server actually refers to the SQL Server 2008 instance. • Database – The database scope defines all the objects within a database catalog. Schemas exist in the database scope. • Schema – A schema is a namespace for database object. Each database can contain one or more schemas. All data objects in a database reside in a specific schema. 03/20/12 ©Yuefeng Li :INB343 week 2 10
  • 11. SQL Server 2008 Database Objects cont. • Object Names – Every object is identified by a 4-part, fully qualified name, with the form server.database.schema.object – The default schema is dbo. – The fully qualified name can be abbreviated – E.g., Fred’s query “select * from Person” may resolve to FITPA00034343.AdventureWorks.dbo.Person; or FITPA00034343.AdventureWorks.Person.Person – If we use the user-defined schema Person, the query will have to be rewritten to reference the appropriate schema scope: “select * from Person.Person”; or Fred’s default schema can be changed to the Person schema use AdventureWorks; GO Alter user Fred with DEFAULT_SCHEMA=Person; 03/20/12 ©Yuefeng Li :INB343 week 2 11
  • 12. The most basic object: Table • A table can be thought of as equating to an accountant’s ledger or an Excel spreadsheet. • It is made up of what is called domain data (columns) and entity data (rows). The actual data for the database is stored in the tables. • Each table definition also contains the metadata (descriptive information about data) that describes the nature of the data it is to contain. • Each column has its own set of rules about what can be stored in that column. • A violation of the rules of any one column can cause the system to reject an inserted row, an update to an existing row, or the deletion of a row. 03/20/12 ©Yuefeng Li :INB343 week 2 12
  • 13. Example of a table 03/20/12 ©Yuefeng Li :INB343 week 1 13
  • 14. Example of a table cont. • The table in Figure 1-1 is made up of five columns of data. The number of columns remains constant regardless of how much data (even zero) is in the table. • Currently, the table has fourteen records. The number of records will go up and down as we add or delete data, but the nature of the data in each record (or row) is described and restricted by the data type of the column. 03/20/12 ©Yuefeng Li :INB343 week 1 14
  • 15. Partitioning Tables • All data pages in logical units are stored in partitions. • Normally, tables are stored in a single partition defined on a single filegroup. • SQL server also provides the ability to separate large table into smaller units by horizontally partitioning the table across multiple files managed by filegroup definitions. 03/20/12 ©Yuefeng Li :INB(N)343 week 2 15
  • 16. Indexes • An index is an object that exists only within the framework of a particular table or view. • An index works much like the index does in the back of an encyclopedia. • There is some sort of lookup (or “key”) value that is sorted in a particular way and, once you have that, you are provided another key with which you can look up the actual information. • An index provides us ways of speeding the lookup of our information. 03/20/12 ©Yuefeng Li :INB343 week 2 16
  • 17. Two Indexes categories ❑ Clustered — You can have only one of these per table. – If an index is clustered, it means that the table on which the clustered index is based is physically sorted according to that index. – If you were indexing an encyclopedia, the clustered index would be the page numbers (the information in the encyclopedia is stored in the order of the page numbers). ❑ Non-clustered — You can have many of these for every table. – This is more along the lines of what you probably think of when you hear the word “index.” – This kind of index points to some other value that will let you find the data. – For our encyclopedia, this would be the keyword index at the back of the book. 03/20/12 ©Yuefeng Li :INB343 week 2 17
  • 18. Triggers • A trigger is an object that exists only within the framework of a table. • Triggers are pieces of logical code that are automatically executed when certain things (such as inserts, updates, or deletes) happen to your table. • Triggers can be used for a great variety of things, but are mainly used for either copying data as it is entered, or checking the update to make sure that it meets some criteria. 03/20/12 ©Yuefeng Li :INB(N)343 week 2 18
  • 19. Triggers: An Example • E.g. whenever a row is deleted from particular table, we might want to execute an INSERT statement: CREATE TRIGGER MyTrigger AFTER DELETE ON MyTable REFERENCING OLD ROW AS MyOldRow FOR EACH ROW INSERT INTO MyLog VALUES (‘Deleted’, MyOldRow.MyColumn1, MyOldRow.MyColumn2, CURRENT_TIMESTAMP); (Because a DELETE statement may delete more than one row, we need to say FOR EACH ROW here, so that we get one row inserted for each row deleted.) ©Yuefeng Li :INB(N)343 week 2
  • 20. Triggers: Why use triggers? • People use triggers for: – logging/audit-trail (for security and/or statistical analysis); – archiving to another table (for keeping historical data); – enforce validation-rules/constraints that cannot easily be achieved through SQL constraints (CHECK, CREATE ASSERTION, etc.). • Triggers cannot be applied to SELECT statements, although this may change in future SQL standards and implementations (to enforce better security & privacy policies). ©Yuefeng Li :INB(N)343 week 2
  • 21. What are the parts of a trigger? • Every trigger must be created with 5 parts: – a name; – a triggering event, which determines when the trigger is activated − this specifies: • the event type (Insert, Update or Delete); • for Updates: which columns are involved; • the table involved; 3. whether the trigger is fired immediately before or after the triggering event (see later slide); ©Yuefeng Li :INB(N)343 week 2
  • 22. What are the parts of a trigger? cont. 1. a trigger level, either: – row-level: the trigger is fired once for each row inserted/updated/deleted; or – statement-level: the trigger is fired once for each SQL Insert/Update/Delete statement (regardless of the number of rows affected). 2. a trigger body, i.e. the statements executed when the trigger is activated. ©Yuefeng Li :INB(N)343 week 2
  • 23. Why BEFORE and AFTER triggers? • An AFTER trigger fires after the action has completed. Both the old and new row values are "visible" to the trigger (where appropriate). • So, an AFTER trigger is good for logging and/or archiving, as shown in the earlier example. ©Yuefeng Li :INB(N)343 week 2
  • 24. Why BEFORE and AFTER triggers? • On the other hand, a BEFORE trigger fires before the action (Insert, Update or Delete) is attempted. • What use is this? Such a trigger has the ability to: – skip the action for one or more rows (i.e. prevent certain rows being inserted/updated/deleted); or – effectively change the action, (e.g. a Delete action could be turned into an Update that sets a “logically-deleted” flag in each of the rows affected); or – change the row being inserted/updated (for INSERT and UPDATE actions only) – as shown by example on next slide. • So, a BEFORE trigger is good for enforcing rules/constraints. ©Yuefeng Li :INB(N)343 week 2
  • 25. Example of a BEFORE trigger CREATE TRIGGER MyInsertTrigger BEFORE INSERT ON MyTable REFERENCING NEW ROW AS NewRow FOR EACH ROW SET NewRow.ContactPhoneNumber = COALESCE (NewRow.ContactPhoneNumber, NewRow.HomePhoneNumber); • As you can see, this trigger tries to ensure that each newly-inserted row has a (non-null) ContactPhoneNumber. – If the new row has a non-null ContactPhoneNumber already, that value will be used (i.e. unchanged). – But if the ContactPhoneNumber is null, then the HomePhoneNumber will be used (which won’t be null, we hope). ©Yuefeng Li :INB(N)343 week 2
  • 26. Constraints • A constraint is yet another object that exists only within the confines of a table. • Constraints are much like they sound; they confine the data in your table to meet certain conditions. • Constraints, in a way, compete with triggers as possible solutions to data integrity issues. They are not, however, the same thing: Each has its own distinct advantages. 03/20/12 ©Yuefeng Li :INB(N)343 week 2 26
  • 27. Triggers vs. Constraints • Remember that CHECK constraints can be specified within CREATE TABLE (and within CREATE ASSERTION). • Some DBMSs use (system-generated) triggers for handling Referential Integrity constraints, e.g. cascaded deletes/updates. – PostgreSQL does this. – It’s relatively easy to implement, but is not the most machine-efficient way to do things. ©Yuefeng Li :INB(N)343 week 2
  • 28. Triggers vs. Constraints (cont.) • Triggers are more flexible. This can be an advantage: a trigger can execute a complex SQL routine. • Constraints are more rigid. But this rigidity can be an advantage in some situations: – It’s easier for humans to see what the constraint does. – It’s easier for the DBMS to see what the constraint does, and to optimise accordingly. – Constraints can be deferred (i.e. checked at the end of a transaction, rather than during it) – triggers can’t. ©Yuefeng Li :INB(N)343 week 2
  • 29. Diagrams • A database diagram is a visual representation of the database design, including the various tables, the column names in each table, and the relationships between tables. 03/20/12 ©Yuefeng Li :INB343 week 2 29
  • 30. Views • A view is, at its core, really nothing more than a stored query, to reduce the complexity for the end user, or hiding sensitive data. • The basic form CREATE VIEW <view name> AS <SELECT statement> 03/20/12 ©Yuefeng Li :INB343 week 2 30
  • 31. Example USE Oly2000a; GO CREATE view Name_vw AS SELECT GivenName, CountryCode FROM dbo.Competitors WHERE CompetitorNum <=9000 • Notice it does not return any rows. It just lets us know that the view has been created: Command(s) completed successfully. We can run a SELECT statement against this view SELECT * FROM dbo.Name_vw 03/20/12 ©Yuefeng Li :INB343 week 2 31
  • 32. SQL Server 2008 Database • Two types of databases: system databases and user databases. • The system databases are used to store system-wide data and metadata, including master, model. msdb, and tempdb databases , as well as the hidden resources database. • User databases are created by users who have the appropriate level of permission to store application data. 03/20/12 ©Yuefeng Li :INB343 week 2 32
  • 33. SQL Server 2008 Database Storage • All system and user databases are stored in files. • There is always a minimum of two files: one data file (e.g., “mydb.mdf”) and one transaction log file (e.g., mydb_log.ldf). • When a user database is created, it must contain at least a data file, or called primary data file, which is a member of the default Primary filegroup. • More filegroups can aslo be defined. • Upon initial creation of database, one transaction log must be defined, which is used to record all modifications to the database. 03/20/12 ©Yuefeng Li :INB343 week 2 33
  • 34. SQL Server Data Types • We are going to assume that you already understand how data types work. • Normal Numerics: Integer, Decimal and Float Data Type Class Size in Nature of the data Name Bytes Bit Integer 1 The first bit data type in a table takes up 1 byte; the next 7 make use of the same byte. Bigint Integer 8 -263 to 263-1 Int Integer 4 -2,147,483,648 to 2,147,483,647 SmallInt Integer 2 -32,768 to 32,767 TinyInt Integer 1 0 to 255 Decimal or Decimal/ Varies Fixed precison and scale from -1038-1 to Numeric Numeric 1038-1. The two names are synonymous, Float Approximate Varies -1.79E+308 to 1.79E+308 Numerics 03/20/12 ©Yuefeng Li :INB343 week 2 34
  • 35. SQL Server Data Types: Money, Date/Time Data Type Name Class Size Nature of the data in Bytes Money Money 8 From -263 to 263 plus precision to four decimal places. SmallMoney Money 4 -214, 748.3648 to +214,748.3647 DateTime Date/Time 8 from January 1, 1753 to December 31, 9999 with an accuracy of three hundredths of a second. DateTime2 Date/Time Varies Supports larger date (6-8) ranges and large time-fraction precision SamllDateTime Date/Time 4 from January 1, 1900, to June 6, 2079, with an accuracy of one minute DateTimeOffset Date/Time Varies Time is stored internally as UTC time (8-10) Date Date/Time 3 from January 1, 0001 to December 31, 9999, as defined by the Gregorian calendar. Time Date/Time Varies precisions as granular as 100 nano econds (3-5) 03/20/12 ©Yuefeng Li :INB343 week 2 35
  • 36. SQL Server Data Types: Character, Unicode and Binary Data Type Class Size in Nature of the data Name Bytes Char Character Varies Fixed-length character data VarChar Character Varies Variable-length character data Text Character Varies Legacy support as of SQL Server 2005. Use varchar(max) instead! XML Character Varies Defines a character field as being for XML data Nchar Unicode Varies Fixed-length Unicode character data NVarChar Unicode Varies Variable-length Unicode character data Ntext Unicode Varies Variable-length Unicode character data. It is legacy support only Binary Binary Varies Fixed-length binary data with a maximum length of 8,000 bytes VarBinary Binary Varies Variable-length binary data with a maximum specified length of 8,000 bytes Image Binary Varies Legacy support only as of SQL Server 2005. Use varbinary(max) instead! 03/20/12 ©Yuefeng Li :INB343 week 2 36
  • 37. T-SQL • The most fundamental Transact-SQL (T-SQL) statements. • T-SQL is SQL Server’s own dialect of Structured Query Language (SQL). • The T-SQL statements that we will learn in this lecture are: ❑ SELECT ❑ INSERT ❑ UPDATE ❑ DELETE 03/20/12 ©Yuefeng Li :INB343 week 2 37
  • 38. The Basic Syntax Rules of a SELECT Statement SELECT [ALL|DISTINCT] [TOP (<expression>) [PERCENT] [WITH TIES]] <column list> [FROM <source table(s)/view(s)>] [WHERE <restrictive condition>] [GROUP BY <column name or expression using a column in the SELECT list>] [HAVING <restrictive condition based on the GROUP BY results>] [ORDER BY <column list>] [[FOR XML {RAW | AUTO | EXPLICIT | PATH [(<element>)]} [,XMLDATA][,ELEMENTS][,BINARY base 64]] [OPTION (<query hint>, [, ...n])] 03/20/12 ©Yuefeng Li :INB343 week 2 38
  • 39. Example USE Oly2000a SELECT GivenName, CountryCode, COUNT(*) AS Count FROM dbo.Competitors WHERE CompetitorNum <= 9000 GROUP BY GivenName, CountryCode ORDER BY GivenName, CountryCode; 03/20/12 ©Yuefeng Li :INB343 week 2 39
  • 40. Result 03/20/12 ©Yuefeng Li :INB343 week 2 40
  • 41. The full syntax for INSERT INSERT [TOP ( <expression> ) [PERCENT] ] [INTO] <tabular object> [(<column list>)] [ OUTPUT <output clause> ] { VALUES (<data values>) [,(<data values>)] [, …n] | <table source> | EXEC <prodecure> | DEFAULT VALUES The more basic syntax for an INSERT statement looks like this: INSERT [INTO] <table> [(<column list>)] VALUES (<data values>) [,(<data values>)] [, …n] 03/20/12 ©Yuefeng Li :INB343 week 2 41
  • 42. Examples of Insert INSERT INTO Sales (StoreCode, OrderNumber, OrderDate, Quantity, Terms, TitleID) VALUES (‘TEST’, ‘TESTORDER’, ‘01/01/1999’, 10, ‘NET 30’, 1234567); USE AdventureWorks2008; DECLARE @MyTable Table ( SalesOrderID int, CustomerID char(5) ); INSERT INTO @MyTable SELECT SalesOrderID, CustomerID FROM AdventureWorks2008.Sales.SalesOrderHeader WHERE SalesOrderID BETWEEN 44000 AND 44010; 03/20/12 ©Yuefeng Li :INB343 week 2 42
  • 43. Update Statement UPDATE [TOP ( <expression> ) [PERCENT] ] <tabular object> SET <column> = <value>[.WRITE(<expression>, <offset>, <length>)] [,<column> = <value>[.WRITE(<expression>, <offset>, <length>)]] [ OUTPUT <output clause> ] [FROM <source table(s)>] [WHERE <restrictive condition>] Let’s look at the more basic syntax: UPDATE <table name> SET <column> = <value> [,<column> = <value>] [FROM <source table(s)>] [WHERE <restrictive condition>] 03/20/12 ©Yuefeng Li :INB343 week 2 43
  • 44. Delete DELETE [TOP ( <expression> ) [PERCENT] ] [FROM] <tabular object> [ OUTPUT <output clause> ] [FROM <table or join condition>] [WHERE <search condition> | CURRENT OF [GLOBAL] <cursor name>] The basic syntax couldn’t be much easier: DELETE <table name> [WHERE <condition>] 03/20/12 ©Yuefeng Li :INB343 week 2 44
  • 45. Joins • A JOIN does just what it sounds like — it puts the information from two tables together into one result set. • We can think of a result set as being a “virtual” table. It has both columns and rows, and the columns have data types. • The various forms of the JOIN clause. These will include: ❑ INNER JOIN ❑ OUTER JOIN (both LEFT and RIGHT) ❑ FULL JOIN ❑ CROSS JOIN 03/20/12 ©Yuefeng Li :INB343 week 2 45
  • 46. INNER JOIN • The preferred code for an INNER JOIN looks something like this: SELECT <select list> FROM <first_table> <join_type> <second_table> [ON <join_condition>] 03/20/12 ©Yuefeng Li :INB343 week 2 46
  • 47. Examples of INNER JOINS SELECT * FROM dbo.Events INNER JOIN dbo.Results ON dbo.Events.EventId = dbo.Results.EventId SELECT pbe.EventId, pbe.Style, hre.CompetitorNum, hre.ElapsedTime, pp.CountryCode, pp.FamilyName FROM dbo.Events pbe INNER JOIN dbo.Results hre ON pbe.EventId = hre.EventId INNER JOIN dbo.Competitors pp ON hre.CompetitorNum = pp.CompetitorNum 03/20/12 ©Yuefeng Li :INB343 week 2 47
  • 48. Results of the first join 03/20/12 ©Y Li :ITB(N)740 week 2 48
  • 49. The result of the second join 03/20/12 ©Yuefeng Li :INB343 week 2 49
  • 50. The result of the second join cont. (208 row(s) affected) 03/20/12 ©Yuefeng Li :INB343 week 2 50
  • 51. OUTER JOINs SELECT <SELECT list> FROM <the table you want to be the “LEFT” table> <LEFT|RIGHT> [OUTER] JOIN <table you want to be the “RIGHT” table> ON <join condition> • OUTER JOINs are, as I’ve said, inclusive in nature. What specifically gets included depends on which side of the join you have emphasized. • A LEFT OUTER JOIN includes all the information from the table on the left, and a RIGHT OUTER JOIN includes all the information from the table on the right. 03/20/12 ©Yuefeng Li :INB343 week 2 51
  • 52. Examples of OUTER JOINs SELECT * FROM dbo.Events LEFT OUTER JOIN dbo.Results ON dbo.Events.EventId = dbo.Results.EventId SELECT pbe.EventId, pbe.Style, hre.CompetitorNum, hre.ElapsedTime, pp.CountryCode, pp.FamilyName FROM dbo.Events pbe LEFT OUTER JOIN dbo.Results hre ON pbe.EventId = hre.EventId LEFT OUTER JOIN dbo.Competitors pp ON hre.CompetitorNum = pp.CompetitorNum 03/20/12 ©Yuefeng Li :INB343 week 2 52
  • 53. The result of the first outer join Left Right 03/20/12 ©Y Li :ITB(N)740 week 2 53
  • 54. The result of the second outer join (214 row(s) affected) 03/20/12 ©Yuefeng Li :INB343 week 2 54
  • 55. OUTER vs. INNER • The outer join yields similar results, but with a rather important difference. • If we are joining and we don’t have a matching record for the table, then what happens? • Since it is not on the inclusive side of the JOIN (in this case, the LEFT side), SQL Server will fill in a NULL for any value that comes from the opposite side of the join if there is no match with the inclusive side of the JOIN. • In this case, all but 3 of our rows have EventIDs. What we can discern from that is that all of our Events are associated with at least one result except three (SWM411, SWM412, SWM451). 03/20/12 ©Yuefeng Li :INB343 week 1 55
  • 56. FULL JOIN • Like many things in SQL, a FULL JOIN (also known as a FULL OUTER JOIN) is basically what it sounds like — it is a matching up of data on both sides of the JOIN with everything included, no matter what side of the JOIN it is on. • A FULL JOIN is perhaps best described as what you would get if you could do a LEFT JOIN and a RIGHT JOIN in the same JOIN. You get all the records that match, based on the JOIN field(s). • You also get any records that exist only on the left side, with NULLs being returned for columns from the right side. Finally, you get any records that exist only on the right side, with NULLs being returned for columns from the left side. 03/20/12 ©Yuefeng Li :INB343 week 1 56
  • 57. Inner Join Full Outer Join Competitors Countries Competitors Countries Left Outer Join Right Outer Join Competitors Countries Competitors Countries ©Yuefeng Li :INB343 week 2
  • 58. Examples SELECT ev.EventId, re.CompetitorNum, re.ElapsedTime FROM dbo.Events ev FULL JOIN dbo.Results re ON ev.EventId = re.EventId WHERE re.CompetitorNum is NULL GO SELECT ev.EventId, re.CompetitorNum, re.ElapsedTime FROM dbo.Events ev LEFT OUTER JOIN dbo.Results re ON ev.EventId = re.EventId WHERE re.CompetitorNum is NULL GO SELECT ev.EventId, re.CompetitorNum, re.ElapsedTime FROM dbo.Events ev RIGHT OUTER JOIN dbo.Results re ON ev.EventId = re.EventId 03/20/12 ©Yuefeng Li :INB343 week 2 58
  • 59. CROSS JOIN • CROSS JOINs are very strange critters indeed. • A CROSS JOIN differs from other JOINs in that there is no ON operator and that it joins every record on one side of the JOIN with every record on the other side of the JOIN. • In short, you wind up with a Cartesian product of all the records on both sides of the JOIN. • The syntax is the same as any other JOIN, except that it uses the keyword CROSS (instead of INNER, OUTER, or FULL) and that it has no ON operator. 03/20/12 ©Yuefeng Li :INB(N)343 week 2 59
  • 60. References – Vieira, Robert Author, Beginning Microsoft SQL Server 2008 Programming, (QUT Library E book). – Chris Leiter et al., Beginning Microsoft SQL Server 2008 Administration, Wrox, Hoboken. (QUT Library E book) 03/20/12 © Y Li: INB(N)343 week 2 60

Notas do Editor

  1. InSync: Triggers