PostgreSQL Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A
  • PostgreSQL is a powerful, free, open source Relational Database Management System (RDBMS)
  • MySQL, SQL Server, ORACLE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are some advantages of learning a relational database?

A
  • widely used
  • data integrity: 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”/”half-written” data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is one way to see if PostgreSQL is still running?

A
  • top
  • sudo service postgresql status
  • try connecting to it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a database schema?

A

a collection of tables - defines how the data in a relational database should be organized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a table?

A
  • relational databases store data in relations, commonly referred to as tables
  • a list of rows each having the same set of attributes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a row?

A

a list of attributes that are all the same across each row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is SQL and how is it different from languages like JavaScript?

A
  • Structured Query Language (SQL) is the primary way of interacting with relational databases.
  • SQL is a declarative programming language. In declarative languages, programmers describe the results they want and the programming environment comes up with its own plan for getting those results. (Whereas imperative languages like JavaScript you basically tell the JavaScript runtime what to do and how to do it.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you retrieve specific columns from a database table?

A

select “columnName”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you filter rows based on some specific criteria?

A

use a ‘where’ clause - an expression that has to evaluate to true or false - so can use < > = etc..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the benefits of formatting your SQL?

A

it’s easier to read - easier to debug - easier to remove stuff

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are four comparison operators that can be used in a where clause?

A

, =, !=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you limit the number of rows returned in a result set?

A

use a ‘limit’ clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you retrieve all columns from a database table?

A

*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you control the sort order of a result set?

A

use ‘order by’ clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you add a row to a SQL table?

A
  • use the insert clause
  • insert into “tableName” ( “colName”, “colName”, …)
    values ( ‘colValue’ , ‘colValue’, …)
    returning *;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a tuple?

A

a list of ordered values ( )

17
Q

How do you add multiple rows to a SQL table at once?

A
  • specify more than one tuple of values, separated by commas
18
Q

How do you get back the row being inserted into a table without a separate select statement?

A
  • use ‘returning * ‘ clause after the values tuple
19
Q

How do you update rows in a database table?

A
  • using the ‘update’ clause
  • update “tableName”
    set “colName” = ‘colValue’ ,
    “colName” = ‘colValue’
    where “colName” = value
20
Q

Why is it important to include a where clause in your update statements?

A
  • without specifying a certain row using the where clause after setting the value, the update statement would update ALL the rows!
21
Q

How do you delete rows from a database table?

A
  • using the delete from keyword
  • delete from “tableName”
    where “colName” = ‘colValue’
    returning * ;
22
Q

How do you accidentally delete ALL rows from a table?

A

delete from “tableName” ; BAD! - needs a where clause after the delete

23
Q

What is a foreign key?

A

one column that links one table to another table (exists in both table with the same values)

24
Q

How do you join two SQL tables?

A
  • use the join clause
25
Q

How do you temporarily rename columns or tables in a SQL statement?

A

using aliasing

- “tableName”.”colName” as “alias”

26
Q

What are some examples of aggregate functions?

A

avg( ), count( ), sum( ), max( ), min( ), jsonAgg( )

27
Q

What is the purpose of a group by clause?

A
  • to separate rows into groups and perform aggregate functions on those groups of rows.