Upload
amos-preston
View
246
Download
4
Embed Size (px)
Citation preview
Data modeling using ER
SQL joins & views1Intro to JOINs
SQL INNER JOIN
SQL OUTER JOIN
SQL FULL JOINSQL CROSS JOINIntro to VIEWs
Simple VIEWsConsiderations about VIEWs VIEWs as filters ALTER VIEW + DROP VIEW Creating VIEWs in Management Studio
Steen Jensen, autumn 2013
Combining table data with joins 1A 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 records2
Joined in a one-to-one relationshipCombining table data with joins 23
Joined in a one-to-many relationship4 kinds of joinsThere are 4 kinds of joins:INNER JOINOUTER JOIN (both LEFT and RIGHT)FULL JOINCROSS JOIN
INNER JOIN is the most usedOUTER JOIN is a bit more rarely usedFULL JOIN is almost never usedCROSS JOIN is seldom used, but could be used for generating test data4SQL INNER JOIN 15INNER 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 includedSQL INNER JOIN 26The general syntax for an INNER JOIN:
In general avoid using the * operator for both tables in JOINS
SQL INNER JOIN 37Before making a JOIN it could be a good idea to draw a diagram showing, how the tables should be connected
Person.Person..BusinessentityidHumanResources.Employee..BusinessentityidPerson.BusinessEntity..BusinessentityidSQL INNER JOIN 48The 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
SQL OUTER JOIN 19Where 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
SQL OUTER JOIN 210
SQL OUTER JOIN 311
Also see the following links:http://www.w3schools.com/sql/sql_join_left.asphttp://www.w3resource.com/sql/joins/perform-a-left-join.php
SQL FULL JOIN12A 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
SQL CROSS JOIN13A 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
Alternative syntax for JOIN14An alternative (and maybe older) version of an INNER JOIN:
Parts, which can be skipped15Complex OUTER joins: page 106-114
Alternative OUTER / CROSS joins: page 119middle121
UNION: page 121-125Exercise in SQL JOINExperiment 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 )
16Intro to viewsA 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
17Simple viewsThe general syntax:
An example:
After this the VIEW can be used just as any normal table in a SELECT
18
Considerations about VIEWsA 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 twiceSynchronization problems, where one table is updated and the other notDouble I/O operations, as two tables have to be maintained
In this case a VIEW is a better solution
19Views as filters 1Just as the WHERE clause on a SELECT can filter the results of a query, so can a WHERE be used on a VIEW
20
Views as filters 221
More complex Views22
Parts, which can be skipped23DATEADD + CAST: page 361middle-365
WITH CHECK OPTION: page 365367top
ALTER VIEW + DROP VIEWJust 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
Creating VIEWS in Management StudioApart 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
25Exercise in VIEWExperiment 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