An Introduction to
the Relational Model and SQL

by K. Yue

1. Introduction

Example:

Toyu: A drastically simplified university.

Relationship Diagram (in MS Access):

toyu_access_relationsihp

ER Diagram (Entity-Relationship Diagram).

toyu_relationship.jpg

MS Access: toyu.accdb

MySQL creation script: Createtoyu.sql.txt.

mysql> SELECT * FROM grade;
+-------+------------+
| grade | gradePoint |
+-------+------------+
| A     |     4.0000 |
| A-    |     3.6667 |
| B     |     3.0000 |
| B+    |     3.3333 |
| B-    |     2.6667 |
| C     |     2.0000 |
| C+    |     2.3333 |
| C-    |     1.6667 |
| D     |     1.0000 |
| D+    |     1.3333 |
| D-    |     0.6667 |
| F     |     0.0000 |
| WX    |       NULL |
+-------+------------+
13 rows in set (0.00 sec)

mysql> SELECT * FROM school;
+------------+-------------------------------+
| schoolCode | schoolName                    |
+------------+-------------------------------+
| BUS        | Business                      |
| EDU        | Education                     |
| HSH        | Human Sciences and Humanities |
| CSE        | Science and Engineering       |
+------------+-------------------------------+
4 rows in set (0.00 sec)

mysql> 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        |          7 |
+----------+------------------------------+------------+------------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM faculty;
+-------+----------+----------+----------+---------------------+
| facId | fname    | lname    | deptCode | rank                |
+-------+----------+----------+----------+---------------------+
|  1011 | Paul     | Smith    | CSCI     | Professor           |
|  1012 | Mary     | Tran     | CSCI     | Associate Professor |
|  1013 | David    | Love     | CSCI     |                     |
|  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)

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

mysql> 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 |  1013 | D242 |
|   11002 |     2002 | Spring   | 2020 |  1013 | D136 |
|   11003 |     2020 | Spring   | 2020 |  1016 | D217 |
|   11004 |     2061 | Spring   | 2020 |  1018 | B101 |
+---------+----------+----------+------+-------+------+
14 rows in set (0.00 sec)

mysql> 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  |       |      66 |    1017 |
| 100005 | Linda     | Johnson | CINF  | ENGL  |      13 |    1015 |
| 100006 | Lillian   | Johnson | CINF  | ITEC  |      18 |    1015 |
| 100007 | Ben       | Zico    | NULL  | NULL  |      16 |    NULL |
| 100008 | Bill      | Ching   | ARTS  | ENGL  |      90 |    1018 |
| 100009 | Linda     | King    | ARTS  | CSCI  |     125 |    1018 |
+--------+-----------+---------+-------+-------+---------+---------+
10 rows in set (0.00 sec)

mysql> SELECT * FROM enroll;
+--------+---------+-------+----------+
| stuId  | classId | grade | n_alerts |
+--------+---------+-------+----------+
| 100000 |   10000 | A     |        0 |
| 100001 |   10000 | NULL  |     NULL |
| 100002 |   10000 | B-    |        3 |
| 100000 |   10001 | A     |        2 |
| 100001 |   10001 | A-    |        0 |
| 100000 |   10002 | B+    |        1 |
| 100002 |   10002 | B+    |        2 |
| 100000 |   10003 | C     |        0 |
| 100002 |   10003 | D     |        4 |
| 100004 |   10003 | A     |        0 |
| 100005 |   10003 | NULL  |     NULL |
| 100000 |   10004 | A-    |        1 |
| 100004 |   10004 | B+    |     NULL |
| 100005 |   10004 | A-    |        0 |
| 100006 |   10004 | C+    |     NULL |
| 100005 |   10005 | A-    |        0 |
| 100006 |   10005 | A     |     NULL |
| 100005 |   10006 | B+    |     NULL |
| 100007 |   10007 | F     |        4 |
| 100008 |   10007 | C-    |        0 |
| 100007 |   10008 | A-    |        0 |
| 100000 |   11001 | D     |        4 |
+--------+---------+-------+----------+
22 rows in set (0.00 sec)
 

Example:

Consider the following instance of the table 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        |          7 |
+----------+------------------------------+------------+------------+
7 rows in set (0.00 sec)

mysql> desc department;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| deptCode   | varchar(4)  | NO   | PRI | NULL    |       |
| deptName   | varchar(30) | YES  | UNI | NULL    |       |
| schoolCode | varchar(3)  | YES  | MUL | NULL    |       |
| numFaculty | tinyint(4)  | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

 

Example:

After inserting a new row:

INSERT INTO department VALUES ('PHYS', 'Physics','CSE',3); 

The new relation instance of the table department:

mysql> 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        |          7 |
| PHYS     | Physics                      | CSE        |          3 |
+----------+------------------------------+------------+------------+
8 rows in set (0.00 sec)

After:

DELETE FROM department WHERE deptCode = 'PHYS';

the relation instance reverts back.

mysql> 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        |          7 |
+----------+------------------------------+------------+------------+
7 rows in set (0.00 sec)

 

mysql> 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  |       |      66 |    1017 |
| 100005 | Linda     | Johnson | CINF  | ENGL  |      13 |    1015 |
| 100006 | Lillian   | Johnson | CINF  | ITEC  |      18 |    1015 |
| 100007 | Ben       | Zico    | NULL  | NULL  |      16 |    NULL |
| 100008 | Bill      | Ching   | ARTS  | ENGL  |      90 |    1018 |
| 100009 | Linda     | King    | ARTS  | CSCI  |     125 |    1018 |
+--------+-----------+---------+-------+-------+---------+---------+
10 rows in set (0.00 sec)

