An Introduction to
the Relational Model and SQL

by K. Yue

1. Introduction

Example:

Toyu: A drastically simplified university.

ER Diagram:

toyu_relationship.jpg

MS Access: toyu.accdb

MySQL creation script: Createtoyu.sql.txt.

MariaDB [toyu]> select * from department;
+----------+------------------------------+------------+------------+
| deptCode | deptName                     | schoolCode | numFaculty |
+----------+------------------------------+------------+------------+
| 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 |
+----------+------------------------------+------------+------------+
6 rows in set (0.00 sec)

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     | Associate Professor |
|  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.00 sec)

MariaDB [toyu]> select * from course;
+----------+--------+--------+-------------------------------------+
| courseId | rubric | number | name                                |
+----------+--------+--------+-------------------------------------+
|     2000 | CSCI   | 3333   | Data Structures                     |
|     2001 | CSCI   | 4333   | Design of Database Systems          |
|     2002 | CSCI   | 5333   | DBMS                                |
|     2020 | CINF   | 3321   | Introduction to Information Systems |
|     2021 | CINF   | 4320   | Web Application Development         |
|     2040 | ITEC   | 3335   | Database Development                |
|     2041 | ITEC   | 3312   | Introduction to Scripting           |
|     2060 | ENGL   | 1310   | English I                           |
|     2061 | ENGL   | 1311   | English II                          |
|     2080 | ARTS   | 3311   | Hindu Arts                          |
|     2090 | ACCT   | 3311   | Managerial Accounting               |
+----------+--------+--------+-------------------------------------+
11 rows in set (0.00 sec)

MariaDB [toyu]> select * from class;
+---------+----------+----------+------+-------+------+
| classId | courseId | semester | year | facId | room |
+---------+----------+----------+------+-------+------+
|   10000 |     2000 | Fall     | 2017 |  1011 | D241 |
|   10001 |     2001 | Fall     | 2017 |  1011 | D242 |
|   10002 |     2002 | Fall     | 2017 |  1012 | D136 |
|   10003 |     2020 | Fall     | 2017 |  1014 | D241 |
|   10004 |     2021 | Fall     | 2017 |  1014 | D241 |
|   10005 |     2040 | Fall     | 2017 |  1015 | D237 |
|   10006 |     2041 | Fall     | 2017 |  1019 | D217 |
|   10007 |     2060 | Fall     | 2017 |  1020 | B101 |
|   10008 |     2080 | Fall     | 2017 |  1018 | D241 |
|   11000 |     2000 | Spring   | 2018 |  1011 | D241 |
|   11001 |     2001 | Spring   | 2018 |  1013 | D242 |
|   11002 |     2002 | Spring   | 2018 |  1013 | D136 |
|   11003 |     2020 | Spring   | 2018 |  1016 | D217 |
|   11004 |     2061 | Spring   | 2018 |  1020 | B101 |
+---------+----------+----------+------+-------+------+
14 rows in set (0.00 sec)

MariaDB [toyu]> select * from student;
+--------+-----------+---------+-------+-------+---------+---------+
| stuId  | fname     | lname   | major | minor | credits | advisor |
+--------+-----------+---------+-------+-------+---------+---------+
| 100000 | Tony      | Hawk    | CSCI  | CINF  |      40 |    1011 |
| 100001 | Mary      | Hawk    | CSCI  | CINF  |      35 |    1011 |
| 100002 | David     | Hawk    | CSCI  | ITEC  |      66 |    1011 |
| 100003 | Catherine | Lim     | ITEC  | CINF  |      20 |    1017 |
| 100004 | Larry     | Johnson | ITEC  | NULL  |      66 |    1017 |
| 100005 | Linda     | Johnson | CINF  | ENGL  |      13 |    1015 |
| 100006 | Lillian   | Johnson | CINF  | ITEC  |      18 |    1015 |
| 100007 | Ben       | Zico    | NULL  | NULL  |      16 |    1014 |
| 100008 | Bill      | Ching   | ARTS  | ENGL  |      90 |    1018 |
| 100009 | Linda     | King    | ARTS  | CSCI  |     125 |    1018 |
+--------+-----------+---------+-------+-------+---------+---------+
12 rows in set (0.00 sec)

