PostgreSQL Flashcards

1
Q

PostgreSQL

use Homebrew to start and stop the psql server

A

brew services start postgresql

brew services stop postgresql

brew services restart postgresql

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

PostgreSQL

create a new database from the command line

A

$ createdb dbname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

PostgreSQL

delete a database from the command line

A

$ dropdb dbname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

PostgreSQL

backup or export a database from the command line

A

$ pg_dump dbname > file.sql

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

PostgreSQL

restore a database from an sql file

A

$ psql -d dbname < file.sql

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

PostgreSQL

open an existing database from the command line

A

$ psql dbname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

PostgreSQL

print a list of current databases

A

\l

\list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

PostgreSQL

switch to a different database

A

\c dbname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

PostgreSQL

list existing tables

A

\dt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

PostgreSQL

create a new table in a database

A

CREATE TABLE

ex

CREATE TABLE users

( id serial,

username CHAR(25) NOT NULL,

enabled boolean DEFAULT TRUE,

PRIMARY KEY (id)

);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

PostgreSQL

different data types you can choose for fields added with CREATE TABLE

A

serial

varchar

boolean

date

time

timestamp

int

decimal / numeric

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

PostgreSQL

constraints / restrictions that can be specified for columns in a PostgreSQL db

A

NOT NULL

NULL

DEFAULT

UNIQUE

PRIMARY KEY

FOREIGN KEY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

PostgreSQL

list all the columns of the users table

A

\d users

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

PostgreSQL

add a new record to the users table

A

INSERT INTO

ex

INSERT INTO users (id, username, enabled)

VALUES (20, ‘Anthony Giuliano’, true);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

PostgreSQL

list all the rows and columns from the users table

A

SELECT

ex

SELECT * FROM users;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

PostgreSQL

clause to add criteria to a SELECT statement so you can filter the results

A

WHERE

ex

SELECT *

FROM users

WHERE id = 20;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

PostgreSQL

clause to sort the results of a SELECT query

A

ORDER BY

ex

SELECT *

FROM users

ORDER BY id;

SELECT *

FROM users

ORDER BY id DESC;

SELECT *

FROM users

ORDER BY enabled, id DESC;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

PostgreSQL

command to alter records in the users table

A

UPDATE

updates all records

UPDATE users

SET enabled = false;

updates a specific record specified by where clause UPDATE users

SET enabled = true

WHERE username = ‘John Smith’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

PostgreSQL

clause used with WHERE to compare a string to a pattern

A

LIKE

ex

% is a wildcard character

SELECT *

FROM users

WHERE username LIKE ‘%II’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

PostgreSQL

functions applied to a column in order to remove whitespace

A

trim()

rtrim()

ltrim()

ex

SELECT *

FROM users

WHERE rtrim(username) LIKE ‘% II’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

PostgreSQL

command to delete rows from a table

A

DELETE

ex

DELETE FROM users

WHERE id = 20;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

PostgreSQL

command to change the schema of a table

A

ALTER TABLE

ex

ALTER TABLE users

ADD COLUMN last_login TIMESTAMP NOT NULL DEFAULT NOW();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

PostgreSQL

function that returns the current date and time

A

NOW()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

PostgreSQL

clause to change the name of an existing column

A

RENAME COLUMN TO

ex

ALTER TABLE users

RENAME COLUMN username TO full_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

PostgreSQL

clause to change the data type of an existing column

A

ALTER COLUMN TYPE

ex

ALTER TABLE users

ALTER COLUMN full_name TYPE VARCHAR(25);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

PostgreSQL

clause to remove a column from an existing table

A

DROP COLUMN

ex

ALTER TABLE users

DROP COLUMN enabled;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

PostgreSQL

clause to rename an existing table in your database

A

RENAME TO

ex

ALTER TABLE users

RENAME TO all_users;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

PostgreSQL

command to delete an entire table from your database

A

DROP TABLE

ex

DROP TABLE all_users;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

PostgreSQL

ERD (entity relationship diagram)

A

a graphical representation of the tables in a database and the relationships between them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

PostgreSQL

set up a One to One relationship between two tables

A

The primary key of the first table is both the foreign key and the primary key of the second table.

