introduction to postgresql

33
Introduction to PostgreSQL Joel Brewer @jahbrewski brewerdigital.com

Upload: joel-brewer

Post on 12-Aug-2015

218 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Introduction to PostgreSQL

Introduction to PostgreSQL

Joel Brewer@jahbrewski

brewerdigital.com

Page 2: Introduction to PostgreSQL

• Introduction to databases

• Introduction to PostgreSQL

• CRUD

• Create

• Read/query

• Update

• Destroy

• Joins

Overview

Page 3: Introduction to PostgreSQL

What’s a database?

• A database stores data!

• It is a collection of information that is organized so that it can be easily accessed, managed, and updated

Page 4: Introduction to PostgreSQL

Why do I care?• If you’re building a web application, you’re

going to have data to store

• What data do these websites store?

• Facebook (users, profiles, groups, events, businesses)

• Twitter (users, tweets)

• CNN (articles, comments)

Page 5: Introduction to PostgreSQL

What’s PostgreSQL?• PostgreSQL (“post-gress-Q-L”) is an open

source object-relational database system

• Open source = free!

• Object - some object-oriented paradigms are natively supported, such as objects, classes, and inheritance

• Relational - database is structured to recognize relations among stored items of information

Page 6: Introduction to PostgreSQL

Let’s make a database

$ createdb testdb

$ psql testdb

=> SELECT version();

Note: to exit psql just type “\q”

Page 7: Introduction to PostgreSQL

Let’s make a table

=> CREATE TABLE spaceship ( nickname varchar(35), crew_size int, commission_date date );

Page 8: Introduction to PostgreSQL

CRUD

• CRUD (create, read, update, delete) are the four basic functions you perform on a database

Page 9: Introduction to PostgreSQL

Create

• Examples:

• Creating a new account on Facebook

• Posting a picture on Instagram

• Creating a new tweet on Twitter

Page 10: Introduction to PostgreSQL

Read

• Examples:

• Browsing your Facebook timeline

• Looking at pictures on Instagram

• Reading the latest article on CNN

Page 11: Introduction to PostgreSQL

Update

• Examples:

• Changing your password

• Editing a comment

• Changing your relationship status on Facebook

Page 12: Introduction to PostgreSQL

Destroy

• Examples:

• Deleting a picture on Facebook (well, maybe…)

• Deleting a tweet on Twitter

• Unfriending somebody on Facebook

Page 13: Introduction to PostgreSQL

Create

• SQL uses the INSERT statement to create a new row

=> INSERT INTO spaceship VALUES (‘falcon’, 15, ‘2040-04-16’);

• Add a few more records to your spaceship table

Note: you must enter values in the same order you used during creation of the table

Page 14: Introduction to PostgreSQL

Read

• To read data from your database, one or more tables must be queried

• The SELECT statement is used to perform queries

=> SELECT * FROM spaceship;

Page 15: Introduction to PostgreSQL

Read

• The * is a shorthand for “all columns”

• The same result could be achieved with:

=> SELECT nickname, crew_size, commission_date FROM spaceship;

Page 16: Introduction to PostgreSQL

Read

• Queries can be ordered by adding ORDER BY [column] to the end of your query

=> SELECT * FROM spaceship ORDER BY nickname;

=> SELECT * FROM spaceship ORDER BY commission_date;

Page 17: Introduction to PostgreSQL

Read• Queries can be “qualified” by adding a

WHERE clause that specifies which rows are wanted

=> SELECT * FROM spaceship WHERE commission_date > ‘2030-04-16’;

=> SELECT * FROM spaceship WHERE nickname = ‘falcon’;

=> SELECT * FROM spaceship WHERE crew_size > 5;

Page 18: Introduction to PostgreSQL

Update• Rows can be updated using the UPDATE

command

• Update row(s) matching WHERE clause

=> UPDATE spaceship SET nickname = ‘eagle’ WHERE nickname = ‘falcon’;

• Careful, default updates all rows!

=> UPDATE spaceship SET crew_size = 20;

Page 19: Introduction to PostgreSQL

Destroy• Rows can be destroyed using the DELETE

command

• Delete row(s) matching WHERE clause

=> DELETE from spaceship WHERE nickname = ‘falcon’;

• Careful, default deletes all rows!

=> DELETE from spaceship;

Page 20: Introduction to PostgreSQL

Congratulations!

• Now you know how to CRUD a PostgreSQL database!

Page 21: Introduction to PostgreSQL

Digging Deeper…

• Joins between tables

• Transactions

Page 22: Introduction to PostgreSQL

But First…

• Install sample database that we can run some joins/queries on

• https://code.google.com/p/northwindextended/downloads/detail?name=northwind.postgre.sql

Page 23: Introduction to PostgreSQL

But First…

$ createdb northwind

$ psql northwind < northwind.postgre.sql

$ psql northwind

Page 24: Introduction to PostgreSQL

But First…

• Lets do a little exploration:

=> \dt

=> \d+ orders

=> \d+ customers

Page 25: Introduction to PostgreSQL

Joins - Inner Join• SQL inner join is the most common type of

join

• Used to combine rows from two or more tables, based on a common field between them

Page 26: Introduction to PostgreSQL

Joins - Inner Join• Which company placed each order?

• Return the OrderID, CustomerID, and CompanyName for each order

• Using an Inner Join we can join the orders table with the customers table, using the CustomerID

=> SELECT “OrderID”, orders.”CustomerID’, “CompanyName” FROM orders INNER JOIN customers ON orders.”CustomerID”=customers.”CustomerID”;

Page 27: Introduction to PostgreSQL

Joins - Inner Join• What products were ordered, and what were

their prices?

• Return the ProductID, UnitPrice, and ProductName for each item of a given order

• Using an Inner Join we can join the order_details table with the products table

=> SELECT order_details.”ProductID”, ”UnitPrice”, “ProductName” FROM order_details INNER JOIN products ON order_details.”ProductID”=products.”ProductID” WHERE “OrderID” = 10248;

Page 28: Introduction to PostgreSQL

Joins - Left Outer Join• The LEFT OUTER JOIN keyword returns all

rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

Page 29: Introduction to PostgreSQL

Joins - Left Outer Join• “Let me see a list of all of our customers, and

any orders they have made”

• Need to return the CompanyName and associated OrderID (if any)

• Using a Left Outer Join we can join the orders table with the customers table

=> SELECT customers."CompanyName", orders.”OrderID" FROM customers LEFT OUTER JOIN orders ON customers."CustomerID"=orders."CustomerID" ORDER BY customers."CompanyName";

Page 30: Introduction to PostgreSQL

Joins - Right Outer Join

• The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

Page 31: Introduction to PostgreSQL

Joins - Full Outer Join• The FULL OUTER JOIN keyword returns all

rows from the left table (table1) and from the right table (table2).The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

Page 32: Introduction to PostgreSQL

Transactions• “A transaction is a unit of work that is performed against a

database. Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program.”

• “You use transactions when the set of database operations you are making needs to be atomic. That is - they all need to succeed or fail. Nothing in between.” http://stackoverflow.com/questions/9317866/when-to-use-transactions-in-sql-server

• Examples:

• Money transfer

• Order fulfillment

• Can you think of any others?

Page 33: Introduction to PostgreSQL

Joel Brewer

@jahbrewskibrewerdigital.com

[email protected]