PostgresQL Flashcards
What is PostgreSQL and what are some alternative relational databases?
It’s a relational database. Some other relational databases are MySQL, Oracle, and SQLite
Important commands for starting, stopping and checking status for PostgreSQL
Sudo service postgreSQL (start / stop / status)
Is PostgreSQL its own application?
Yes! We just interact with it. No building or debugging —> we send it a request and it sends a response.
What is pgweb?
Not our code —> just code somebody else wrote to help us visualize our database.
What are some advantages of learning a relational database?
It’s used literally everywhere. It also allows you to store your data somewhere secure.
What is one way to see if PostgreSQL is running?
Sudo service postgresql status
What is a database schema?
A collection of tables — the design of a database —> like a blueprint
What is SQL and how is it different from languages like JavaScript?
It’s the primary way of interacting with relational databases —> a declarative programming language —> programmers describe the results they want the the end gets the result
How do you retrieve specific columns from a database table?
Use a select statement formatted as…
select “desired-column”,
“another-column”
from “table-name”;
How do you filter rows based on some specific criteria?
Use a where clause formatted as…
where “column-name” = ‘text-to-query’;
If you are adding a number, bool, etc, should you keep the quotes?
Nope — remove them!
What are the benefits of formatting your SQL?
It makes it easier to read, understand, and debug.
What are four comparison operators that can be used in a where clause?
=, !=, <, >
How do you limit the number of rows returned in a result set?
Limit x(integer);
How do you retrieve all columns from a database table?
Use a select statement formatted as…
select *
from “table-name”;
How do you control the sort order of a result set?
Order by “column-to-sort-by”;
Default order is ascending or descending?
Ascending order. (Smallest to largest)
How do you add a row to a SQL table?
Use an insert statement.
INSERT INTO “table-name” (“column1”, “column2”)
VALUES (‘value1’, ‘value2’)
What is a tuple?
An ordered set of values typically wrapped in parenthesis
How do you add multiple rows to a SQL table at once?
Specify more than one set of tuples seperated by commas (they’ll both be returned if you use a returning statement)
How do you get back the row being inserted into a table with a separate select statement?
Add a returning clause after the values clause
How do you update rows in a database table?
Use an sql update statement
UPDATE “table-to-update”
SET “column-to-update” = ‘value-to-update-with’
WHERE “column-name” = ‘value’
Why is it important to include a where clause in your update statements?
If you don’t include one, you’ll overwrite all data in that column (very bad yikes RIP)
How do you delete rows from a database table?
Use “delete”
DELETE
FROM “table-name”
WHERE “column-name” = ‘value’
RETURNING *
How do you accidentally delete all rows from a table?
You leave off the where statement (big oops)
What is a foreign key?
A constraint column with data that refers to values in another table that links those tables together
How do you join two SQL tables?
SELECT *
FROM “table-name”
JOIN “table-to-join” using (“column-to-join-by”)
What is JOIN ON?
Same as join, but give specific IDs to join on.
JOIN “table2” on “table1”.“table1ID” = “table2”.“table2ID”
How do you temporarily rename columns or tables in a SQL statement?
SELECT “p”.“name” as “product”,
“p”.“category”,
“s”.“name” as “supplier,
FROM “products” as “p”
JOIN “suppliers” as “s” using (“supplierId”);
What are some examples of aggregate functions?
Max(), Avg(), Count(), Min(), Sum(), Every()
What is the purpose of a group by clause?
Performing aggregate functions only on a specific group of rows