SlideShare una empresa de Scribd logo
1 de 39
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Introduction to NoSQL Databases
Introduction to NoSQL DatabasesIntroduction to NoSQL Databases
Introduction to NoSQL Databases
Derek Stainer
 

La actualidad más candente (20)

Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performance
 
Optimizing Delta/Parquet Data Lakes for Apache Spark
Optimizing Delta/Parquet Data Lakes for Apache SparkOptimizing Delta/Parquet Data Lakes for Apache Spark
Optimizing Delta/Parquet Data Lakes for Apache Spark
 
Diving into Delta Lake: Unpacking the Transaction Log
Diving into Delta Lake: Unpacking the Transaction LogDiving into Delta Lake: Unpacking the Transaction Log
Diving into Delta Lake: Unpacking the Transaction Log
 
Oracle Database 12c : Multitenant
Oracle Database 12c : MultitenantOracle Database 12c : Multitenant
Oracle Database 12c : Multitenant
 
Introduction to Map Reduce
Introduction to Map ReduceIntroduction to Map Reduce
Introduction to Map Reduce
 
Stl meetup cloudera platform - january 2020
Stl meetup   cloudera platform  - january 2020Stl meetup   cloudera platform  - january 2020
Stl meetup cloudera platform - january 2020
 
Spark SQL
Spark SQLSpark SQL
Spark SQL
 
A Reference Architecture for ETL 2.0
A Reference Architecture for ETL 2.0 A Reference Architecture for ETL 2.0
A Reference Architecture for ETL 2.0
 
Hadoop Hive Tutorial | Hive Fundamentals | Hive Architecture
Hadoop Hive Tutorial | Hive Fundamentals | Hive ArchitectureHadoop Hive Tutorial | Hive Fundamentals | Hive Architecture
Hadoop Hive Tutorial | Hive Fundamentals | Hive Architecture
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Cloud Data Warehousing presentation by Rogier Werschkull, including tips, bes...
Cloud Data Warehousing presentation by Rogier Werschkull, including tips, bes...Cloud Data Warehousing presentation by Rogier Werschkull, including tips, bes...
Cloud Data Warehousing presentation by Rogier Werschkull, including tips, bes...
 
Introduction to NoSQL Databases
Introduction to NoSQL DatabasesIntroduction to NoSQL Databases
Introduction to NoSQL Databases
 
The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...
The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...
The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...
 
Oracle 12c and its pluggable databases
Oracle 12c and its pluggable databasesOracle 12c and its pluggable databases
Oracle 12c and its pluggable databases
 
From Data Warehouse to Lakehouse
From Data Warehouse to LakehouseFrom Data Warehouse to Lakehouse
From Data Warehouse to Lakehouse
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer Works
 
Webinar: PostgreSQL continuous backup and PITR with Barman
Webinar: PostgreSQL continuous backup and PITR with BarmanWebinar: PostgreSQL continuous backup and PITR with Barman
Webinar: PostgreSQL continuous backup and PITR with Barman
 
Cassandra Database
Cassandra DatabaseCassandra Database
Cassandra Database
 
Introduction VAUUM, Freezing, XID wraparound
Introduction VAUUM, Freezing, XID wraparoundIntroduction VAUUM, Freezing, XID wraparound
Introduction VAUUM, Freezing, XID wraparound
 
Greenplum Architecture
Greenplum ArchitectureGreenplum Architecture
Greenplum Architecture
 

Destacado (6)

A brief introduction to PostgreSQL
A brief introduction to PostgreSQLA brief introduction to PostgreSQL
A brief introduction to PostgreSQL
 
Get to know PostgreSQL!
Get to know PostgreSQL!Get to know PostgreSQL!
Get to know PostgreSQL!
 
Postgres Presentation
Postgres PresentationPostgres Presentation
Postgres Presentation
 
PostgreSQL and PL/Java
PostgreSQL and PL/JavaPostgreSQL and PL/Java
PostgreSQL and PL/Java
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
 

