PostgreSQL Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A

PostgreSQL (Structured Query Language) is a powerful, free, open source, Relational Database Management System (RDBMS) that uses and extends the SQL language combined with many features that safely store and scale the most complicated data workloads.

MySQL, SQP Server by Microsoft, and Oracle by Oracle Corporation

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

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”

All relational databases share the same syntax. Data you store can be compacted well. Does not require a complex structure - simple model. Data accuracy and integrity, high security. Flexibility, can combine or update data, joining tables.

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 running?

A

sudo service postgresql status

Check top to list currently running processes

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. A schema defines structure - how the data in a relational database should be organized. In relational databases, you typically have to define your schema up front and the database server will make sure that any data being written to the database conforms to that schema.

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. A schema defines structure - how the data in a relational database should be organized. In relational databases, you typically have to define your schema up front and the database server will make sure that any data being written to the database conforms to that schema.

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 table is a list of rows each having the same set of attributes .
Tables define what attributes (columns) that each row should have.

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

What is a row (tuple)?

A

A single record of data. Each row is an instance of a row type.

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

SQL (structured query language) is the 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 whereas JavaScript is an imperative language where you tell the JavaScript runtime what to do and how to do it. In declarative languages, programmers describe the results they want and the programming environment comes up with its own plan for getting those results.

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 “column1”,
“column2”
from “table”;

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
select “column1”,
       “column2”
       “column3”
  from “table”
 where “col-filter” = ‘value’;
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

Consistent style and readability

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 awhereclause?

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
select “column1”,
       “column2”
  from “table”
 order by “col-order” desc
 limit 1;
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

select *
from “table”;

Order of results is NOT predictable

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

select *
from “table”
order by “column”;

Default sort order is ascending order
Add desc to sort descending order

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

Insert statement

insert into “table” (“col1”, “col2”, “col3”)
values (‘val1’, ‘val2’, num3)
returning *;

16
Q

What is a tuple?

A

A list of values
A tuple is stored as a row/record
A data structure that looks like a JavaScript array, each position refers to a specific attribute

17
Q

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

A

Data rows can be batch inserted into a database table by specifying more than one tuple of values, separated by commas.

insert into “table” (“col1”, “col2”, “col3”)
values (‘val1’, ‘val2’, num1)
(‘val3’, num2, ‘val4’)
returning *;

18
Q

How do you get back the row being inserted into a table without a separateselectstatement?

A

returning *

19
Q

How do you update rows in a database table?

A

Update statement

update “table”
  set “col1” = ‘val1’
          “col2” = num,
          “col3” = ‘val2’
 where “col” = ‘val’ or num
returning *;
20
Q

Why is it important to include awhereclause in yourupdatestatements?

A

Need to specify to only target specific rows

21
Q

Why is it important to include awhereclause in yourupdatestatements?

A

Need to specify to only target specific rows

22
Q

How do you delete rows from a database table?

A

delete from “table”
where “col” = num or ‘val’
returning *;

23
Q

How do you accidentally delete all rows from a table?

A

delete from “table”

Without specifying a where clause

24
Q

What is a foreign key?

A

A foreign key is a field (or collection of fields) in one table, that refers to the primary key in another table. The table with the foreign key is called the child table, and the table with the primary key is the called the referenced or parent table.

25
Q

How do you join two SQL tables?

A

Join clause

select *
from “table1”
join “table2” using “colId”;

26
Q

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

A

Aliasing names using ‘as’ keyword

select “table1”.”col” as “newcolname”,
“table2”.”col” as “newName”
from “table1”
join “table2” using “colId”;

27
Q

What are some examples of aggregate functions?

A

max(), avg(), count()

28
Q

What is the purpose of agroup byclause?

A

Want to separate rows into groups and perform aggregate functions on those groups of rows