w3d1-w3d2 revisions Flashcards

1
Q

T/F: When working in a TDD paradigm and you encounter a bug, write a test to isolate and capture the bug.

A

T

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

T/F: Comment Ruby liberally

A

F; Use comments sparingly; Ruby code should strive to be self-explanatory.

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

How do && and || short-circuit?

A

&& short circuits on false, || short circuits on true.

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

What is ‘fail’ a synonym for?

A

‘raise’

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

T/F: Subqueries are run once and used by each row.

A

F; the subquery is run again for each row that calls it.

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

What is a join table, and does it need a primary key?

A

It is a table that serves to join together two otherwise unrelated tables. It needs a primary key, even if it is never used.

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

When planning a SQL workflow, when is a good time to use a JOIN?

A

When you need a specific piece of data.

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

Describe an efficient SQL workflow.

A

1) Identify the queries that will be needed
2) Start with the FROM clause
3) Break down the pieces of data you need; each one will probably be a JOIN
4) In your WHERE clause, reference the alias of each JOIN

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

What is “normalization” in SQL, and how is it implemented?

A

Normalization refers to breaking apart repetitive data into different tables. For example, if you have a table with post and user columns, but many posts are by the same user, it makes more sense to make the user column a foreign key value that points to the primary (id) key of the users table.

This allows you to make a change in one place, and should be implemented whenever you encounter repetition in a table.

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

When should you use an INNER JOIN?

A

When you don’t care if you lose values that don’t have a match.

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

When should you use an OUTER JOIN?

A

When you want to retain values, even if they don’t have a match (NULL value)

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

What are the naming conventions for foreign keys?

A

They should be nameofothertable_id.id

Ex:

posts.user_id = user.id

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

What is the difference between COUNT(*) and COUNT(colname)

A

The former will count all rows including NULLs, whereas the latter will only count values that are not NULL.

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

What is the ‘singleton’ gem, and why bother with it?

A

It restricts a class to creating only one instance of itself. It is important for threading (i.e. two users shouldn’t be able to access a value at the same time), and avoids large costs (if the size of the instance is massive)

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

What happens when you JOIN tables where there is a column name collision (besides the keys you are matching)

A

The column that is being merged to the other column (read: the SECOND one) is retained, the other is not.

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

What JOIN should you use when you want to augment a table (i.e. not lose any data?)

A

Use a LEFT OUTER JOIN

17
Q

Describe the relationship between Ruby classes of tables and SQL tables.

A

Each row becomes an instance of the table’s class. The table class should be named as the singular version of the table name.

18
Q

What is a SQL Injection?

A

SQL takes in values via interpolation (‘?’ in Ruby). Malicious code can be inserted by hackers if there are no restrictions on what can be interpolated.

19
Q

T/F: When interpolating a ‘?’ in a SQL string, an escape character is NOT needed.

A

T

20
Q

How are ‘?’ used in SQL string interpolation.

A

A list of arguments is given in the heredoc, every time a ‘?’ is reached, a value is shifted off of that list and stands in for that ‘?’

21
Q

How do you declare a foreign key in a schema?

A

Declare it as a regular column first; afterwords specify that it is a foreign key.

22
Q

How can a heredoc take arguments so that ‘?’ do not have to be used, and values do not have to be listed in the order they will be used?

A

Use a hash:

(«-SQL, name: val)

You can then get the val by referencing :name in the string.

23
Q

What does the LIMIT(n) function do?

A

Takes the first n values.