Designing a database with MySQL using the Command Line Interface — Part 2

Afroshok
Afroshok
Sep 3, 2018 · 10 min read

In the first part of Designing a database with MySQL using the Command Line Interface — Part 1, we have built a database and in the process used the most important queries in MySQL, namely, CREATE, USE, DESCRIBE, INSERT, SELECT, DELETE and ALTER.

We will continue to build out the Waks Noma database.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| waks_noma |
+--------------------+
5 rows in set (0.01 sec)

Then:

mysql> use waks_noma;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

We are ready.

ii) Adding Categories Table: maskils

A good designer’s portfolio may have a number of categories: web development, motion graphics, branding, graphic design, and video production. Inexperienced database designers will just add a categories column (or columns) to next to the jobs so that data entry is of a job and its category. This is simple enough, but by adding a column called job_type, what would happen is that every so often, the type of work would be repeated or a project would have more than one job category, typically, like when making a multimedia presentation.

Unfortunately, this table is now inflexible. Because category names are repeated, any changes in spelling or wording will require more updating and are more likely to result in both system and user error.

Design decisions

Our categories are: Graphic Design, Web Design, Video production. Should we break this into sub categories, like under graphic design; brand identity, poster development, print layout? Can we list all the current and add future categories?

The decision is made to add a separate table of job categories:

/*** CREATE Categories table ***/
create table maskils (
cat_id int not null auto_increment primary key,
cat_name varchar(20) not null,
cat_desc text not null
);
/*** end CREATE ***/

We get:

mysql> show tables;
+---------------------+
| Tables_in_waks_noma |
+---------------------+
| majobo |
| maskils |
+---------------------+
2 rows in set (0.00 sec)

We will load the data form a local file.

mysql> load data local infile '[absolute path/to/]madiambo.txt' into table maskils;
Query OK, 7 rows affected, 7 warnings (0.00 sec)
Records: 7 Deleted: 0 Skipped: 0 Warnings: 0

Our maskils table look thus:

mysql> select * from maskils;
+--------+--------------------+------------------------------------+| cat_id | cat_name | cat_desc +--------+--------------------+------------------------------------+| 1 | Graphic Design | This category deals single page design and layouts that include brand identities, posters, banners, T-shirt designs and so on. |
| 2 | Print Layout | This category includes magazine layouts brochures, posters, banners and brand collateral. |
| 3 | Web Design | This category describes User Interface design (UI) whether left at design or exploded to a working prototype. It also includes Information Architecture (IA) flows with the subsequent Wire Frames. |
| 4 | Web Development | This category deals with the middleware used such as PHP or Content Management Solutions (CMS) such as Wordpress, Joomla, and Drupal or frameworks such as Ruby on Rails and Django. |
| 5 | Video Production | This category deals with video product development and delivery from script development to the finished product ready for delivery. | | 6 | Motion Graphics | This category has animated graphics for Television and film. It can incorporate web animation that enhances UX. |
| 7 | Animation | This category deals with 2D and 3D animation, whether story based, character driven, web banners or motion graphics for insertion in TV and web content. |
| 8 | Mobile Development | This category covers work singly or incorporating other media that is specifically geared for delivery on mobile handsets and smart phones. | +--------+--------------------+------------------------------------+8 rows in set (0.00 sec)

The only purpose of this table is to hold information about categories. Ultimately, this design is simpler because each table makes sense on its own, independent of other tables. Each table will have one job and only one job. majobo will only hold information about the work done. maskils will only hold information about the categories of the work done.

maskils is structured as follows:

maskills ERD

By assigning only one job to each table, we can simplify the data and enable ourselves to better manage the data. For instance, if we need to rename the category "Video Production" to "Film", we now only need to update one row in maskils. If our categories were a column in the table majobo, we would have to go through the pain of searching every record in majobo for "Video Production" and updating each of those rows - an unnecessarily difficult process. In fact, all of our SQL statements will become easier to write and faster to process.

Assigning Jobs to Categories

If each table only has one job, then majobskils will be to link a specific job done to a specific category describing the type of work done. Thus, it will only hold two columns: job_id and cat_id. Both columns are called Foreign Keys. Columns designated as foreign keys will hold values that are primary keys in other tables. That is, job_id is a primary key in majobo. In majobskils, the column work_id is a foreign key. The same goes for cat_id and maskils.

/*** CREATE majobskils Table ***/
create table majobskils (
job_id int not null,
cat_id int not null
);
/*** end CREATE TABLE ***/

