Intro to SQL & PostgreSQL

Relational Databases

A relational database stores data in a row-based table structure (much like a spreadsheet) as opposed to a document-oriented database like MongoDB. Data from one table can relate to another, for example a table of users, and a table of cities, where you link a city from the cities table to each user in the users table.

Examples of Relational DataBase Management Systems (RDBMS):

  • PostgreSQL

  • MySQL

  • SQLite

  • Oracle DB

For this class, we will be using PostgreSQL.

Structured Query Language (SQL)

SQL is the language you use to talk to a relational database. Most RDBMSes will include language extensions specific to them but they all implement the same standard "base" language which is what we'll focus on.

Syntax

UPDATE dogs SET pats = pats + 1 WHERE id = 42;

The snippet above is a complete SQL statement. Statements are terminated with a semicolon ; and are comprised of:

Keywords

Words defined in the language

UPDATE, SET, WHERE

Identifiers

Names of tables, columns, etc.

dogs, pats, id

Clauses

Constituent components of a full statement

UPDATE dogs, SET pats = pats, WHERE id = 42

Predicates

Specifies conditions that evaluate to a Boolean

id = 42

Expressions

Results in a value

pats + 1, 42

NOTE Even though keywords in SQL are not case sensitive, the convention is to capitalize them.

  • Comments start with a --

  • Use single quotes for string values

Connect to PostgreSQL

Make sure your PostgreSQL service is running

List all databases

If you want to create a new database

Connect to the server and database

Delete a database

Create and Delete a Database from inside the psql shell

Create a new database

CREATE DATABASE test;

Connect to the new database

\c test

Delete database (NOTE: you can't drop a database you're currently connected to)

DROP DATABASE test;

PostgreSQL Data Types

The most common data types are

int

whole numbers

decimal

floats

bool

boolean

varchar(n)

small text

text

big text

timestamptz

date

Data types restrict what kind of data is allowed in a table.

Create and Delete Tables

Create a new table foo with a column name which will store text data with a maximum of 20 characters

See your new table

Delete the table

Create a better table

CRUD

CRUD is an acronym that describes the four essential database operations: Create, Read, Update, Delete.

Operation
SQL

Create

INSERT

Read

SELECT

Update

UPDATE

Delete

DELETE

Insert Data

NOTE You don't have to remember the order of the columns that you created, but you do have to match the order in the insert statement.

Create a row in the actors table

You also don't have to enter all the fields (only the ones required NOT NULL)

You can also insert multiple rows in one statement

Moar Data

Let's copy paste a few more actors so we can play around with more data.

Select Data

Update Data

Delete Data

BE CAREFUL! DON'T WRITE A DELETE STATEMENT WITHOUT A WHERE CLAUSE

Last updated

Was this helpful?