mysql> SELECT * FROM enroll;
+--------+---------+-------+----------+
| stuId  | classId | grade | n_alerts |
+--------+---------+-------+----------+
| 100000 |   10000 | A     |        0 |
| 100001 |   10000 | NULL  |     NULL |
| 100002 |   10000 | B-    |        3 |
| 100000 |   10001 | A     |        2 |
| 100001 |   10001 | A-    |        0 |
| 100000 |   10002 | B+    |        1 |
| 100002 |   10002 | B+    |        2 |
| 100000 |   10003 | C     |        0 |
| 100002 |   10003 | D     |        4 |
| 100004 |   10003 | A     |        0 |
| 100005 |   10003 | NULL  |     NULL |
| 100000 |   10004 | A-    |        1 |
| 100004 |   10004 | B+    |     NULL |
| 100005 |   10004 | A-    |        0 |
| 100006 |   10004 | C+    |     NULL |
| 100005 |   10005 | A-    |        0 |
| 100006 |   10005 | A     |     NULL |
| 100005 |   10006 | B+    |     NULL |
| 100007 |   10007 | F     |        4 |
| 100008 |   10007 | C-    |        0 |
| 100007 |   10008 | A-    |        0 |
| 100000 |   11001 | D     |        4 |
+--------+---------+-------+----------+
22 rows in set (0.00 sec)

2.SQL and MySQL

MySQL

Simple SQL

select distinct <<result_columns>>
from <<source_tables>> -- conceptually join to form a large table
where <<conditions_for_inclusion>>

  1. <<source_tables>>: the source tables to gather the result data
  2. <<conditions_for_inclusion>>: the conditions to be satisfied for results to be included and the conditions the tables should be connected together.
  3. <<result_columns>>: the result columns or expressions desired to be displayed.


Examples:

Using toyu:

mysql> -- Department codes and their names
mysql> SELECT DISTINCT deptCode, deptName
    -> FROM department;
+----------+------------------------------+
| deptCode | deptName                     |
+----------+------------------------------+
| ACCT     | Accounting                   |
| ARTS     | Arts                         |
| CINF     | Computer Information Systems |
| CSCI     | Computer Science             |
| ENGL     | English                      |
| ITEC     | Information Technology       |
| MATH     | Mathematics                  |
+----------+------------------------------+
7 rows in set (0.00 sec)

mysql>
mysql> -- Faculty information from the department 'CSCI'
mysql> 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     |                     |
|  1014 | Sharon | Mannes | CSCI     | Assistant Professor |
+-------+--------+--------+----------+---------------------+
4 rows in set (0.00 sec)

mysql>
mysql> -- Faculty names from the department 'CSCI'
mysql> 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)

mysql>
mysql> -- Associate professor names from the department 'CSCI'
mysql> SELECT DISTINCT fname, lname
    -> FROM faculty
    -> WHERE deptCode = 'CSCI'
    -> AND `rank` = 'Associate Professor';
+-------+-------+
| fname | lname |
+-------+-------+
| Mary  | Tran  |
+-------+-------+
1 row in set (0.00 sec)

mysql>
mysql> -- Department names and numbers of faculty with the numbers
mysql> 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)

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

mysql>
mysql> -- Names of all faculty members and their
mysql> -- department names and ranks in the school CSE
mysql> 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                |
+----------+--------+------------------------------+---------------------+
| Daniel   | Kim    | Computer Information Systems | Professor           |
| Andrew   | Byre   | Computer Information Systems | Associate Professor |
| Paul     | Smith  | Computer Science             | Professor           |
| Mary     | Tran   | Computer Science             | Associate Professor |
| David    | Love   | Computer Science             |                     |
| Sharon   | Mannes | Computer Science             | Assistant Professor |
| Deborah  | Gump   | Information Technology       | Professor           |
| Benjamin | Yu     | Information Technology       | Lecturer            |
+----------+--------+------------------------------+---------------------+
8 rows in set (0.00 sec)


Classroom Exercises:

Example. Show all student names.

Expected Result:

+---------+-----------+
| lname   | fname     |
+---------+-----------+
| Hawk    | Tony      |
| Hawk    | Mary      |
| Hawk    | David     |
| Lim     | Catherine |
| Johnson | Larry     |
| Johnson | Linda     |
| Johnson | Lillian   |
| Zico    | Ben       |
| Ching   | Bill      |
| King    | Linda     |
+---------+-----------+
10 rows in set (0.00 sec)

Analysis:

[1] Sources: student
[2] Conditions: none
[3] Output fields: lname, fname

SELECT DISTINCT s.lname, s.fname -- [3]
FROM student s; -- [1]

Example. List the last names and first names of students minoring in CINF and having 1011 as faculty advisor.

+-------+-------+
| lname | fname |
+-------+-------+
| Hawk  | Tony  |
| Hawk  | Mary  |
+-------+-------+
2 rows in set (0.01 sec)

[Analysis]

[1] Source tables: student

[2] Conditions:

[3] Output columns:

[suggested solution of sample question]

SELECT DISTINCT s.lname, s.fname
FROM student s
WHERE minor = 'CINF'
AND advisor = 1011;

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. Show all information of students majoring in ‘MATH’.
5. Show the names and credits of students majoring in 'MATH'.
6. Show the names and credits of students majoring in 'MATH' and having 40 or more credits.
7. Show the id of students enrolled in 'CSC201A'.
8. Show the code of departments with faculty in the rank of 'Professor'.
9. Show the names of departments with faculty in the rank of 'Professor'.
10. Show the names of students who have enrolled in 'CSC201A'.
11. Show the names and major names of every student.
12. Show the names, major names, and advisor names of every student.
11. Show the student names and their major names for all students who have received a grade A in a class offered by a faculty from the MATH department.