Designing the database
Tags: databasedesign
Published: 2019-08-14 08:57:35

---
title: Designing the database
published: true
series: Building a chatbot with the RaMP stack (RaspberryPi, MySQL, Python)
description: User-centric design of a database
tags: database_design
cover_image: https://thepracticaldev.s3.amazonaws.com/i/9sfbec8ouz5dhgcw5h2n.PNG
---

My chatbot is going to answer queries on the Mahabharat, so my database will be a Mahabharat<sup><a href = "#1">1</a></sup> database. Let's design one :construction:

I should be able to apply the design principles in this post to any story corpus, epic, or novel (and not <i>just</i> the Mahabharat). The Bible, for example, or the Harry Potter series. :star2::zap:
<blockquote>
<dl>
<dt>Who am I designing the database for?</dt><dd>A chabot.</dd>
<dt>Who will use the chatbot?</dt><dd>Human beings.</dd>
<dt>Why will they use the chatbot?</dt><dd>To get quick info on the story.</dd>
<dt>What kind of info?</dt><dd>Info about the people in the epic, what they do, how they live day to day, how they die.</dd>
</dl>
</blockquote>

I know that my chatbot is not a story-telling bot; it is an info-supplying bot. How do human beings look for info? They do so by asking questions. 
<blockquote>
<dl>
<dt><u>Why</u> is the sky blue?</dt><dd>&nbsp;</dd>
<dt><u>How</u> do I dance the foxtrot?</dt><dd>&nbsp;</dd>
<dt><u>When</u> is that moron going to resign?</dt><dd>&nbsp;</dd>
<dt><u>Where</u> were they when the lights went out?</dt><dd>&nbsp;</dd>
<dt><u>Who</u> framed Peter Rabbit?</dt><dd>&nbsp;</dd>
</dl>
</blockquote>

Applying these questions to my project, I can see two things:
<ul>
<li>That I need to group the information into categories</li>
<li>That a category should answer one - and only one - type of question</li>
<li>That an answer to a question in one category can lead to further questions that can belong to either the same category or to another</li>
</ul>
I believe, in geek parlance these are known as <i>taxonomy</i>, <i>ontology</i>, and <i>normalisation</i>?

So, categories will be my database tables. The rows in the tables will be the Who, What, Where of that category. The columns will contain the information about one specific Who, What, Where. The info in one cell of one table can be exactly equal to the info in a cell of one or more tables. :boom:

My Mahabharat database is going to have the following categories (tables):
- persons
- families
- clans
- weapons
- killings
- places

A character in the story (a person) can belong to persons, families, clans, weapons, and killings. In one table, a person can appear in one - and only one - row. In all other tables, that person can appear at several rows, but only once in each such row. 

I believe, in geek parlance this is known as <i>primary key</i> and <i>foreign key</i>?

Now that I have an idea of what my database will look like, I should write it all down. So, here we go: <img src = "https://thepracticaldev.s3.amazonaws.com/i/9sfbec8ouz5dhgcw5h2n.PNG"/>

To sum up, for designing my database, I:
- Ask what kind of questions my database will answer.
- Group the questions into categories.
- Create a table for each category.
- Turn the sub-groups of these categories into the table columns.
- Link one table to another through a common column.

Next, I'll create these tables in MySQL on my <a href = "https://dev.to/aninditabasu/building-a-chatbot-with-the-ramp-stack-part-1-preparing-my-raspberry-pi-1jai">RaspberryPi</a> and populate them with the data.

<hr/>
<small>Footnote:</small>
<a name = "1"></a><small>1. The Mahabharat is one of the two epics of ancient India, is the longest epic in the world, and is the epic that contains the Bhagvad Gita (a mini-book that's sacred to the Hindus).</small>