MariaDB [toyu]> select * from enroll;
+--------+---------+-------+
| stuId  | classId | grade |
+--------+---------+-------+
| 100000 |   10000 | A     |
| 100001 |   10000 | NULL  |
| 100002 |   10000 | B-    |
| 100000 |   10001 | A     |
| 100001 |   10001 | A-    |
| 100000 |   10002 | B+    |
| 100002 |   10002 | B+    |
| 100000 |   10003 | A     |
| 100002 |   10003 | B-    |
| 100004 |   10003 | A     |
| 100005 |   10003 | NULL  |
| 100000 |   10004 | A-    |
| 100004 |   10004 | B+    |
| 100005 |   10004 | A-    |
| 100006 |   10004 | C+    |
| 100005 |   10005 | A-    |
| 100006 |   10005 | A     |
| 100005 |   10006 | B+    |
| 100007 |   10007 | B+    |
| 100008 |   10007 | C-    |
| 100007 |   10008 | A     |
+--------+---------+-------+
21 rows in set (0.00 sec)

Consider

MariaDB [toyu]>select * from department;
+----------+------------------------------+------------+------------+
| deptCode | deptName                     | schoolCode | numFaculty |
+----------+------------------------------+------------+------------+
| 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 |
+----------+------------------------------+------------+------------+
6 rows in set (0.00 sec)

Example:

After inserting a new row:

insert into department values
   ('MATH', 'Mathematics','CSE',11);
 

The new relation instance of the table department:

MariaDB [toyu]> select * from department;
+----------+------------------------------+------------+------------+
| deptCode | deptName                     | schoolCode | numFaculty |
+----------+------------------------------+------------+------------+
| 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        |         11 |
+----------+------------------------------+------------+------------+
7 rows in set (0.00 sec)

After:

delete from department where deptCode = 'MATH';

the relation instance reverts back.

MariaDB [toyu]> select * from department;
+----------+------------------------------+------------+------------+
| deptCode | deptName                     | schoolCode | numFaculty |
+----------+------------------------------+------------+------------+
| 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 |
+----------+------------------------------+------------+------------+
6 rows in set (0.00 sec)

 

MariaDB [toyu]> select * from department;
+----------+------------------------------+------------+------------+
| deptCode | deptName                     | schoolCode | numFaculty |
+----------+------------------------------+------------+------------+
| 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 |
+----------+------------------------------+------------+------------+
6 rows in set (0.00 sec)

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     | Associate Professor |
|  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.00 sec)
 

2.SQL and MySQL

Simple SQL

SELECT DISTINCT <<result_columns>>
FROM <<source_tables>>
WHERE <<conditions_for_inclusion>>

  1. <<result_columns>>: result columns to be displayed.
  2. <<source_tables>>: tables that contain necessary data.
  3. <<conditions_for_inclusion>>: conditions for rows to be included in the result.


Examples:

Using toyu:

MariaDB [toyu]> -- department codes and their names
MariaDB [toyu]> select distinct deptCode, deptName
    -> from department;
+----------+------------------------------+
| deptCode | deptName                     |
+----------+------------------------------+
| ACCT     | Accounting                   |
| ARTS     | Arts                         |
| CINF     | Computer Information Systems |
| CSCI     | Computer Science             |
| ENGL     | English                      |
| ITEC     | Information Technology       |
+----------+------------------------------+
6 rows in set (0.00 sec)

MariaDB [toyu]>
MariaDB [toyu]> -- faculty information from the department 'CSCI'
MariaDB [toyu]> select distinct *
    -> from faculty
    -> where deptCode = 'CSCI';
+-------+--------+--------+----------+---------------------+
| facId | fname  | lname  | deptCode | rank                |
+-------+--------+--------+----------+---------------------+
|  1011 | Paul   | Smith  | CSCI     | Professor           |
|  1012 | Mary   | Tran   | CSCI     | Associate Professor |
|  1013 | David  | Love   | CSCI     | Associate Professor |
|  1014 | Sharon | Mannes | CSCI     | Assistant Professor |
+-------+--------+--------+----------+---------------------+
4 rows in set (0.00 sec)

