SlideShare a Scribd company logo
Introduction to Databases with PostgreSQL
Gabrielle Roth
PDXPUG & FreeGeek
Nov 9, 2010
Outline
Intro to Databases (discussion)
PostgreSQL (discussion)
psql (hands-on)
SQL (hands-on)
...break somewhere in here, 7:30...
Practice db + more SQL (hands-on)
Basic Admin + GUI Tools (discussion)
Questions
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 2 / 1
Introductions
__ __
/ ~~~/  . o O ( Hi! )
,----( oo )
/ __ __/
/| ( |(
^  /___ / |
|__| |__|-"
Thanks to Hayley J Wakenshaw for the Elephant
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 3 / 1
Databases!
A place to keep your data!
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 4 / 1
Relational databases
Based on Relational Calculus
Data is stored in ”relations”
”Normalized”
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 5 / 1
PostgreSQL
- How do you say that?
- It’s the database *server*
- We think it’s the best.
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 6 / 1
Get connected.
chmod 600 /path/to/id_pdxpug
ssh -i /path/to/id_pdxpug pdxpug@207.173.203.228
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 7 / 1
Pg command-line interface
- psql
- - -help
- psql -U [username] -d pdxpug
- h
- ?
- other useful commands
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 8 / 1
SQL
- Declarative programming language
- ...let’s do ”Hello World”.
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 9 / 1
Hello, World.
SELECT ’Hello, World.’;
...oh yeah, and there’s command-completion, too.
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 10 / 1
CREATE TABLE
CREATE TABLE animals
(name varchar(32) primary key,
skin varchar(32),
legs int);
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 11 / 1
INSERT
INSERT INTO animals
VALUES (’cat’, ’fur’, ’4’);
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 12 / 1
multi-valued INSERT
insert into animals
values
(’dog’, ’fur’, ’4’),
(’bird’, ’feathers’, ’2’),
(’snake’, ’scales’, ’0’);
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 13 / 1
SELECT
SELECT * FROM animals;
SELECT name, legs FROM animals;
SELECT * FROM animals
ORDER BY name;
SELECT count(*) FROM animals;
SELECT SUM(legs) FROM animals;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 14 / 1
UPDATE
UPDATE animals
SET name = ’kitty’ WHERE name = ’cat’;
SELECT * FROM animals;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 15 / 1
Transactions
BEGIN;
...your stuff...
...check your stuff with a SELECT...
ROLLBACK or COMMIT as desired
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 16 / 1
DELETE
BEGIN;
DELETE FROM animals WHERE legs=2;
SELECT * FROM animals;
[ROLLBACK or COMMIT]
SELECT * FROM animals;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 17 / 1
Exercises
- add an animal of your choice
- update its name
- delete all animals with four legs
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 18 / 1
MOAR TABLEZ
www.postgresqlguide.com/postgresql-sample-database.aspx
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 19 / 1
ERDs
- Entity-Relationship Diagrams
- Graphical representation of the relations
- ...and how they’re connected (JOINed)
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 20 / 1
The time has come, the walrus said, to talk of many things.
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 21 / 1
Load ’er up!
i /home/pdxpug/create_tables.sql
i /home/pdxpug/populate_tables.sql
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 22 / 1
Working with the sample db
- d
- what are these seq relations?
- d item
- s
- e
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 23 / 1
DISTINCT
SELECT fname, lname FROM customer
SELECT count(lname) FROM customer;
SELECT DISTINCT lname FROM customer;
SELECT count(DISTINCT(lname)) FROM customer;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 24 / 1
- PRIMARY vs FOREIGN keys
- Natural vs Surrogate keys
- JOINs
- pset null ’[null]’
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 25 / 1
JOINs
SELECT * FROM item;
SELECT * FROM stock;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i
JOIN stock s ON i.item_id = s.item_id;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i
LEFT JOIN stock s ON i.item_id = s.item_id;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i
RIGHT JOIN stock s ON i.item_id = s.item_id;Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 26 / 1
...and the dreaded Cartesian JOIN
SELECT * FROM item;
SELECT * FROM stock;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i, stock s;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i
CROSS JOIN stock s;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 27 / 1
VIEWs and TEMP TABLEs
CREATE VIEW my_view AS SELECT field FROM table;
vs.
CREATE TEMP TABLE my_temp_table AS SELECT field FROM table;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 28 / 1
VIEWs and TEMP TABLEs
CREATE VIEW view_in_stock AS
SELECT i.item_id, i.description, s.quantity
FROM item i
LEFT JOIN stock s ON i.item_id=s.item_id;
SELECT * FROM view_in_stock;
try:
pset null ’0’
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 29 / 1
subSELECTs
SELECT date_placed FROM orderinfo
WHERE customer_id IN
(SELECT customer_id FROM customer
WHERE lname = ’Matthew’);
SELECT date_placed FROM orderinfo
WHERE customer_id IN
(SELECT customer_id FROM customer
WHERE (fname, lname) = (’Alex’,’Matthew’));
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 30 / 1
Exercises
1 How many items are currently in stock?
2 In which towns do we have customers?
3 Experiment with RIGHT JOIN with the original sample queries.
4 What are the barcodes for each item?
5 What is the name + total ”our cost” value of each item currently in stock?
6 What is the name + total ”selling cost” value of each item currently in stock?
7 What are the total ”our cost” and ”sell cost” values of all items in stock?
8 How much was shipping on each order?
9 What items were ordered by people with the last name ”Stones”?
10 How much was the total shipping per person?
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 31 / 1
Basic Admin
- initdb
- PGDATA
- postgresql.conf
- pg hba.conf
- stop/start/restart/reload
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 32 / 1
Basic Admin
- SELECT version();
- CREATE ROLE
- backups and upgrades
- pg dump, pg dumpall, pg upgrade
- logs
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 33 / 1
GUI Tools
- pHpPgAdmin
- pgAdminIII
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 34 / 1
Extras
- Natural keys vs Surrogate keys
- indexes
GRANT USAGE ON SCHEMA [schema] TO [otheruser];
GRANT SELECT ON [table] TO [otheruser];
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 35 / 1
Book giveaway!
SELECT (random() * 100);
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 36 / 1
Where to get help
- www.postgresql.org/community/lists/
- #postgresql
- PDXPUG
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 37 / 1
Reading List
www.postgresql.org/docs
Manga Guide to Databases Takahashi, Mana
Database Design for Mere Mortals Hernandez, Michael J
SQL for Smarties Celko, Joe
Introduction to Database Systems Date, CJ
Relational Model for Database Management Codd, EF
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 38 / 1
__ __
/ ~~~/  . o O ( Thank you! )
,----( oo )
/ __ __/
/| ( |(
^  /___ / |
|__| |__|-"
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 39 / 1

More Related Content

What's hot (20)

PPT
Database design
Jennifer Polack
 
PPT
Files Vs DataBase
Dr. C.V. Suresh Babu
 
PPT
Database schema
HerbertSemana1
 
PPT
SQL DDL
Vikas Gupta
 
PPTX
introduction to NOSQL Database
nehabsairam
 
PDF
Microsoft SQL Server - Files and Filegroups
Naji El Kotob
 
PPTX
Postgresql
NexThoughts Technologies
 
PPT
Introduction to-sql
BG Java EE Course
 
ODP
Ms sql-server
Md.Mojibul Hoque
 
PDF
Integrity constraints in dbms
Vignesh Saravanan
 
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
PPTX
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
PPTX
SQL Queries Information
Nishant Munjal
 
PDF
PostgreSQL Deep Internal
EXEM
 
PPTX
Hadoop File system (HDFS)
Prashant Gupta
 
PPTX
Object oriented database
Md. Hasan Imam Bijoy
 
PPTX
Data abstraction in DBMS
Papan Sarkar
 
PPTX
The Relational Database Model
Shishir Aryal
 
PPTX
Relational database
Megha Sharma
 
Database design
Jennifer Polack
 
Files Vs DataBase
Dr. C.V. Suresh Babu
 
Database schema
HerbertSemana1
 
SQL DDL
Vikas Gupta
 
introduction to NOSQL Database
nehabsairam
 
Microsoft SQL Server - Files and Filegroups
Naji El Kotob
 
Introduction to-sql
BG Java EE Course
 
Ms sql-server
Md.Mojibul Hoque
 
Integrity constraints in dbms
Vignesh Saravanan
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
SQL Queries Information
Nishant Munjal
 
PostgreSQL Deep Internal
EXEM
 
Hadoop File system (HDFS)
Prashant Gupta
 
Object oriented database
Md. Hasan Imam Bijoy
 
Data abstraction in DBMS
Papan Sarkar
 
The Relational Database Model
Shishir Aryal
 
Relational database
Megha Sharma
 

Similar to Introduction to PostgreSQL (20)

PPTX
Pig workshop
Sudar Muthu
 
PDF
Old Oracle Versions
Jeffrey Kemp
 
PDF
Pg big fast ugly acid
Federico Campoli
 
PDF
Don't panic! - Postgres introduction
Federico Campoli
 
PDF
Massively Parallel Process with Prodedural Python by Ian Huston
PyData
 
PDF
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
arihantmobileselepun
 
PDF
Hitchikers guide handout
Federico Campoli
 
PPT
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
PPT
Kill the DBA
Knut Haugen
 
PDF
Rails in the enterprise
alexrothenberg
 
PDF
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Sumeet Singh
 
PDF
Getting Started with Hadoop
Josh Devins
 
PPTX
Propel: A Open Source ORM Model and MVC Framework
Sachinkumar Durge
 
PDF
Storm - As deep into real-time data processing as you can get in 30 minutes.
Dan Lynn
 
PPT
Data-Intensive Scalable Science
University of Washington
 
PDF
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Robert Metzger
 
PDF
How to write better code: in-depth best practices for writing readable, simpl...
Jane Chung
 
PDF
How to write better code: in-depth best practices for writing readable, simpl...
Oursky
 
PDF
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
Pig workshop
Sudar Muthu
 
Old Oracle Versions
Jeffrey Kemp
 
Pg big fast ugly acid
Federico Campoli
 
Don't panic! - Postgres introduction
Federico Campoli
 
Massively Parallel Process with Prodedural Python by Ian Huston
PyData
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
arihantmobileselepun
 
Hitchikers guide handout
Federico Campoli
 
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
Kill the DBA
Knut Haugen
 
Rails in the enterprise
alexrothenberg
 
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Sumeet Singh
 
Getting Started with Hadoop
Josh Devins
 
Propel: A Open Source ORM Model and MVC Framework
Sachinkumar Durge
 
Storm - As deep into real-time data processing as you can get in 30 minutes.
Dan Lynn
 
Data-Intensive Scalable Science
University of Washington
 
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Robert Metzger
 
How to write better code: in-depth best practices for writing readable, simpl...
Jane Chung
 
How to write better code: in-depth best practices for writing readable, simpl...
Oursky
 
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
Ad

More from Mark Wong (20)

PDF
OHAI, my name is Chelnik! PGCon 2014 Mockumentary
Mark Wong
 
PDF
OHAI, my name is Chelnik! Postgres Open 2013 Report
Mark Wong
 
PDF
collectd & PostgreSQL
Mark Wong
 
PDF
Android & PostgreSQL
Mark Wong
 
PDF
PGTop for Android: Things I learned making this app
Mark Wong
 
PDF
Developing PGTop for Android
Mark Wong
 
PDF
Pg in-the-brazilian-armed-forces-presentation
Mark Wong
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PDF
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
Mark Wong
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PDF
Filesystem Performance from a Database Perspective
Mark Wong
 
PDF
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
Mark Wong
 
PDF
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
Mark Wong
 
PDF
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
Mark Wong
 
PDF
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
Mark Wong
 
PDF
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
Mark Wong
 
PDF
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
Mark Wong
 
PDF
Linux Filesystems, RAID, and more
Mark Wong
 
PDF
pg_top is 'top' for PostgreSQL
Mark Wong
 
OHAI, my name is Chelnik! PGCon 2014 Mockumentary
Mark Wong
 
OHAI, my name is Chelnik! Postgres Open 2013 Report
Mark Wong
 
collectd & PostgreSQL
Mark Wong
 
Android & PostgreSQL
Mark Wong
 
PGTop for Android: Things I learned making this app
Mark Wong
 
Developing PGTop for Android
Mark Wong
 
Pg in-the-brazilian-armed-forces-presentation
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
Filesystem Performance from a Database Perspective
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
Mark Wong
 
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
Mark Wong
 
Linux Filesystems, RAID, and more
Mark Wong
 
pg_top is 'top' for PostgreSQL
Mark Wong
 
Ad

Introduction to PostgreSQL

  • 1. Introduction to Databases with PostgreSQL Gabrielle Roth PDXPUG & FreeGeek Nov 9, 2010
  • 2. Outline Intro to Databases (discussion) PostgreSQL (discussion) psql (hands-on) SQL (hands-on) ...break somewhere in here, 7:30... Practice db + more SQL (hands-on) Basic Admin + GUI Tools (discussion) Questions Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 2 / 1
  • 3. Introductions __ __ / ~~~/ . o O ( Hi! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" Thanks to Hayley J Wakenshaw for the Elephant Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 3 / 1
  • 4. Databases! A place to keep your data! Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 4 / 1
  • 5. Relational databases Based on Relational Calculus Data is stored in ”relations” ”Normalized” Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 5 / 1
  • 6. PostgreSQL - How do you say that? - It’s the database *server* - We think it’s the best. Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 6 / 1
  • 7. Get connected. chmod 600 /path/to/id_pdxpug ssh -i /path/to/id_pdxpug [email protected] Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 7 / 1
  • 8. Pg command-line interface - psql - - -help - psql -U [username] -d pdxpug - h - ? - other useful commands Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 8 / 1
  • 9. SQL - Declarative programming language - ...let’s do ”Hello World”. Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 9 / 1
  • 10. Hello, World. SELECT ’Hello, World.’; ...oh yeah, and there’s command-completion, too. Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 10 / 1
  • 11. CREATE TABLE CREATE TABLE animals (name varchar(32) primary key, skin varchar(32), legs int); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 11 / 1
  • 12. INSERT INSERT INTO animals VALUES (’cat’, ’fur’, ’4’); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 12 / 1
  • 13. multi-valued INSERT insert into animals values (’dog’, ’fur’, ’4’), (’bird’, ’feathers’, ’2’), (’snake’, ’scales’, ’0’); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 13 / 1
  • 14. SELECT SELECT * FROM animals; SELECT name, legs FROM animals; SELECT * FROM animals ORDER BY name; SELECT count(*) FROM animals; SELECT SUM(legs) FROM animals; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 14 / 1
  • 15. UPDATE UPDATE animals SET name = ’kitty’ WHERE name = ’cat’; SELECT * FROM animals; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 15 / 1
  • 16. Transactions BEGIN; ...your stuff... ...check your stuff with a SELECT... ROLLBACK or COMMIT as desired Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 16 / 1
  • 17. DELETE BEGIN; DELETE FROM animals WHERE legs=2; SELECT * FROM animals; [ROLLBACK or COMMIT] SELECT * FROM animals; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 17 / 1
  • 18. Exercises - add an animal of your choice - update its name - delete all animals with four legs Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 18 / 1
  • 19. MOAR TABLEZ www.postgresqlguide.com/postgresql-sample-database.aspx Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 19 / 1
  • 20. ERDs - Entity-Relationship Diagrams - Graphical representation of the relations - ...and how they’re connected (JOINed) Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 20 / 1
  • 21. The time has come, the walrus said, to talk of many things. Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 21 / 1
  • 22. Load ’er up! i /home/pdxpug/create_tables.sql i /home/pdxpug/populate_tables.sql Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 22 / 1
  • 23. Working with the sample db - d - what are these seq relations? - d item - s - e Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 23 / 1
  • 24. DISTINCT SELECT fname, lname FROM customer SELECT count(lname) FROM customer; SELECT DISTINCT lname FROM customer; SELECT count(DISTINCT(lname)) FROM customer; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 24 / 1
  • 25. - PRIMARY vs FOREIGN keys - Natural vs Surrogate keys - JOINs - pset null ’[null]’ Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 25 / 1
  • 26. JOINs SELECT * FROM item; SELECT * FROM stock; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i JOIN stock s ON i.item_id = s.item_id; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i LEFT JOIN stock s ON i.item_id = s.item_id; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i RIGHT JOIN stock s ON i.item_id = s.item_id;Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 26 / 1
  • 27. ...and the dreaded Cartesian JOIN SELECT * FROM item; SELECT * FROM stock; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i, stock s; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i CROSS JOIN stock s; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 27 / 1
  • 28. VIEWs and TEMP TABLEs CREATE VIEW my_view AS SELECT field FROM table; vs. CREATE TEMP TABLE my_temp_table AS SELECT field FROM table; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 28 / 1
  • 29. VIEWs and TEMP TABLEs CREATE VIEW view_in_stock AS SELECT i.item_id, i.description, s.quantity FROM item i LEFT JOIN stock s ON i.item_id=s.item_id; SELECT * FROM view_in_stock; try: pset null ’0’ Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 29 / 1
  • 30. subSELECTs SELECT date_placed FROM orderinfo WHERE customer_id IN (SELECT customer_id FROM customer WHERE lname = ’Matthew’); SELECT date_placed FROM orderinfo WHERE customer_id IN (SELECT customer_id FROM customer WHERE (fname, lname) = (’Alex’,’Matthew’)); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 30 / 1
  • 31. Exercises 1 How many items are currently in stock? 2 In which towns do we have customers? 3 Experiment with RIGHT JOIN with the original sample queries. 4 What are the barcodes for each item? 5 What is the name + total ”our cost” value of each item currently in stock? 6 What is the name + total ”selling cost” value of each item currently in stock? 7 What are the total ”our cost” and ”sell cost” values of all items in stock? 8 How much was shipping on each order? 9 What items were ordered by people with the last name ”Stones”? 10 How much was the total shipping per person? Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 31 / 1
  • 32. Basic Admin - initdb - PGDATA - postgresql.conf - pg hba.conf - stop/start/restart/reload Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 32 / 1
  • 33. Basic Admin - SELECT version(); - CREATE ROLE - backups and upgrades - pg dump, pg dumpall, pg upgrade - logs Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 33 / 1
  • 34. GUI Tools - pHpPgAdmin - pgAdminIII Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 34 / 1
  • 35. Extras - Natural keys vs Surrogate keys - indexes GRANT USAGE ON SCHEMA [schema] TO [otheruser]; GRANT SELECT ON [table] TO [otheruser]; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 35 / 1
  • 36. Book giveaway! SELECT (random() * 100); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 36 / 1
  • 37. Where to get help - www.postgresql.org/community/lists/ - #postgresql - PDXPUG Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 37 / 1
  • 38. Reading List www.postgresql.org/docs Manga Guide to Databases Takahashi, Mana Database Design for Mere Mortals Hernandez, Michael J SQL for Smarties Celko, Joe Introduction to Database Systems Date, CJ Relational Model for Database Management Codd, EF Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 38 / 1
  • 39. __ __ / ~~~/ . o O ( Thank you! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 39 / 1