PostgreSQL Flashcards
What is PostgreSQL and what are some alternative relational databases?
- PostgreSQL is a powerful, free, open source Relational Database Management System. (relational databases use some variation of the SQL language… opposite of NoSQL)
- alternatives: mySQL, SQLite, Oracle
What are some advantages of learning a relational database?
- they are ubiquitous
- they support good guarantees about data integrity
- they can store and modify data in a way that makes data corruption as unlikely as possible
- developers can set up their database to reject “bad” data and not worry about data being “half written”
What is one way to see if PostgreSQL is running?
sudo service postgresql status
What is a database schema?
-a collection of tables that defines how the data in a relational database should be organized
What is a table in a database?
- a list of rows each having the same set of attributes (columns)
- a sort of spreadsheet where each row is a record in that spreadsheet
What are rows and columns in a database?
- -columns are a set of data values of a particular type. they contain the column name, data type, or any other attributes for the column
- rows, aka “tuples”, represent the data item in a table
What is SQL and how is it different from languages like JavaScript?
-where JavaScript is imperative in that you tell JavaScript what to do, SQL is declarative. you just describe the results you want, and SQL will come up with its own plan for getting those results (like html/css)
How do you retrieve specific columns from a database table?
-“select” clause
select “column-name-1”,
“column-name-2”
from “database-table-name”
How do you filter rows based on some specific criteria?
-“where” clause
select “xyz-column”
from “xyz-table-name”
where “xyz-column-2” = “abc”
What are four comparison operators that can be used in a where clause?
, =, and !=
How do you limit the number of rows returned in a result set?
-“limit” clause (comes last)
select *
from “xyz-table-name”
limit 10
How do you retrieve all columns from a database table?
select *
How do you control the sort order of a result set?
-“order by” clause; asc/desc
How do you add a row to a SQL table?
insert into “products” (“name”, “description”, “price”, “category”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’);
What is a tuple?
a list of values (aka a row in a database table)
How do you add multiple rows to a SQL table at once?
specify more than one tuple of values, separated by commas
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” clause
How do you update rows in a database table?
update “products”
set “price” = 200,
“name” = ‘Super ShakeWeight’,
“description” = ‘Makes you ULTRA strong!’
where “productId” = 24;
Why is it important to include a where clause in your update statements?
-if you don’t include a criteria, then the query will 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?
delete from “table-name”;
no “where” clause
What is a foreign key?
a column that specifically refers to values in a column from another table
How do you join two SQL tables?
-“join” clause
select “products”.”name” as “product”,
“suppliers”.”name” as “supplier”
from “products”
join “suppliers” using (“supplierId”);
How do you temporarily rename columns or tables in a SQL statement?
-use keyword “as”
select “products”.”name” as “product”,
“table2”.”columnName” as “supplier”
What are some examples of aggregate functions?
MIN, MAX, AVG, SUM, and COUNT
What is the purpose of a group by clause?
used with aggregate functions (min, max, avg, etc) to show one value per grouped field