2.2. Database Management¶
Back in the Database and SQL chapter exercises,
you created a new database in MySQL Workbench with one table, seeds
.
Let’s go through the process again, but this time we will set up one-to-many
relationships between three tables—writing_supply
, pencil_drawer
,
and pen_drawer
. You will soon use these tables to practice more complex SQL
queries.
2.2.1. Setup¶
- Open a connection in MySQL Workbench. Click Create New Schema and call the
model
storage
. - Click Apply, and accept all of the default options when prompted.
The three tables form a pair of one-to-many relationships. Each
writing_supply
record relates either to several pencil_drawer
entries
or several pen_drawer
entries.
Each table will need a primary key column, and pencil_drawer
and
pen_drawer
will require foreign key columns. In the query tab of MySQL
Workbench, CREATE
the tables as described below.
2.2.1.1. writing_supply
¶
- Each record in this table will have three fields:
- A primary key,
supply_id
. - A
utensil_type
, which will be anENUM
(“Pencil” or “Pen”). Note the syntax for setting this up in line 3 of the example. - An integer number of drawers,
num_drawers
.
- A primary key,
Example
1 2 3 4 5 | CREATE TABLE writing_supply (
supply_id INTEGER PRIMARY KEY AUTO_INCREMENT,
utensil_type ENUM ("Pencil", "Pen"),
num_drawers INTEGER
);
|
2.2.1.2. pencil_drawer
¶
- Each record in this table will have five fields:
- A primary key,
drawer_id
. - The
pencil_type
, which will be anENUM
(“Wood” or “Mechanical”). - An integer number of pencils,
quantity
. - A boolean describing whether or not it is time to
refill
the drawer. - A foreign key (
supply_id
) that connectspencil_drawer
withwriting_supply
.
- A primary key,
2.2.1.3. pen_drawer
¶
- Each record in this table will have five fields:
- A primary key,
drawer_id
. - The
color
of the pens, which will be anENUM
(“Black”, “Blue”, “Red”, “Green”, “Purple”). - An integer number of pens,
quantity
. - A boolean describing whether or not it is time to
refill
the drawer. - A foreign key (
supply_id
) that connectspen_drawer
withwriting_supply
.
- A primary key,
2.2.2. Import Data¶
Note
Whenever you import existing data into your empty tables, it is helpful to use the same column names as the external source.
Follow this link to the sql-starter-data. Fork the repository and then clone it to your machine.
Unlike previous repositories, this one only holds folders of CSV data files. It does NOT contain executable code.
Pay attention to where you save the folder for this repository. In the following steps, you need to access the files inside
lesson-15-reading-data
.In MySQL Workbench, right-click on the
writing_supply
table. Select Table Data Import Wizard.In the next panel, click the Browse button. Find and select the
writing_supply.csv
file. Click Next.In the following panel, select the default option Use existing table –> storage.writing_supply, and click Next.
Once again, accept the default options for importing data into your table columns. Click Next.
Clicking Next again will import the data. Clicking Finish on the final panel returns you to the editor.
In the query tab, run
SELECT * FROM writing_supply
to confirm that 6 entries now exist in the table.Repeat the steps 4 - 9 for the
pencil_drawer
andpen_drawer
tables.
Confirm that the pencil_drawer
and pen_drawer
tables hold 6 and 15
entries, respectively. You are now ready to practice more advanced SQL queries.
2.2.3. Import Troubleshooting¶
Did something go wrong with your import? No worries—the process is not always perfect, and errors occur more often than we would like. A few issues are just random glitches, but most others involve syntax or selection mistakes.
Here are some common errors:
- Filename: Make sure that each file ends with the
.csv
extension. Also, select the correct file to import into each table (e.g.pen_drawer.csv
will not import into thewriting_supply
table). - Mismatched data types: MySQL Workbench will not complete an import if
you define a table column as one data type, but the matching column in the
.csv
file contains a different data type. Check to make sure that yourCREATE
statements correctly define the data types described in the Setup section. - Import order: For one-to-many relationships, the one table must be
created before the many, and this idea also applies to filling the tables
with data.
pen_drawer
andpencil_drawer
both contain foreign keys that link towriting_supply
. Thus, you must import data intowriting_supply
FIRST, before attempting to add data to the other two tables.
Note
This is not an exhaustive list of everything that could go wrong with a data import. Be patient and persistent, and don’t hesitate to consult the MySQL Workbench documentation, Google, your TA, or your instructor.
2.2.4. Check Your Understanding¶
Question
Examine the setup you used for the writing_supply
, pencil_drawer
,
and pen_drawer
tables. Which of the following pairs does NOT have a
one-to-many relationship?
writing_supply
andpencil_drawer
writing_supply
andpen_drawer
pencil_drawer
andpen_drawer