For our majobskils, the table does not need to handle transactions since it carries data that will not change in the near future.

So comparing the two tables:

majobo:

+--------+-------------+-------------------------+-----------------+| job_id | job_name    | job_desc            ...  |      +--------+-------------+-------------------------+-----------------+|      1 | The Warehouse  | ... The web space was required  ... ||      2 | Aim to Succeed | This was a graphic design poster ... ||      3 | Oiling Life    | ... The brochure was to be ... ||      4 | Dying to Give  | The main piece was a video documentary ...  |
+--------+-------------+-------------------------+-----------------+

maskils:

+--------+--------------------+------------------------------------+
| cat_id | cat_name | cat_desc |
+--------+--------------------+------------------------------------+| 1 | Graphic Design | ...
| 2 | Print Layout | ...
| 3 | Web Design | ...
| 4 | Web Development | ...
| 5 | Video Production | ...
| 6 | Animation | ...
| 7 | Mobile Development | ...
+--------+--------------------+------------------------------------+

We then insert data into majobskils as follows:

/*** INSERT DATA into majobskils ***/
insert into majobskils (job_id, cat_id) values ('1','3');
insert into majobskils (job_id, cat_id) values ('2','1');
insert into majobskils (job_id, cat_id) values ('3','1');
insert into majobskils (job_id, cat_id) values ('4','5');
/*** end INSERT DATA ***/

The results:

mysql> select * from majobskils;
+--------+--------+
| job_id | cat_id |
+--------+--------+
| 1 | 3 |
| 2 | 1 |
| 3 | 1 |
| 4 | 5 |
+--------+--------+
4 rows in set (0.00 sec)

We use job_id and cat_id because these ID numbers are unique to each row in majobo and maskils, respectively.

If we had instead used job_name and cat_name in majobskils, we would have redundant data - data that is duplicated elsewhere. Unlike name fields, ID fields are never subject to change, so using them to identify a row is safer and requires less storage and rework.

The structure for majobskils is shown in the following ERD as well as the relationships to majobo and maskils.

majobskills ERD

Joining ‘majobo’ and ‘maskils’ tables

At this point, the database is set up to hold our job and the corresponding categories. Because the data is spread across three tables, we need to join the tables within our SELECT query. Joining tables means that we combine tables based on common fields (related fields). Joins do not physically join the tables, but rather join the query result sets from implied queries. There is no physical change to the database design.

/*** Get the category names associated with job_id #1 ***/select maskils.cat_name from majobo,maskils,majobskils
where majobo.job_id = 1
and majobo.job_id = majobskils.job_id
and majobskils.cat_id = maskils.cat_id;

We get:

+------------+
| cat_name |
+------------+
| Web Design |
+------------+
1 row in set (0.00 sec)

If we look at the previous ERD, we can follow the query through the linking fields. A table like majobskils is called a linking tables (or mapping tables) because it links together (maps) other tables like majobo and maskils.

Adding Technology Classifications Table: teknoma

We will handle adding technology classifications just as we did the categories. A variety of technologies exist: HTML5, ReactJS, PHP, Python, AR/VR , etc. Because we want our database to capture what technology was used for each work, we will create a technologies table: tbl_tech. Like previous tables, tbl_tech will have an ID column, tech_id, that is the primary key and is also set to auto increment. It will only hold information about technologies.

/*** CREATE teknoma table ***/
create table teknoma (
tek_id int not null auto_increment primary key,
tek_name varchar(20) not null
);
/*** end CREATE ***/

We load data from a local file with all the definitions of the technologies used:

mysql> load data local infile '[absolute path/to/]teknoma.txt' into table teknoma;mysql> select * from teknoma;
+--------+--------------+
| tek_id | tek_name |
+--------+--------------+
| 1 | Vector Art |
| 2 | Pixel Art |
| 3 | HTML5 |
| 4 | CSS3 |
| 5 | Javascript |
| 6 | ReactJS |
| 7 | PHP |
| 8 | Laravel |
| 9 | Ruby |
| 10 | Rails |
| 11 | Python |
| 12 | Django |
| 13 | REST API |
| 14 | MySQL |
| 15 | PostreSQL |
| 16 | iOS |
| 17 | Andriod |
| 18 | React Native |
| 19 | Video |
| 20 | AR / VR |
+--------+--------------+
20 rows in set (0.00 sec)

