350 likes | 462 Views
Database Design 1. CMS 476 Dr. Karl Horak, Instructor. Session 3. Week in Review Application du Jour Lecture: Delimiters Logical operators and expressions Multiple table queries Demonstration: AND, OR, NOT & Null Demonstration: Relationships & Joins Exercises. Week in Review.
E N D
Database Design 1 CMS 476 Dr. Karl Horak, Instructor
Session 3 • Week in Review • Application du Jour • Lecture: • Delimiters • Logical operators and expressions • Multiple table queries • Demonstration: AND, OR, NOT & Null • Demonstration: Relationships & Joins • Exercises
Week in Review In a word, frantic • RPT goes to Merry Old England • Web portal end-of-FY deadline • On-going portals
Lecture Topic A:Delimiters • Most compilers (and database engines) never allow variables to begin with a digit (or a minus sign). • Hence, anything starting with a digit is either: • An integer or • Floating point • Things beginning with characters can be all sorts of other stuff
Delimiters • Things with digits or +/- signs are always numeric. • Everything else needs to be characterized for the database engine. • [xyz] in Access indicates that xyz is a field name or a parameter (more on that later) • “xyz” and ‘xyz’ tell the system that xyz is a string of characters • #1/26/53# is how one indicates a date in Access • Otherwise, the database engine divides 1 by 26 by 53 and you get a mess • Actually, you get 0.0007257
String Delimiters • ‘Moby Dick’—a string • “Moby Dick”—a string • [Moby Dick]—the Moby Dick(!) field • but “Prisoner’s Dilemma” is needed for an embedded single apostrophe • ‘Prisoner’s Dilemma’ returns an error for unmatched delimiters • [First Name] will indicate a field name, parameter, or variable • Hint: Omit spaces in field names and [ ] are not needed
Dates and Times While we’re talking about dates: • ‘<’ (less than) is “before” • ‘>’ (greater than) is “after” Always remember and don’t ever forget: • Dates are stored as floating point numbers, usually based on the number of days since something like January 1, 1900 • Times are stored as decimal fractions of a day • 6:00 AM is 1/4 of a day, so its 0.25 • Some time functions are accurate to seconds • Other time functions are accurate to milliseconds
Intervals An interval has: • A start time AND • A finish time Example: #1/1/1714# <= Mydate <= #3/26/1837# or (#1/1/1714# <= Mydate) AND (Mydate <= #3/26/1837#)
Lecture Topic B:Logical Operators and Expressions • Only two values in Boolean logic: • True & False • Yes & No • On & Off • 0 & -1* • But then there’s that pesky Null thingie * Clever trick #437: Often any non-zero value is treated as True
Boolean Operators • AND—True if both are true • OR—True if either or both are true • NOT—TrueFalse & FalseTrue • XOR—Exclusive OR; rarely seen, true if either one or the other but not both are true
Venn Diagrams p = True Not(p) = True p = False NOT
AND p = True p and q q = True
OR p = True p or q q = True
XOR p = True p xor q p xor q q = True What the heck is this gray area?
Some more examples p = True B? A? D? q = True C? r = True
Boolean Logic in the QBE SELECT [IT survey].Name FROM [IT survey] WHERE ([IT survey].Staff=False AND [IT survey].A<>False) OR ([IT survey].Staff=False AND [IT survey].B<>False);
Null • An unknown value • Not 0 • Neither true nor false • Not an empty string, “” • Not ASCII 0 (or \0) • Not /dev/null • Not a Nil pointer • Frankly, its not anything except Null • Almost any operation or function on Null will return a surprising and often incorrect value • Tests for Null values • Isnull() in expressions • ‘Is Null’ in WHERE clauses
Operations on Null • A = Null is neither True nor False • A <> Null is neither True nor False • Null and False = False • Null or True = True
Lecture Topic C:Multi-table Queries—Joins • Observation #1—You can keep adding tables (and queries) to the QBE grid What is the percentage of Iranian enrichment capacity relative to the U.S?
Joining Tables • Observation #2—You can drag-and-drop lines connecting tables in the QBE grid What are the enrichment facilities in Western Europe?
Relationships • Observation #3—You can connect more than two tables in the QBE grid What are the enrichment facilities in Europe?
Key Fields • The “key” to joining tables is to have common information that references the same data in each table. • A “primary key” is a unique identifier for a record. • A “foreign key” is used as a look-up value and matches a primary key in another table.
Compound Keys • Keys may be made up of more than one field [Last Name] + [First Name] [State] + [City] • Keys may include expressions Left([Last Name],1)
Relationships • A “relationship” means that there are corresponding primary and foreign keys between tables. • Temporary relationships may be created “on the fly” in the QBE • Permanent relationships may be created with the Tools | Relationships
Joining Tables with Where Clauses* SELECT [EFAC data].[Plant Name/Location], MemberStates.IAEAregion FROM [EFAC data], MemberStates WHERE [EFAC data].Country = MemberStates.Country; * Access will automatically convert these into JOIN statements
Inner Joins SELECT [EFAC data].[Plant Name/Location], MemberStates.IAEAregion FROM [EFAC data] INNER JOIN MemberStates ON [EFAC data].Country = MemberStates.Country;
Outer Joins • May be either Left or Right Joins SELECT [EFAC data].[Plant Name/Location], MemberStates.Country FROM [EFAC data] LEFT JOIN MemberStates ON [EFAC data].Country = MemberStates.Country;
Use the Force, Luke Inner Join Left Join
Kick it up another notch NB: All arrows “point” in the same direction.
Gives you… SELECT [EFAC data].[Plant Name/Location], Continents.Continent FROM ([EFAC data] LEFT JOIN MemberStates ON [EFAC data].Country = MemberStates.Country) LEFT JOIN Continents ON MemberStates.IAEAregion = Continents.IAEAregion;
Next Week • Database Design & Relationships • Modeling Relationships • Entity-Relationship (ER) Diagrams • Normalization • 3rd Normal Form • Object-Role Modeling for perfect 3NF
Demonstration • Boolean expressions • Date logic • Relationships—Joins http://70.56.215.209/khorak/CSF/SQLExercises.mdb (Download new version—lots of new tables are included.)
Exercises and Q&A • List all the inventions created between 1835 and 1845 inclusive along with their inventors. • Which was invented first, the microscope or the thermometer? • Who invented the seed drill? (What the heck is a ‘seed drill’ anyway?) • Did DeGuerre (of DeGuerrotype fame) invent photography? • Claude Bernard is rarely credited with inventing what process with a more famous colleague?
More Exercises • List the names of those in IT who are also in Distribution Group A. • List the names of staff who are characterized as ‘B’. • List all those in ‘C’ or those who did not respond (neither in ‘A’, ‘B’, nor ‘C’). • Who has a ‘K’ or ‘k’ in their name? • Provide a list of persons sorted by first name.
Still More Exercises • List employees and their department names and locations. • List all departments, including one without any current employees. • Draw a map of the five hypothetical countries showing which ones are adjacent. • In the Inventions table, write a query that will fill in the columns Year and Inventor from the original Scratch field (copied out of Wikipedia)