Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. # Domain Modeling and Databases
  2.  
  3. ## Learning Objectives
  4.  
  5. * Describe the basic parts of an ERD
  6. * Construct ERDs to model the nouns and relationships in a domain
  7. * Explain what a database is and why you would use one as opposed to other
  8. persistent storage mechanisms
  9. * Explain the difference between a database management system (R/DBMS) and a
  10. database, and name the major DBMSes
  11. * Convert an ERD into a schema and load it into SQLite3
  12.  
  13. ## Framing (5 minutes)
  14.  
  15. Mention no slides today, but I can provide brief notes / resources if desired.
  16.  
  17. We've been working with objects for a bit now, and can see some of the benefits.
  18. **What are those benefits?**
  19.  
  20. ### Think-Pair-Share (30 sec + 2 minutes + 2 minutes)
  21.  
  22. What problems do we still have working with object? In other words, what's
  23. missing? What happens when our program quits?
  24.  
  25.  
  26. While objects and OOP are great, we don't have a way to save our objects, do we?
  27.  
  28. **What happens when our program quits?**
  29.  
  30. Even if we could save our objects, how do we 'find' them?
  31.  
  32. By the end of today, we should have a solution to these problems using
  33. ActiveRecord, which is a major component of Rails. ActiveRecord uses a database
  34. to store our objects even when our program quits.
  35.  
  36. We can use it to create, retrieve, modify, and delete objects.
  37.  
  38. To get there, we need to talk about four topics:
  39. * Domain modeling
  40. * Databases
  41. * Gems
  42. * Active Record Basics
  43.  
  44.  
  45. ## Domain Modeling
  46.  
  47. ### Background - What is domain modeling? (5 minutes)
  48. **I do**
  49.  
  50. * When talking about a problem, we often think of things in terms of nouns and
  51. verbs.
  52. * The verbs are really methods in our code (so they are 'saved there')
  53. * The nouns are the objects in our program.
  54.  
  55. Often what we can do in our code is dictated by what our objects look like and
  56. how they relate to each other. So it's good practice to think through what
  57. types objects are in our program, and what attributes they have.
  58.  
  59. One tool we have for doing this is called an ERD. ERDs only capture the nouns,
  60. their attributes (instance variables), and their relationships, NOT the verbs.
  61.  
  62.  
  63. ### Class Exercise - Modeling a Library (10 minutes)
  64. **We do**
  65.  
  66. Work with students to model a library. List the sorts of nouns that we need to
  67. include.
  68.  
  69. As we build the ERD, talk about the concept of relationships (one-one, one-many,
  70. many-many)
  71.  
  72. Eg:
  73. * Books
  74. * Authors
  75. * Patrons
  76. * Shelves
  77. * Employees
  78.  
  79. Once we're done, highlight the section of the ERD that we'll be working with for
  80. today (books and authors).
  81.  
  82. ### Group Exercise - Modeling the class (5 minutes + 10 minutes to present)
  83. (3 ppl / group)
  84. **You do**
  85.  
  86. In groups of 3, choose a problem you're interested in and construct an ERD. Your
  87. ERD should have at least 3 models, and appropriate relationships and attributes.
  88.  
  89. Give each group 2 minutes to explain their ERD to the class.
  90.  
  91. ## BREAK (5 minutes)
  92.  
  93.  
  94. ## Intro to Databases
  95.  
  96. ### Background - Why DBs (5 minutes)
  97. **I DO**
  98. So we have these ERDs / models for our objects. They only describe the general
  99. attributes. When we create instances, that's where the actual 'data' is.
  100.  
  101. We need a way to store that data. How can we do that?
  102. **Ask class for a few ways to store data**
  103.  
  104. It turns out that DBs are generally a good solution to solve this problem, why?
  105.  
  106. * Fast
  107. * Efficient
  108. * Very reliable
  109. * Adaptable
  110. * Lots of features ('querying', consistency, GIS, etc)
  111.  
  112. ### What is a DB? (10 minutes)
  113.  
  114. A set of tables, where each table has a structure (columns), and the data
  115. (rows).
  116.  
  117. If you've ever used a spreadsheet, DBs are like really advanced spreadsheets.
  118.  
  119. Demonstrate by drawing the table(s) from the library db on the whiteboard.
  120.  
  121. ### DB Software (5 minutes)
  122.  
  123. It's important to mention that there are lots of different database tools.
  124. Common ones include:
  125.  
  126. * MySQL
  127. * PostgreSQL
  128. * SQLite
  129. * MS SQL Server
  130.  
  131. Each one has some pros/cons, and features, etc, but they all share something in
  132. common (SQL - Structured Query Language). These are often called 'Relational DBs'
  133.  
  134. **Why is the 'relational' important?**
  135. Consider the R in ERD...
  136.  
  137. We'll be using SQLite3 because it's easy to set up, but much of what we'll learn
  138. will transfer to other SQL DBs.
  139.  
  140. There are other types of DBs that differ in fundamental approach, such as the
  141. new NoSQL DBs (e.g. MongoDB, Cassandra, Redis)
  142.  
  143. ### Creating a DB (5 minutes)
  144.  
  145. Demonstrate by creating a simple database, e.g. pokeman:
  146.  
  147. sqlite3 pokedex
  148.  
  149. Write the following schema and use Use .read pokemon_schema.sql to read load it in.
  150.  
  151. ```sql
  152. CREATE TABLE pokemon (
  153. id INTEGER PRIMARY KEY,
  154. name VARCHAR(255),
  155. type VARCHAR(255),
  156. hp INTEGER
  157. );
  158. ```
  159.  
  160. Describe the different datatypes and why we have to define them in advance.
  161. Describe the purpose of a primary key and auto-increment.
  162.  
  163.  
  164. ### Group Exercise - Write the schema for Library (10 minutes)
  165. **YOU DO**
  166. In groups of 2, create a file folder called `my_library`.
  167. In that folder create a file called `schema.sql`.
  168. In that file, create the necessary CREATE statements to create the books and authors
  169. table from our example.
  170.  
  171. Provide students with the seed.sql file and have them load it into their DB.
  172.  
  173. ### Wrap Up - Quick demo of SQL (5 minutes)
  174.  
  175. Frame that we're not going to really be using SQL in this class, instead we'll
  176. use ActiveRecord to abstract away the DB / SQL.
  177.  
  178. Demonstrate using SELECT to find records. Demonstrate a JOIN to show how SQL can
  179. handle relationships.
  180.  
  181. ### BREAK (10 minutes)
  182.  
  183. Should be 8pm at this point!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement