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.

Relational Databases 101

412 views

Published on

A brief introduction to relational databases, SQL and ActiveRecord.

What's a SELECT, UPDATE, INSERT or DELETE query? What's an Index, a Foreign Key, a Primary Key?

Published in: Internet
  • Be the first to comment

  • Be the first to like this

Relational Databases 101

  1. 1. Relational Databases 101 Ombu Labs, August 2015
  2. 2. Think spreadsheets
  3. 3. A database is a set of interconnected relations (tables)
  4. 4. Id Username Name 1 mauro-oto Mauro 2 alanhala Alan 3 ceci Cecilia 4 etagwerker Ernesto usersTable Name Columns Rows Unique Ids
  5. 5. Id Name 1 OmbuShop 2 InfractoresBA 3 Alpha 4 Beta projects
  6. 6. Interconnected?
  7. 7. “A User has many Addresses.” For example
  8. 8. Users Addresses has many has one
  9. 9. Id Address User Id 1 Peru 212 23 2 Moldes 1333 55 3 Gorriti 332 212 4 Carranza 321 98 addresses Reference Column References
  10. 10. That is a “one to many” relationship
  11. 11. “A project has many people, and a person belongs to many projects.” For example
  12. 12. Users Projects has many has many
  13. 13. Users Projects has many has many Users_Projects has onehas one
  14. 14. That is a “many to many” relationship
  15. 15. Users Partner has one has one
  16. 16. That is a “one to one” relationship
  17. 17. Database Management Systems
  18. 18. MySQL, PostgreSQL, SQLServer, Oracle, DB2, MariaDB, etc…
  19. 19. SQL
  20. 20. Structured Query Language
  21. 21. A standardized way to CRUD records
  22. 22. SQL Queries (SELECT)
  23. 23. SELECT * FROM users
  24. 24. Id Username Name 1 mauro-oto Mauro 2 alanhala Alan 3 ceci Cecilia 4 etagwerker Ernesto users
  25. 25. SELECT id, name FROM users WHERE name = ‘Ernesto’
  26. 26. Id Name 4 Ernesto users
  27. 27. SELECT <field names separated by commas> FROM <table name> WHERE <key> = <value>
  28. 28. SQL Updates (UPDATE)
  29. 29. UPDATE users SET name = ‘Ernesto T’ WHERE name = ‘Ernesto’
  30. 30. UPDATE <table_name> SET <key> = <value>, … WHERE <key> = <value>
  31. 31. SQL Inserts (INSERT)
  32. 32. INSERT INTO users (username, name) VALUES (‘pepe’, ‘Pedro P’)
  33. 33. INSERT INTO <table_name> (<column_name_1, …) VALUES (<value_1, …)
  34. 34. SQL Deletes (DELETE)
  35. 35. DELETE FROM users WHERE username = ‘pepe’
  36. 36. DELETE FROM <table_name> WHERE <key> = <value>
  37. 37. SQL Joins
  38. 38. “Find all the users with name Ernesto who live in the Moldes St.” For example
  39. 39. Users Addresses has many has one username name address zipcode
  40. 40. SELECT id, name FROM users INNER JOIN addresses ON users.id = addresses.user_id WHERE addresses.address LIKE ‘Moldes%’ AND users.name = ‘Ernesto’
  41. 41. Constraints
  42. 42. Primary keys
  43. 43. Id Username Name 1 mauro-oto Mauro 2 alanhala Alan 3 ceci Cecilia 4 etagwerker Ernesto users
  44. 44. users: primary key (id)
  45. 45. users_projects: primary key (user_id, project_id)
  46. 46. Foreign keys
  47. 47. Id Address User Id 1 Peru 212 23 2 Moldes 1333 55 3 Gorriti 332 212 4 Carranza 321 98 addresses Reference Column Foreign Keys
  48. 48. Indexes
  49. 49. Id Username Name 1 mauro-oto Mauro 2 alanhala Alan 3 ceci Cecilia 4 etagwerker Ernesto users Unique Indexes
  50. 50. ActiveRecord Pattern
  51. 51. Users (MySql) user.rb (Rails) Table Record/row Column Row value Class Object instance Attribute name Attribute
  52. 52. Thank you!
  53. 53. Questions?

×