TheorySet Theory The branch of mathematics that deals with the formal properties of sets as units (without regard to the nature of their individual constituents) and the expression of other branches of mathematics in terms of sets
Why This Is ImportantA whole branch of mathematics that dealswith collections of items and operations.SQL is just one of many ways to describecollections of items and operations.Learning to think about data as discrete setsof items opens new possibilities, because youare given new operations to manipulate sets.
SQLStructured Query LanguageSQL is a language standard Different databases products have extra features tacked on to the languageUnderstand the structure of the language Everything else is just syntax
SQLiteFree and Open Source relational database LIBRARYMost SQL products are considered a “RDBMS” Relational Database Management System Which means they contain extra functionality Networking Backups Authentication Etc....
Things We Don’t CareAbout. All We Want Is SQL.
The SQL Data ModelData in an SQL database Tables Columns Rows
TablesTables are the “sets” of discourse in SQLTables contain data of the same type. Customers in a Customer table Orders in an Order table
Theory To PracticePull up all of our customers, and theirorders. UNIONPull up all of our customers who have everordered something INTERSECTION
ColumnsTables contain columns Columns can have different data types Dates Timestamps Integers Currency
Digression: KeysMost data has a unique identiﬁer, somewhere People: Social Security Numbers Groceries: UPC/Barcodes Cars: VINSQL has a data type to give hints to thedatabase engine about these unique identiﬁers
KeysTypically a table will have a PRIMARY KEYOften, it is a automatically incrementedinteger Blog post #1 Blog post #2 ....
Connecting Tables (Relations)Two tables can be related through the use ofFOREIGN KEYSCustomer table has a primary key (customer_id)Order table has a primary key (order_id) and acolumn (customer) that references the customertable.The SQL database now knows about this relation.
Foreign Keys &Referential IntegrityForeign keys are used to enforceREFERENTIAL INTEGRITYYou cannot have an order for a customerthat doesn’t exist.You cannot delete a customer unless you alsodelete their orders.
SQLite ExampleWe will now go through a hands-on exercisewith SQLite and the sample database fromGitHubA more complete example database can befound on the Chinook Project:http://chinookdatabase.codeplex.com/This is listed in Lesson_04.md of the GitHubproject