Common Command Example TablesSharkTbl ID Name Street 1 Alf 111 Street 2 Barry 222 Street 3 Charlie 333 Street 4 Dirk 444 StreetWhaleTblfe ID Name Street Greeting 1 Antenna Assessor 5 Ave howdy 2 Bear Barber 6 Ave hello 3 Chorizo Chef 7 Ave hi 4 Database Diver 8 Ave salve citizen
Multi Table SELECTsThey look like this:SELECT *FROM SharkTbl, WhaleTbl;But you can run into trouble if they sharecommon column names.
Table Defined ColumnsThis is going to break...SELECT Name, Street FROM SharkTbl, WhaleTbl;...because both SharkTbl and WhaleTbl have columnsnamed Name and Street.Result:
Table Defined ColumnsFor more than one table, use this format:SELECT <tbl 1>.<col 1>, <tbl 2>.<col 2>FROM <tbl 1>, <tbl 2>Heres how it looks in action:SELECT SharkTbl.Name, WhaleTbl.StreetFROM SharkTbl, WhaleTbl;
Table Defined ColumnsIf some columns are unique and some areambiguous, you can only include tabledefinitions where you must, like so:SELECT Greeting, SharkTbl.NameFROM SharkTbl, WhaleTbl;
The "AS" Column Alias CommandYou use this to clean up table names.Structure:SELECT <tbl 1>.<col 1> AS <alias 1>FROM <tbl 1>, <tbl 2>
The "AS" Column Alias CommandExample:SELECT SharkTbl.Name AS Pudding,WhaleTbl.Street AS BrowniesFROM SharkTbl, WhaleTbl;Changes The Column Names:
JOIN Example TablesPersonTbl ID Name JobID 1 Alf 3 2 Barry 3 3 Charlie 1 4 Dirk 2JobTbl ID Name 1 Antenna Assessor 2 Bear Barber 3 Chorizo Chef 4 Database Diver
The Shape Of Things In Our World● Oceans ○ Swimming Creatures ■ Orcas ● Transient ● Resident ● Offshore ■ Sharks ● Hammer Heads ● Great White Sharks ■ Other Less Interesting Fish ○ Reefs ■ Coral ■ Cement
The Shape Of Things In Our World● Orders ○ Products ■ Product Attributes ■ Manufacturers ■ Billing Codes ■ Part Numbers ○ Addresses ○ Tracking Numbers ○ Faxes ○ Notes
The Shape Of Things In Our WorldSo, just about anything you look at can bebroken down into "A has Bs which have Cs":● As ○ have Bs ■ which in turn have Cs ○ have Fs ○ have Gs ○ have Hs
The Shape Of Things In Our WorldTo turn this pattern into databases:1. Make A, B and C into tables2. Relate the tables to one another3. Describe any object or relation of objects
The Shape Of Things In Our WorldThese three things allow table relations:1. Primary Keys2. Foreign Keys3. JOINs in your SQL statements
Primary KeysRemember that tables have IDs?Like so: ID Name 1 Alf 2 Barry 3 Charlie 4 DirkAll the IDs we have seen so far are formallyknown as Primary Keys.
Primary KeysPrimary keys are the IDs used to identify a row.PersonTbl ID Name 1 Alf 2 Barry 3 Charlie 4 Dirk ^ Primary key of the PersonTbl
Foreign KeysPrimary Keys are going to reference rows inthis tableForeign Keys are going to reference rows inanother tableSometimes called Secondary Keys
Foreign KeysThey look like this: JobID is a Foreign Key... ID Name JobID 1 Alf 3 2 Barry 3 3 Charlie 1 4 Dirk 2...and ID is the Primary Key
Foreign KeysWhy? Because: JobID talks about another table ID Name JobID 1 Alf 3 2 Barry 3 3 Charlie 1 4 Dirk 2...but ID talks about this table
Foreign KeysFormal:Foreign Keys are keys that relate back to thePrimary Keys of other tables.Boiled Down:You can use the JobID in PersonTbl to "lookup" values in JobTbl
Use A Foreign Key, Get A ValueSo, what just happened?● PersonTbls 4th row had a JobID of 2● We looked the JobTbl row with ID of 2● The name of JobTbl ID = 2 is Bear Barber● So JobID 2 is the same as Bear Barber!
Use A Foreign Key, Get A ValueBig Concept:The arrow is the JOIN action.
Use A Foreign Key, Get A ValueThe arrow drawn between PersonTbl andJobTbl represents a JOIN.A JOIN needs to know which Foreign Key inPersonTbl relates to which Primary Key inJobTbl.
Breaking Down A JOINStructure:SELECT * FROM <table 1>JOIN <table 2>ON <table 1>.<col 1> = <table 2>.<col 2>
Breaking Down A JOINSELECT * FROM <table 1>JOIN <table 2>ON <table 1>.<col 1> = <table 2>.<col 2>What each part does:SELECT...FROM - Read all cols from table 1JOIN - Read all cols from table 2 as wellON - Use these keys to link the two tables
Breaking Down A JOINIn English:Select all columns from the one or more tablesnamed where the primary key of one table isequal to the secondary key of the other.Further boiled down:Replace the JobID in PersonTbl with its namein JobTbl
Breaking Down A JOINLive SQL Example:SELECT PersonTbl.ID,PersonTbl.Name AS PersonName, JobTbl.Name AS JobNameFROM PersonTblJOIN JobTblON PersonTbl.JobID = JobTbl.ID;
Breaking Down A JOINResult:Boiled Down Concept:We replaced JobIDs with Job Names usingJOIN
Implicit vs Explicit JOIN StatementsImplicit:SELECT * FROM PersonTbl, JobTblWHERE PersonTbl.JobID = JobTbl.IDExplicit:SELECT * FROM PersonTblINNER JOIN JobTbl on JobTbl.ID = PersonTbl.JobID
Implicit vs Explicit JOIN StatementsMy examples have all be explicit.Explicit is better, because the reader does nothave to guess if or how you are joining.You write it out on this line:INNER JOIN JobTbl on JobTbl.ID = PersonTbl.JobID
JOINs Put Another WayWhen getting your head around JOINs, it isgood to hear the same thing in many differentways.Heres a lady on youtube explaining it:http://www.youtube.com/watch?v=oWHO4lJlX54
Real World Example From MadCowGoal:Query all the available sizes of the ComfortGelOriginal
Real World Example From MadCowPartsTbl PNum PName 1802 ComfortGel Original Nasal CPAP Mask with HeadgearPartsAttributeTbl PAID PNum Description 44 4266 Petite 45 4266 Small 46 4266 Medium 47 4266 Large
Real World Example From MadCowThe SQL:SELECT PartsTbl.PNum, PartAttributeTbl.PAID, PartsTbl.PName, PartAttributeTbl.Description FROM PartsTblJOIN PartAttributeTblON PartsTbl.PNum = PartAttributeTbl.PNumWHERE PartsTbl.PNum = 1802