The Relational Model
by K. Yue
1. Introduction
1.1 Data Model
1.2 The Relational Model: an introduction
More theoretically:
1.3 Toyu Example
Toyu: A drastically simplified university
SQL statement: SELECT * FROM class;
Select every column from the table class.
MariaDB [toyu]> SELECT * FROM class;
+---------+----------+----------+------+-------+------+
| classId | courseId | semester | year | facId | room |
+---------+----------+----------+------+-------+------+
| 10000 | 2000 | Fall | 2019 | 1011 | D241 |
| 10001 | 2001 | Fall | 2019 | 1011 | D242 |
| 10002 | 2002 | Fall | 2019 | 1012 | D136 |
| 10003 | 2020 | Fall | 2019 | 1014 | D241 |
| 10004 | 2021 | Fall | 2019 | 1014 | D241 |
| 10005 | 2040 | Fall | 2019 | 1015 | D237 |
| 10006 | 2041 | Fall | 2019 | 1019 | D217 |
| 10007 | 2060 | Fall | 2019 | 1020 | B101 |
| 10008 | 2080 | Fall | 2019 | 1018 | D241 |
| 11000 | 2000 | Spring | 2020 | 1011 | D241 |
| 11001 | 2001 | Spring | 2020 | 1012 | D242 |
| 11002 | 2002 | Spring | 2020 | 1013 | D136 |
| 11003 | 2020 | Spring | 2020 | 1016 | D217 |
| 11004 | 2061 | Spring | 2020 | 1018 | B101 |
+---------+----------+----------+------+-------+------+
14 rows in set (0.005 sec)
A tuple/row: {classId: 10004, courseId: 2021, semester: 'Fall', year: 2019, facId: 1014, room: 'D241'}. Note that it is a set of mappings from attribute names to attribute values.
It can also be represented as:
{classId: 10004, facId: 1014, room: 'D241', semester: 'Fall', year: 2019, courseId: 2021}
or
{10004, 2021, 'Fall', 2019, 1014, 'D241'} if the attribute names are assumed in be in the right order.
or using a more computer science-style notation.
(10004, 2021, 'Fall', 2019, 1014, 'D241')
Identify some examples of the database terms in the class table above as much as possible.
Example:
+---------+----------+----------+------+-------+------+
| classId | courseId | semester | year | facId | room |
+---------+----------+----------+------+-------+------+
| 10000 | 2000 | Fall | 2019 | 1011 | D241 |
| 10001 | 2001 | Fall | 2019 | 1011 | D242 |
| 10002 | 2002 | Fall | 2019 | 1012 | D136 |
| 10003 | 2020 | Fall | 2019 | 1014 | D241 |
| 10004 | 2021 | Fall | 2019 | 1014 | D241 |
| 10005 | 2040 | Fall | 2019 | 1015 | D237 |
| 10006 | 2041 | Fall | 2019 | 1019 | D217 |
| 10007 | 2060 | Fall | 2019 | 1020 | B101 |
| 10008 | 2080 | Fall | 2019 | 1018 | D241 |
| 11000 | 2000 | Spring | 2020 | 1011 | D241 |
| 11001 | 2001 | Spring | 2020 | 1013 | D242 |
| 11002 | 2002 | Spring | 2020 | 1013 | D136 |
| 11003 | 2020 | Spring | 2020 | 1016 | D217 |
| 11004 | 2061 | Spring | 2020 | 1018 | B101 |
+---------+----------+----------+------+-------+------+
and
+---------+------+----------+----------+------+-------+
| classId | year | semester | courseId | room | facId |
+---------+------+----------+----------+------+-------+
| 11004 | 2020 | Spring | 2061 | B101 | 1018 |
| 10007 | 2019 | Fall | 2060 | B101 | 1020 |
| 10002 | 2019 | Fall | 2002 | D136 | 1012 |
| 11002 | 2020 | Spring | 2002 | D136 | 1013 |
| 11003 | 2020 | Spring | 2020 | D217 | 1016 |
| 10006 | 2019 | Fall | 2041 | D217 | 1019 |
| 10005 | 2019 | Fall | 2040 | D237 | 1015 |
| 10000 | 2019 | Fall | 2000 | D241 | 1011 |
| 11000 | 2020 | Spring | 2000 | D241 | 1011 |
| 10003 | 2019 | Fall | 2020 | D241 | 1014 |
| 10004 | 2019 | Fall | 2021 | D241 | 1014 |
| 10008 | 2019 | Fall | 2080 | D241 | 1018 |
| 10001 | 2019 | Fall | 2001 | D242 | 1011 |
| 11001 | 2020 | Spring | 2001 | D242 | 1013 |
+---------+------+----------+----------+------+-------+
store the same information.
The second table can be obtained in SQL by:
SELECT classId, year, semester, courseId, room, facId
FROM class
ORDER BY room, facId;
2. Keys
Example:
Consider:
MariaDB [toyu]> SELECT * FROM faculty;
+-------+----------+----------+----------+---------------------+
| facId | fname | lname | deptCode | rank |
+-------+----------+----------+----------+---------------------+
| 1011 | Paul | Smith | CSCI | Professor |
| 1012 | Mary | Tran | CSCI | Associate Professor |
| 1013 | David | Love | CSCI | NULL |
| 1014 | Sharon | Mannes | CSCI | Assistant Professor |
| 1015 | Daniel | Kim | CINF | Professor |
| 1016 | Andrew | Byre | CINF | Associate Professor |
| 1017 | Deborah | Gump | ITEC | Professor |
| 1018 | Art | Allister | ARTS | Assistant Professor |
| 1019 | Benjamin | Yu | ITEC | Lecturer |
| 1020 | Katrina | Bajaj | ENGL | Lecturer |
| 1021 | Jorginlo | Neymar | ACCT | Assistant Professor |
+-------+----------+----------+----------+---------------------+
11 rows in set (0.001 sec)
MariaDB [toyu]> SELECT * FROM department;
+----------+------------------------------+------------+----------+
| deptCode | deptName | schoolCode | numStaff |
+----------+------------------------------+------------+----------+
| ACCT | Accounting | BUS | 10 |
| ARTS | Arts | HSH | 5 |
| CINF | Computer Information Systems | CSE | 5 |
| CSCI | Computer Science | CSE | 12 |
| ENGL | English | HSH | 12 |
| ITEC | Information Technology | CSE | 4 |
| MATH | Mathematics | CSE | 7 |
+----------+------------------------------+------------+----------+
7 rows in set (0.001 sec)
Consider the tables student and enroll:
2.1 More about keys:
Consider the example above. The attribute deptCode is a foreign key in the relation faculty, referencing department(deptCode)
Likewise, stuid is a foreign key in the relation Enroll: stuid references student(stuid)
Note that the student table has three foreign keys.
Example: In the department table in toyu:
Department(deptCode, deptName, schoolCode, numStaff)
Candidate keys:
Primary key: deptCode
Alternate key:
Prime attributes:
Non-prime attributes:
Superkeys:
Example:
Study the list of all foreign keys in toyu:
Example:
-- INNER JOINMariaDB [toyu]> SELECT * FROM faculty;
+-------+----------+----------+----------+---------------------+
| facId | fname | lname | deptCode | rank |
+-------+----------+----------+----------+---------------------+
| 1011 | Paul | Smith | CSCI | Professor |
| 1012 | Mary | Tran | CSCI | Associate Professor |
| 1013 | David | Love | CSCI | NULL |
| 1014 | Sharon | Mannes | CSCI | Assistant Professor |
| 1015 | Daniel | Kim | CINF | Professor |
| 1016 | Andrew | Byre | CINF | Associate Professor |
| 1017 | Deborah | Gump | ITEC | Professor |
| 1018 | Art | Allister | ARTS | Assistant Professor |
| 1019 | Benjamin | Yu | ITEC | Lecturer |
| 1020 | Katrina | Bajaj | ENGL | Lecturer |
| 1021 | Jorginlo | Neymar | ACCT | Assistant Professor |
+-------+----------+----------+----------+---------------------+
11 rows in set (0.000 sec)
MariaDB [toyu]> SELECT * FROM department;
+----------+------------------------------+------------+----------+
| deptCode | deptName | schoolCode | numStaff |
+----------+------------------------------+------------+----------+
| ACCT | Accounting | BUS | 10 |
| ARTS | Arts | HSH | 5 |
| CINF | Computer Information Systems | CSE | 5 |
| CSCI | Computer Science | CSE | 12 |
| ENGL | English | HSH | 12 |
| ITEC | Information Technology | CSE | 4 |
| MATH | Mathematics | CSE | 7 |
+----------+------------------------------+------------+----------+
7 rows in set (0.000 sec)
MariaDB [toyu]> SELECT s.fname, s.lname, s.advisor, f.`rank`
-> FROM student AS s INNER JOIN faculty AS f
-> ON (s.advisor = f.facId);
+---------+----------+---------+---------------------+
| fname | lname | advisor | rank |
+---------+----------+---------+---------------------+
| Tony | Hawk | 1011 | Professor |
| Mary | Hawk | 1011 | Professor |
| David | Hawk | 1012 | Associate Professor |
| Larry | Johnson | 1017 | Professor |
| Linda | Johnson | 1015 | Professor |
| Lillian | Johnson | 1016 | Associate Professor |
| Linda | King | 1018 | Assistant Professor |
| Cathy | Johanson | 1018 | Assistant Professor |
+---------+----------+---------+---------------------+
8 rows in set (0.000 sec)
Classroom Exercise:
(1) Give a realistic example of a relation with two candidate keys. State the assumptions you have made.
(2) A relation R has an arity of 4. What are the possible minimum and maximum number of superkeys of R? What are the possible minimum and maximum number of candidate keys of R?
What about the general case?