Relational Models, Selection + Projection Flashcards

(13 cards)

1
Q

What is the equivalent SQL for πœ‹firstname, birthday(Person)?

A

SELECT DISTINCT firstname, birthday FROM Person

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

Which SQL condition would fulfill the following:
- matches “bat”
- matches “chat”
- does NOT match “bank”

A

LIKE “%a_”

% = match everything
_ = match one thing

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

What is projection? Given a relation R, how to select columns A and B?

A
  • step which select columns
    πœ‹A,B(R)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does this mean:
πœ‹Title (𝜎genre=”suspense”^”audience”=”Teenagers”(Books))

A

SELECT DISTINCT Title FORM Books WHERE genre = “Suspense” AND audience = “Teenagers”

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

What is the cartesian product? How to represent in SQL? in RA?

A

Contains every combination of rows in the 2 tables:
SQL: SELECT * FROM R1, R2;
RA: R1 x R2

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

In SQL and RA, show to how to use cartesian product to find all stores based in Winnipeg

A

SELECT storeName FROM Store, City
WHERE Store.CID = City.CID
AND cityName = “Winnipeg”

πœ‹storeName(𝜎Store.CID=City.CID^CityName=”Winnipeg”(Store x City))

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

Why doesn’t this work in cartesian product?
S𝐸𝐿𝐸𝐢𝑇 𝐷𝐼𝑆𝑇𝐼𝑁𝐢𝑇 𝑑𝑖𝑑𝑙𝑒 𝐹𝑅𝑂𝑀 π΅π‘œπ‘œπ‘˜π‘ , π‘…π‘’π‘Žπ‘‘, π‘ƒπ‘’π‘œπ‘π‘™π‘’
π‘Šπ»πΈπ‘…πΈ π‘“π‘–π‘Ÿπ‘ π‘‘ = “𝐴𝑖𝑑𝑒𝑛” 𝐴𝑁𝐷 π‘™π‘Žπ‘ π‘‘ = “π΅π‘Žπ‘˜π‘’π‘Ÿβ€œ

A

You need to tell SQL how the tables are connected. To do so, do joins on People, Read, and Books.

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

What is a natural join?

A

Join which matches on all columns that share a name, and doesn’t duplicate columns

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

How to represent this in RA?
𝑆𝐸𝐿𝐸𝐢𝑇 π‘“π‘–π‘Ÿπ‘ π‘‘, π‘™π‘Žπ‘ π‘‘, 𝐴𝐼𝐷 𝐹𝑅𝑂𝑀 π‘ƒπ‘’π‘œπ‘π‘™π‘’ π‘π΄π‘‡π‘ˆπ‘…π΄πΏ 𝐽𝑂𝐼𝑁 π΄π‘’π‘‘β„Žπ‘œπ‘Ÿπ‘ 

A

πœ‹first,last,AID(Peopleβ‹ˆAuthors)

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

When to use a JOIN ON?

A

Tables don’t share same column name, or want to be more explicit about what’s happening.

SQL:
𝑆𝐸𝐿𝐸𝐢𝑇 π‘“π‘–π‘Ÿπ‘ π‘‘, π‘™π‘Žπ‘ π‘‘, 𝐴𝐼𝐷
𝐹𝑅𝑂𝑀 π‘ƒπ‘’π‘œπ‘π‘™π‘’ 𝐽𝑂𝐼𝑁 π΄π‘’π‘‘β„Žπ‘œπ‘Ÿπ‘  𝑂𝑁 π‘ƒπ‘’π‘œπ‘π‘™π‘’. 𝑆𝐼𝑁 = π΄π‘’π‘‘β„Žπ‘œπ‘Ÿπ‘ . 𝑆𝐼𝑁

RA:
πœ‹first,last,AID(Peopleβ‹ˆPeople.SIN=Authors.SIN Authors)

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

In RA and SQL, Find the names of all the people who live in Winnipeg and have read at least
one book

A

S𝐸𝐿𝐸𝐢𝑇 𝐷𝐼𝑆𝑇𝐼𝑁𝐢𝑇 π‘“π‘–π‘Ÿπ‘ π‘‘, π‘™π‘Žπ‘ π‘‘
𝐹𝑅𝑂𝑀 π‘ƒπ‘’π‘œπ‘π‘™π‘’ π‘π΄π‘‡π‘ˆπ‘…π΄πΏ 𝐽𝑂𝐼𝑁 𝐢𝑖𝑑𝑦 π‘π΄π‘‡π‘ˆπ‘…π΄πΏ 𝐽𝑂𝐼𝑁 π‘…π‘’π‘Žπ‘‘
π‘Šπ»πΈπ‘…πΈ π‘π‘–π‘‘π‘¦π‘π‘Žπ‘šπ‘’ = “π‘Šπ‘–π‘›π‘›π‘–π‘π‘’π‘”β€œ

πœ‹first,last(𝜎cityName=”Winnipeg”(Peopleβ‹ˆCityβ‹ˆRead))

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

Describe the different set operations and their corresponding SQL/RA translations

A

UNION (U in RA):
result set contains tuples in 1 or other, or both

INTERSECT (∩ in RA):
result set contains only tuples in both

EXCEPT (- in RA) (aka subtraction):
result set contains tuples in first relation but not the second

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