ex

Users

CREATE TABLE users (

id serial,

username varchar not null,

primary key (id) );

Addresses

CREATE TABLE addresses (

user_id int not null,

city varchar not null,

state varchar not null,

primary key (user_id),

CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users(id) );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

PostgreSQL

set up a One to Many relationship between two tables

A

The primary key of the ‘one’ table is the foreign key of the ‘many’ table.

ex

Books table

CREATE TABLE books (

id serial,

title varchar not null,

primary key (id) );

Reviews table

CREATE TABLE reviews (

id serial,

book_id int not null,

review_content varchar,

primary key (id),

foreign key (book_id) references books (id) on delete cascade );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

PostgreSQL

an alias for NOW()

A

CURRENT_TIMESTAMP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

PostgreSQL

set up a a Many to Many relationship between books and users

A

CREATE TABLE books_users (

user_id int not null,

book_id int not null,

checkout_date timestamp,

return_date timestamp,

primary key (user_id, book_id), – composite key

foreign key (user_id) references users (id) on update cascade,

foreign key (book_id) references books (id) on update cascade );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

PostgreSQL

clause to ensure that child records in other tables are deleted

A

ON DELETE CASCADE

Add this clause when setting the foreign key field on the table

ex

CREATE TABLE reviews (

id serial,

book_id int not null,

user_id int not null,

review_content varchar,

rating int,

published_date timestamp default current_timestamp,

primary key (id),

foreign key (book_id) references books (id) on delete cascade,

foreign key (user_id) references users (id) on delete cascade );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

PostgreSQL

syntax to make a column a foreign key

A

FOREIGN KEY (field) REFERENCES table (field)

ex

CREATE TABLE users_books (

book_id int NOT NULL,

FOREIGN KEY (book_id) REFERENCES books (id) ON DELETE CASCADE );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

PostgreSQL

make a composite key on a join table

A

PRIMARY KEY (field1, field2)

ex

