PostgreSQL Flashcards
What is PostgreSQL and what are some alternative relational databases?
Relational database system.
SQLite, MySQL, Oracle, SQL Azure, Teradata
What are some advantages of learning a relational database?
Relational databases are commonly referred to as “SQL databases” because you usually do work with them using some variation of the SQL language.
If you are storing related data, then a relational database is probably a good first choice. A quality of many relational databases is that they support good guarantees about data integrity. They can store and modify data in a way that makes data corruption as unlikely as possible.
What is one way to see if PostgreSQL is running?
sudo service postgresql status
What is a database schema?
A collection of tables is called a schema. A schema defines how the data in a relational database should be organized.
What is a table?
A table is a list of rows each having the same set of attributes.
What is a row?
You might visualize a database table as a sort of spreadsheet where each row is a record in that spreadsheet.
What is SQL and how is it different from languages like JavaScript?
You may be familiar with imperative programming languages such as JavaScript, where you basically tell the JavaScript runtime what to do and how to do it.
QL is a declarative programming language. In declarative languages, programmers describe the results they want.
How do you retrieve specific columns from a database table?
select “column name”
from “table”
How do you filter rows based on some specific criteria?
where “filter topic” = ‘category’
What are the benefits of formatting your SQL?
SQL does not have to be indented, but you should do it anyway for consistent style and therefore readability.
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 #;
How do you retrieve all columns from a database table?
select *
How do you control the sort order of a result set?
desc
(The default sort order of the results is ascending order)
How do you add a row to a SQL table?
insert into “table” (‘column name’)
values (‘value’)
What is a tuple?
In SQL, a list of values is referred to as a tuple.
How do you add multiple rows to a SQL table at once?
insert into “products” (“name”, “description”, “price”, “category”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’),
(‘Tater Mitts’, ‘Scrub some taters!’, 6, ‘cooking’)
returning *;
How do you get back the row being inserted into a table without a separate select statement?
returning *;
If you only want specific values back, you can use a comma-separated list of column names instead of an * asterisk.
How do you update rows in a database table?
update “products”
set “price” = 100;
Why is it important to include a where clause in your update statements?
it would update every row in the table!
How do you delete rows from a database table?
delete
from “products”
where “productId” = 24
returning *;
How do you accidentally delete all rows from a table?
if you don’t specify “where”
What is a foreign key?
usually IDs
How do you join two SQL tables?
select *
from “products”
join “suppliers” using (“supplierId”);
How do you temporarily rename columns or tables in a SQL statement?
select “p”.”name” as “product”,
“p”.”category”,
“s”.”name” as “supplier”,
“s”.”state”
from “products” as “p”
join “suppliers” as “s” using (“supplierId”);