PostgreSQL Flashcards
What is PostgreSQL and what are some alternative relational databases?
- Relational database management system
- MySQL, Oracle, SQL Server by Microsoft
What are some advantages of learning a relational database?
Widely adapted database, so can be a highly transferrable language
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
- Define a schema, and the database will make sure the data conforms to that schema
What is a table?
A table is a list of rows, each having the same set of attributes
What is a row?
- Records that are contained inside the table
- All the data that describes one item inside a table
What is SQL and how is it different from languages like JavaScript?
SQL is a declarative language, so we declare or query for data, and the SQL language finds a path to get us the results
JavaScript is imperative, we write functions by hand, so we tell JS how do handle the action as well.
How do you retrieve specific columns from a database table?
select “column”
If multiple columns, add comma then next column name.
How do you filter rows based on some specific criteria?
Using the where keyword with comparison operator between “column” and data (string, number, boolean)
where “column” = data
What are the benefits of formatting your SQL?
Data is easier to read and comprehend when organized
What are four comparison operators that can be used in awhereclause?
Comparison operators: =, !=, >,
How do you limit the number of rows returned in a result set?
- Using the limit keyword with a number
- limit 10
How do you retrieve all columns from a database table?
select *
How do you control the sort order of a result set?
- Ascending order is default
- order by “column” desc
How do you add a row to a SQL table?
Using insert into keyword followed by the “table”, and the column names in double quotes and parenthesis, separated by commas.
insert into actors (firstName, lastName)
values (‘Ken’ ,’Kieu’)
What is a tuple?
A list of values in SQL
How do you add multiple rows to a SQL table at once?
- values keyword once
- Each value should be a new line in parenthesis and single quote
insert into programming (languages)
values (‘HTML’),
(‘CSS’),
(‘JavaScript’);
How do you get back the row being inserted into a table without a separateselectstatement?
returning *;
or returning specific “column”
How do you update rows in a database table?
- update keyword “table”
- set keyword “column” = value
- where keyword “column” = value
update store
set price = 100
where item = ‘electronics’;
Why is it important to include awhereclause in yourupdatestatements?
If where is omitted, it will update all instances of that category
How do you delete rows from a database table?
- delete from table
- where column = value
delete from store
where item = ‘electronics’
How do you accidentally delete all rows from a table?
If where is omitted, it will delete everything
What is a foreign key?
Shared attribute that links two tables together
How do you join two SQL tables?
SELECTorders.orderID, customers.customerName
FROMOrders
JOINcustomersONorders.customerID=customers.customerID;
How do you temporarily rename columns or tables in a SQL statement?
Using the as keyword will allow you to temporarily rename columns or tables
“actors” as “a”
What are some examples of aggregate functions?
- max()
- min()
- avg()
- sum()
- count()
- every()
What is the purpose of agroup byclause?
Group rows that you want to perform aggregate functions on