Overview

Teaching: 15 min
Exercises: 0 min
Questions
  • What is a database?

  • How are data structured within a database?

  • What are the advantages of working with data in a database?

Objectives
  • Learn the fundamentals of the relational data model

  • Begin to identify in which cases it makes sense to put your data in a database

  • Learn a bit of Structured Query Language (SQL)

  • Learn about some of the specific rules of how to structure a database

What is a database?



Motivation for using a database

The relational data model:

Structured Query Language (SQL):



Creating a database table:

CREATE TABLE seattlecrimesincidents
    ("crimesID" int,
     "Offense type" character,
     "Offense code" int,
     "Date" timestamp,
     "Location" character);

Data Types

Name Aliases Description
boolean bool logical Boolean (true/false)
character char fixed-length character string
date   calendar date (year, month, day)
double precision float8 double precision floating-point number (8 bytes)
integer int, int4 signed four-byte integer
json   JSON data
money   currency amount
timestamp   date and time (no time zone)
xml   XML data



Populating the database records:

INSERT INTO seattlecrimeincidents VALUES

    (1,'trespass', 5700,'2015-01-28 09:30:00','12XX Block of E Pike St'),
    
    (2,'larceny-theft',2300, '2015-02-21 08:24:21','15XX Block of Aurora St');
crimesID Offense type Offense code Date Location
1 tresspass 5700 2015-01-28 09:30:00 12XX Block of E Pike St
2 larceny-theft 2300 2015-02-21 08:24:21 15XX Block of Aurora St



Database rules:

All databases adhere to strict rules about how the data are structured


Normalization


crimesID Offense type Offense code Date Location
1 tresspass and burglary 5700 and 5710 2015-01-28 09:30:00 12XX Block of E Pike St
2 larceny-theft 2300 2015-02-21 08:24:21 15XX Block of Aurora St



crimesID Offense type Offense code Date Location
1 tresspass 5700 2015-01-28 09:30:00 12XX Block of E Pike St
2 burglary 5710 2015-01-28 09:30:00 12XX Block of E Pike St
3 larceny-theft 2300 2015-02-21 08:24:21 15XX Block of Aurora St



NULL Values


crimesID Offense type Offense code Date Location
1 tresspass X 2015-01-28 09:30:00 12XX Block of E Pike St
2 burglary 5710 2015-01-28 09:30:00 12XX Block of E Pike St
3 larceny-theft 2300 2015-02-21 08:24:21 15XX Block of Aurora St




Selecting Data:

SELECT "Offense type", "Offense code", "Date", "Location"
   FROM seattlecrimeincidents 
   WHERE "Offense code" = 5700;


crimesID Offense type Offense code Date Location
1 tresspass 5700 2015-01-28 09:30:00 12XX Block of E Pike St


SELECT "Offense type", "Date" 
   FROM seattlecrimeincidents;


Offense type Date
tresspass 2015-01-28 09:30:00
larceny-theft 2015-02-21 08:24:21



Functions

SELECT "Date Reported", date_part('hour', "Date Reported")
FROM seattlecrimeincidents
LIMIT 5;

Data Analysis:


crimesID Offense code Date Location Damage
1 5700 2015-01-28 09:30:00 12XX Block of E Pike St $1,220
1 5700 2015-02-12 03:25:00 1XX Block of Aloha St $11,420
2 5710 2015-01-28 09:30:00 12XX Block of E Pike St $5,389
2 5710 2015-1-02 12:31:20 12XX Block of E Pine St $15,231
3 2300 2015-02-21 08:24:21 15XX Block of Aurora St $2,405





SELECT SUM("Damage") 
   FROM seattlecrimeincidents
   GROUP BY "Offense code";
Offense code totalDamage
5700 $12,640
5710 $20,620
2300 $2,405



Column aliasing:

SELECT "Date Reported", date_part('hour', "Date Reported") AS "reported hour"
FROM seattlecrimeincidents
LIMIT 5;
Date Reported reported hour
2015-01-28 09:30:00 9.0
2015-01-28 11:05:00 11.0
2015-01-29 19:57:00 19.0
2015-01-28 15:17:00 15.0
2015-01-27 04:25:00 4.0



Joining Tables



Database Implementation:

Database Interface:


Key Points