M2hY7oj - QsuoWO6I - 8LLcg - Weeks 1 and 2 Introduction To SQL and Single Table SQL
M2hY7oj - QsuoWO6I - 8LLcg - Weeks 1 and 2 Introduction To SQL and Single Table SQL
Databases
Charles Severance
www.pg4e.com
OLD Sequential NEW
Master
Sorte Sorte
Update
d 1970s d
Merg
e
Transacti
ons
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
- PostgreSQL – 100% Open source, feature rich
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-
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),
Creating a
people(>
people(> );
email VARCHAR(128)
CREATE TABLE
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=>
Working with Tables and
PostgreSQL
SQL: Insert
The INSERT statement inserts a row
into a table
- INTEGER (2 Billion)
https://www.postgresql.org/docs/9.1/datatype-
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'
• Built-in PostgreSQL function NOW()
https://www.postgresql.org/docs/11/datatype-
https://xkcd.com/607/
Database Keys and Indexes
AUTO_INCREMENT
Often as we make
multiple tables and need DROP TABLE users;
to JOIN them together we
need an integer primary CREATE TABLE users (
key for each row so we id SERIAL,
name VARCHAR(128),
can efficiently add a
email VARCHAR(128) UNIQUE,
reference to a row in PRIMARY KEY(id)
some other table as a );
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
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.