Introduction to Databases

Class 1 - Data Modeling

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Some "rules"

  • We are here for you!
  • Every question is important
  • Help each other
  • Have fun

Welcome!

Tell us about yourself.

  • Who are you?
  • What do you hope to get out of the class?
  • What is your favorite part about your day so far?

What is data modeling?

  • First step in database design and object oriented programming
  • Goal is to create a conceptual model of how data relate to each other
  • Exploratory process that progresses from:
    • Conceptual Models - high level domain models
    • Logical Data Models - describe logical entities and relationships
    • Physical Data Models - used to design internal database schemas

Modeling Real Things

Kinds of data

  • Strings (characters within quotes, "Hi my name is Liz.")
  • Numbers (numeral characters - 1, 3, 27, 49)
  • Boolean (true or false)
  • Arrays(Lists of anything ["Hello", 27, "Arugula"])
  • Objects (key-value pairs)

An object looks like this:


{
"name" : "value",
"property" : "value",
"age" : "35",
"weight" : "180",
"favorite_foods" : ["Artichoke", "Alphalpha", "Anchovies"],
"favorite_books" : [{"title": "Moby Dick", "author" : "Hermann Melville"}, {"title" : "Where the Wild Things Are", "author" : "Maurice Sendak"}]
}
                    

Data Comes in Hierarchies

Often, data is "nested" in "hierarchies" of objects within objects.

Methods of storing

  • Plain Text (.txt)
  • CSV (.csv)
  • JSON (JavaScript Object Notation)
  • Relational Database (SQL, Oracle)
  • Non-Relational Database (MongoDB, Cassandra, etc)

Structure


car = {
"name": "Herby",
"make": "BMW",
"model": "Bug",
"purpose": "Love",
"engineType": "Back",
"color": "Stripes",
"year": "1970"
}
					  

Might not be as helpful as...


car = {
"name": "Herby",
"make": "BMW",
"model": "Bug",
"purposes": ["Love", "Driving around", "Saving people?"],
"engine": {"location": "Back", "cylinders": 4, "fuel-injected": false, "loud": true},
"description": {"paint_profile": "Stripes", "colors": ["black", "white", "silver"], "attitude": "sassy"},
"year": "1970"
}
					

This matters a lot!

Data can limit your capabilities, or expand them.

Grouping similar pieces of data together helps you stay organized, and helps the computer use it faster and easier. It also helps engineers program things more efficiently.


if (car.engine.loud == true && car.description.attitude == "sassy") {
console.log("I think Herby the love bug is comin' down the road!");
}
            

How are things the same?

Let's try to model these books together.

What unique properties do they have? What properties do they share?

How are things the same?

What does it mean to be a book?


{
title: "",
author: "",
length: "",
ISBN: "",
cover: "",
language: "",
customer_rating: "",
tags: "",
amazon_link: "http://www.amazon.com/The-Power-Habit-What-Business/dp/1400069289/ref=sr_1_1?ie=UTF8&qid=1355257104&sr=8-1&keywords=power+of+habit"
}
                    

{
title: "",
author: "",
length: "",
ISBN: "",
cover: "",
language: "",
customer_rating: "",
tags: "",
amazon_link: "http://www.amazon.com/Hyperspace-Scientific-Parallel-Universes-Dimension/dp/0195085140/ref=tmm_hrd_title_0?ie=UTF8&qid=1355257238&sr=1-1",
amazon_link2:"http://www.amazon.com/Hyperspace-Scientific-Odyssey-Parallel-Universes/dp/0385477058/ref=wl_it_dp_o_pC_nS_nC?ie=UTF8&colid=N55WK4E5RGPM&coliid=I39ORQQR1YUM18"
}
                        

Baselines

Usually we try to go from generality - what does everything share in common, to specific - what makes everything unique?

Asking "what does something have to have at minimum to be this type of thing?" is a good way to find out a lot about your models.

Schema.org did a lot of this - check them out for some examples.

Exercise Time!

Modeling Places

In this exercise, you'll model two places for storage in a reviews website like Yelp, Google Local, or Foursquare.

We're not terribly worried about format for this. PAIR UP and don't worry about getting JSON exactly right - the important part is "name" : "value". If you get stuck trying to read something you wrote, here's a JSON Parser that will organize things for you.

Exercise Time!

Modeling Places

Exercise Time!

Modeling Places

Ask yourself these questions:

  • What properties do each of these places have? An address? A name?
  • How are they different?
  • What makes them different in the same way? (like type: grocery store, or pub, or coffee shop)
  • What information is useful, vs not useful? (phone numbers are useful, but number of bricks isn't. Unless you are building a national brick database.)
  • What is someone looking for when they look up information about this thing? What am I looking for?

Entity Modeling

  • Entity type - any type of object that we want to store data about
  • Attribute - the data that we want to keep about each entity
  • Primary key - the attribute used to identify a specific entity
  • Composite key - a primary key made up of a unique combination of attributes

Relationships

Non-relational Systems

Modeling Relationships

Or... How does a non-relational DB work?

Recommended reading:

Explaining Non-Relational Databases to My Mom

Types of Relationships

  • One-to-One: A car model is made by one company.
  • One-to-Many: One class has many students.
  • Many-to-One: Many employees work in one department.
  • Many-to-Many: Many orders have many products.


Deeper reading on Entity Relationship Modeling

Together!

Let's try to model recipes together.

How are Foods and Recipes related?
Foods can appear in multiple recipes, so it makes sense not to duplicate foods.

It's mostly decision-making

Questions to ask:

  • What does it mean to be an object? (duuuuuude.)
  • If I split these objects up, what does that mean for related data?
  • If I create a relationship, what does that mean for related data?
  • If I create a hierarchy, what does that mean for sub-objects? Related data?
  • Will this allow me to do more things later, or restrict what I can do later?
  • Is it worth the time right now to have an existential crisis about this?! (Usually not.)

Exercise Time

Modeling Movies!

Making movies takes lots of relationships - Actors, Producers, Studios - we don't want to duplicate data, so let's form some relationships!

  • First, create a new file - call it movies.json
  • Start with three arrays with four or more objects -
    Movies = [{}, {}, {}, {}]
    Actors = [{}, {}, {}, {}]
    Studios = [{}, {}, {}, {}]
                    

Exercise Time

Modeling Movies!

  • We're going to model four different movies!
  • Exercise Time

    Modeling Movies!

  • These share actors, so we're going to assign an ID to each actor, studio, and movie.
  • In each movie there are lots of actors - list only the top-billed actors for simplicity's sake. Create an array to hold just the IDs of the actors.
  • Reference the ID of the actor you want to name by looking in your list of actors, and using the ID field.
  • Do the same for the studios- reference them by ID (but you don't have to use an array)
  • Try to simplify the studios - don't distinguish between Universal Studios and Universal Studios France. We're not IMDB :)
  • Don't be an "Architecture Astronaut"



    "It is better to have a codebase you're moderately ashamed of that's full of hacks than nothing at all."
    - Ancient Native American Proverb

    Resources

    Questions?

    ?