Like majobskils, we will create majobtek which will link majobo to teknoma. Both job_id and tek_id are foreign keys within majobtek.

/*** CREATE majobtek Table ***/
create table majobtek (
job_id int not null,
tek_id int not null
);
/*** end CREATE TABLE ***/

We compare the two tables teknoma and majobo and join them by adding the data into majobtek:

/*** INSERT DATA into majobtek ***/
insert into majobtek (job_id, tek_id) values ('1','3');
insert into majobtek (job_id, tek_id) values ('1','4');
insert into majobtek (job_id, tek_id) values ('1','7');
insert into majobtek (job_id, tek_id) values ('1','12');
insert into majobtek (job_id, tek_id) values ('2','1');
insert into majobtek (job_id, tek_id) values ('2','2');
insert into majobtek (job_id, tek_id) values ('3','1');
insert into majobtek (job_id, tek_id) values ('3','2');
insert into majobtek (job_id, tek_id) values ('4','18');
insert into majobtek (job_id, tek_id) values ('4','19');
insert into majobtek (job_id, tek_id) values ('4','20');
/*** end INSERT DATA ***/

So the fun part begins. To see what technology was used in what project, we create the following queries. If a, b, and c look confusing in the following query, don't be alarmed. a, b, and c are just alias table names. In essence, a, b, and c just represent a shorter way to type the table names that are used later in the query:

/*** Get the technology names used with job_id #1 ***/
select c.tek_name from majobo a, majobtek b, teknoma c
where a.job_id = 1
and a.job_id = b.job_id
and b.tek_id = c.tek_id;

We get:

+----------+
| tek_name |
+----------+
| HTML5 |
| CSS3 |
| PHP |
| Django |
+----------+
4 rows in set (0.00 sec)

Another selection:

/*** Get the job names used with tek_id #1 ***/
select a.job_name from majobo a, majobtek b, teknoma c
where c.tek_id = 1
and a.job_id = b.job_id
and b.tek_id = c.tek_id;

We get:

+----------------+
| job_name |
+----------------+
| Aim to Succeed |
| Oiling Life |
+----------------+

So our majobtek ERD looks like so:

majobtek ERD

Adding the Clients Table: maklayo

Appropriately, maklayo will have klayo_id as the primary key set to auto increment.

/*** CREATE maklayo table ***/
create table maklayo (
klayo_id int not null auto_increment primary key,
klayo_name varchar(20) not null
);

We then insert client names from a text file:

/*** LOAD DATA INTO maklayo ***/
load data local infile '[absolute path/to/]maklayo.txt' into table maklayo;

And again a table linking jobs to clients:

/*** CREATE maklayojobo Table ***/
create table maklayojobo (
job_id int not null,
klayo_id int not null
);
/*** end CREATE TABLE ***/

We insert data:

/*** INSERT DATA into maklayojobo ***/
insert into maklayojobo (job_id, klayo_id) values ('1','1');
insert into maklayojobo (job_id, klayo_id) values ('2','4');
insert into maklayojobo (job_id, klayo_id) values ('3','5');
insert into maklayojobo (job_id, klayo_id) values ('4','6');
/*** end INSERT DATA ***/

To make this a little interesting, here is a query that finds out which technologies were used on a particular job for a particular client:

/*** Get the technology names used for klayo_id #1 ***/
select e.tek_name from maklayo a, maklayojobo b, majobo c, majobtek d, teknoma e
where a.klayo_id = 1
and a.klayo_id = b.klayo_id
and b.job_id = c.job_id
and c.job_id = d.job_id
and d.tek_id = e.tek_id;

We get:

+ — — — — — +
| tek_name |
+ — — — — — +
| HTML5 |
| CSS3 |
| PHP |
| Django |
+ — — — — — +
4 rows in set (0.01 sec)

Below is the ERD that makes the possible queries above happen:

waksnoma ERD

This concludes our designing a database in MySQL using the CLI.

Next is in the series is Designing a database using MySQL Workbench.

Shok and Oh!

Afroshok is a digital production and consulting, web design and development boutique in Nairobi, Kenya. We specialise in creating cutting edge, ground breaking, brand driven immersive projects. We work through Progressive Web Applications to deliver content where it matters.

Afroshok

Written by

Afroshok

Shok and Oh!

Afroshok is a digital production and consulting, web design and development boutique in Nairobi, Kenya. We specialise in creating cutting edge, ground breaking, brand driven immersive projects. We work through Progressive Web Applications to deliver content where it matters.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade