Insert Necessary Data
Explore how to insert necessary data into customers, orders, and order_items tables in MySQL. Practice creating multiple orders and items to prepare for join, left join, and subquery queries in relational databases.
We’ll now execute the following commands to create the necessary data to support our query exercises.
First, we create the customers:
We get a Succeeded response, which means the queries executed successfully and there’s nothing to show.
If we select the customers, we should see this:
mysql> select * from customers;
+----+-------------+----------+
| id | name | identity |
+----+-------------+----------+
| 4 | John Woo | JW001 |
| 5 | Maria Foo | MF001 |
| 6 | Jim Papas | JP001 |
| 7 | Jessy Romeo | JR001 |
| 8 | Arya Stark | AS001 |
+----+-------------+----------+
5 rows in set (0.00 sec)
Click “Run” to verify the result:
The first customer’s order
Then, create orders and order items. Insert one order with one order item for the first customer:
mysql> insert into orders (order_number, ordered_at, customer_id) values ('ABC001', current_timestamp, 4);
Query OK
mysql> select * from orders;
+----+--------------+---------------------+-------------+
| id | order_number | ordered_at | customer_id |
+----+--------------+---------------------+-------------+
| 2 | ABC001 | 2016-09-12 21:36:56 | 4 |
+----+--------------+---------------------+-- ...