PostgreSQL Flashcards
Why do we use databases in Web development?
What is PostgreSQL and what are some alternative relational databases?
What are some advantages of learning a relational database?
What is one way to see if PostgreSQL is running?
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. It dictates how the table will be set up and how it will look.
What is a table?
A table is what relational databases store data in. They are also known as relations, but are more commonly referred to as tables.
What is a row?
A row is all the information about one entry or id of the table.
What is an attribute and what other names are used to describe them?
An attribute is something related to a row. Other names used to describe them are column, property, key, characteristic, etc.
What is SQL and how is it different from languages like JavaScript?
Structured Query Language (SQL) is the primary way of interacting with relational databases and is a powerful way of retrieving, creating, and manipulating data in a relational database. It’s different from languages like JavaScript because it is a declarative programming language while languages like JavaScript are imperative programming languages.
How do you retrieve specific columns from a database table?
You must “select” the columns/properties/keys in a comma separated list.
select “column”,
“property”,
“key”
How do you filter rows based on some specific criteria?
After “select”ing “from” a table, use “where” followed by the criteria. The criteria will be in double quotes “ “ while the criteria value will be in single quotes ‘ ‘ if it is a string, while things like numbers and booleans will not be in quotes at all.
What are the benefits of formatting your SQL?
Formatted SQL makes code look more consistent and therefore increases readability.
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?
At the end of a “select” statement, use “limit” followed by the number of rows you want returned. The number should not be in quotes.
How do you retrieve all columns from a database table?
By using “select” with the universal operator *.
How do you control the sort order of a result set?
By using “order by” after select, from, where but before limit, followed by a column name (in double quotes “ “). After the column name, you can also include desc to sort in descending order since the default sort order of the results is in ascending order.
How do you add a row to a SQL table?
By using an SQL “insert” statement.
insert into “table” (“column”, “attribute”, “property”, “key”)
values (‘value’, ‘value’, number, boolean);
What is a tuple?
A list of values.
How do you add multiple rows to a SQL table at once?
By using an SQL “insert” statement but specifying multiple “tuples” of values, separated by commas.
How do you get back the row being inserted into a table without a separate select statement?
By including “returning” followed by what you want to get back about the row. It can be returning * to get back the full inserted row back from the database, including its auto-generates attribute(s), or, 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?
By using an SQL “update” statement.
update “table”
set “column” = number,
“column” = ‘string’,
“column” = ‘string’
where “column” = number;
Why is it important to include a where clause in your update statements?
It is important because without a where clause, your update statements would update every row in the table.
How do you delete rows from a database table?
By using an SQL “delete” statement.
delete
from “row”
where “column” = 24
returning *;
How do you accidentally delete all rows from a table?
By using an SQL “delete” statement but forgetting to include a where clause.