MariaDB [toyu]>
MariaDB [toyu]> -- faculty names from the department 'CSCI'
MariaDB [toyu]> select distinct fname, lname
    -> from faculty
    -> where deptCode = 'CSCI';
+--------+--------+
| fname  | lname  |
+--------+--------+
| Paul   | Smith  |
| Mary   | Tran   |
| David  | Love   |
| Sharon | Mannes |
+--------+--------+
4 rows in set (0.00 sec)

MariaDB [toyu]>
MariaDB [toyu]> -- associate professor names from the department 'CSCI'
MariaDB [toyu]> select distinct fname, lname
    -> from faculty
    -> where deptCode = 'CSCI'
    -> and rank = 'Associate Professor';
+-------+-------+
| fname | lname |
+-------+-------+
| Mary  | Tran  |
| David | Love  |
+-------+-------+
2 rows in set (0.00 sec)

MariaDB [toyu]>
MariaDB [toyu]> -- department names and numbers of faculty with the numbers
MariaDB [toyu]> -- or faculty >= 10.
MariaDB [toyu]> select distinct deptName, numFaculty
    -> from department
    -> where numFaculty >= 10;
+------------------+------------+
| deptName         | numFaculty |
+------------------+------------+
| Accounting       |         10 |
| Computer Science |         12 |
| English          |         12 |
+------------------+------------+
3 rows in set (0.00 sec)

MariaDB [toyu]>
MariaDB [toyu]> -- names of all faculty members and their
MariaDB [toyu]> -- department names and ranks.
MariaDB [toyu]> select distinct faculty.fName, faculty.lname,
    ->  department.deptName, faculty.rank
    -> from department, faculty
    -> where faculty.deptCode = department.deptCode;
+----------+----------+------------------------------+---------------------+
| fName    | lname    | deptName                     | rank                |
+----------+----------+------------------------------+---------------------+
| Paul     | Smith    | Computer Science             | Professor           |
| Mary     | Tran     | Computer Science             | Associate Professor |
| David    | Love     | Computer Science             | Associate Professor |
| Sharon   | Mannes   | Computer Science             | Assistant Professor |
| Daniel   | Kim      | Computer Information Systems | Professor           |
| Andrew   | Byre     | Computer Information Systems | Associate Professor |
| Deborah  | Gump     | Information Technology       | Professor           |
| Art      | Allister | Arts                         | Assistant Professor |
| Benjamin | Yu       | Information Technology       | Lecturer            |
| Katrina  | Bajaj    | English                      | Lecturer            |
| Jorginlo | Neymar   | Accounting                   | Assistant Professor |
+----------+----------+------------------------------+---------------------+
11 rows in set (0.00 sec)

MariaDB [toyu]>
MariaDB [toyu]> -- names of faculty of the school 'CSE' and their
MariaDB [toyu]> -- department names and ranks.
MariaDB [toyu]> select distinct faculty.fName, faculty.lname,
    ->  department.deptName, faculty.rank
    -> from department, faculty
    -> where faculty.deptCode = department.deptCode
    -> and department.schoolCode = 'CSE';
+----------+--------+------------------------------+---------------------+
| fName    | lname  | deptName                     | rank                |
+----------+--------+------------------------------+---------------------+
| Paul     | Smith  | Computer Science             | Professor           |
| Mary     | Tran   | Computer Science             | Associate Professor |
| David    | Love   | Computer Science             | Associate Professor |
| Sharon   | Mannes | Computer Science             | Assistant Professor |
| Daniel   | Kim    | Computer Information Systems | Professor           |
| Andrew   | Byre   | Computer Information Systems | Associate Professor |
| Deborah  | Gump   | Information Technology       | Professor           |
| Benjamin | Yu     | Information Technology       | Lecturer            |
+----------+--------+------------------------------+---------------------+
8 rows in set (0.00 sec)


Classroom Exercises:

1. All student names and the major codes.
2. All student names and the major department names.
3. All student names enrolled in the class with id 10003.
4. All department names with a Professor as a faculty members.