Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.

Similar presentations


Presentation on theme: "1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER."— Presentation transcript:

1 1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER VIEW + DROP VIEW Creating VIEWs in Management Studio Steen Jensen, autumn 2013

2  A JOIN joins together information from two tables into one result set  A JOIN match up one record with one or more other records to make a record, that is a superset created by the combined columns of both records 2 Joined in a one-to-one relationship

3 3 Joined in a one-to-many relationship

4  There are 4 kinds of joins: INNER JOIN OUTER JOIN (both LEFT and RIGHT) FULL JOIN CROSS JOIN  INNER JOIN is the most used  OUTER JOIN is a bit more rarely used  FULL JOIN is almost never used  CROSS JOIN is seldom used, but could be used for generating test data 4

5 5  INNER JOIN is the most common kind of JOIN  An INNER JOIN returns only records, where there are matches for the field(s) used in the JOIN NB! Bogart not included

6 6  The general syntax for an INNER JOIN:  In general avoid using the * operator for both tables in JOINS

7 7  Before making a JOIN it could be a good idea to draw a diagram showing, how the tables should be connected Person.Person ….. Businessentityid … HumanResources.Employee ….. Businessentityid … Person.BusinessEntity ….. Businessentityid …

8 8  The INNER JOIN corresponding to the previous slide:  Also see the following links: http://www.w3schools.com/sql/sql_join_inner.asp http://www.w3resource.com/sql/joins/perform-an-inner-join.php

9 9  Where an INNER JOIN is exclusive by nature, an OUTER JOIN is inclusive  The first named table is considered to be the left, and the second named table is considered to be the right  The keyword OUTER is optional, just include either LEFT or RIGHT  A LEFT OUTER JOIN includes all the information from the left table  A RIGHT OUTER JOIN includes all the information from the right table  E.g.: you want to see, what the special offers are for which products

10 10

11 11 Also see the following links: http://www.w3schools.com/sql/sql_join_left.asp http://www.w3resource.com/sql/joins/perform-a-left-join.php

12 12  A FULL JOIN matches up data on both sides of the JOIN with everything included, no matter which side of the JOIN it is on  FULL JOIN is almost never used  A FULL JOIN is 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 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 foir columns from the right side and vice versa

13 13  A CROSS JOIN differs from other JOINs, as there is no ON operator, and it joins every record on one side with every record on the other side of the JOIN

14 14  An alternative (and maybe older) version of an INNER JOIN:

15 15  Complex OUTER joins: page 106-114  Alternative OUTER / CROSS joins: page 119middle–121  UNION: page 121-125

16  Experiment making different SQL JOINS for The AdwentureWorks and/or The Amazon database  Try both INNER and OUTER JOIN  Before making the JOIN it might be a good idea to make a diagram with the connections between the primary and the foreign keys (see slide …) 16

17  A VIEW is nothing more than a stored query  You can create a simple query, that selects from only bone table and leaves some rows or columns out, or you can create a complex query that joins several tables 17

18  The general syntax:  An example:  After this the VIEW can be used just as any normal table in a SELECT 18

19  A view is never going to run as fast, as if you had just run the underlying SELECT statement directly  VIEWs exist for a reason: security or simplification for the user – just balance your need against the extra overhead  If you want to restrict some columns for a group of people, one solution might be to make an extra table – this gives some disadvantages: Disk space used twice Synchronization problems, where one table is updated and the other not Double I/O operations, as two tables have to be maintained  In this case a VIEW is a better solution 19

20  Just as the WHERE clause on a SELECT can filter the results of a query, so can a WHERE be used on a VIEW 20

21 21

22 22

23 23  DATEADD + CAST: page 361middle-365  WITH CHECK OPTION: page 365–367top

24  Just like the layout of a table can be changed with the ALTER TABLE, the SQL command ALTER VIEW can change the layout of a VIEW  Likewise the command DROP VIEW deletes one or more VIEWs 24

25  Apart from the SQL way views can also be created the GUI way  Just expand the database node and right-click Views  Follow the instructions on page 368-370 in the book 25

26  Experiment making different views for The AdwentureWorks and/or The Amazon database  Try making views both the SQL way and the GUI way (see previous slide) 26


Download ppt "1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER."

Similar presentations


Ads by Google