Relational Databases and Postgresql: Charles Severance
Relational Databases and Postgresql: Charles Severance
and PostgreSQL
Charles Severance
www.pg4e.com
Sequential
OLD Master NEW
Sorted Update Sorted
1970s
Merge
Transaction
s
Sorted https://en.wikipedia.org/wiki/IBM_729
Random Access
• When you can randomly access
data...
https://en.wikipedia.org/wiki/Hard_disk_drive_platter
Relational Databases
Relational databases model data by storing
rows and columns in tables. The power of
the relational database lies in its ability to
efficiently retrieve data from those tables - in
particular, where the query involves multiple
tables and the relationships between those
tables.
http://en.wikipedia.org/wiki/Relational_database
Structured Query Language
• Structured Query Language
(SQL) came out of a
government / industry
partnership
• National Institute of
Standards and Technology
(NIST)
https://youtu.be/rLUm3vst87g
SQL
Structured Query Language is the
language we use to issue commands
to the database
- Create/Insert data
- Update data
Rows /
Tuples
Tables / Relations
Common Database Systems
• Major Database Management Systems in wide use
Desktop) Database
User
D.B.A. Server
psql PostgresSQL
(Command SQL
Line)
Starting PostgreSQL Command Line
$ psql –U postgres
Password for user postgres: <password here>
psql (9.3.5, server 11.2)
Type "help" for help.
postgres=#
postgres-#
Creating a User and Database
postgres=# CREATE USER WITH PASSWORD 'secret';
CREATE ROLE
postgres=# CREATE DATABASE people WITH OWNER '';
CREATE DATABASE
postgres=# \q
https://www.postgresql.org/docs/11/sql-createuser.html
https://www.postgresql.org/docs/11/sql-createdatabase.html
Connecting to a Database
$ psql people
Password for user : <password here>
psql (9.3.5, server 11.2)
people=> \dt
No relations found.
people=>
https://www.postgresql.org/docs/11/app-psql.html
people=> CREATE TABLE users(
people(> name VARCHAR(128),
people(> email VARCHAR(128) Creating a Table
people(> );
CREATE TABLE
people=> \dt CREATE TABLE users(
List of relations
Schema | Name | Type | Owner name VARCHAR(128),
--------+-------+-------+------- email VARCHAR(128)
public | users | table |
(1 row) );
people=> \d+ users
Table "public.users"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------------------------+-----------+----------+--------------+-------------
name | character varying(128) | | extended | |
email | character varying(128) | | extended | |
Has OIDs: no
people=>
SQL: Insert
The INSERT statement inserts a row into a table
• The WHERE and ORDER BY clauses happen *before* the LIMIT / OFFSET
are applied.
• The power comes when we have more than one table and we can
exploit the relationships between the tables.
Data Types in PostgreSQL
Looking at Data Types
• Text fields (small and large)
• Numeric fields
• AUTO_INCREMENT fields
String Fields
• Understand character sets and are indexable for searching
• CHAR(n) allocates the entire space (faster for small strings where
length is known)
• Generally not used with indexing or sorting - and only then limited to a
prefix
Binary Types (rarely used)
• Character = 8 - 32 bits of information depending on character set
- INTEGER (2 Billion)
https://www.postgresql.org/docs/9.1/datatype-numeric.html
Floating Point Numbers
Floating point numbers can represent a wide range of values, but accuracy is limited.
https://www.postgresql.org/docs/11/datatype-numeric.html
Dates
• TIMESTAMP - 'YYYY-MM-DD HH:MM:SS'
(4713 BC, 294276 AD)
• DATE - 'YYYY-MM-DD'
• TIME - 'HH:MM:SS'
https://www.postgresql.org/docs/11/datatype-datetime.html
https://xkcd.com/607/
Database Keys and Indexes
AUTO_INCREMENT
Often as we make multiple DROP TABLE users;
tables and need to JOIN them
together we need an integer CREATE TABLE users (
primary key for each row so we id SERIAL,
name VARCHAR(128),
can efficiently add a reference to
email VARCHAR(128) UNIQUE,
a row in some other table as a PRIMARY KEY(id)
foreign key. );
PostgreSQL Functions
https://www.postgresql.org/docs/11/functions.html
Indexes
• As a table gets large (they always do), scanning all the data to find a
single row becomes very costly
• There are techniques to greatly shorten the scan as long as you create
data structures and maintain those structures - like shortcuts
http://en.wikipedia.org/wiki/B-tree
Hashes
A hash function is any algorithm or subroutine
that maps large data sets to smaller data sets,
called keys. For example, a single integer can
serve as an index to an array (cf. associative
array). The values returned by a hash function
are called hash values, hash codes, hash sums,
checksums, or simply hashes.
Hash functions are mostly used to accelerate
table lookup or data comparison tasks such as
finding items in a database...
http://en.wikipedia.org/wiki/Hash_function
Summary
• SQL allows us to describe the shape of data to be stored and give many
hints to the database engine as to how we will be accessing or using the
data.