Pythonlearn 15 Databases
Pythonlearn 15 Databases
and SQLite
Charles Severance
http://sqlitebrowser.org/
Sequential
OLD Master NEW
Sorted Update Sorted
1970s
Merge
Transactions
Sorted
https://en.wikipedia.org/wiki/IBM_729
Random Access
• When you can randomly
access data...
http://en.wikipedia.org/wiki/Relational_database
Terminology
• Database - contains many tables
Tables /
Relations
SQL
Structured Query Language is the language we use to issue
commands to the database
- Retrieve data
- Update data
- Delete data
http://en.wikipedia.org/wiki/SQL
Input
Files Python Database
SQL
Programs File
R SQL
Output
Files
Excel SQLite
You Browser
D3.js
Web Applications w/ Databases
• Application Developer - Builds the logic for the application, the look
and feel of the application - monitors the application for problems
SQL
Developer
Database
DBA Tools
Database Administrator
A database administrator (DBA) is a person responsible for the
design, implementation, maintenance, and repair of an
organization’s database. The role includes the development and
design of database strategies, monitoring and improving
database performance and capacity, and planning for future
expansion requirements. They may also plan, coordinate, and
implement security measures to safeguard the database.
http://en.wikipedia.org/wiki/Database_administrator
Database Model
A database model or database schema is the structure
or format of a database, described in a formal language
supported by the database management system. In
other words, a “database model” is the application of a
data model when used in conjunction with a database
management system.
http://en.wikipedia.org/wiki/Database_model
Common Database Systems
• Three major Database Management Systems in wide use
- Oracle - Large, commercial, enterprise-scale, very very tweakable
- MySql - Simpler but very fast and scalable - commercial open source
- SqlServer - Very nice - from Microsoft (also Access)
• Many other smaller projects, free and open source
- HSQL, SQLite, Postgres, ...
SQLite is in Lots of Software...
http://www.sqlite.org/famous.html
SQLite Browser
• SQLite is a very popular database - it is free and fast and small
• SQLite Browser allows us to directly manipulate SQLite files
• http://sqlitebrowser.org/
• SQLite is embedded in Python and a number of other languages
http://sqlitebrowser.org/
Lets Make a Database
https://www.py4e.com/lectures3/Pythonlearn-15-Database-Handout.txt
Start Simple - A Single Table
- Retrieve data
- Update data
- Delete data
http://en.wikipedia.org/wiki/SQL
SQL: Insert
The Insert statement inserts a row into a table
• The power comes when we have more than one table and we can
exploit the relationships between the tables
Complex Data Models and
Relationships
http://en.wikipedia.org/wiki/Relational_model
Database Design
• Database design is an art form of its own with particular skills and
experience
• Our goal is to avoid the really bad mistakes and design clean and
easily understood databases
• Basic Rule: Don’t put the same string data in twice - use a
relationship instead
• When there is one thing in the “real world” there should be one
copy of that thing in the database
Track Len Artist Album Genre Rating Count
For each “piece of info”...
• Is the column an object or an attribute of Len Album
another object? Genre
Artist Rating
• Once we define objects, we need to define
the relationships between objects Track
Count
Track
Album
Artist
Genre
Rating
Len
Count
Artist Track
Track
Album belongs-to Rating
Artist Len
Album Count
Genre belongs-to
Rating
Len belongs-to
Genre
Count
Artist Track
belongs-to Rating
Len
Album Count
belongs-to
Genre belongs-to
Representing Relationships
in a Database
Database Normalization (3NF)
• There is *tons* of database theory - way too much to understand
without excessive predicate calculus
• Do not replicate data - reference data - point at data
• Use integers for keys and for references
• Add a special “key” column to each table which we will make
references to. By convention, many programmers call this
column “id”
http://en.wikipedia.org/wiki/Database_normalization
We want to keep track of which band is the “creator” of each music track...
Album
Three Kinds of Keys
• Primary key - generally an integer auto-
increment field Album
id
• Logical key - What the outside world uses
for lookup title
artist_id
• Foreign key - generally an integer key ...
pointing to a row in another table
Key Rules
Best practices User
id
• Never use your logical key as the primary login
key password
name
• Logical keys can and do change, albeit email
slowly created_at
modified_at
• Relationships that are based on matching login_at
string fields are less efficient than integers
Foreign Keys
• A foreign key is when a table has a Album
Artist
column that contains a key which id
id
points to the primary key of another title
name
table. artist_id
...
...
• When all primary keys are integers,
then all foreign keys are integers - this
is good - very good
Relationship Building (in tables)
Artist Track
Rating
belongs-to Len
Count
Album belongs-to
Genre belongs-to
belongs-to Track
Album Title
Rating
Len Track
Count id
title
Album
Table rating
id
Primary key len
Logical key title
count
Foreign key
album_id
Artist Track
id Album id
name id title
title rating
Table artist_id len
Primary key
Logical key count
Foreign key album_id
Genre
genre_id
id
Naming FK artist_id is a
convention name
CREATE TABLE Genre (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT
)
CREATE TABLE Album (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
artist_id INTEGER,
title TEXT
)
Album
Genre
Artist
Using Join Across Tables
http://en.wikipedia.org/wiki/Join_(SQL)
Relational Power
• By removing the replicated data and replacing it with references to
a single copy of each bit of data we build a “web” of information
that the relational database can read through very quickly - even
for very large amounts of data
• Often when you want some data it comes from a number of tables
linked by these foreign keys
The JOIN Operation
• The JOIN operation links across several tables as part of a select
operation
• You must tell the JOIN how to use the keys that make the
connection between the tables using an ON clause
Album
Artist
https://en.wikipedia.org/wiki/Many-to-many_(data_model)
Review:
belongs-to Track One to
Album Title Many
One Many Rating
Len Track
Count
id
Table
Primary key title
Album
Logical key One rating
Foreign key id
len
title Many
count
album_id
https://en.wikipedia.org/wiki/One-to-many_(data_model)
One One Many
Many
https://en.wikipedia.org/wiki/One-to-many_(data_model)
Many to Many
https://en.wikipedia.org/wiki/Many-to-many_(data_model)
member-of
User
Course
name
title Many Many
email
User
Course Member
Many id
id user_id
Many One name
title One course_id
email
https://en.wikipedia.org/wiki/Many-to-many_(data_model)
CREATE TABLE User (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT UNIQUE,
email TEXT
)
• By normalizing the data and linking it with integer keys, the overall
amount of data which the relational database must scan is far
lower than if the data were simply flattened out
• The key is to have one copy of any data element and use relations
and joins to link the data to multiple places