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. (relational databases use some variation of the SQL language… opposite of NoSQL)
  • alternatives: mySQL, SQLite, 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
  • they are ubiquitous
  • 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”
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

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 that 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 in a database?

A
  • a list of rows each having the same set of attributes (columns)
  • a sort of spreadsheet where each row is a record in that spreadsheet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are rows and columns in a database?

A
  • -columns are a set of data values of a particular type. they contain the column name, data type, or any other attributes for the column
  • rows, aka “tuples”, represent the data item in a table
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

-where JavaScript is imperative in that you tell JavaScript what to do, SQL is declarative. you just describe the results you want, and SQL will come up with its own plan for getting those results (like html/css)

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” clause

select “column-name-1”,
“column-name-2”
from “database-table-name”

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

-“where” clause

select “xyz-column”
from “xyz-table-name”
where “xyz-column-2” = “abc”

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

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

A

, =, and !=

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

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

A

-“limit” clause (comes last)

select *
from “xyz-table-name”
limit 10

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

How do you retrieve all columns from a database table?

A

select *

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

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

A

-“order by” clause; asc/desc

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

How do you add a row to a SQL table?

A

insert into “products” (“name”, “description”, “price”, “category”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’);

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

What is a tuple?

A

a list of values (aka a row in a database table)

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

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

A

specify more than one tuple of values, separated by commas

insert into “products” (“name”, “description”, “price”, “category”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’),
(‘Tater Mitts’, ‘Scrub some taters!’, 6, ‘cooking’)
returning *;

17
Q

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

A

“returning” clause

18
Q

How do you update rows in a database table?

A

update “products”
set “price” = 200,
“name” = ‘Super ShakeWeight’,
“description” = ‘Makes you ULTRA strong!’
where “productId” = 24;

19
Q

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

A

-if you don’t include a criteria, then the query will update every row in the table

20
Q

How do you delete rows from a database table?

A

delete from “products”
where “productId” = 24
returning *;

21
Q

How do you accidentally delete all rows from a table?

A

delete from “table-name”;

no “where” clause

22
Q

What is a foreign key?

A

a column that specifically refers to values in a column from another table

23
Q

How do you join two SQL tables?

A

-“join” clause

select “products”.”name” as “product”,
“suppliers”.”name” as “supplier”
from “products”
join “suppliers” using (“supplierId”);

24
Q

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

A

-use keyword “as”

select “products”.”name” as “product”,
“table2”.”columnName” as “supplier”

25
Q

What are some examples of aggregate functions?

A

MIN, MAX, AVG, SUM, and COUNT

26
Q

What is the purpose of a group by clause?

A

used with aggregate functions (min, max, avg, etc) to show one value per grouped field