PostgreSQL & SQL Flashcards
What is PostgreSQL and what are some alternative relational databases?
Relational Database Management System (RDBMS).
MySQL, SQL Server by Microsoft, and Oracle by Oracle Corporation.
What are some advantages of learning a relational database?
Good for storing related data.
They support good guarantees about data integrity. They can store and modify data in a way that makes data corruption as unlikely as possible. This means that 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; defines how the data in a relational database should be organized
What is a table?
Stored data in relations; a list of rows each having the same set of attributes; Attributes are commonly referred to as columns.
sort of spreadsheet
What is a row?
Data entries with the same set of attributes.
What is SQL and how is it different from languages like JavaScript?
Structured Query Language (SQL); primary way of interacting with relational databases. It is a powerful way of retrieving, creating, and manipulating data in a relational database;
SQL is a declarative programming language, programmers describe the results they want and the programming environment comes up with its own plan for getting those results.
How do you retrieve specific columns from a database table?
select “column-name”,
“column-name”
from “table-name”;
How do you filter rows based on some specific criteria?
where “column-name” = ‘column-value’;
string using single quotes ‘column-value’; number just use number
What are the benefits of formatting your SQL?
for consistent style and therefore readability.
What are four comparison operators that can be used in a ‘where’ clause?
!=, =, ‘>’, ‘less-than’
How do you limit the number of rows returned in a result set?
limit ‘number’
How do you retrieve all columns from a database table?
select *
How do you control the sort order of a result set?
order by “price” desc
How do you add a row to a SQL table?
insert into “table-name” (“column-name”)
What is a tuple?
a list of values in SQL
How do you add multiple rows to a SQL table at once?
insert into “table-name” (“column1”, “column2”,
“column3”, “column4”)
values (‘value1, ‘value2’, 99, ‘value4’);
How do you get back the row being inserted into a table without a separate ‘select’ statement?
returning *
returning ‘column-name1’, ‘column-name2’
How do you update rows in a database table?
update “table-name”
set “column-name” = new-value
[where “column-name” = column-value;]
Why is it important to include a ‘where’ clause in your ‘update’ statements?
Without ‘where’, you will update all entries in the table.
How do you delete rows from a database table?
delete from “table-name”
where “column-name” = column-value
[returning *];
delete from “table-name”
where “column-name1” = ‘column-value’
and “column-name2” < column-value
How do you accidentally delete all rows from a table?
delete from “table-name”;
leaving out the ‘where’ statement
What is a foreign key?
a column that links 2 different tables (same column names in 2 different tables)
How do you join two SQL tables?
select *
from “table-name1”
join “table-name2” using (“foreign-key”);
select “table-name1”.”column-name1”,
“table-name2”.”column-name2”
from “table-name1”
join “table-name2” using (“foreign-key”);