An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL - .

16
An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL - http://www.postgresql.org/docs/9.3/static/i ndex.html Acknowledgement – Thanks to Dr. Mayfield for the lab activity.

Transcript of An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL - .

Page 1: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

An introduction to SQL1/21/2014 – See chapter 2.3 and 6.1

PostgreSQL - http://www.postgresql.org/docs/9.3/static/index.htmlAcknowledgement – Thanks to Dr. Mayfield for the lab activity.

Page 2: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

SQL Query language• Divided into “clauses” each of which does a particular piece of

work.

• Two kinds of languages• DDL – Data Definition Language – Manipulates the database

schema

• DML – Data Manipulation Language – Manipulates the database data

Page 3: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

DDL (2.3)• CREATE – Makes new database objects• CREATE TABLE …• CREATE USER …• CREATE DATABASE …

• ALTER – Changes database objects• ALTER TABLE …

Page 4: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

DML – 6.1• SELECT – Read only query to examine data from the database.

May be used in conjunction with update statements.

• UPDATE – Update existing data

• INSERT – Insert new rows

• DELETE – Delete existing rows

Page 5: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Important clauses• SELECT – Chooses the individual fields (columns) from the

database (not necessarily a particular table)

• FROM – Chooses which table(s) we are working with

• WHERE – Provides criteria for the selection

Page 6: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

PostgreSQL• Relational DBMS use SQL but each has their own dialects and

supported features.

• PostgreSQL is: • “PostgreSQL is a powerful, open source object-relational

database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.” (www.postgresql.org/about)

Page 7: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Our server• db.cs.jmu.edu

• Accessible from JMU networks only

• Hardware• 2*12 Core Opteron 2.2 GHz• 48 GB RAM (plus 48 GB swap)• 6*275 GB Disks (1.5 TB free)

• Software• Linux 3.8 (Ubuntu 12.04.4)• PostgreSQL 9.1

Page 8: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Additional server• For those interested in exploring MySQL it is available on:• daisy.cs.jmu.edu

• See me if you want an account.

Page 9: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Logging in

• From the command line:• psql -h db.cs.jmu.edu -U username

• To change your password:• \ password

• Your db account is separate from your JMU account!

• Do it now

Page 10: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Create a table using DDLCREATE TABLE movie (

id integer, title text,year integer,genres text,mpaa text, budget text

);

White space does not matter but style dictates indentation for readability.

Case does not matter for the keywords like CREATE and TABLE but convention has us capitalize them.

Do it now!

Page 11: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Loading the db• Choices – You can hand enter the data ….

• OR – You can load it from a file.

• Today we will load from a file.

Page 12: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Load the database• Download• http://w3.cs.jmu.edu/mayfiecs/cs474/movies.csv.gz• About 1.4 MB

• Uncompress• gunzip movies.csv.gz• Now 3.8 MB

• With psql• \copy movie FROM movies.csv WITH CSV HEADER;• ANALYZE VERBOSE movie;

• 63,508 rows

• NOTE: \ is an escape which says to do this outside of psql

Page 13: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Basic psql commands

General•\? help on psql commands•\h: help on SQL syntax•\i execute commands from file•\q: quit psql (or Ctrl-D)Browsing•\d: list tables, views, etc•\d NAME: describe table, NAME,/etcImportant•Ctrl-C cancels the current query•Tab completion is your friend!

Page 14: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Your first DML query

SELECT title -- what you want from the dbFROM movie -- the table to operate onWHERE year = 2012; -- the criterion

Use up/down arrows and page up/downg or G: go to first / last line/ or ?: search forward / backwardPress ‘h’ for helpPress ‘q’ to quit

Page 15: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Explore• Write another query that includes more than one field in the

select and a different criterion.

• You can use AND and OR to combine criteria.

• Multiple fields are separated by commas in the SELECT statement.

Page 16: An introduction to SQL 1/21/2014 – See chapter 2.3 and 6.1 PostgreSQL -  .

Homework• Download any data set from:

• http://archive.ics.uci.edu/ml/

• Create the table

• Import the data into your database

• Write several queries exploring the data set.

• See HW03 in Canvas