key-value pairs

23
key-value pairs (the) Little Data Project littledataproject.org

Upload: littledata

Post on 25-May-2015

1.010 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Key-Value Pairs

key-value pairs

( t h e ) Little Data Projectlittledataproject.org

Page 2: Key-Value Pairs

key-value pairs

A key is a field name, an attribute, an identifier. The content of that field is its value, the data that is being identified and stored.

Page 3: Key-Value Pairs

key-value pairs

A field name, together with the data entered into that field, is a key-value pair.

Page 4: Key-Value Pairs

a unique identifier

A key is a unique identifier, and a value is the actual data that is being identified.

Page 5: Key-Value Pairs

show me an example

For instance, in a Contacts database (i.e., an address book), each record will have a field called "city".

Page 6: Key-Value Pairs

city: “Buffalo”

Every record contains the key "city", and in one particular record its value might be "Buffalo". It's pretty straightforward.

Page 7: Key-Value Pairs

city: “Toronto”

In another record in the same Contacts database, the city might be "Toronto”. Same key, different value.

Page 8: Key-Value Pairs

age 19: “yes”

Now the pair above is not well designed. The key itself holds some data, which is a no-no.

Data should be only be stored in the value.

Page 9: Key-Value Pairs

age 19: “yes”

And what if:

age 19 = "no” … ?

How old is the person then?

Page 10: Key-Value Pairs

consistency

In a relational database, each record includes the same keys. Each record is consistent: it is similar to its neighbor in structure (the keys), but different in content (the values).

Page 11: Key-Value Pairs

good questions

Consistency makes it easy to ask the database some good questions. Often we are looking for some particular value, and the key helps us find it.

Page 12: Key-Value Pairs

good questions

In the following examples, each record includes a field with the key "city", and each record has a value entered into that field. There are all sorts of things we can ask (query) the database.

Page 13: Key-Value Pairs

who lives in Buffalo?

"George Hanna", "Lisa Durante", "Carl Young"

"Buffalo" is the value listed in the field named "city" in each of these three records. In other records, another city is listed (or no city is recorded).

Page 14: Key-Value Pairs

SELECT * from Contacts WHERE city == "Buffalo"

Page 15: Key-Value Pairs

what cities are listed in the db?

"Buffalo", "Hamilton", "Dallas", "Toronto"

Across all the records in the database, these city names are mentioned in the field named "city". This database holds twenty records. "Buffalo" is listed in three different records.

Page 16: Key-Value Pairs

SELECT DISTINCT city from Contacts

Page 17: Key-Value Pairs

where does Jim Smith live?

"Dallas"

Tell the database management system to go to Jim Smith's record and return the value of the city field.

Page 18: Key-Value Pairs

SELECT city WHERE person == “Jim Smith”

Page 19: Key-Value Pairs

when did Jim Smith live in Dallas?

Can't say. But maybe if we had included more key-value pairs in each record, this query could have been answered.

beginning : "1998"ending : "2005"

Page 20: Key-Value Pairs

SELECT beginning WHERE person == "Jim Smith" & city == "Dallas"

SELECT ending WHERE person == "Jim Smith" & city == "Dallas"

Page 21: Key-Value Pairs

why did Jim Smith live in Dallas?

This database is just an address book. It does not contain the kind of data needed to answer this query. A useful design might include these keys:

workhometownspouse

Page 22: Key-Value Pairs

good design

When designing a database, we should consider what questions we will want answered. Then we can make sure we capture the data needed to answer those questions.

Database design starts with key-value pairs.

Page 23: Key-Value Pairs

( the ) Little Data Projectlittledataproject.org