Similar a Introduction to PostgreSQL

Old Oracle Versions
Old Oracle VersionsOld Oracle Versions
Old Oracle Versions
Jeffrey Kemp
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
arihantmobileselepun
 
Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.
Dan Lynn
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 

Similar a Introduction to PostgreSQL (20)

Pig workshop
Pig workshopPig workshop
Pig workshop
 
Old Oracle Versions
Old Oracle VersionsOld Oracle Versions
Old Oracle Versions
 
Pg big fast ugly acid
Pg big fast ugly acidPg big fast ugly acid
Pg big fast ugly acid
 
Don't panic! - Postgres introduction
Don't panic! - Postgres introductionDon't panic! - Postgres introduction
Don't panic! - Postgres introduction
 
Massively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian HustonMassively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian Huston
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
 
Hitchikers guide handout
Hitchikers guide handoutHitchikers guide handout
Hitchikers guide handout
 
Hadoop
HadoopHadoop
Hadoop
 
Kill the DBA
Kill the DBAKill the DBA
Kill the DBA
 
Rails in the enterprise
Rails in the enterpriseRails in the enterprise
Rails in the enterprise
 
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
 
Getting Started with Hadoop
Getting Started with HadoopGetting Started with Hadoop
Getting Started with Hadoop
 
Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.
 
Data-Intensive Scalable Science
Data-Intensive Scalable ScienceData-Intensive Scalable Science
Data-Intensive Scalable Science
 
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
 
How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...
 
How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
 
Polyalgebra
PolyalgebraPolyalgebra
Polyalgebra
 
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
 

Más de Mark Wong

OHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 MockumentaryOHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 Mockumentary
Mark Wong
 

Más de Mark Wong (20)

OHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 MockumentaryOHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 Mockumentary
 
OHAI, my name is Chelnik! Postgres Open 2013 Report
OHAI, my name is Chelnik! Postgres Open 2013 ReportOHAI, my name is Chelnik! Postgres Open 2013 Report
OHAI, my name is Chelnik! Postgres Open 2013 Report
 
collectd & PostgreSQL
collectd & PostgreSQLcollectd & PostgreSQL
collectd & PostgreSQL
 
Android & PostgreSQL
Android & PostgreSQLAndroid & PostgreSQL
Android & PostgreSQL
 
PGTop for Android: Things I learned making this app
PGTop for Android: Things I learned making this appPGTop for Android: Things I learned making this app
PGTop for Android: Things I learned making this app
 
Developing PGTop for Android
Developing PGTop for AndroidDeveloping PGTop for Android
Developing PGTop for Android
 
Pg in-the-brazilian-armed-forces-presentation
Pg in-the-brazilian-armed-forces-presentationPg in-the-brazilian-armed-forces-presentation
Pg in-the-brazilian-armed-forces-presentation
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
PostgreSQL Portland Performance Practice Project - Database Test 2 TuningPostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
Filesystem Performance from a Database Perspective
Filesystem Performance from a Database PerspectiveFilesystem Performance from a Database Perspective
Filesystem Performance from a Database Perspective
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
PostgreSQL Portland Performance Practice Project - Database Test 2 HowtoPostgreSQL Portland Performance Practice Project - Database Test 2 Howto
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
PostgreSQL Portland Performance Practice Project - Database Test 2 BackgroundPostgreSQL Portland Performance Practice Project - Database Test 2 Background
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
 
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
pg_top is 'top' for PostgreSQL: pg_top + pg_proctabpg_top is 'top' for PostgreSQL: pg_top + pg_proctab
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
 
Linux Filesystems, RAID, and more
Linux Filesystems, RAID, and moreLinux Filesystems, RAID, and more
Linux Filesystems, RAID, and more
 
pg_top is 'top' for PostgreSQL
pg_top is 'top' for PostgreSQLpg_top is 'top' for PostgreSQL
pg_top is 'top' for PostgreSQL
 

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 pdxpug@207.173.203.228 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