Databases Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A

A relational database

-Oracle, MySQL, SQL server by Microsoft

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

Doesn’t require a complex structure.
Data integrity – they can store and modify data in a way that makes data corruption as unlikely as possible.
Used frequently.
All relational databases share the same syntax–if you learn one, you can easily learn another one.
Data that’s stored in them can be compacted really well.
Prevent data replication (also good for data integrity)
Information is easy to access. Data is stored in tables and the tables can be joined together in any way that you need to. (flexible based on what your need is).

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

top command

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

What’s the difference between pgweb and postgreSQL?

A

pgweb is a gui. It’s the graphical interface for postgreSQL.

you need postgreSQL for pgweb. But, you don’t need pgweb for postgreSQL.

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

What is a database schema?

A

A collection of tables. Defines how the data in a relational database should be organized..

Defines the structure

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

What is a table?

A

A list of rows, each having the same set of attributes.

Attributes are commonly referred to as columns. You might visualize a database table as 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
7
Q

What is a row?

A

Each row in a table is a single record of data. One instance of each attribute in the table

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

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

A

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.

JS is an imperative language –> you tell it what you want and how to do it.

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

How do you retrieve specific columns from a database table?

A

select “columnName”,
“columnName2”
from “tableName”;

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

How do you filter rows based on some specific criteria?

A

where “columnName” = ‘criteria’

can use any logical operator

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

What are the benefits of formatting your SQL?

A

cleaner and easier to read

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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
13
Q

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

A

limit number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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
15
Q

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

A

order by

default is to sort in ascending order

ex) order by “price” desc

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

How do you add a row to a SQL table?

A

insert into “table” (“col1”, “col2”, …)
values (‘val1’, ‘val2’,…);

alt syntax

Insert into "tableName"
set
   "col1" = 'val1'
   "col2"='val2'
;
17
Q

What is a tuple?

A

A list of values

18
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.

ex)
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 *;

19
Q

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

A

returning *;

20
Q

How do you update rows in a database table?

A

update “tableName”
set “colA” = ‘ValA’,
“colC” = ‘valc’
where “colB’=’valB’

ex)

update “products”
set “price” = 100
where “productId” = 24;

21
Q

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

A

if you don’t, you’ll update every row in the table

22
Q

How do you delete rows from a database table?

A

delete from “tableName”
where “col1”=’val1’
returning*;

ex)

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

23
Q

How do you accidentally delete all rows from a table?

A

If you don’t include a where clause

24
Q

What is a foreign key?

A

A column that’s common to two tables.

A column in a table that’s constrained to match values in a column in another table.

25
Q

How do you join two SQL tables?

A
select
      "columnA"
     "columnB" 
from "tableName1" 
join "tableName2" using ("foreignKeyColumn")
-or-
select....
from "tableName1"
join "tableName2" on "tableName1". "col1" = "tableName2" . "col1"

ex)
select *
from “products”
join “suppliers” using (“supplierId”);

26
Q

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

A

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

select "p"."name" as "product",
       "p"."category",
       "s"."name" as "supplier",
       "s"."state"
  from "products" as "p"
  join "suppliers" as "s" using ("supplierId")
 where "p"."category" = 'cleaning'
   and "p"."price"    < 20;
27
Q

What are some examples of aggregate functions?

A
average()
sum()
max()
min()
count()
28
Q

What is the purpose of a group by clause?

A
29
Q

What do aggregate functions do?

A

Combine a bunch of values into a single value