Overview
Teaching: 15 min Exercises: 0 minQuestions
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
CREATE TABLE seattlecrimesincidents
("crimesID" int,
"Offense type" character,
"Offense code" int,
"Date" timestamp,
"Location" character);
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 |
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 |
All databases adhere to strict rules about how the data are structured
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 |
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 |
SELECT
dataSELECT "Offense type", "Offense code", "Date", "Location"
FROM seattlecrimeincidents
WHERE "Offense code" = 5700;
WHERE
clause to select specific rowscrimesID | Offense type | Offense code | Date | Location |
---|---|---|---|---|
1 | tresspass | 5700 | 2015-01-28 09:30:00 | 12XX Block of E Pike St |
this example shows how to use a comma separated list to select specific columns:
selecting data:
SELECT "Offense type", "Date"
FROM seattlecrimeincidents;
Offense type | Date |
---|---|
tresspass | 2015-01-28 09:30:00 |
larceny-theft | 2015-02-21 08:24:21 |
SELECT "Date Reported", date_part('hour', "Date Reported")
FROM seattlecrimeincidents
LIMIT 5;
SUM()
, MAX()
, MIN()
, AVG()
, COUNT()
, STDDEV()
GROUP
sets of datacrimesID | 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 |
“What is the total damage that occurred for each offense type?”
GROUP
the data by “Offense code”:
SELECT SUM("Damage")
FROM seattlecrimeincidents
GROUP BY "Offense code";
Offense code | totalDamage |
---|---|
5700 | $12,640 |
5710 | $20,620 |
2300 | $2,405 |
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 |
Key Points
Databases offer a highly structured framework for storing and rapidly manipulating data
Setting up this structure takes some time
Advantages include the ability to investigate complex relationships between data using a simple query language