CREATE TABLE users_books (

book_id int NOT NULL,

user_id int NOT NULL,

PRIMARY KEY (book_id, user_id),

FOREIGN KEY (book_id) REFERENCES books (id) ON UPDATE CASCADE,

FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

PostgreSQL

set a unique restriction on a field

A

UNIQUE (field)

ex

CREATE TABLE users (

email varchar,

UNIQUE (email) );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

PostgreSQL

inner join syntax

A

SELECT users.*, addresses.*

FROM users INNER JOIN addresses

ON users.id = addresses.user_id;

An inner join connects two tables based on the equality of one field from each table. It returns records where the two tables intersect (i.e. only records that exist in both tables).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

PostgreSQL

left join syntax

A

SELECT users.*, addresses.*

FROM users LEFT JOIN addresses

ON users.id = addresses.user_id;

A left join returns all matching records in table A based on certain conditions. with columns from table A and B.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

PostgreSQL

cross join syntax

A

SELECT users.*, addresses.*

FROM users CROSS JOIN addresses;

SELECT *

FROM users CROSS JOIN addresses;

A cross join, or cartensian join, returns all records from both tables. There is no ON clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

PostgreSQL

right join syntax

A

SELECT users.*, addresses.*

FROM users RIGHT JOIN addresses

ON users.id = addresses.user_id;

A right join returns all matching records in table B based on certain conditions. with columns from table A and B.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

PostgreSQL

export records to CSV

A

COPY (query)

TO ‘path’

WITH CSV HEADER;

ex

COPY

( SELECT users.*, addresses.* FROM users INNER JOIN addresses ON users.id = addresses.user_id )

TO /absolute/path/to/export.csv

WITH CSV HEADER;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

PostgreSQL

the difference between COPY and \copy

A

COPY is server side and therefore requires absolute file paths;

\copy allows you to specify relative filepaths by initiating copies from the client. COPY is faster.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

PostgreSQL

import records from CSV

A

COPY table_name (field1, field2, field3) FROM ‘path/to/data.csv’ WITH (DELIMITER ‘delim’, HEADER 1, FILEFORMAT CSV);

ex

COPY collections (advance_id, date_booked, amount, management_fee) FROM ‘/users/anthonygiuliano/Desktop/collections_import.csv’ WITH (HEADER 1, FORMAT CSV, DELIMITER ‘,’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

PostgreSQL

combine criteria in a WHERE clause

A

AND / OR

ex

SELECT *

FROM users

WHERE username = ‘John Smith’ OR username = ‘Jane Smiley’;

ex

SELECT *

FROM books

WHERE title LIKE ‘%My%’ AND author = ‘John Mayer’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

PostgreSQL

function that combines columns and / or strings together into one field

A

CONCAT

ex

SELECT CONCAT(title, ‘ ‘, ‘by’, ‘ ‘, author) AS “Books By”

FROM books;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

PostgreSQL

alias a column name in your results

A

AS

ex

SELECT count(id) AS enabled_count

FROM users

WHERE enabled = true;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

PostgreSQL

filter out duplicate rows in a query

A

DISTINCT

ex

SELECT DISTINCT u.username

FROM users u

LEFT JOIN users_books ub ON u.id = ub.user_id;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

PostgreSQL

cap the number of records returned by a select statement

A

LIMIT / OFFSET

ex

SELECT *

FROM users

LIMIT 10 OFFSET 10;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

PostgreSQL

clause used with WHERE to return records that don’t exist in another set of records

A

NOT IN

ex

SELECT u.username

FROM users u

WHERE u.id NOT IN

( SELECT ub.user_id FROM users_books ub );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

PostgreSQL

function that returns the length of a string

A

length()

ex

SELECT * FROM books WHERE length(author) = 0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

PostgreSQL

functions that aggregate record count, minimum and maximum values

A

count()

min()

max()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

PostgreSQL

functions that return the current date, time and timestamp

A

current_date()

current_time()

now()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

PostgreSQL

use count to return the number of unique values in a column

A

COUNT(DISTINCT field)

ex

SELECT COUNT(DISTINCT description)

FROM transactions;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

PostgreSQL

return the sums of various groups of records in one query

A

SUM, GROUP BY

ex

SELECT SUM(paid_out) AS paid_out_total, description

FROM transactions

GROUP BY description;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

PostgreSQL

function to select subfields from date / time values

A

EXTRACT(SUBFIELD FROM date_field)

ex

SELECT SUM(paid_out), EXTRACT(MONTH FROM date)

FROM transactions

GROUP BY EXTRACT(MONTH FROM date);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

PostgreSQL

function to convert various data types to formatted strings

A

TO_CHAR

ex

SELECT SUM(paid_out), TO_CHAR(date, ‘Month’)

FROM transactions

GROUP BY TO_CHAR(date, ‘Month’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

PostgreSQL

create a field whose value can be one of only a limited number of options

A

CREATE TYPE day AS ENUM(‘monday’, ‘tuesday’, ‘wednesday’, ‘thursday’, ‘friday’, ‘saturday’, ‘sunday’);

Then use like any other data type

CREATE TABLE messages(

id SERIAL,

day_of_week DAY DEFAULT NULL,

message VARCHAR NOT NULL,

PRIMARY KEY (id) );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

PostgreSQL

three different kinds of INTEGER data types

A

SMALLINT: -32768 to 32767

INT: -2147483648 to 2147483647

BIGINT: -9223372036854775808 to 9223372036854775807

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

PostgreSQL

clause to ensure that an integer column accept only positive numbers

A

CHECK

CREATE TABLE people (

age SMALLINT CHECK (age > 0) );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

PostgreSQL

create a column that contains decimals

A

NUMERIC / DECIMAL(total_digits, decimal_digits)

ex

CREATE TABLE decimal_values (

id SERIAL,

item VARCHAR NOT NULL,

value DECIMAL(13, 2) NOT NULL,

PRIMARY KEY (id) );

INSERT INTO decimal_values (item, value)

VALUES (‘Test Item’, 12.35);

INSERT INTO decimal_values (item, value)

VALUES (‘Test Item’, 12.355);

INSERT INTO decimal_values (item, value)

VALUES (‘Test Item’, 12.354);

SELECT * FROM decimal_values;

id | item | value

—-+———–+——-

1 | Test Item | 12.35

2 | Test Item | 12.36

3 | Test Item | 12.35

(3 rows)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

PostgreSQL

create an index on a single column

A

CREATE INDEX ON table (field);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

PostgreSQL

delete an existing index on a single column

A

DROP INDEX index_name

Get the index name by looking at \d table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

PostgreSQL

brew services start postgresql

brew services stop postgresql

brew services restart postgresql

A

use Homebrew to start and stop the psql server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

PostgreSQL

$ createdb dbname

A

create a new database from the command line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

PostgreSQL

$ dropdb dbname

A

delete a database from the command line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

PostgreSQL

$ pg_dump dbname > file.sql

A

backup or export a database from the command line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

PostgreSQL

$ psql -d dbname < file.sql

A

restore a database from an sql file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

PostgreSQL

$ psql dbname

A

open an existing database from the command line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

PostgreSQL

\l

\list

A

print a list of current databases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

PostgreSQL

\c dbname

A

switch to a different database

72
Q

PostgreSQL

\dt

A

list existing tables

73
Q

PostgreSQL

CREATE TABLE

ex

CREATE TABLE users

( id serial,

username CHAR(25) NOT NULL,

enabled boolean DEFAULT TRUE,

PRIMARY KEY (id)

);

A

create a new table in a database

74
Q

PostgreSQL

serial

varchar

boolean

date

time

timestamp

int

decimal / numeric

A

different data types you can choose for fields added with CREATE TABLE

75
Q

PostgreSQL

NOT NULL

NULL

DEFAULT

UNIQUE

PRIMARY KEY

FOREIGN KEY

A

constraints / restrictions that can be specified for columns in a PostgreSQL db

76
Q

PostgreSQL

\d users

A

list all the columns of the users table

77
Q

PostgreSQL

INSERT INTO

ex

INSERT INTO users (id, username, enabled)

VALUES (20, ‘Anthony Giuliano’, true);

A

add a new record to the users table

78
Q

PostgreSQL

SELECT

ex

SELECT * FROM users;

A

list all the rows and columns from the users table

79
Q

PostgreSQL

WHERE

ex

SELECT *

FROM users

WHERE id = 20;

A

clause to add criteria to a SELECT statement so you can filter the results

80
Q

PostgreSQL

ORDER BY

ex

SELECT *

FROM users

ORDER BY id;

SELECT *

FROM users

ORDER BY id DESC;

SELECT *

FROM users

ORDER BY enabled, id DESC;

A

clause to sort the results of a SELECT query

81
Q

PostgreSQL

UPDATE

updates all records

UPDATE users

SET enabled = false;

updates a specific record specified by where clause UPDATE users

SET enabled = true

WHERE username = ‘John Smith’;

A

command to alter records in the users table

82
Q

PostgreSQL

LIKE

ex

% is a wildcard character

SELECT *

FROM users

WHERE username LIKE ‘%II’;

A

clause used with WHERE to compare a string to a pattern

83
Q

PostgreSQL

trim()

rtrim()

ltrim()

ex

SELECT *

FROM users

WHERE rtrim(username) LIKE ‘% II’;

A

functions applied to a column in order to remove whitespace

84
Q

PostgreSQL

DELETE

ex

DELETE FROM users

WHERE id = 20;

A

command to delete rows from a table

85
Q

PostgreSQL

ALTER TABLE

ex

ALTER TABLE users

ADD COLUMN last_login TIMESTAMP NOT NULL DEFAULT NOW();

A

command to change the schema of a table

86
Q

PostgreSQL

NOW()

A

function that returns the current date and time

87
Q

PostgreSQL

RENAME COLUMN TO

ex

ALTER TABLE users

RENAME COLUMN username TO full_name;

A

clause to change the name of an existing column

88
Q

PostgreSQL

ALTER COLUMN TYPE

ex

ALTER TABLE users

ALTER COLUMN full_name TYPE VARCHAR(25);

A

clause to change the data type of an existing column

89
Q

PostgreSQL

DROP COLUMN

ex

ALTER TABLE users

DROP COLUMN enabled;

A

clause to remove a column from an existing table

90
Q

PostgreSQL

RENAME TO

ex

ALTER TABLE users

RENAME TO all_users;

A

clause to rename an existing table in your database

91
Q

PostgreSQL

DROP TABLE

ex

DROP TABLE all_users;

A

command to delete an entire table from your database

92
Q

PostgreSQL

a graphical representation of the tables in a database and the relationships between them.

A

ERD (entity relationship diagram)

93
Q

PostgreSQL

The primary key of the first table is both the foreign key and the primary key of the second table.

ex

Users

CREATE TABLE users (

id serial,

username varchar not null,

primary key (id) );

Addresses

CREATE TABLE addresses (

user_id int not null,

city varchar not null,

state varchar not null,

primary key (user_id),

CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users(id) );

A

set up a One to One relationship between two tables

94
Q

PostgreSQL

The primary key of the ‘one’ table is the foreign key of the ‘many’ table.

ex

Books table

CREATE TABLE books (

id serial,

title varchar not null,

primary key (id) );

Reviews table

CREATE TABLE reviews (

id serial,

book_id int not null,

review_content varchar,

primary key (id),

foreign key (book_id) references books (id) on delete cascade );

A

set up a One to Many relationship between two tables

95
Q

PostgreSQL

CURRENT_TIMESTAMP

A

an alias for NOW()

96
Q

PostgreSQL

CREATE TABLE books_users (

user_id int not null,

book_id int not null,

checkout_date timestamp,

return_date timestamp,

primary key (user_id, book_id), – composite key

foreign key (user_id) references users (id) on update cascade,

foreign key (book_id) references books (id) on update cascade );

A

set up a a Many to Many relationship between books and users

97
Q

PostgreSQL

ON DELETE CASCADE

Add this clause when setting the foreign key field on the table

ex

CREATE TABLE reviews (

id serial,

book_id int not null,

user_id int not null,

review_content varchar,

rating int,

published_date timestamp default current_timestamp,

primary key (id),

foreign key (book_id) references books (id) on delete cascade,

foreign key (user_id) references users (id) on delete cascade );

A

clause to ensure that child records in other tables are deleted

98
Q

PostgreSQL

FOREIGN KEY (field) REFERENCES table (field)

ex

CREATE TABLE users_books (

book_id int NOT NULL,

FOREIGN KEY (book_id) REFERENCES books (id) ON DELETE CASCADE );

A

syntax to make a column a foreign key

99
Q

PostgreSQL

PRIMARY KEY (field1, field2)

ex

CREATE TABLE users_books (

book_id int NOT NULL,

user_id int NOT NULL,

PRIMARY KEY (book_id, user_id),

FOREIGN KEY (book_id) REFERENCES books (id) ON UPDATE CASCADE,

FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE );

A

make a composite key on a join table

100
Q

PostgreSQL

UNIQUE (field)

ex

CREATE TABLE users (

email varchar,

UNIQUE (email) );

A

set a unique restriction on a field

101
Q

PostgreSQL

SELECT users.*, addresses.*

FROM users INNER JOIN addresses

ON users.id = addresses.user_id;

An inner join connects two tables based on the equality of one field from each table. It returns records where the two tables intersect (i.e. only records that exist in both tables).

A

inner join syntax

102
Q

PostgreSQL

SELECT users.*, addresses.*

FROM users LEFT JOIN addresses

ON users.id = addresses.user_id;

A left join returns all matching records in table A based on certain conditions. with columns from table A and B.

A

left join syntax

103
Q

PostgreSQL

SELECT users.*, addresses.*

FROM users CROSS JOIN addresses;

SELECT *

FROM users CROSS JOIN addresses;

A cross join, or cartensian join, returns all records from both tables. There is no ON clause.

A

cross join syntax

104
Q

PostgreSQL

SELECT users.*, addresses.*

FROM users RIGHT JOIN addresses

ON users.id = addresses.user_id;

A right join returns all matching records in table B based on certain conditions. with columns from table A and B.

A

right join syntax

105
Q

PostgreSQL

COPY (query)

TO ‘path’

WITH CSV HEADER;

ex

COPY

( SELECT users.*, addresses.* FROM users INNER JOIN addresses ON users.id = addresses.user_id )

TO /absolute/path/to/export.csv

WITH CSV HEADER;

A

export records to CSV

106
Q

PostgreSQL

COPY is server side and therefore requires absolute file paths;

\copy allows you to specify relative filepaths by initiating copies from the client. COPY is faster.

A

the difference between COPY and \copy

107
Q

PostgreSQL

COPY table_name (field1, field2, field3) FROM ‘path/to/data.csv’ WITH (DELIMITER ‘delim’, HEADER 1, FILEFORMAT CSV);

ex

COPY collections (advance_id, date_booked, amount, management_fee) FROM ‘/users/anthonygiuliano/Desktop/collections_import.csv’ WITH (HEADER 1, FORMAT CSV, DELIMITER ‘,’)

A

import records from CSV

108
Q

PostgreSQL

AND / OR

ex

SELECT *

FROM users

WHERE username = ‘John Smith’ OR username = ‘Jane Smiley’;

ex

SELECT *

FROM books

WHERE title LIKE ‘%My%’ AND author = ‘John Mayer’;

A

combine criteria in a WHERE clause

109
Q

PostgreSQL

CONCAT

ex

SELECT CONCAT(title, ‘ ‘, ‘by’, ‘ ‘, author) AS “Books By”

FROM books;

A

function that combines columns and / or strings together into one field

110
Q

PostgreSQL

AS

ex

SELECT count(id) AS enabled_count

FROM users

WHERE enabled = true;

A

alias a column name in your results

111
Q

PostgreSQL

DISTINCT

ex

SELECT DISTINCT u.username

FROM users u

LEFT JOIN users_books ub ON u.id = ub.user_id;

A

filter out duplicate rows in a query

112
Q

PostgreSQL

LIMIT / OFFSET

ex

SELECT *

FROM users

LIMIT 10 OFFSET 10;

A

cap the number of records returned by a select statement

113
Q

PostgreSQL

NOT IN

ex

SELECT u.username

FROM users u

WHERE u.id NOT IN

( SELECT ub.user_id FROM users_books ub );

A

clause used with WHERE to return records that don’t exist in another set of records

114
Q

PostgreSQL

length()

ex

SELECT * FROM books WHERE length(author) = 0;

A

function that returns the length of a string

115
Q

PostgreSQL

count()

min()

max()

A

functions that aggregate record count, minimum and maximum values

116
Q

PostgreSQL

current_date()

current_time()

now()

A

functions that return the current date, time and timestamp

117
Q

PostgreSQL

COUNT(DISTINCT field)

ex

SELECT COUNT(DISTINCT description)

FROM transactions;

A

use count to return the number of unique values in a column

118
Q

PostgreSQL

SUM, GROUP BY

ex

SELECT SUM(paid_out) AS paid_out_total, description

FROM transactions

GROUP BY description;

A

return the sums of various groups of records in one query

119
Q

PostgreSQL

EXTRACT(SUBFIELD FROM date_field)

ex

SELECT SUM(paid_out), EXTRACT(MONTH FROM date)

FROM transactions

GROUP BY EXTRACT(MONTH FROM date);

A

function to select subfields from date / time values

120
Q

PostgreSQL

TO_CHAR

ex

SELECT SUM(paid_out), TO_CHAR(date, ‘Month’)

FROM transactions

GROUP BY TO_CHAR(date, ‘Month’);

A

function to convert various data types to formatted strings

121
Q

PostgreSQL

CREATE TYPE day AS ENUM(‘monday’, ‘tuesday’, ‘wednesday’, ‘thursday’, ‘friday’, ‘saturday’, ‘sunday’);

Then use like any other data type

CREATE TABLE messages(

id SERIAL,

day_of_week DAY DEFAULT NULL,

message VARCHAR NOT NULL,

PRIMARY KEY (id) );

A

create a field whose value can be one of only a limited number of options

122
Q

PostgreSQL

SMALLINT: -32768 to 32767

INT: -2147483648 to 2147483647

BIGINT: -9223372036854775808 to 9223372036854775807

A

three different kinds of INTEGER data types

123
Q

PostgreSQL

CHECK

CREATE TABLE people (

age SMALLINT CHECK (age > 0) );

A

clause to ensure that an integer column accept only positive numbers

124
Q

PostgreSQL

NUMERIC / DECIMAL(total_digits, decimal_digits)

ex

CREATE TABLE decimal_values (

id SERIAL,

item VARCHAR NOT NULL,

value DECIMAL(13, 2) NOT NULL,

PRIMARY KEY (id) );

INSERT INTO decimal_values (item, value)

VALUES (‘Test Item’, 12.35);

INSERT INTO decimal_values (item, value)

VALUES (‘Test Item’, 12.355);

INSERT INTO decimal_values (item, value)

VALUES (‘Test Item’, 12.354);

SELECT * FROM decimal_values;

id | item | value

—-+———–+——-

1 | Test Item | 12.35

2 | Test Item | 12.36

3 | Test Item | 12.35

(3 rows)

A

create a column that contains decimals

125
Q

PostgreSQL

CREATE INDEX ON table (field);

A

create an index on a single column

126
Q

PostgreSQL

DROP INDEX index_name

Get the index name by looking at \d table

A

delete an existing index on a single column

127
Q

PostgreSQL

What kind of programming language is SQL?

A

SQL is a “special purpose language”, since it’s typically only used to interact with relational databases

128
Q

PostgreSQL

What are the three sublanguages of SQL?

A

Data Definition Language (DDL)

Data Manipulation Language (DML)

Data Control Langugage (DCL)

129
Q

PostgreSQL

What function returns a lowercased version of a string?

A

lower

ex

SELECT lower(‘AlPhAbEt’);

130
Q

PostgreSQL

How does the psql console display true and false values?

A

t and f

131
Q

PostgreSQL

Use SQL to compute the surface area of a sphere with a radius of 26.3cm, truncated to return an integer

A

SELECT trunc(4 * pi() * 26.3 ^ 2);

132
Q

PostgreSQL

SQL is a “special purpose language”, since it’s typically only used to interact with relational databases

A

What kind of programming language is SQL?

133
Q

PostgreSQL

Data Definition Language (DDL)

Data Manipulation Language (DML)

Data Control Langugage (DCL)

A

What are the three sublanguages of SQL?

134
Q

PostgreSQL

lower

ex

SELECT lower(‘AlPhAbEt’);

A

What function returns a lowercased version of a string?

135
Q

PostgreSQL

t and f

A

How does the psql console display true and false values?

136
Q

PostgreSQL

SELECT trunc(4 * pi() * 26.3 ^ 2);

A

Use SQL to compute the surface area of a sphere with a radius of 26.3cm, truncated to return an integer

137
Q

PostgreSQL

add a constraint to an existing table

A

ALTER TABLE ADD

ex

ALTER TABLE films

ADD CHECK(year > 0);

138
Q

PostgreSQL

ALTER TABLE ADD

ex

ALTER TABLE films

ADD CHECK(year > 0);

A

add a constraint to an existing table

139
Q

PostgreSQL

create a new sequence called ‘counter’

A

CREATE SEQUENCE counter;

140
Q

PostgreSQL

return the next value from the sequence called ‘counter’

A

SELECT nextval(‘counter’);

141
Q

PostgreSQL

delete the sequence called ‘counter’

A

DROP SEQUENCE counter;

142
Q

PostgreSQL

CREATE SEQUENCE counter;

A

create a new sequence called ‘counter’

143
Q

PostgreSQL

SELECT nextval(‘counter’);

A

return the next value from the sequence called ‘counter’

144
Q

PostgreSQL

DROP SEQUENCE counter;

A

delete the sequence called ‘counter’

145
Q

PostgreSQL

create a sequence called ‘even_counter’ that starts at 2 and increments by 2

A

CREATE SEQUENCE even_counter

INCREMENT BY 2

MINVALUE 2;

146
Q

PostgreSQL

CREATE SEQUENCE even_counter

INCREMENT BY 2

MINVALUE 2;

A

create a sequence called ‘even_counter’ that starts at 2 and increments by 2

147
Q

PostgreSQL

aggregate function that works like ruby’s Array#join

A

string_agg(field, delimiter)

ex

select (year / 10 * 10) as decade, genre, string_agg(title, ‘, ‘) as films from films group by decade, genre order by decade, genre;

148
Q

PostgreSQL

string_agg(field, delimiter)

ex

select (year / 10 * 10) as decade, genre, string_agg(title, ‘, ‘) as films from films group by decade, genre order by decade, genre;

A

aggregate function that works like ruby’s Array#join

149
Q

PostgreSQL

special value that represents the absence of any other value

A

NULL

150
Q

PostgreSQL

NULL

A

special value that represents the absence of any other value

151
Q

PostgreSQL

high level design focused on identifying entities and their relationships

A

conceptual schema

152
Q

PostgreSQL

low level database specific design focused on implementation

A

physical schema

153
Q

PostgreSQL

conceptual schema

A

high level design focused on identifying entities and their relationships

154
Q

PostgreSQL

physical schema

A

low level database specific design focused on implementation

155
Q

PostgreSQL

the number of objects on each side of a relationship (1:1, 1:M, M:M)

A

cardinality

156
Q

PostgreSQL

cardinality

A

the number of objects on each side of a relationship (1:1, 1:M, M:M)

157
Q

PostgreSQL

whether the relationship is required (1) or optional (0)

A

modality

158
Q

PostgreSQL

modality

A

whether the relationship is required (1) or optional (0)

159
Q

PostgreSQL

a constraint that enforces certain rules about what values are permitted in a foreign key field

A

foreign key constraint

160
Q

PostgreSQL

foreign key constraint

A

a constraint that enforces certain rules about what values are permitted in a foreign key field

161
Q

PostgreSQL

create a foreign key column

A

create a column of the same type as the primary key column it will point to, followed by a REFERENCES constraint

ex

CREATE TABLE orders (

id serial primary key,

product id int REFERENCES products (id) );

162
Q

PostgreSQL

create a column of the same type as the primary key column it will point to, followed by a REFERENCES constraint

ex

CREATE TABLE orders (

id serial primary key,

product id int REFERENCES products (id) );

A

create a foreign key column

163
Q

add a foreign key constraint on an existing field

A

ALTER TABLE table_name

ADD FOREIGN KEY (field_name)

REFERENCES other_table (field_name);

ex

alter table addresses add foreign key (user_id) references users (id);

164
Q

PostgreSQL

add a not null constraint to an existing column called product_id on the orders table

A

ALTER TABLE orders

ALTER COLUMN product_id

SET not null;

165
Q

PostgreSQL

true or false: a foreign key constrain prevents that column from having NULL values

A

false; it is common to include NOT NULL and FOREIGN KEY constraints together

166
Q

PostgreSQL

a situation where the database becomes inconsistent, so it contains more than one answer for a given question

A

update anomoly

167
Q

PostgreSQL

update anomoly

A

a situation where the database becomes inconsistent, so it contains more than one answer for a given question

168
Q

PostgreSQL

a situation where the schema can’t store the information for an entity without having an entry in a related entity

A

insertion anomaly

169
Q

PostgreSQL

insertion anomaly

A

a situation where the schema can’t store the information for an entity without having an entry in a related entity

170
Q

PostgreSQL

a situation where we lose all the information about an entity if we delete records from a related entity

A

deletion anomaly

171
Q

PostgreSQL

deletion anomaly

A

a situation where we lose all the information about an entity if we delete records from a related entity

172
Q

PostgreSQL

the process of designing schema that minimizes or eliminates the possible occurrence of update, insertion and deletion anomalies

A

normalization

173
Q

PostgreSQL

normalization

A

the process of designing schema that minimizes or eliminates the possible occurrence of update, insertion and deletion anomalies

174
Q

PostgreSQL

clause used with WHERE to compare a string to a pattern, case insensitive

A

ILIKE

ex

% is a wildcard character

SELECT *

FROM users

WHERE username ILIKE ‘%II’;

175
Q

PostgreSQL

ILIKE

ex

% is a wildcard character

SELECT *

FROM users

WHERE username ILIKE ‘%II’;

A

clause used with WHERE to compare a string to a pattern, case insensitive