Renumber chapters and listing numbers per new book order
This commit is contained in:
parent
b452769f96
commit
7189b2c405
@ -2,14 +2,14 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 1 Code Examples
|
||||
-- Chapter 2 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 1-1: Creating a database named analysis
|
||||
-- Listing 2-1: Creating a database named analysis
|
||||
|
||||
CREATE DATABASE analysis;
|
||||
|
||||
-- Listing 1-2: Creating a table named teachers with six columns
|
||||
-- Listing 2-2: Creating a table named teachers with six columns
|
||||
|
||||
CREATE TABLE teachers (
|
||||
id bigserial,
|
||||
@ -23,7 +23,7 @@ CREATE TABLE teachers (
|
||||
-- This command will remove (drop) the table.
|
||||
-- DROP TABLE teachers;
|
||||
|
||||
-- Listing 1-3 Inserting data into the teachers table
|
||||
-- Listing 2-3 Inserting data into the teachers table
|
||||
|
||||
INSERT INTO teachers (first_name, last_name, school, hire_date, salary)
|
||||
VALUES ('Janet', 'Smith', 'F.D. Roosevelt HS', '2011-10-30', 36200),
|
||||
|
||||
@ -2,41 +2,41 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 2 Code Examples
|
||||
-- Chapter 3 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 2-1: Querying all rows and columns from the teachers table
|
||||
-- Listing 3-1: Querying all rows and columns from the teachers table
|
||||
|
||||
SELECT * FROM teachers;
|
||||
|
||||
-- Listing 2-2: Querying a subset of columns
|
||||
-- Listing 3-2: Querying a subset of columns
|
||||
|
||||
SELECT last_name, first_name, salary FROM teachers;
|
||||
|
||||
-- Listing 2-3: Querying distinct values in the school column
|
||||
-- Listing 3-3: Querying distinct values in the school column
|
||||
|
||||
SELECT DISTINCT school
|
||||
FROM teachers;
|
||||
|
||||
-- Listing 2-4: Querying distinct pairs of values in the school and salary
|
||||
-- Listing 3-4: Querying distinct pairs of values in the school and salary
|
||||
-- columns
|
||||
|
||||
SELECT DISTINCT school, salary
|
||||
FROM teachers;
|
||||
|
||||
-- Listing 2-5: Sorting a column with ORDER BY
|
||||
-- Listing 3-5: Sorting a column with ORDER BY
|
||||
|
||||
SELECT first_name, last_name, salary
|
||||
FROM teachers
|
||||
ORDER BY salary DESC;
|
||||
|
||||
-- Listing 2-6: Sorting multiple columns with ORDER BY
|
||||
-- Listing 3-6: Sorting multiple columns with ORDER BY
|
||||
|
||||
SELECT last_name, school, hire_date
|
||||
FROM teachers
|
||||
ORDER BY school ASC, hire_date DESC;
|
||||
|
||||
-- Listing 2-7: Filtering rows using WHERE
|
||||
-- Listing 3-7: Filtering rows using WHERE
|
||||
|
||||
SELECT last_name, school, hire_date
|
||||
FROM teachers
|
||||
@ -69,7 +69,7 @@ SELECT first_name, last_name, school, salary
|
||||
FROM teachers
|
||||
WHERE salary BETWEEN 40000 AND 65000;
|
||||
|
||||
-- Listing 2-8: Filtering with LIKE AND ILIKE
|
||||
-- Listing 3-8: Filtering with LIKE AND ILIKE
|
||||
|
||||
SELECT first_name
|
||||
FROM teachers
|
||||
@ -79,7 +79,7 @@ SELECT first_name
|
||||
FROM teachers
|
||||
WHERE first_name ILIKE 'sam%';
|
||||
|
||||
-- Listing 2-9: Combining operators using AND and OR
|
||||
-- Listing 3-9: Combining operators using AND and OR
|
||||
|
||||
SELECT *
|
||||
FROM teachers
|
||||
@ -96,7 +96,7 @@ FROM teachers
|
||||
WHERE school = 'F.D. Roosevelt HS'
|
||||
AND (salary < 38000 OR salary > 40000);
|
||||
|
||||
-- Listing 2-10: A SELECT statement including WHERE and ORDER BY
|
||||
-- Listing 3-10: A SELECT statement including WHERE and ORDER BY
|
||||
|
||||
SELECT first_name, last_name, school, hire_date, salary
|
||||
FROM teachers
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 3 Code Examples
|
||||
-- Chapter 4 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 3-1: Character data types in action
|
||||
-- Listing 4-1: Character data types in action
|
||||
|
||||
CREATE TABLE char_data_types (
|
||||
varchar_column varchar(10),
|
||||
@ -22,7 +22,7 @@ COPY char_data_types TO 'C:\YourDirectory\typetest.txt'
|
||||
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
||||
|
||||
|
||||
-- Listing 3-2: Number data types in action
|
||||
-- Listing 4-2: Number data types in action
|
||||
|
||||
CREATE TABLE number_data_types (
|
||||
numeric_column numeric(20,5),
|
||||
@ -38,8 +38,8 @@ VALUES
|
||||
|
||||
SELECT * FROM number_data_types;
|
||||
|
||||
-- Listing 3-3: Rounding issues with float columns
|
||||
-- Assumes table created and loaded with Listing 3-2
|
||||
-- Listing 4-3: Rounding issues with float columns
|
||||
-- Assumes table created and loaded with Listing 4-2
|
||||
|
||||
SELECT
|
||||
numeric_column * 10000000 AS "Fixed",
|
||||
@ -47,7 +47,7 @@ SELECT
|
||||
FROM number_data_types
|
||||
WHERE numeric_column = .7;
|
||||
|
||||
-- Listing 3-4: Timestamp and interval types in action
|
||||
-- Listing 4-4: Timestamp and interval types in action
|
||||
|
||||
CREATE TABLE date_time_types (
|
||||
timestamp_column timestamp with time zone,
|
||||
@ -63,8 +63,8 @@ VALUES
|
||||
|
||||
SELECT * FROM date_time_types;
|
||||
|
||||
-- Listing 3-5: Using the interval data type
|
||||
-- Assumes script 3-4 has been run
|
||||
-- Listing 4-5: Using the interval data type
|
||||
-- Assumes script 4-4 has been run
|
||||
|
||||
SELECT
|
||||
timestamp_column,
|
||||
@ -72,7 +72,7 @@ SELECT
|
||||
timestamp_column - interval_column AS new_date
|
||||
FROM date_time_types;
|
||||
|
||||
-- Listing 3-6: Three CAST() examples
|
||||
-- Listing 4-6: Three CAST() examples
|
||||
|
||||
SELECT timestamp_column, CAST(timestamp_column AS varchar(10))
|
||||
FROM date_time_types;
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 4 Code Examples
|
||||
-- Chapter 5 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 4-1: Using COPY for data import
|
||||
-- Listing 5-1: Using COPY for data import
|
||||
-- This is example syntax only; running it will produce an error
|
||||
|
||||
COPY table_name
|
||||
@ -13,7 +13,7 @@ FROM 'C:\YourDirectory\your_file.csv'
|
||||
WITH (FORMAT CSV, HEADER);
|
||||
|
||||
|
||||
-- Listing 4-2: A CREATE TABLE statement for Census county data
|
||||
-- Listing 5-2: A CREATE TABLE statement for Census county data
|
||||
-- Full data dictionary available at: http://www.census.gov/prod/cen2010/doc/pl94-171.pdf
|
||||
-- Note: Some columns have been given more descriptive names
|
||||
|
||||
@ -124,7 +124,7 @@ CREATE TABLE us_counties_2010 (
|
||||
|
||||
SELECT * FROM us_counties_2010;
|
||||
|
||||
-- Listing 4-3: Importing Census data using COPY
|
||||
-- Listing 5-3: Importing Census data using COPY
|
||||
-- Note! If you run into an import error here, be sure you downloaded the code and
|
||||
-- data for the book according to the steps listed on page xxvii in the Introduction.
|
||||
-- Windows users: Please check the Note on page xxvii as well.
|
||||
@ -148,7 +148,7 @@ ORDER BY internal_point_lon DESC
|
||||
LIMIT 5;
|
||||
|
||||
|
||||
-- Listing 4-4: Creating a table to track supervisor salaries
|
||||
-- Listing 5-4: Creating a table to track supervisor salaries
|
||||
|
||||
CREATE TABLE supervisor_salaries (
|
||||
town varchar(30),
|
||||
@ -159,7 +159,7 @@ CREATE TABLE supervisor_salaries (
|
||||
benefits money
|
||||
);
|
||||
|
||||
-- Listing 4-5: Importing salaries data from CSV to three table columns
|
||||
-- Listing 5-5: Importing salaries data from CSV to three table columns
|
||||
|
||||
COPY supervisor_salaries (town, supervisor, salary)
|
||||
FROM 'C:\YourDirectory\supervisor_salaries.csv'
|
||||
@ -168,7 +168,7 @@ WITH (FORMAT CSV, HEADER);
|
||||
-- Check the data
|
||||
SELECT * FROM supervisor_salaries LIMIT 2;
|
||||
|
||||
-- Listing 4-6 Use a temporary table to add a default value to a column during
|
||||
-- Listing 5-6 Use a temporary table to add a default value to a column during
|
||||
-- import
|
||||
|
||||
DELETE FROM supervisor_salaries;
|
||||
@ -188,20 +188,20 @@ DROP TABLE supervisor_salaries_temp;
|
||||
-- Check the data
|
||||
SELECT * FROM supervisor_salaries LIMIT 2;
|
||||
|
||||
-- Listing 4-7: Export an entire table with COPY
|
||||
-- Listing 5-7: Export an entire table with COPY
|
||||
|
||||
COPY us_counties_2010
|
||||
TO 'C:\YourDirectory\us_counties_export.txt'
|
||||
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
||||
|
||||
|
||||
-- Listing 4-8: Exporting selected columns from a table with COPY
|
||||
-- Listing 5-8: Exporting selected columns from a table with COPY
|
||||
|
||||
COPY us_counties_2010 (geo_name, internal_point_lat, internal_point_lon)
|
||||
TO 'C:\YourDirectory\us_counties_latlon_export.txt'
|
||||
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
||||
|
||||
-- Listing 4-9: Exporting query results with COPY
|
||||
-- Listing 5-9: Exporting query results with COPY
|
||||
|
||||
COPY (
|
||||
SELECT geo_name, state_us_abbreviation
|
||||
|
||||
@ -2,23 +2,23 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 5 Code Examples
|
||||
-- Chapter 6 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 5-1: Basic addition, subtraction and multiplication with SQL
|
||||
-- Listing 6-1: Basic addition, subtraction and multiplication with SQL
|
||||
|
||||
SELECT 2 + 2; -- addition
|
||||
SELECT 9 - 1; -- subtraction
|
||||
SELECT 3 * 4; -- multiplication
|
||||
|
||||
-- Listing 5-2: Integer and decimal division with SQL
|
||||
-- Listing 6-2: Integer and decimal division with SQL
|
||||
|
||||
SELECT 11 / 6; -- integer division
|
||||
SELECT 11 % 6; -- modulo division
|
||||
SELECT 11.0 / 6; -- decimal division
|
||||
SELECT CAST(11 AS numeric(3,1)) / 6;
|
||||
|
||||
-- Listing 5-3: Exponents, roots and factorials with SQL
|
||||
-- Listing 6-3: Exponents, roots and factorials with SQL
|
||||
|
||||
SELECT 3 ^ 4; -- exponentiation
|
||||
SELECT |/ 10; -- square root (operator)
|
||||
@ -34,7 +34,7 @@ SELECT (7 + 8) * 9; -- answer: 135
|
||||
SELECT 3 ^ 3 - 1; -- answer: 26
|
||||
SELECT 3 ^ (3 - 1); -- answer: 9
|
||||
|
||||
-- Listing 5-4: Selecting Census population columns by race with aliases
|
||||
-- Listing 6-4: Selecting Census population columns by race with aliases
|
||||
|
||||
SELECT geo_name,
|
||||
state_us_abbreviation AS "st",
|
||||
@ -48,7 +48,7 @@ SELECT geo_name,
|
||||
p0010009 AS "Two or More Races"
|
||||
FROM us_counties_2010;
|
||||
|
||||
-- Listing 5-5: Adding two columns in us_counties_2010
|
||||
-- Listing 6-5: Adding two columns in us_counties_2010
|
||||
|
||||
SELECT geo_name,
|
||||
state_us_abbreviation AS "st",
|
||||
@ -57,7 +57,7 @@ SELECT geo_name,
|
||||
p0010003 + p0010004 AS "Total White and Black"
|
||||
FROM us_counties_2010;
|
||||
|
||||
-- Listing 5-6: Checking Census data totals
|
||||
-- Listing 6-6: Checking Census data totals
|
||||
|
||||
SELECT geo_name,
|
||||
state_us_abbreviation AS "st",
|
||||
@ -69,7 +69,7 @@ SELECT geo_name,
|
||||
FROM us_counties_2010
|
||||
ORDER BY "Difference" DESC;
|
||||
|
||||
-- Listing 5-7: Calculating the percent of the population that is
|
||||
-- Listing 6-7: Calculating the percent of the population that is
|
||||
-- Asian by county (percent of the whole)
|
||||
|
||||
SELECT geo_name,
|
||||
@ -78,7 +78,7 @@ SELECT geo_name,
|
||||
FROM us_counties_2010
|
||||
ORDER BY "pct_asian" DESC;
|
||||
|
||||
-- Listing 5-8: Calculating percent change
|
||||
-- Listing 6-8: Calculating percent change
|
||||
|
||||
CREATE TABLE percent_change (
|
||||
department varchar(20),
|
||||
@ -102,13 +102,13 @@ SELECT department,
|
||||
spend_2014 * 100, 1 ) AS "pct_change"
|
||||
FROM percent_change;
|
||||
|
||||
-- Listing 5-9: Using sum() and avg() aggregate functions
|
||||
-- Listing 6-9: Using sum() and avg() aggregate functions
|
||||
|
||||
SELECT sum(p0010001) AS "County Sum",
|
||||
round(avg(p0010001), 0) AS "County Average"
|
||||
FROM us_counties_2010;
|
||||
|
||||
-- Listing 5-10: Testing SQL percentile functions
|
||||
-- Listing 6-10: Testing SQL percentile functions
|
||||
|
||||
CREATE TABLE percentile_test (
|
||||
numbers integer
|
||||
@ -124,7 +124,7 @@ SELECT
|
||||
WITHIN GROUP (ORDER BY numbers)
|
||||
FROM percentile_test;
|
||||
|
||||
-- Listing 5-11: Using sum(), avg(), and percentile_cont() aggregate functions
|
||||
-- Listing 6-11: Using sum(), avg(), and percentile_cont() aggregate functions
|
||||
|
||||
SELECT sum(p0010001) AS "County Sum",
|
||||
round(avg(p0010001), 0) AS "County Average",
|
||||
@ -132,7 +132,7 @@ SELECT sum(p0010001) AS "County Sum",
|
||||
WITHIN GROUP (ORDER BY p0010001) AS "County Median"
|
||||
FROM us_counties_2010;
|
||||
|
||||
-- Listing 5-12: Passing an array of values to percentile_cont()
|
||||
-- Listing 6-12: Passing an array of values to percentile_cont()
|
||||
|
||||
-- quartiles
|
||||
SELECT percentile_cont(array[.25,.5,.75])
|
||||
@ -150,7 +150,7 @@ SELECT percentile_cont(array[.1,.2,.3,.4,.5,.6,.7,.8,.9])
|
||||
WITHIN GROUP (ORDER BY p0010001) AS "deciles"
|
||||
FROM us_counties_2010;
|
||||
|
||||
-- Listing 5-13: Using unnest() to turn an array into rows
|
||||
-- Listing 6-13: Using unnest() to turn an array into rows
|
||||
|
||||
SELECT unnest(
|
||||
percentile_cont(array[.25,.5,.75])
|
||||
@ -158,7 +158,7 @@ SELECT unnest(
|
||||
) AS "quartiles"
|
||||
FROM us_counties_2010;
|
||||
|
||||
-- Listing 5-14: Creating a median() aggregate function in PostgreSQL
|
||||
-- Listing 6-14: Creating a median() aggregate function in PostgreSQL
|
||||
-- Source: https://wiki.postgresql.org/wiki/Aggregate_Median
|
||||
|
||||
CREATE OR REPLACE FUNCTION _final_median(anyarray)
|
||||
@ -192,7 +192,7 @@ CREATE AGGREGATE median(anyelement) (
|
||||
INITCOND='{}'
|
||||
);
|
||||
|
||||
-- Listing 5-15: Using a median() aggregate function
|
||||
-- Listing 6-15: Using a median() aggregate function
|
||||
|
||||
SELECT sum(p0010001) AS "County Sum",
|
||||
round(avg(p0010001), 0) AS "County Average",
|
||||
@ -201,7 +201,7 @@ SELECT sum(p0010001) AS "County Sum",
|
||||
WITHIN GROUP (ORDER BY P0010001) AS "50th Percentile"
|
||||
FROM us_counties_2010;
|
||||
|
||||
-- Listing 5-16: Finding the most-frequent value with mode()
|
||||
-- Listing 6-16: Finding the most-frequent value with mode()
|
||||
|
||||
SELECT mode() WITHIN GROUP (ORDER BY p0010001)
|
||||
FROM us_counties_2010;
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
-- Chapter 6 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 6-1: Creating the departments and employees tables
|
||||
-- Listing 7-1: Creating the departments and employees tables
|
||||
|
||||
CREATE TABLE departments (
|
||||
dept_id bigserial,
|
||||
@ -37,13 +37,13 @@ VALUES
|
||||
('Soo', 'Nguyen', 83000, 2),
|
||||
('Janet', 'King', 95000, 2);
|
||||
|
||||
-- Listing 6-2: Joining the employees and departments tables
|
||||
-- Listing 7-2: Joining the employees and departments tables
|
||||
|
||||
SELECT *
|
||||
FROM employees JOIN departments
|
||||
ON employees.dept_id = departments.dept_id;
|
||||
|
||||
-- Listing 6-3: Creating two tables to explore JOIN types
|
||||
-- Listing 7-3: Creating two tables to explore JOIN types
|
||||
|
||||
CREATE TABLE schools_left (
|
||||
id integer CONSTRAINT left_id_key PRIMARY KEY,
|
||||
@ -68,7 +68,7 @@ INSERT INTO schools_right (id, right_school) VALUES
|
||||
(4, 'Chase Magnet Academy'),
|
||||
(6, 'Jefferson High School');
|
||||
|
||||
-- Listing 6-4: Using JOIN
|
||||
-- Listing 7-4: Using JOIN
|
||||
|
||||
SELECT *
|
||||
FROM schools_left JOIN schools_right
|
||||
@ -80,51 +80,51 @@ SELECT *
|
||||
FROM schools_left INNER JOIN schools_right
|
||||
ON schools_left.id = schools_right.id;
|
||||
|
||||
-- Listing 6-5: Using LEFT JOIN
|
||||
-- Listing 7-5: Using LEFT JOIN
|
||||
|
||||
SELECT *
|
||||
FROM schools_left LEFT JOIN schools_right
|
||||
ON schools_left.id = schools_right.id;
|
||||
|
||||
-- Listing 6-6: Using RIGHT JOIN
|
||||
-- Listing 7-6: Using RIGHT JOIN
|
||||
|
||||
SELECT *
|
||||
FROM schools_left RIGHT JOIN schools_right
|
||||
ON schools_left.id = schools_right.id;
|
||||
|
||||
-- Listing 6-7: Using FULL OUTER JOIN
|
||||
-- Listing 7-7: Using FULL OUTER JOIN
|
||||
|
||||
SELECT *
|
||||
FROM schools_left FULL OUTER JOIN schools_right
|
||||
ON schools_left.id = schools_right.id;
|
||||
|
||||
-- Listing 6-8: Using CROSS JOIN
|
||||
-- Listing 7-8: Using CROSS JOIN
|
||||
|
||||
SELECT *
|
||||
FROM schools_left CROSS JOIN schools_right;
|
||||
|
||||
-- Listing 6-9: Filtering to show missing values with IS NULL
|
||||
-- Listing 7-9: Filtering to show missing values with IS NULL
|
||||
|
||||
SELECT *
|
||||
FROM schools_left LEFT JOIN schools_right
|
||||
ON schools_left.id = schools_right.id
|
||||
WHERE schools_right.id IS NULL;
|
||||
|
||||
-- Listing 6-10: Querying specific columns in a join
|
||||
-- Listing 7-10: Querying specific columns in a join
|
||||
SELECT schools_left.id,
|
||||
schools_left.left_school,
|
||||
schools_right.right_school
|
||||
FROM schools_left LEFT JOIN schools_right
|
||||
ON schools_left.id = schools_right.id;
|
||||
|
||||
-- Listing 6-11: Simplifying code with table aliases
|
||||
-- Listing 7-11: Simplifying code with table aliases
|
||||
SELECT lt.id,
|
||||
lt.left_school,
|
||||
rt.right_school
|
||||
FROM schools_left AS lt LEFT JOIN schools_right AS rt
|
||||
ON lt.id = rt.id;
|
||||
|
||||
-- Listing 6-12: Joining multiple tables
|
||||
-- Listing 7-12: Joining multiple tables
|
||||
CREATE TABLE schools_enrollment (
|
||||
id integer,
|
||||
enrollment integer
|
||||
@ -155,7 +155,7 @@ FROM schools_left AS lt LEFT JOIN schools_enrollment AS en
|
||||
LEFT JOIN schools_grades AS gr
|
||||
ON lt.id = gr.id;
|
||||
|
||||
-- Listing 6-13: Performing math on joined Census tables
|
||||
-- Listing 7-13: Performing math on joined Census tables
|
||||
-- Decennial Census 2000. Full data dictionary at https://www.census.gov/prod/cen2000/doc/pl94-171.pdf
|
||||
-- Note: Some non-number columns have been given more descriptive names
|
||||
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 7 Code Examples
|
||||
-- Chapter 8 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 7-1: Declaring a single-column natural key as primary key
|
||||
-- Listing 8-1: Declaring a single-column natural key as primary key
|
||||
|
||||
-- As a column constraint
|
||||
CREATE TABLE natural_key_example (
|
||||
@ -25,14 +25,14 @@ CREATE TABLE natural_key_example (
|
||||
CONSTRAINT license_key PRIMARY KEY (license_id)
|
||||
);
|
||||
|
||||
-- Listing 7-2: Example of a primary key violation
|
||||
-- Listing 8-2: Example of a primary key violation
|
||||
INSERT INTO natural_key_example (license_id, first_name, last_name)
|
||||
VALUES ('T229901', 'Lynn', 'Malero');
|
||||
|
||||
INSERT INTO natural_key_example (license_id, first_name, last_name)
|
||||
VALUES ('T229901', 'Sam', 'Tracy');
|
||||
|
||||
-- Listing 7-3: Declaring a composite primary key as a natural key
|
||||
-- Listing 8-3: Declaring a composite primary key as a natural key
|
||||
CREATE TABLE natural_key_composite_example (
|
||||
student_id varchar(10),
|
||||
school_day date,
|
||||
@ -40,7 +40,7 @@ CREATE TABLE natural_key_composite_example (
|
||||
CONSTRAINT student_key PRIMARY KEY (student_id, school_day)
|
||||
);
|
||||
|
||||
-- Listing 7-4: Example of a composite primary key violation
|
||||
-- Listing 8-4: Example of a composite primary key violation
|
||||
|
||||
INSERT INTO natural_key_composite_example (student_id, school_day, present)
|
||||
VALUES(775, '1/22/2017', 'Y');
|
||||
@ -51,7 +51,7 @@ VALUES(775, '1/23/2017', 'Y');
|
||||
INSERT INTO natural_key_composite_example (student_id, school_day, present)
|
||||
VALUES(775, '1/23/2017', 'N');
|
||||
|
||||
-- Listing 7-5: Declaring a bigserial column as a surrogate key
|
||||
-- Listing 8-5: Declaring a bigserial column as a surrogate key
|
||||
|
||||
CREATE TABLE surrogate_key_example (
|
||||
order_number bigserial,
|
||||
@ -67,7 +67,7 @@ VALUES ('Beachball Polish', '2015-03-17'),
|
||||
|
||||
SELECT * FROM surrogate_key_example;
|
||||
|
||||
-- Listing 7-6: A foreign key example
|
||||
-- Listing 8-6: A foreign key example
|
||||
|
||||
CREATE TABLE licenses (
|
||||
license_id varchar(10),
|
||||
@ -92,7 +92,7 @@ VALUES ('A203391', '3/17/2017', 'T229901');
|
||||
INSERT INTO registrations (registration_id, registration_date, license_id)
|
||||
VALUES ('A75772', '3/17/2017', 'T000001');
|
||||
|
||||
-- Listing 7-7: CHECK constraint examples
|
||||
-- Listing 8-7: CHECK constraint examples
|
||||
|
||||
CREATE TABLE check_constraint_example (
|
||||
user_id bigserial,
|
||||
@ -110,7 +110,7 @@ VALUES ('admin');
|
||||
INSERT INTO check_constraint_example (salary)
|
||||
VALUES (0);
|
||||
|
||||
-- Listing 7-8: UNIQUE constraint example
|
||||
-- Listing 8-8: UNIQUE constraint example
|
||||
|
||||
CREATE TABLE unique_constraint_example (
|
||||
contact_id bigserial CONSTRAINT contact_id_key PRIMARY KEY,
|
||||
@ -129,7 +129,7 @@ VALUES ('Betty', 'Diaz', 'bdiaz@example.org');
|
||||
INSERT INTO unique_constraint_example (first_name, last_name, email)
|
||||
VALUES ('Sasha', 'Lee', 'slee@example.org');
|
||||
|
||||
-- Listing 7-9: NOT NULL constraint example
|
||||
-- Listing 8-9: NOT NULL constraint example
|
||||
|
||||
CREATE TABLE not_null_example (
|
||||
student_id bigserial,
|
||||
@ -138,7 +138,7 @@ CREATE TABLE not_null_example (
|
||||
CONSTRAINT student_id_key PRIMARY KEY (student_id)
|
||||
);
|
||||
|
||||
-- Listing 7-10: Dropping and adding a primary key and a NOT NULL constraint
|
||||
-- Listing 8-10: Dropping and adding a primary key and a NOT NULL constraint
|
||||
|
||||
-- Drop
|
||||
ALTER TABLE not_null_example DROP CONSTRAINT student_id_key;
|
||||
@ -152,7 +152,7 @@ ALTER TABLE not_null_example ALTER COLUMN first_name DROP NOT NULL;
|
||||
-- Add
|
||||
ALTER TABLE not_null_example ALTER COLUMN first_name SET NOT NULL;
|
||||
|
||||
-- Listing 7-11: Importing New York City address data
|
||||
-- Listing 8-11: Importing New York City address data
|
||||
|
||||
CREATE TABLE new_york_addresses (
|
||||
longitude numeric(9,6),
|
||||
@ -168,7 +168,7 @@ COPY new_york_addresses
|
||||
FROM 'C:\YourDirectory\city_of_new_york.csv'
|
||||
WITH (FORMAT CSV, HEADER);
|
||||
|
||||
-- Listing 7-12: Benchmark queries for index performance
|
||||
-- Listing 8-12: Benchmark queries for index performance
|
||||
|
||||
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
|
||||
WHERE street = 'BROADWAY';
|
||||
@ -179,6 +179,6 @@ WHERE street = '52 STREET';
|
||||
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
|
||||
WHERE street = 'ZWICKY AVENUE';
|
||||
|
||||
-- Listing 7-13: Creating a B-Tree index on the new_york_addresses table
|
||||
-- Listing 8-13: Creating a B-Tree index on the new_york_addresses table
|
||||
|
||||
CREATE INDEX street_idx ON new_york_addresses (street);
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 8 Code Examples
|
||||
-- Chapter 9 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 8-1: Creating and filling the 2014 Public Libraries Survey table
|
||||
-- Listing 9-1: Creating and filling the 2014 Public Libraries Survey table
|
||||
|
||||
CREATE TABLE pls_fy2014_pupld14a (
|
||||
stabr varchar(2) NOT NULL,
|
||||
@ -91,7 +91,7 @@ COPY pls_fy2014_pupld14a
|
||||
FROM 'C:\YourDirectory\pls_fy2014_pupld14a.csv'
|
||||
WITH (FORMAT CSV, HEADER);
|
||||
|
||||
-- Listing 8-2: Creating and filling the 2009 Public Libraries Survey table
|
||||
-- Listing 9-2: Creating and filling the 2009 Public Libraries Survey table
|
||||
|
||||
CREATE TABLE pls_fy2009_pupld09a (
|
||||
stabr varchar(2) NOT NULL,
|
||||
@ -173,7 +173,7 @@ COPY pls_fy2009_pupld09a
|
||||
FROM 'C:\YourDirectory\pls_fy2009_pupld09a.csv'
|
||||
WITH (FORMAT CSV, HEADER);
|
||||
|
||||
-- Listing 8-3: Using count() for table row counts
|
||||
-- Listing 9-3: Using count() for table row counts
|
||||
|
||||
SELECT count(*)
|
||||
FROM pls_fy2014_pupld14a;
|
||||
@ -181,12 +181,12 @@ FROM pls_fy2014_pupld14a;
|
||||
SELECT count(*)
|
||||
FROM pls_fy2009_pupld09a;
|
||||
|
||||
-- Listing 8-4: Using count() for the number of values in a column
|
||||
-- Listing 9-4: Using count() for the number of values in a column
|
||||
|
||||
SELECT count(salaries)
|
||||
FROM pls_fy2014_pupld14a;
|
||||
|
||||
-- Listing 8-5: Using count() for the number of distinct values in a column
|
||||
-- Listing 9-5: Using count() for the number of distinct values in a column
|
||||
|
||||
SELECT count(libname)
|
||||
FROM pls_fy2014_pupld14a;
|
||||
@ -205,11 +205,11 @@ SELECT libname, city, stabr
|
||||
FROM pls_fy2014_pupld14a
|
||||
WHERE libname = 'OXFORD PUBLIC LIBRARY';
|
||||
|
||||
-- Listing 8-6: Finding the most and fewest visits using max() and min()
|
||||
-- Listing 9-6: Finding the most and fewest visits using max() and min()
|
||||
SELECT max(visits), min(visits)
|
||||
FROM pls_fy2014_pupld14a;
|
||||
|
||||
-- Listing 8-7: Using GROUP BY on the stabr column
|
||||
-- Listing 9-7: Using GROUP BY on the stabr column
|
||||
|
||||
-- There are 56 in 2014.
|
||||
SELECT stabr
|
||||
@ -223,7 +223,7 @@ FROM pls_fy2009_pupld09a
|
||||
GROUP BY stabr
|
||||
ORDER BY stabr;
|
||||
|
||||
-- Listing 8-8: Using GROUP BY on the city and stabr columns
|
||||
-- Listing 9-8: Using GROUP BY on the city and stabr columns
|
||||
|
||||
SELECT city, stabr
|
||||
FROM pls_fy2014_pupld14a
|
||||
@ -236,21 +236,21 @@ FROM pls_fy2014_pupld14a
|
||||
GROUP BY city, stabr
|
||||
ORDER BY count(*) DESC;
|
||||
|
||||
-- Listing 8-9: GROUP BY with count() on the stabr column
|
||||
-- Listing 9-9: GROUP BY with count() on the stabr column
|
||||
|
||||
SELECT stabr, count(*)
|
||||
FROM pls_fy2014_pupld14a
|
||||
GROUP BY stabr
|
||||
ORDER BY count(*) DESC;
|
||||
|
||||
-- Listing 8-10: GROUP BY with count() on the stabr and stataddr columns
|
||||
-- Listing 9-10: GROUP BY with count() on the stabr and stataddr columns
|
||||
|
||||
SELECT stabr, stataddr, count(*)
|
||||
FROM pls_fy2014_pupld14a
|
||||
GROUP BY stabr, stataddr
|
||||
ORDER BY stabr ASC, count(*) DESC;
|
||||
|
||||
-- Listing 8-11: Using the sum() aggregate function to total visits to
|
||||
-- Listing 9-11: Using the sum() aggregate function to total visits to
|
||||
-- libraries in 2014 and 2009
|
||||
|
||||
-- 2014
|
||||
@ -263,7 +263,7 @@ SELECT sum(visits) AS visits_2009
|
||||
FROM pls_fy2009_pupld09a
|
||||
WHERE visits >= 0;
|
||||
|
||||
-- Listing 8-12: Using sum() to total visits on joined 2014 and 2009 library tables
|
||||
-- Listing 9-12: Using sum() to total visits on joined 2014 and 2009 library tables
|
||||
|
||||
SELECT sum(pls14.visits) AS visits_2014,
|
||||
sum(pls09.visits) AS visits_2009
|
||||
@ -271,7 +271,7 @@ FROM pls_fy2014_pupld14a pls14 JOIN pls_fy2009_pupld09a pls09
|
||||
ON pls14.fscskey = pls09.fscskey
|
||||
WHERE pls14.visits >= 0 AND pls09.visits >= 0;
|
||||
|
||||
-- Listing 8-13: Using GROUP BY to track percent change in library visits by state
|
||||
-- Listing 9-13: Using GROUP BY to track percent change in library visits by state
|
||||
|
||||
SELECT pls14.stabr,
|
||||
sum(pls14.visits) AS visits_2014,
|
||||
@ -284,7 +284,7 @@ WHERE pls14.visits >= 0 AND pls09.visits >= 0
|
||||
GROUP BY pls14.stabr
|
||||
ORDER BY pct_change DESC;
|
||||
|
||||
-- Listing 8-14: Using HAVING to filter the results of an aggregate query
|
||||
-- Listing 9-14: Using HAVING to filter the results of an aggregate query
|
||||
|
||||
SELECT pls14.stabr,
|
||||
sum(pls14.visits) AS visits_2014,
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 9 Code Examples
|
||||
-- Chapter 10 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 9-1: Importing the FSIS Meat, Poultry, and Egg Inspection Directory
|
||||
-- Listing 10-1: Importing the FSIS Meat, Poultry, and Egg Inspection Directory
|
||||
-- https://catalog.data.gov/dataset/meat-poultry-and-egg-inspection-directory-by-establishment-name
|
||||
|
||||
CREATE TABLE meat_poultry_egg_inspect (
|
||||
@ -30,7 +30,7 @@ CREATE INDEX company_idx ON meat_poultry_egg_inspect (company);
|
||||
-- Count the rows imported:
|
||||
SELECT count(*) FROM meat_poultry_egg_inspect;
|
||||
|
||||
-- Listing 9-2: Finding multiple companies at the same address
|
||||
-- Listing 10-2: Finding multiple companies at the same address
|
||||
SELECT company,
|
||||
street,
|
||||
city,
|
||||
@ -41,14 +41,14 @@ GROUP BY company, street, city, st
|
||||
HAVING count(*) > 1
|
||||
ORDER BY company, street, city, st;
|
||||
|
||||
-- Listing 9-3: Grouping and counting states
|
||||
-- Listing 10-3: Grouping and counting states
|
||||
SELECT st,
|
||||
count(*) AS st_count
|
||||
FROM meat_poultry_egg_inspect
|
||||
GROUP BY st
|
||||
ORDER BY st;
|
||||
|
||||
-- Listing 9-4: Using IS NULL to find missing values in the st column
|
||||
-- Listing 10-4: Using IS NULL to find missing values in the st column
|
||||
SELECT est_number,
|
||||
company,
|
||||
city,
|
||||
@ -57,7 +57,7 @@ SELECT est_number,
|
||||
FROM meat_poultry_egg_inspect
|
||||
WHERE st IS NULL;
|
||||
|
||||
-- Listing 9-5: Using GROUP BY and count() to find inconsistent company names
|
||||
-- Listing 10-5: Using GROUP BY and count() to find inconsistent company names
|
||||
|
||||
SELECT company,
|
||||
count(*) AS company_count
|
||||
@ -65,7 +65,7 @@ FROM meat_poultry_egg_inspect
|
||||
GROUP BY company
|
||||
ORDER BY company ASC;
|
||||
|
||||
-- Listing 9-6: Using length() and count() to test the zip column
|
||||
-- Listing 10-6: Using length() and count() to test the zip column
|
||||
|
||||
SELECT length(zip),
|
||||
count(*) AS length_count
|
||||
@ -73,7 +73,7 @@ FROM meat_poultry_egg_inspect
|
||||
GROUP BY length(zip)
|
||||
ORDER BY length(zip) ASC;
|
||||
|
||||
-- Listing 9-7: Filtering with length() to find short zip values
|
||||
-- Listing 10-7: Filtering with length() to find short zip values
|
||||
|
||||
SELECT st,
|
||||
count(*) AS st_count
|
||||
@ -82,7 +82,7 @@ WHERE length(zip) < 5
|
||||
GROUP BY st
|
||||
ORDER BY st ASC;
|
||||
|
||||
-- Listing 9-8: Backing up a table
|
||||
-- Listing 10-8: Backing up a table
|
||||
|
||||
CREATE TABLE meat_poultry_egg_inspect_backup AS
|
||||
SELECT * FROM meat_poultry_egg_inspect;
|
||||
@ -92,21 +92,21 @@ SELECT
|
||||
(SELECT count(*) FROM meat_poultry_egg_inspect) AS original,
|
||||
(SELECT count(*) FROM meat_poultry_egg_inspect_backup) AS backup;
|
||||
|
||||
-- Listing 9-9: Creating and filling the st_copy column with ALTER TABLE and UPDATE
|
||||
-- Listing 10-9: Creating and filling the st_copy column with ALTER TABLE and UPDATE
|
||||
|
||||
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN st_copy varchar(2);
|
||||
|
||||
UPDATE meat_poultry_egg_inspect
|
||||
SET st_copy = st;
|
||||
|
||||
-- Listing 9-10: Checking values in the st and st_copy columns
|
||||
-- Listing 10-10: Checking values in the st and st_copy columns
|
||||
|
||||
SELECT st,
|
||||
st_copy
|
||||
FROM meat_poultry_egg_inspect
|
||||
ORDER BY st;
|
||||
|
||||
-- Listing 9-11: Updating the st column for three establishments
|
||||
-- Listing 10-11: Updating the st column for three establishments
|
||||
|
||||
UPDATE meat_poultry_egg_inspect
|
||||
SET st = 'MN'
|
||||
@ -120,7 +120,7 @@ UPDATE meat_poultry_egg_inspect
|
||||
SET st = 'WI'
|
||||
WHERE est_number = 'M263A+P263A+V263A';
|
||||
|
||||
-- Listing 9-12: Restoring original st column values
|
||||
-- Listing 10-12: Restoring original st column values
|
||||
|
||||
-- Restoring from the column backup
|
||||
UPDATE meat_poultry_egg_inspect
|
||||
@ -132,14 +132,14 @@ SET st = backup.st
|
||||
FROM meat_poultry_egg_inspect_backup backup
|
||||
WHERE original.est_number = backup.est_number;
|
||||
|
||||
-- Listing 9-13: Creating and filling the company_standard column
|
||||
-- Listing 10-13: Creating and filling the company_standard column
|
||||
|
||||
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN company_standard varchar(100);
|
||||
|
||||
UPDATE meat_poultry_egg_inspect
|
||||
SET company_standard = company;
|
||||
|
||||
-- Listing 9-14: Use UPDATE to modify field values that match a string
|
||||
-- Listing 10-14: Use UPDATE to modify field values that match a string
|
||||
|
||||
UPDATE meat_poultry_egg_inspect
|
||||
SET company_standard = 'Armour-Eckrich Meats'
|
||||
@ -149,26 +149,26 @@ SELECT company, company_standard
|
||||
FROM meat_poultry_egg_inspect
|
||||
WHERE company LIKE 'Armour%';
|
||||
|
||||
-- Listing 9-15: Creating and filling the zip_copy column
|
||||
-- Listing 10-15: Creating and filling the zip_copy column
|
||||
|
||||
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN zip_copy varchar(5);
|
||||
|
||||
UPDATE meat_poultry_egg_inspect
|
||||
SET zip_copy = zip;
|
||||
|
||||
-- Listing 9-16: Modify codes in the zip column missing two leading zeros
|
||||
-- Listing 10-16: Modify codes in the zip column missing two leading zeros
|
||||
|
||||
UPDATE meat_poultry_egg_inspect
|
||||
SET zip = '00' || zip
|
||||
WHERE st IN('PR','VI') AND length(zip) = 3;
|
||||
|
||||
-- Listing 9-17: Modify codes in the zip column missing one leading zero
|
||||
-- Listing 10-17: Modify codes in the zip column missing one leading zero
|
||||
|
||||
UPDATE meat_poultry_egg_inspect
|
||||
SET zip = '0' || zip
|
||||
WHERE st IN('CT','MA','ME','NH','NJ','RI','VT') AND length(zip) = 4;
|
||||
|
||||
-- Listing 9-18: Creating and filling a state_regions table
|
||||
-- Listing 10-18: Creating and filling a state_regions table
|
||||
|
||||
CREATE TABLE state_regions (
|
||||
st varchar(2) CONSTRAINT st_key PRIMARY KEY,
|
||||
@ -179,38 +179,38 @@ COPY state_regions
|
||||
FROM 'C:\YourDirectory\state_regions.csv'
|
||||
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
||||
|
||||
-- Listing 9-19: Adding and updating an inspection_date column
|
||||
-- Listing 10-19: Adding and updating an inspection_date column
|
||||
|
||||
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN inspection_date date;
|
||||
|
||||
UPDATE meat_poultry_egg_inspect inspect
|
||||
SET inspection_date = '2019-12-01'
|
||||
SET inspection_date = '20110-12-01'
|
||||
WHERE EXISTS (SELECT state_regions.region
|
||||
FROM state_regions
|
||||
WHERE inspect.st = state_regions.st
|
||||
AND state_regions.region = 'New England');
|
||||
|
||||
-- Listing 9-20: Viewing updated inspection_date values
|
||||
-- Listing 10-20: Viewing updated inspection_date values
|
||||
|
||||
SELECT st, inspection_date
|
||||
FROM meat_poultry_egg_inspect
|
||||
GROUP BY st, inspection_date
|
||||
ORDER BY st;
|
||||
|
||||
-- Listing 9-21: Delete rows matching an expression
|
||||
-- Listing 10-21: Delete rows matching an expression
|
||||
|
||||
DELETE FROM meat_poultry_egg_inspect
|
||||
WHERE st IN('PR','VI');
|
||||
|
||||
-- Listing 9-22: Remove a column from a table using DROP
|
||||
-- Listing 10-22: Remove a column from a table using DROP
|
||||
|
||||
ALTER TABLE meat_poultry_egg_inspect DROP COLUMN zip_copy;
|
||||
|
||||
-- Listing 9-23: Remove a table from a database using DROP
|
||||
-- Listing 10-23: Remove a table from a database using DROP
|
||||
|
||||
DROP TABLE meat_poultry_egg_inspect_backup;
|
||||
|
||||
-- Listing 9-24: Demonstrating a transaction block
|
||||
-- Listing 10-24: Demonstrating a transaction block
|
||||
|
||||
-- Start transaction and perform update
|
||||
START TRANSACTION;
|
||||
@ -243,14 +243,14 @@ WHERE company = 'AGRO Merchants Oakland, LLC';
|
||||
|
||||
COMMIT;
|
||||
|
||||
-- Listing 9-25: Backing up a table while adding and filling a new column
|
||||
-- Listing 10-25: Backing up a table while adding and filling a new column
|
||||
|
||||
CREATE TABLE meat_poultry_egg_inspect_backup AS
|
||||
SELECT *,
|
||||
'2018-02-07'::date AS reviewed_date
|
||||
FROM meat_poultry_egg_inspect;
|
||||
|
||||
-- Listing 9-26: Swapping table names using ALTER TABLE
|
||||
-- Listing 10-26: Swapping table names using ALTER TABLE
|
||||
|
||||
ALTER TABLE meat_poultry_egg_inspect RENAME TO meat_poultry_egg_inspect_temp;
|
||||
ALTER TABLE meat_poultry_egg_inspect_backup RENAME TO meat_poultry_egg_inspect;
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 10 Code Examples
|
||||
-- Chapter 11 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 10-1: Create Census 2011-2015 ACS 5-Year stats table and import data
|
||||
-- Listing 11-1: Create Census 2011-2015 ACS 5-Year stats table and import data
|
||||
|
||||
CREATE TABLE acs_2011_2015_stats (
|
||||
geoid varchar(14) CONSTRAINT geoid_key PRIMARY KEY,
|
||||
@ -24,14 +24,14 @@ WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
||||
|
||||
SELECT * FROM acs_2011_2015_stats;
|
||||
|
||||
-- Listing 10-2: Using corr(Y, X) to measure the relationship between
|
||||
-- Listing 11-2: Using corr(Y, X) to measure the relationship between
|
||||
-- education and income
|
||||
|
||||
SELECT corr(median_hh_income, pct_bachelors_higher)
|
||||
AS bachelors_income_r
|
||||
FROM acs_2011_2015_stats;
|
||||
|
||||
-- Listing 10-3: Using corr(Y, X) on additional variables
|
||||
-- Listing 11-3: Using corr(Y, X) on additional variables
|
||||
|
||||
SELECT
|
||||
round(
|
||||
@ -45,7 +45,7 @@ SELECT
|
||||
) AS bachelors_travel_r
|
||||
FROM acs_2011_2015_stats;
|
||||
|
||||
-- Listing 10-4: Regression slope and intercept functions
|
||||
-- Listing 11-4: Regression slope and intercept functions
|
||||
|
||||
SELECT
|
||||
round(
|
||||
@ -56,7 +56,7 @@ SELECT
|
||||
) AS y_intercept
|
||||
FROM acs_2011_2015_stats;
|
||||
|
||||
-- Listing 10-5: Calculating the coefficient of determination, or r-squared
|
||||
-- Listing 11-5: Calculating the coefficient of determination, or r-squared
|
||||
|
||||
SELECT round(
|
||||
regr_r2(median_hh_income, pct_bachelors_higher)::numeric, 3
|
||||
@ -76,7 +76,7 @@ FROM acs_2011_2015_stats;
|
||||
SELECT covar_pop(median_hh_income, pct_bachelors_higher)
|
||||
FROM acs_2011_2015_stats;
|
||||
|
||||
-- Listing 10-6: The rank() and dense_rank() window functions
|
||||
-- Listing 11-6: The rank() and dense_rank() window functions
|
||||
|
||||
CREATE TABLE widget_companies (
|
||||
id bigserial,
|
||||
@ -102,7 +102,7 @@ SELECT
|
||||
dense_rank() OVER (ORDER BY widget_output DESC)
|
||||
FROM widget_companies;
|
||||
|
||||
-- Listing 10-7: Applying rank() within groups using PARTITION BY
|
||||
-- Listing 11-7: Applying rank() within groups using PARTITION BY
|
||||
|
||||
CREATE TABLE store_sales (
|
||||
store varchar(30),
|
||||
@ -130,7 +130,7 @@ SELECT
|
||||
rank() OVER (PARTITION BY category ORDER BY unit_sales DESC)
|
||||
FROM store_sales;
|
||||
|
||||
-- Listing 10-8: Create and fill a 2015 FBI crime data table
|
||||
-- Listing 11-8: Create and fill a 2015 FBI crime data table
|
||||
|
||||
CREATE TABLE fbi_crime_data_2015 (
|
||||
st varchar(20),
|
||||
@ -151,7 +151,7 @@ WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
||||
SELECT * FROM fbi_crime_data_2015
|
||||
ORDER BY population DESC;
|
||||
|
||||
-- Listing 10-9: Find property crime rates per thousand in cities with 500,000
|
||||
-- Listing 11-9: Find property crime rates per thousand in cities with 500,000
|
||||
-- or more people
|
||||
|
||||
SELECT
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 11 Code Examples
|
||||
-- Chapter 12 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 11-1: Extracting components of a timestamp value using date_part()
|
||||
-- Listing 12-1: Extracting components of a timestamp value using date_part()
|
||||
|
||||
SELECT
|
||||
date_part('year', '2019-12-01 18:37:12 EST'::timestamptz) AS "year",
|
||||
@ -23,7 +23,7 @@ SELECT
|
||||
|
||||
SELECT extract('year' from '2019-12-01 18:37:12 EST'::timestamptz) AS "year";
|
||||
|
||||
-- Listing 11-2: Three functions for making datetimes from components
|
||||
-- Listing 12-2: Three functions for making datetimes from components
|
||||
|
||||
-- make a date
|
||||
SELECT make_date(2018, 2, 22);
|
||||
@ -42,7 +42,7 @@ SELECT
|
||||
localtimestamp,
|
||||
now();
|
||||
|
||||
-- Listing 11-3: Comparing current_timestamp and clock_timestamp() during row insert
|
||||
-- Listing 12-3: Comparing current_timestamp and clock_timestamp() during row insert
|
||||
|
||||
CREATE TABLE current_time_example (
|
||||
time_id bigserial,
|
||||
@ -59,11 +59,11 @@ SELECT * FROM current_time_example;
|
||||
|
||||
-- Time Zones
|
||||
|
||||
-- Listing 11-4: Showing your PostgreSQL server's default time zone
|
||||
-- Listing 12-4: Showing your PostgreSQL server's default time zone
|
||||
|
||||
SHOW timezone; -- Note: You can see all run-time defaults with SHOW ALL;
|
||||
|
||||
-- Listing 11-5: Showing time zone abbreviations and names
|
||||
-- Listing 12-5: Showing time zone abbreviations and names
|
||||
|
||||
SELECT * FROM pg_timezone_abbrevs;
|
||||
SELECT * FROM pg_timezone_names;
|
||||
@ -72,7 +72,7 @@ SELECT * FROM pg_timezone_names;
|
||||
SELECT * FROM pg_timezone_names
|
||||
WHERE name LIKE 'Europe%';
|
||||
|
||||
-- Listing 11-6: Setting the time zone for a client session
|
||||
-- Listing 12-6: Setting the time zone for a client session
|
||||
|
||||
SET timezone TO 'US/Pacific';
|
||||
|
||||
@ -101,7 +101,7 @@ SELECT '9/30/1929'::date + '5 years'::interval;
|
||||
|
||||
-- Taxi Rides
|
||||
|
||||
-- Listing 11-7: Creating a table and importing NYC yellow taxi data
|
||||
-- Listing 12-7: Creating a table and importing NYC yellow taxi data
|
||||
|
||||
CREATE TABLE nyc_yellow_taxi_trips_2016_06_01 (
|
||||
trip_id bigserial PRIMARY KEY,
|
||||
@ -155,7 +155,7 @@ ON nyc_yellow_taxi_trips_2016_06_01 (tpep_pickup_datetime);
|
||||
|
||||
SELECT count(*) FROM nyc_yellow_taxi_trips_2016_06_01;
|
||||
|
||||
-- Listing 11-8: Counting taxi trips by hour
|
||||
-- Listing 12-8: Counting taxi trips by hour
|
||||
|
||||
SELECT
|
||||
date_part('hour', tpep_pickup_datetime) AS trip_hour,
|
||||
@ -164,7 +164,7 @@ FROM nyc_yellow_taxi_trips_2016_06_01
|
||||
GROUP BY trip_hour
|
||||
ORDER BY trip_hour;
|
||||
|
||||
-- Listing 11-9: Exporting taxi pickups per hour to a CSV file
|
||||
-- Listing 12-9: Exporting taxi pickups per hour to a CSV file
|
||||
|
||||
COPY
|
||||
(SELECT
|
||||
@ -177,7 +177,7 @@ COPY
|
||||
TO 'C:\YourDirectory\hourly_pickups_2016_06_01.csv'
|
||||
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
||||
|
||||
-- Listing 11-10: Calculating median trip time by hour
|
||||
-- Listing 12-10: Calculating median trip time by hour
|
||||
|
||||
SELECT
|
||||
date_part('hour', tpep_pickup_datetime) AS trip_hour,
|
||||
@ -188,7 +188,7 @@ FROM nyc_yellow_taxi_trips_2016_06_01
|
||||
GROUP BY trip_hour
|
||||
ORDER BY trip_hour;
|
||||
|
||||
-- Listing 11-11: Creating a table to hold train trip data
|
||||
-- Listing 12-11: Creating a table to hold train trip data
|
||||
|
||||
SET timezone TO 'US/Central';
|
||||
|
||||
@ -210,21 +210,21 @@ VALUES
|
||||
|
||||
SELECT * FROM train_rides;
|
||||
|
||||
-- Listing 11-12: Calculating the length of each trip segment
|
||||
-- Listing 12-12: Calculating the length of each trip segment
|
||||
|
||||
SELECT segment,
|
||||
to_char(departure, 'YYYY-MM-DD HH12:MI a.m. TZ') AS departure,
|
||||
arrival - departure AS segment_time
|
||||
FROM train_rides;
|
||||
|
||||
-- Listing 11-13: Calculating cumulative intervals using OVER
|
||||
-- Listing 12-13: Calculating cumulative intervals using OVER
|
||||
|
||||
SELECT segment,
|
||||
arrival - departure AS segment_time,
|
||||
sum(arrival - departure) OVER (ORDER BY trip_id) AS cume_time
|
||||
FROM train_rides;
|
||||
|
||||
-- Listing 11-14: Better formatting for cumulative trip time
|
||||
-- Listing 12-14: Better formatting for cumulative trip time
|
||||
|
||||
SELECT segment,
|
||||
arrival - departure AS segment_time,
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 12 Code Examples
|
||||
-- Chapter 13 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 12-1: Using a subquery in a WHERE clause
|
||||
-- Listing 13-1: Using a subquery in a WHERE clause
|
||||
|
||||
SELECT geo_name,
|
||||
state_us_abbreviation,
|
||||
@ -17,7 +17,7 @@ WHERE p0010001 >= (
|
||||
)
|
||||
ORDER BY p0010001 DESC;
|
||||
|
||||
-- Listing 12-2: Using a subquery in a WHERE clause for DELETE
|
||||
-- Listing 13-2: Using a subquery in a WHERE clause for DELETE
|
||||
|
||||
CREATE TABLE us_counties_2010_top10 AS
|
||||
SELECT * FROM us_counties_2010;
|
||||
@ -30,7 +30,7 @@ WHERE p0010001 < (
|
||||
|
||||
SELECT count(*) FROM us_counties_2010_top10;
|
||||
|
||||
-- Listing 12-3: Subquery as a derived table in a FROM clause
|
||||
-- Listing 13-3: Subquery as a derived table in a FROM clause
|
||||
|
||||
SELECT round(calcs.average, 0) as average,
|
||||
calcs.median,
|
||||
@ -43,7 +43,7 @@ FROM (
|
||||
)
|
||||
AS calcs;
|
||||
|
||||
-- Listing 12-4: Joining two derived tables
|
||||
-- Listing 13-4: Joining two derived tables
|
||||
|
||||
SELECT census.state_us_abbreviation AS st,
|
||||
census.st_population,
|
||||
@ -69,7 +69,7 @@ JOIN
|
||||
ON plants.st = census.state_us_abbreviation
|
||||
ORDER BY plants_per_million DESC;
|
||||
|
||||
-- Listing 12-5: Adding a subquery to a column list
|
||||
-- Listing 13-5: Adding a subquery to a column list
|
||||
|
||||
SELECT geo_name,
|
||||
state_us_abbreviation AS st,
|
||||
@ -78,7 +78,7 @@ SELECT geo_name,
|
||||
FROM us_counties_2010) AS us_median
|
||||
FROM us_counties_2010;
|
||||
|
||||
-- Listing 12-6: Using a subquery expression in a calculation
|
||||
-- Listing 13-6: Using a subquery expression in a calculation
|
||||
|
||||
SELECT geo_name,
|
||||
state_us_abbreviation AS st,
|
||||
@ -136,7 +136,7 @@ WHERE EXISTS (
|
||||
|
||||
|
||||
|
||||
-- Listing 12-7: Using a simple CTE to find large counties
|
||||
-- Listing 13-7: Using a simple CTE to find large counties
|
||||
|
||||
WITH
|
||||
large_counties (geo_name, st, p0010001)
|
||||
@ -158,7 +158,7 @@ WHERE p0010001 >= 100000
|
||||
GROUP BY state_us_abbreviation
|
||||
ORDER BY count(*) DESC;
|
||||
|
||||
-- Listing 12-8: Using CTEs in a table join
|
||||
-- Listing 13-8: Using CTEs in a table join
|
||||
|
||||
WITH
|
||||
counties (st, population) AS
|
||||
@ -179,7 +179,7 @@ FROM counties JOIN plants
|
||||
ON counties.st = plants.st
|
||||
ORDER BY per_million DESC;
|
||||
|
||||
-- Listing 12-9: Using CTEs to minimize redundant code
|
||||
-- Listing 13-9: Using CTEs to minimize redundant code
|
||||
|
||||
WITH us_median AS
|
||||
(SELECT percentile_cont(.5)
|
||||
@ -201,7 +201,7 @@ WHERE (p0010001 - us_median_pop)
|
||||
|
||||
CREATE EXTENSION tablefunc;
|
||||
|
||||
-- Listing 12-10: Creating and filling the ice_cream_survey table
|
||||
-- Listing 13-10: Creating and filling the ice_cream_survey table
|
||||
|
||||
CREATE TABLE ice_cream_survey (
|
||||
response_id integer PRIMARY KEY,
|
||||
@ -213,7 +213,7 @@ COPY ice_cream_survey
|
||||
FROM 'C:\YourDirectory\ice_cream_survey.csv'
|
||||
WITH (FORMAT CSV, HEADER);
|
||||
|
||||
-- Listing 12-11: Generating the ice cream survey crosstab
|
||||
-- Listing 13-11: Generating the ice cream survey crosstab
|
||||
|
||||
SELECT *
|
||||
FROM crosstab('SELECT office,
|
||||
@ -233,7 +233,7 @@ AS (office varchar(20),
|
||||
strawberry bigint,
|
||||
vanilla bigint);
|
||||
|
||||
-- Listing 12-12: Creating and filling a temperature_readings table
|
||||
-- Listing 13-12: Creating and filling a temperature_readings table
|
||||
|
||||
CREATE TABLE temperature_readings (
|
||||
reading_id bigserial PRIMARY KEY,
|
||||
@ -248,7 +248,7 @@ COPY temperature_readings
|
||||
FROM 'C:\YourDirectory\temperature_readings.csv'
|
||||
WITH (FORMAT CSV, HEADER);
|
||||
|
||||
-- Listing 12-13: Generating the temperature readings crosstab
|
||||
-- Listing 13-13: Generating the temperature readings crosstab
|
||||
|
||||
SELECT *
|
||||
FROM crosstab('SELECT
|
||||
@ -279,7 +279,7 @@ AS (station varchar(50),
|
||||
dec numeric(3,0)
|
||||
);
|
||||
|
||||
-- Listing 12-14: Re-classifying temperature data with CASE
|
||||
-- Listing 13-14: Re-classifying temperature data with CASE
|
||||
|
||||
SELECT max_temp,
|
||||
CASE WHEN max_temp >= 90 THEN 'Hot'
|
||||
@ -291,7 +291,7 @@ SELECT max_temp,
|
||||
END AS temperature_group
|
||||
FROM temperature_readings;
|
||||
|
||||
-- Listing 12-15: Using CASE in a Common Table Expression
|
||||
-- Listing 13-15: Using CASE in a Common Table Expression
|
||||
|
||||
WITH temps_collapsed (station_name, max_temperature_group) AS
|
||||
(SELECT station_name,
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 13 Code Examples
|
||||
-- Chapter 14 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Commonly used string functions
|
||||
@ -34,7 +34,7 @@ SELECT right('703-555-1212', 8);
|
||||
SELECT replace('bat', 'b', 'c');
|
||||
|
||||
|
||||
-- Table 13-2: Regular Expression Matching Examples
|
||||
-- Table 14-2: Regular Expression Matching Examples
|
||||
|
||||
-- Any character one or more times
|
||||
SELECT substring('The game starts at 7 p.m. on May 2, 2019.' from '.+');
|
||||
@ -54,7 +54,7 @@ SELECT substring('The game starts at 7 p.m. on May 2, 2019.' from 'May \d, \d{4}
|
||||
|
||||
-- Turning Text to Data with Regular Expression Functions
|
||||
|
||||
-- Listing 13-2: Creating and loading the crime_reports table
|
||||
-- Listing 14-2: Creating and loading the crime_reports table
|
||||
-- Data from https://sheriff.loudoun.gov/dailycrime
|
||||
|
||||
CREATE TABLE crime_reports (
|
||||
@ -75,29 +75,29 @@ WITH (FORMAT CSV, HEADER OFF, QUOTE '"');
|
||||
|
||||
SELECT original_text FROM crime_reports;
|
||||
|
||||
-- Listing 13-3: Using regexp_match() to find the first date
|
||||
-- Listing 14-3: Using regexp_match() to find the first date
|
||||
SELECT crime_id,
|
||||
regexp_match(original_text, '\d{1,2}\/\d{1,2}\/\d{2}')
|
||||
FROM crime_reports;
|
||||
|
||||
-- Listing 13-4: Using the regexp_matches() function with the 'g' flag
|
||||
-- Listing 14-4: Using the regexp_matches() function with the 'g' flag
|
||||
SELECT crime_id,
|
||||
regexp_matches(original_text, '\d{1,2}\/\d{1,2}\/\d{2}', 'g')
|
||||
FROM crime_reports;
|
||||
|
||||
-- Listing 13-5: Using regexp_match() to find the second date
|
||||
-- Listing 14-5: Using regexp_match() to find the second date
|
||||
-- Note that the result includes an unwanted hyphen
|
||||
SELECT crime_id,
|
||||
regexp_match(original_text, '-\d{1,2}\/\d{1,2}\/\d{1,2}')
|
||||
FROM crime_reports;
|
||||
|
||||
-- Listing 13-6: Using a capture group to return only the date
|
||||
-- Listing 14-6: Using a capture group to return only the date
|
||||
-- Eliminates the hyphen
|
||||
SELECT crime_id,
|
||||
regexp_match(original_text, '-(\d{1,2}\/\d{1,2}\/\d{1,2})')
|
||||
FROM crime_reports;
|
||||
|
||||
-- Listing 13-7: Matching case number, date, crime type, and city
|
||||
-- Listing 14-7: Matching case number, date, crime type, and city
|
||||
|
||||
SELECT
|
||||
regexp_match(original_text, '(?:C0|SO)[0-9]+') AS case_number,
|
||||
@ -127,7 +127,7 @@ SELECT crime_id,
|
||||
regexp_match(original_text, '(?:C0|SO)[0-9]+') AS case_number
|
||||
FROM crime_reports;
|
||||
|
||||
-- Listing 13-8: Retrieving a value from within an array
|
||||
-- Listing 14-8: Retrieving a value from within an array
|
||||
|
||||
SELECT
|
||||
crime_id,
|
||||
@ -135,7 +135,7 @@ SELECT
|
||||
AS case_number
|
||||
FROM crime_reports;
|
||||
|
||||
-- Listing 13-9: Updating the crime_reports date_1 column
|
||||
-- Listing 14-9: Updating the crime_reports date_1 column
|
||||
|
||||
UPDATE crime_reports
|
||||
SET date_1 =
|
||||
@ -151,7 +151,7 @@ SELECT crime_id,
|
||||
original_text
|
||||
FROM crime_reports;
|
||||
|
||||
-- Listing 13-10: Updating all crime_reports columns
|
||||
-- Listing 14-10: Updating all crime_reports columns
|
||||
|
||||
UPDATE crime_reports
|
||||
SET date_1 =
|
||||
@ -193,7 +193,7 @@ SET date_1 =
|
||||
description = (regexp_match(original_text, ':\s(.+)(?:C0|SO)'))[1],
|
||||
case_number = (regexp_match(original_text, '(?:C0|SO)[0-9]+'))[1];
|
||||
|
||||
-- Listing 13-11: Viewing selected crime data
|
||||
-- Listing 14-11: Viewing selected crime data
|
||||
|
||||
SELECT date_1,
|
||||
street,
|
||||
@ -201,7 +201,7 @@ SELECT date_1,
|
||||
crime_type
|
||||
FROM crime_reports;
|
||||
|
||||
-- Listing 13-12: Using regular expressions in a WHERE clause
|
||||
-- Listing 14-12: Using regular expressions in a WHERE clause
|
||||
|
||||
SELECT geo_name
|
||||
FROM us_counties_2010
|
||||
@ -214,7 +214,7 @@ WHERE geo_name ~* '.+ash.+' AND geo_name !~ 'Wash.+'
|
||||
ORDER BY geo_name;
|
||||
|
||||
|
||||
-- Listing 13-13: Regular expression functions to replace and split
|
||||
-- Listing 14-13: Regular expression functions to replace and split
|
||||
|
||||
SELECT regexp_replace('05/12/2018', '\d{4}', '2017');
|
||||
|
||||
@ -222,7 +222,7 @@ SELECT regexp_split_to_table('Four,score,and,seven,years,ago', ',');
|
||||
|
||||
SELECT regexp_split_to_array('Phil Mike Tony Steve', ' ');
|
||||
|
||||
-- Listing 13-14: Finding an array length
|
||||
-- Listing 14-14: Finding an array length
|
||||
|
||||
SELECT array_length(regexp_split_to_array('Phil Mike Tony Steve', ' '), 1);
|
||||
|
||||
@ -234,21 +234,21 @@ SELECT array_length(regexp_split_to_array('Phil Mike Tony Steve', ' '), 1);
|
||||
-- | (OR)
|
||||
-- ! (NOT)
|
||||
|
||||
-- Listing 13-15: Converting text to tsvector data
|
||||
-- Listing 14-15: Converting text to tsvector data
|
||||
|
||||
SELECT to_tsvector('I am walking across the sitting room to sit with you.');
|
||||
|
||||
-- Listing 13-16: Converting search terms to tsquery data
|
||||
-- Listing 14-16: Converting search terms to tsquery data
|
||||
|
||||
SELECT to_tsquery('walking & sitting');
|
||||
|
||||
-- Listing 13-17: Querying a tsvector type with a tsquery
|
||||
-- Listing 14-17: Querying a tsvector type with a tsquery
|
||||
|
||||
SELECT to_tsvector('I am walking across the sitting room') @@ to_tsquery('walking & sitting');
|
||||
|
||||
SELECT to_tsvector('I am walking across the sitting room') @@ to_tsquery('walking & running');
|
||||
|
||||
-- Listing 13-18: Creating and filling the president_speeches table
|
||||
-- Listing 14-18: Creating and filling the president_speeches table
|
||||
|
||||
-- Sources:
|
||||
-- https://archive.org/details/State-of-the-Union-Addresses-1945-2006
|
||||
@ -270,23 +270,23 @@ WITH (FORMAT CSV, DELIMITER '|', HEADER OFF, QUOTE '@');
|
||||
|
||||
SELECT * FROM president_speeches;
|
||||
|
||||
-- Listing 13-19: Converting speeches to tsvector in the search_speech_text column
|
||||
-- Listing 14-19: Converting speeches to tsvector in the search_speech_text column
|
||||
|
||||
UPDATE president_speeches
|
||||
SET search_speech_text = to_tsvector('english', speech_text);
|
||||
|
||||
-- Listing 13-20: Creating a GIN index for text search
|
||||
-- Listing 14-20: Creating a GIN index for text search
|
||||
|
||||
CREATE INDEX search_idx ON president_speeches USING gin(search_speech_text);
|
||||
|
||||
-- Listing 13-21: Finding speeches containing the word "Vietnam"
|
||||
-- Listing 14-21: Finding speeches containing the word "Vietnam"
|
||||
|
||||
SELECT president, speech_date
|
||||
FROM president_speeches
|
||||
WHERE search_speech_text @@ to_tsquery('Vietnam')
|
||||
ORDER BY speech_date;
|
||||
|
||||
-- Listing 13-22: Displaying search results with ts_headline()
|
||||
-- Listing 14-22: Displaying search results with ts_headline()
|
||||
|
||||
SELECT president,
|
||||
speech_date,
|
||||
@ -299,7 +299,7 @@ SELECT president,
|
||||
FROM president_speeches
|
||||
WHERE search_speech_text @@ to_tsquery('Vietnam');
|
||||
|
||||
-- Listing 13-23: Finding speeches with the word "transportation" but not "roads"
|
||||
-- Listing 14-23: Finding speeches with the word "transportation" but not "roads"
|
||||
|
||||
SELECT president,
|
||||
speech_date,
|
||||
@ -312,7 +312,7 @@ SELECT president,
|
||||
FROM president_speeches
|
||||
WHERE search_speech_text @@ to_tsquery('transportation & !roads');
|
||||
|
||||
-- Listing 13-24: Find speeches where "defense" follows "military"
|
||||
-- Listing 14-24: Find speeches where "defense" follows "military"
|
||||
|
||||
SELECT president,
|
||||
speech_date,
|
||||
@ -337,7 +337,7 @@ SELECT president,
|
||||
FROM president_speeches
|
||||
WHERE search_speech_text @@ to_tsquery('military <2> defense');
|
||||
|
||||
-- Listing 13-25: Scoring relevance with ts_rank()
|
||||
-- Listing 14-25: Scoring relevance with ts_rank()
|
||||
|
||||
SELECT president,
|
||||
speech_date,
|
||||
@ -348,7 +348,7 @@ WHERE search_speech_text @@ to_tsquery('war & security & threat & enemy')
|
||||
ORDER BY score DESC
|
||||
LIMIT 5;
|
||||
|
||||
-- Listing 13-26: Normalizing ts_rank() by speech length
|
||||
-- Listing 14-26: Normalizing ts_rank() by speech length
|
||||
|
||||
SELECT president,
|
||||
speech_date,
|
||||
|
||||
@ -2,27 +2,27 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 14 Code Examples
|
||||
-- Chapter 15 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- Listing 14-1: Creating a gis_analysis database
|
||||
-- Listing 15-1: Creating a gis_analysis database
|
||||
|
||||
CREATE DATABASE gis_analysis;
|
||||
-- Note: Switch to this new database before continuing the examples
|
||||
|
||||
-- Listing 14-2: Loading the PostGIS extension
|
||||
-- Listing 15-2: Loading the PostGIS extension
|
||||
|
||||
CREATE EXTENSION postgis;
|
||||
|
||||
SELECT postgis_full_version(); -- shows PostGIS version
|
||||
|
||||
-- Listing 14-3: Retrieving the well-known text for SRID 4326
|
||||
-- Listing 15-3: Retrieving the well-known text for SRID 4326
|
||||
|
||||
SELECT srtext
|
||||
FROM spatial_ref_sys
|
||||
WHERE srid = 4326;
|
||||
|
||||
-- Listing 14-4: Using ST_GeomFromText() to create spatial objects
|
||||
-- Listing 15-4: Using ST_GeomFromText() to create spatial objects
|
||||
|
||||
SELECT ST_GeomFromText('POINT(-74.9233606 42.699992)', 4326);
|
||||
|
||||
@ -44,24 +44,24 @@ SELECT ST_GeomFromText('MULTIPOLYGON((
|
||||
-74.98 42.64, -74.98 42.66,
|
||||
-75.0 42.66)))', 4326);
|
||||
|
||||
-- Listing 14-5: Using ST_GeogFromText() to create spatial objects
|
||||
-- Listing 15-5: Using ST_GeogFromText() to create spatial objects
|
||||
|
||||
SELECT
|
||||
ST_GeogFromText('SRID=4326;MULTIPOINT(-74.9 42.7, -75.1 42.7, -74.924 42.6)');
|
||||
|
||||
-- Listing 14-6: Functions specific to making points
|
||||
-- Listing 15-6: Functions specific to making points
|
||||
|
||||
SELECT ST_PointFromText('POINT(-74.9233606 42.699992)', 4326);
|
||||
|
||||
SELECT ST_MakePoint(-74.9233606, 42.699992);
|
||||
SELECT ST_SetSRID(ST_MakePoint(-74.9233606, 42.699992), 4326);
|
||||
|
||||
-- Listing 14-7: Functions specific to making LineStrings
|
||||
-- Listing 15-7: Functions specific to making LineStrings
|
||||
|
||||
SELECT ST_LineFromText('LINESTRING(-105.90 35.67,-105.91 35.67)', 4326);
|
||||
SELECT ST_MakeLine(ST_MakePoint(-74.92, 42.69), ST_MakePoint(-74.12, 42.45));
|
||||
|
||||
-- Listing 14-8: Functions specific to making Polygons
|
||||
-- Listing 15-8: Functions specific to making Polygons
|
||||
|
||||
SELECT ST_PolygonFromText('POLYGON((-74.9 42.7, -75.1 42.7,
|
||||
-75.1 42.6, -74.9 42.7))', 4326);
|
||||
@ -84,7 +84,7 @@ SELECT ST_MPolyFromText('MULTIPOLYGON((
|
||||
-- https://www.ams.usda.gov/local-food-directories/farmersmarkets
|
||||
|
||||
|
||||
-- Listing 14-9: Create and load the farmers_markets table
|
||||
-- Listing 15-9: Create and load the farmers_markets table
|
||||
|
||||
CREATE TABLE farmers_markets (
|
||||
fmid bigint PRIMARY KEY,
|
||||
@ -105,7 +105,7 @@ WITH (FORMAT CSV, HEADER);
|
||||
|
||||
SELECT count(*) FROM farmers_markets; -- should return 8,681 rows
|
||||
|
||||
-- Listing 14-10: Creating and indexing a geography column
|
||||
-- Listing 15-10: Creating and indexing a geography column
|
||||
-- There's also a function: https://postgis.net/docs/AddGeometryColumn.html
|
||||
|
||||
-- Add column
|
||||
@ -129,7 +129,7 @@ FROM farmers_markets
|
||||
WHERE longitude IS NOT NULL
|
||||
LIMIT 5;
|
||||
|
||||
-- Listing 14-11: Using ST_DWithin() to locate farmers' markets within 10 kilometers of a point
|
||||
-- Listing 15-11: Using ST_DWithin() to locate farmers' markets within 10 kilometers of a point
|
||||
|
||||
SELECT market_name,
|
||||
city,
|
||||
@ -140,7 +140,7 @@ WHERE ST_DWithin(geog_point,
|
||||
10000)
|
||||
ORDER BY market_name;
|
||||
|
||||
-- Listing 14-12: Using ST_Distance() to calculate the miles between Yankee Stadium
|
||||
-- Listing 15-12: Using ST_Distance() to calculate the miles between Yankee Stadium
|
||||
-- and Citi Field (Mets)
|
||||
-- 1609.344 meters/mile
|
||||
|
||||
@ -149,7 +149,7 @@ SELECT ST_Distance(
|
||||
ST_GeogFromText('POINT(-73.8480153 40.7570917)')
|
||||
) / 1609.344 AS mets_to_yanks;
|
||||
|
||||
-- Listing 14-13: Using ST_Distance() for each row in farmers_markets
|
||||
-- Listing 15-13: Using ST_Distance() for each row in farmers_markets
|
||||
|
||||
SELECT market_name,
|
||||
city,
|
||||
@ -173,13 +173,13 @@ ORDER BY miles_from_dt ASC;
|
||||
-- Cartographic Boundary Shapefiles - Counties
|
||||
-- https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html
|
||||
|
||||
-- Listing 14-14: Checking the geom column's well-known text representation
|
||||
-- Listing 15-14: Checking the geom column's well-known text representation
|
||||
|
||||
SELECT ST_AsText(geom)
|
||||
FROM us_counties_2010_shp
|
||||
LIMIT 1;
|
||||
|
||||
-- Listing 14-15: Find the largest counties by area using ST_Area()
|
||||
-- Listing 15-15: Find the largest counties by area using ST_Area()
|
||||
|
||||
SELECT name10,
|
||||
statefp10 AS st,
|
||||
@ -190,7 +190,7 @@ FROM us_counties_2010_shp
|
||||
ORDER BY square_miles DESC
|
||||
LIMIT 5;
|
||||
|
||||
-- Listing 14-16: Using ST_Within() to find the county belonging to a pair of coordinates
|
||||
-- Listing 15-16: Using ST_Within() to find the county belonging to a pair of coordinates
|
||||
|
||||
SELECT name10,
|
||||
statefp10
|
||||
@ -219,7 +219,7 @@ WHERE ST_Within('SRID=4269;POINT(-118.3419063 34.0977076)'::geometry, geom);
|
||||
-- https://www.census.gov/geo/reference/mtfcc.html
|
||||
-- Here, H3010: A natural flowing waterway
|
||||
|
||||
-- Listing 14-17: Using ST_GeometryType() to determine geometry
|
||||
-- Listing 15-17: Using ST_GeometryType() to determine geometry
|
||||
|
||||
SELECT ST_GeometryType(geom)
|
||||
FROM santafe_linearwater_2016
|
||||
@ -229,7 +229,7 @@ SELECT ST_GeometryType(geom)
|
||||
FROM santafe_roads_2016
|
||||
LIMIT 1;
|
||||
|
||||
-- Listing 14-18: Spatial join with ST_Intersects() to find roads crossing the Santa Fe river
|
||||
-- Listing 15-18: Spatial join with ST_Intersects() to find roads crossing the Santa Fe river
|
||||
|
||||
SELECT water.fullname AS waterway,
|
||||
roads.rttyp,
|
||||
@ -239,7 +239,7 @@ FROM santafe_linearwater_2016 water JOIN santafe_roads_2016 roads
|
||||
WHERE water.fullname = 'Santa Fe Riv'
|
||||
ORDER BY roads.fullname;
|
||||
|
||||
-- Listing 14-19: Using ST_Intersection() to show where roads cross the river
|
||||
-- Listing 15-19: Using ST_Intersection() to show where roads cross the river
|
||||
|
||||
SELECT water.fullname AS waterway,
|
||||
roads.rttyp,
|
||||
|
||||
@ -2,12 +2,12 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 15 Code Examples
|
||||
-- Chapter 17 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- VIEWS
|
||||
|
||||
-- Listing 15-1: Creating a view that displays Nevada 2010 counties
|
||||
-- Listing 17-1: Creating a view that displays Nevada 2010 counties
|
||||
|
||||
CREATE OR REPLACE VIEW nevada_counties_pop_2010 AS
|
||||
SELECT geo_name,
|
||||
@ -18,13 +18,13 @@ CREATE OR REPLACE VIEW nevada_counties_pop_2010 AS
|
||||
WHERE state_us_abbreviation = 'NV'
|
||||
ORDER BY county_fips;
|
||||
|
||||
-- Listing 15-2: Querying the nevada_counties_pop_2010 view
|
||||
-- Listing 17-2: Querying the nevada_counties_pop_2010 view
|
||||
|
||||
SELECT *
|
||||
FROM nevada_counties_pop_2010
|
||||
LIMIT 5;
|
||||
|
||||
-- Listing 15-3: Creating a view showing population change for US counties
|
||||
-- Listing 17-3: Creating a view showing population change for US counties
|
||||
|
||||
CREATE OR REPLACE VIEW county_pop_change_2010_2000 AS
|
||||
SELECT c2010.geo_name,
|
||||
@ -40,7 +40,7 @@ CREATE OR REPLACE VIEW county_pop_change_2010_2000 AS
|
||||
AND c2010.county_fips = c2000.county_fips
|
||||
ORDER BY c2010.state_fips, c2010.county_fips;
|
||||
|
||||
-- Listing 15-4: Selecting columns from the county_pop_change_2010_2000 view
|
||||
-- Listing 17-4: Selecting columns from the county_pop_change_2010_2000 view
|
||||
|
||||
SELECT geo_name,
|
||||
st,
|
||||
@ -50,7 +50,7 @@ FROM county_pop_change_2010_2000
|
||||
WHERE st = 'NV'
|
||||
LIMIT 5;
|
||||
|
||||
-- Listing 15-5: Creating a view on the employees table
|
||||
-- Listing 17-5: Creating a view on the employees table
|
||||
|
||||
CREATE OR REPLACE VIEW employees_tax_dept AS
|
||||
SELECT emp_id,
|
||||
@ -64,7 +64,7 @@ CREATE OR REPLACE VIEW employees_tax_dept AS
|
||||
|
||||
SELECT * FROM employees_tax_dept;
|
||||
|
||||
-- Listing 15-6: Successful and rejected inserts via the employees_tax_dept view
|
||||
-- Listing 17-6: Successful and rejected inserts via the employees_tax_dept view
|
||||
|
||||
INSERT INTO employees_tax_dept (first_name, last_name, dept_id)
|
||||
VALUES ('Suzanne', 'Legere', 1);
|
||||
@ -77,7 +77,7 @@ SELECT * FROM employees_tax_dept;
|
||||
|
||||
SELECT * FROM employees;
|
||||
|
||||
-- Listing 15-7: Updating a row via the employees_tax_dept view
|
||||
-- Listing 17-7: Updating a row via the employees_tax_dept view
|
||||
|
||||
UPDATE employees_tax_dept
|
||||
SET last_name = 'Le Gere'
|
||||
@ -90,7 +90,7 @@ UPDATE employees_tax_dept
|
||||
SET salary = 100000
|
||||
WHERE emp_id = 5;
|
||||
|
||||
-- Listing 15-8: Deleting a row via the employees_tax_dept view
|
||||
-- Listing 17-8: Deleting a row via the employees_tax_dept view
|
||||
|
||||
DELETE FROM employees_tax_dept
|
||||
WHERE emp_id = 5;
|
||||
@ -99,7 +99,7 @@ WHERE emp_id = 5;
|
||||
-- FUNCTIONS
|
||||
-- https://www.postgresql.org/docs/current/static/plpgsql.html
|
||||
|
||||
-- Listing 15-9: Creating a percent_change function
|
||||
-- Listing 17-9: Creating a percent_change function
|
||||
-- To delete this function: DROP FUNCTION percent_change(numeric,numeric,integer);
|
||||
|
||||
CREATE OR REPLACE FUNCTION
|
||||
@ -114,11 +114,11 @@ LANGUAGE SQL
|
||||
IMMUTABLE
|
||||
RETURNS NULL ON NULL INPUT;
|
||||
|
||||
-- Listing 15-10: Testing the percent_change() function
|
||||
-- Listing 17-10: Testing the percent_change() function
|
||||
|
||||
SELECT percent_change(110, 108, 2);
|
||||
|
||||
-- Listing 15-11: Testing percent_change() on Census data
|
||||
-- Listing 17-11: Testing percent_change() on Census data
|
||||
|
||||
SELECT c2010.geo_name,
|
||||
c2010.state_us_abbreviation AS st,
|
||||
@ -132,7 +132,7 @@ ON c2010.state_fips = c2000.state_fips
|
||||
ORDER BY pct_chg_func DESC
|
||||
LIMIT 5;
|
||||
|
||||
-- Listing 15-12: Adding a column to the teachers table and seeing the data
|
||||
-- Listing 17-12: Adding a column to the teachers table and seeing the data
|
||||
|
||||
ALTER TABLE teachers ADD COLUMN personal_days integer;
|
||||
|
||||
@ -142,7 +142,7 @@ SELECT first_name,
|
||||
personal_days
|
||||
FROM teachers;
|
||||
|
||||
-- Listing 15-13: Creating an update_personal_days() function
|
||||
-- Listing 17-13: Creating an update_personal_days() function
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_personal_days()
|
||||
RETURNS void AS $$
|
||||
@ -161,11 +161,11 @@ $$ LANGUAGE plpgsql;
|
||||
-- To run the function:
|
||||
SELECT update_personal_days();
|
||||
|
||||
-- Listing 15-14: Enabling the PL/Python procedural language
|
||||
-- Listing 17-14: Enabling the PL/Python procedural language
|
||||
|
||||
CREATE EXTENSION plpythonu;
|
||||
|
||||
-- Listing 15-15: Using PL/Python to create the trim_county() function
|
||||
-- Listing 17-15: Using PL/Python to create the trim_county() function
|
||||
|
||||
CREATE OR REPLACE FUNCTION trim_county(input_string text)
|
||||
RETURNS text AS $$
|
||||
@ -174,7 +174,7 @@ RETURNS text AS $$
|
||||
return cleaned
|
||||
$$ LANGUAGE plpythonu;
|
||||
|
||||
-- Listing 15-16: Testing the trim_county() function
|
||||
-- Listing 17-16: Testing the trim_county() function
|
||||
|
||||
SELECT geo_name,
|
||||
trim_county(geo_name)
|
||||
@ -185,7 +185,7 @@ LIMIT 5;
|
||||
|
||||
-- TRIGGERS
|
||||
|
||||
-- Listing 15-17: Creating the grades and grades_history tables
|
||||
-- Listing 17-17: Creating the grades and grades_history tables
|
||||
|
||||
CREATE TABLE grades (
|
||||
student_id bigint,
|
||||
@ -212,7 +212,7 @@ CREATE TABLE grades_history (
|
||||
PRIMARY KEY (student_id, course_id, change_time)
|
||||
);
|
||||
|
||||
-- Listing 15-18: Creating the record_if_grade_changed() function
|
||||
-- Listing 17-18: Creating the record_if_grade_changed() function
|
||||
|
||||
CREATE OR REPLACE FUNCTION record_if_grade_changed()
|
||||
RETURNS trigger AS
|
||||
@ -238,7 +238,7 @@ BEGIN
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Listing 15-19: Creating the grades_update trigger
|
||||
-- Listing 17-19: Creating the grades_update trigger
|
||||
|
||||
CREATE TRIGGER grades_update
|
||||
AFTER UPDATE
|
||||
@ -246,7 +246,7 @@ CREATE TRIGGER grades_update
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE record_if_grade_changed();
|
||||
|
||||
-- Listing 15-20: Testing the grades_update trigger
|
||||
-- Listing 17-20: Testing the grades_update trigger
|
||||
|
||||
-- Initially, there should be 0 records in the history
|
||||
SELECT * FROM grades_history;
|
||||
@ -267,7 +267,7 @@ SELECT student_id,
|
||||
new_grade
|
||||
FROM grades_history;
|
||||
|
||||
-- Listing 15-21: Creating a temperature_test table
|
||||
-- Listing 17-21: Creating a temperature_test table
|
||||
|
||||
CREATE TABLE temperature_test (
|
||||
station_name varchar(50),
|
||||
@ -278,7 +278,7 @@ CREATE TABLE temperature_test (
|
||||
PRIMARY KEY (station_name, observation_date)
|
||||
);
|
||||
|
||||
-- Listing 15-22: Creating the classify_max_temp() function
|
||||
-- Listing 17-22: Creating the classify_max_temp() function
|
||||
|
||||
CREATE OR REPLACE FUNCTION classify_max_temp()
|
||||
RETURNS trigger AS
|
||||
@ -301,7 +301,7 @@ BEGIN
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Listing 15-23: Creating the temperature_insert trigger
|
||||
-- Listing 17-23: Creating the temperature_insert trigger
|
||||
|
||||
CREATE TRIGGER temperature_insert
|
||||
BEFORE INSERT
|
||||
@ -309,7 +309,7 @@ CREATE TRIGGER temperature_insert
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE classify_max_temp();
|
||||
|
||||
-- Listing 15-24: Inserting rows to test the temperature_update trigger
|
||||
-- Listing 17-24: Inserting rows to test the temperature_update trigger
|
||||
|
||||
INSERT INTO temperature_test (station_name, observation_date, max_temp, min_temp)
|
||||
VALUES
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 16 Code Examples
|
||||
-- Chapter 18 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
|
||||
@ -16,12 +16,12 @@ psql -d analysis -U postgres
|
||||
\c [database name] [user name]
|
||||
\c gis_analysis postgres
|
||||
|
||||
-- Listing 16-1: Entering a single-line query in psql
|
||||
-- Listing 18-1: Entering a single-line query in psql
|
||||
-- Enter this at the psql prompt:
|
||||
|
||||
SELECT geo_name FROM us_counties_2010 LIMIT 3;
|
||||
|
||||
-- Listing 16-2: Entering a multi-line query in psql
|
||||
-- Listing 18-2: Entering a multi-line query in psql
|
||||
-- Type each line separately, followed by Enter
|
||||
|
||||
SELECT geo_name
|
||||
@ -29,23 +29,23 @@ FROM us_counties_2010
|
||||
LIMIT 3;
|
||||
|
||||
|
||||
-- Listing 16-3: Showing open parentheses in the psql prompt
|
||||
-- Listing 18-3: Showing open parentheses in the psql prompt
|
||||
|
||||
CREATE TABLE wineries (
|
||||
id bigint,
|
||||
winery_name varchar(100)
|
||||
);
|
||||
|
||||
-- Listing 16-4: A query with scrolling results
|
||||
-- Listing 18-4: A query with scrolling results
|
||||
|
||||
SELECT geo_name FROM us_counties_2010;
|
||||
|
||||
-- Listings 16-5 and 16-6: Normal and expanded displays of results
|
||||
-- Listings 18-5 and 18-6: Normal and expanded displays of results
|
||||
-- Use \x to toggle expanded on/off
|
||||
|
||||
SELECT * FROM grades;
|
||||
|
||||
-- Listing 16-7: Importing data using \copy
|
||||
-- Listing 18-7: Importing data using \copy
|
||||
|
||||
DROP TABLE state_regions;
|
||||
|
||||
@ -56,7 +56,7 @@ CREATE TABLE state_regions (
|
||||
|
||||
\copy state_regions FROM 'C:\YourDirectory\state_regions.csv' WITH (FORMAT CSV, HEADER);
|
||||
|
||||
-- Listing 16-8: Saving query output to a file
|
||||
-- Listing 18-8: Saving query output to a file
|
||||
|
||||
-- Enter psql settings
|
||||
\a \f , \pset footer
|
||||
|
||||
@ -2,18 +2,18 @@
|
||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||
-- by Anthony DeBarros
|
||||
|
||||
-- Chapter 17 Code Examples
|
||||
-- Chapter 19 Code Examples
|
||||
--------------------------------------------------------------
|
||||
|
||||
-- VACUUM
|
||||
|
||||
-- Listing 17-1: Creating a table to test vacuuming
|
||||
-- Listing 19-1: Creating a table to test vacuuming
|
||||
|
||||
CREATE TABLE vacuum_test (
|
||||
integer_column integer
|
||||
);
|
||||
|
||||
-- Listing 17-2: Determining the size of vacuum_test
|
||||
-- Listing 19-2: Determining the size of vacuum_test
|
||||
|
||||
SELECT pg_size_pretty(
|
||||
pg_total_relation_size('vacuum_test')
|
||||
@ -24,7 +24,7 @@ SELECT pg_size_pretty(
|
||||
pg_database_size('analysis')
|
||||
);
|
||||
|
||||
-- Listing 17-3: Inserting 500,000 rows into vacuum_test
|
||||
-- Listing 19-3: Inserting 500,000 rows into vacuum_test
|
||||
|
||||
INSERT INTO vacuum_test
|
||||
SELECT * FROM generate_series(1,500000);
|
||||
@ -34,7 +34,7 @@ SELECT pg_size_pretty(
|
||||
pg_table_size('vacuum_test')
|
||||
);
|
||||
|
||||
-- Listing 17-4: Updating all rows in vacuum_test
|
||||
-- Listing 19-4: Updating all rows in vacuum_test
|
||||
|
||||
UPDATE vacuum_test
|
||||
SET integer_column = integer_column + 1;
|
||||
@ -44,7 +44,7 @@ SELECT pg_size_pretty(
|
||||
pg_table_size('vacuum_test')
|
||||
);
|
||||
|
||||
-- Listing 17-5: Viewing autovacuum statistics for vacuum_test
|
||||
-- Listing 19-5: Viewing autovacuum statistics for vacuum_test
|
||||
|
||||
SELECT relname,
|
||||
last_vacuum,
|
||||
@ -59,7 +59,7 @@ SELECT *
|
||||
FROM pg_stat_all_tables
|
||||
WHERE relname = 'vacuum_test';
|
||||
|
||||
-- Listing 17-6: Running VACUUM manually
|
||||
-- Listing 19-6: Running VACUUM manually
|
||||
|
||||
VACUUM vacuum_test;
|
||||
|
||||
@ -67,7 +67,7 @@ VACUUM; -- vacuums the whole database
|
||||
|
||||
VACUUM VERBOSE; -- provides messages
|
||||
|
||||
-- Listing 17-7: Using VACUUM FULL to reclaim disk space
|
||||
-- Listing 19-7: Using VACUUM FULL to reclaim disk space
|
||||
|
||||
VACUUM FULL vacuum_test;
|
||||
|
||||
@ -78,11 +78,11 @@ SELECT pg_size_pretty(
|
||||
|
||||
-- SETTINGS
|
||||
|
||||
-- Listing 17-8: Showing the location of postgresql.conf
|
||||
-- Listing 19-8: Showing the location of postgresql.conf
|
||||
|
||||
SHOW config_file;
|
||||
|
||||
-- Listing 17-10: Show the location of the data directory
|
||||
-- Listing 19-10: Show the location of the data directory
|
||||
|
||||
SHOW data_directory;
|
||||
|
||||
@ -93,12 +93,12 @@ SHOW data_directory;
|
||||
|
||||
-- BACKUP AND RESTORE
|
||||
|
||||
-- Listing 17-11: Backing up the analysis database with pg_dump
|
||||
-- Listing 19-11: Backing up the analysis database with pg_dump
|
||||
pg_dump -d analysis -U [user_name] -Fc > analysis_backup.sql
|
||||
|
||||
-- Back up just a table
|
||||
pg_dump -t 'train_rides' -d analysis -U [user_name] -Fc > train_backup.sql
|
||||
|
||||
-- Listing 17-12: Restoring the analysis database with pg_restore
|
||||
-- Listing 19-12: Restoring the analysis database with pg_restore
|
||||
|
||||
pg_restore -C -d postgres -U postgres analysis_backup_custom.sql
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user