Unique Order Number
Learn to avoid the redundancy of rows.
We'll cover the following
Delete the wrong order
Let’s first delete the wrong order:
1 mysql> delete from orders where id = 2;
2 Query OK, 1 row affected (0.01 sec)
3
4 mysql> select * from orders;
5 +----+--------------+---------------------+-------------+
6 | id | order_number | ordered_at | customer_id |
7 +----+--------------+---------------------+-------------+
8 | 1 | ABC001 | 2016-09-09 08:34:55 | 1 |
9 +----+--------------+---------------------+-------------+
10 1 row in set(0.00 sec)
Create a unique index
Now, let’s create a unique index on the order_number
of the orders
table. This will make our search queries quick and protect us from inserting the same order number twice. This is the command that will create the unique index. Let’s execute it:
Get hands-on with 1400+ tech skills courses.