Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Learning SQL

Presented at PhillyDB

  • Be the first to comment

  • Be the first to like this

Learning SQL

  1. 1. Beginning SQL Sean Collins
  2. 2. Lesson PlanTheory Relational Algebra & SetsPractice SQL SQLiteSelf-Directed Learning
  3. 3. 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
  4. 4. ExamplesUnionIntersectionSet-Difference
  5. 5. Union
  6. 6. Intersection
  7. 7. Set-Difference
  8. 8. 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.
  9. 9. 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
  10. 10. 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....
  11. 11. Things We Don’t CareAbout. All We Want Is SQL.
  12. 12. The SQL Data ModelData in an SQL database Tables Columns Rows
  13. 13. TablesTables are the “sets” of discourse in SQLTables contain data of the same type. Customers in a Customer table Orders in an Order table
  14. 14. Theory To PracticePull up all of our customers, and theirorders. UNIONPull up all of our customers who have everordered something INTERSECTION
  15. 15. ColumnsTables contain columns Columns can have different data types Dates Timestamps Integers Currency
  16. 16. Digression: KeysMost data has a unique identifier, somewhere People: Social Security Numbers Groceries: UPC/Barcodes Cars: VINSQL has a data type to give hints to thedatabase engine about these unique identifiers
  17. 17. KeysTypically a table will have a PRIMARY KEYOften, it is a automatically incrementedinteger Blog post #1 Blog post #2 ....
  18. 18. 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.
  19. 19. 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.
  20. 20. 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: is listed in of the GitHubproject