Keys, Join, and Union Flashcards

1
Q

Primary Keys

A

are used to uniquely identify a row on a table.

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

Natural Key

A

a unique value in the data that can be used to identify a specific row on the table. (example a student id)

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

Surrogate Key

A

a generated unique value that is used when no natural key is available (example a sequentially generated number)

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

Composite Key

A

when a 2 or more columns are used as the key to identify a unique row on a table. (example a card suit and rank)

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

Foreign Key

A

Exist in other tables to reference a unique related row in the source table. Used to create relationships between tables.
Usually References a primary key, but can reference any column that contains a unique value that can be used to identify a specific row.
Can reference a composite key, but all columns that make up the primary key on the source table must be referenced on the table.

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

Cardinality, Degrees of Cardinality

A

One-to-One (1:1)
One-to-Many (1:N)
Many-to-Many (M:N)

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

One-to-One (1:1) Cardinality

A

One entity of data on a table relates to a single entity of data on another table.
An entity on one table relates to a single entity on a second table.
Requires no extra tables, a foreign key can be used either way without duplicating data.

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

One-to-Many (1:N) Cardinality

A

One entity of data on a table relates to a multiple entities of data on another table.
An entity on one table can relate to multiple entities on a second table.
Requires no extra table, the table on the 1 side’s primary key can be added to the table with the M relationships without duplication of data

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

Many-to-Many (M:N) Cardinality

A

Multiple entities of data on a table relates to a multiple entities of data on another table.

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

Joins

A

SQL JOINs allow us to create queries that produce data from one or more tables.
Related records are “joined” into a single result.
Joins are referred to as INNER and OUTER.

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

Inner Join(default)

A

Selects all rows from both participating tables as long as there is a match between the columns.

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

Left Join

Left Outer Join

A

SELECT one.number AS one_number, one.description as one_description, two.number as two_number, two.description as two_description
FROM one
LEFT JOIN two ON one.number = two.number

I’d stress that things that don’t have a match on the right have null values for columns from that table in the results.

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

Right Join

Right Outer Join

A

SELECT one.number AS one_number, one.description as one_description, two.number as two_number, two.description as two_description
FROM one
RIGHT JOIN two ON one.number = two.number

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

Question: What is the difference between a LEFT JOIN and a LEFT OUTER JOIN?

A

Answer: Nothing, they are the same!

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

Question: What is the difference between a RIGHT JOIN and RIGHT OUTER JOIN?

A

Answer: Nothing, they are the same!

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

Full Outer Join

A

SELECT one.number AS one_number, one.description as one_description, two.number as two_number, two.description as two_description
FROM one
FULL OUTER JOIN two ON one.number = two.number

17
Q

Only the Left Table Values

Unnamed

A

SELECT one.number AS one_number, one.description as one_description, two.number as two_number, two.description as two_description
FROM one
LEFT JOIN two ON one.number = two.number
WHERE two.number IS NULL

18
Q

Only the Right Table Values

Unnamed

A

SELECT one.number AS one_number, one.description as one_description, two.number as two_number, two.description as two_description
FROM one
RIGHT JOIN two ON one.number = two.number
WHERE one.number IS NULL

19
Q

In the LEFT or RIGHT, but not both

Unnamed

A

SELECT one.number AS one_number, one.description as one_description, two.number as two_number, two.description as two_description
FROM one
FULL OUTER JOIN two ON one.number = two.number
WHERE one.number IS NULL OR two.number IS NULL

20
Q

Union

A

A SQL UNION combines the results of two or more queries into a single result set.
The number of columns involved must match exactly and data types must be identical.
Duplicate rows are removed.