Skip to content
wannacode

The bakery, stage 5: the database works on its own

In short: Stage 5 adds an index, a view with the order totals, a trigger that lowers the stock and a new column with ALTER TABLE.

Problem

The things you have to remember

In stage 4 the stock was lowered by a query written by hand, and the sum of an order was worked out with three tables every time. The bakery grows: the orders become thousands, and more people write queries. Things that depend on someone remembering get forgotten sooner or later.

This stage hands the work over to the database: an index for fast searching, a view for the sums, a trigger for the store and a new column for the phone.

The idea

How the database searches

The page "My orders" asks for the orders of one customer. EXPLAIN QUERY PLAN shows how the database will do it:

SQLempty database

SCAN orders: the database reads every order and looks at the customer. With six orders it does not matter, with a million it is slow.

Try it

The index

Make an index orders_customer on the column customer_id of orders.

SQLempty database

The same query with the index:

SQLempty database

SEARCH … USING INDEX: the database goes straight to the orders of customer 2.

More precisely, if you are curious

SQLite does not make an index for a foreign key on its own, nor does PostgreSQL. A column that points to another table almost always wants an index: tables are joined and searched by it. The index is not free, though: every INSERT and UPDATE updates it.

Try it

The view

The sum of an order is three tables and a GROUP BY every time. A view is a query with a name: you read from it like from a table.

Make a view order_totals with the number of the order (id), the customer (customer_id), the status and the sum (total, rounded to 2 places) of every order.

SQLempty database

See it

The view as a table

How much every customer has spent, without the cancelled orders. The sum with the three tables is hidden in the view:

SQLempty database

The view does not keep data, it keeps its query. A new order shows up in it at once:

SQLempty database

Try it

The trigger

A trigger is code that the database runs on its own on an INSERT, UPDATE or DELETE in a table. In its body new is the new row.

Make a trigger take_stock: after every new row in order_items the stock of the product in the row goes down by its pieces.

SQLempty database

See it

The trigger and the rules

With the trigger Kalin orders 20 kozunaks, and there are 10:

SQLempty database

The UPDATE in the trigger breaks CHECK (stock >= 0). The trigger is part of the INSERT, so the row of the order does not go in either: the database has no row saying "20 kozunaks sold" when they are not there.

More precisely, if you are curious

Triggers keep a rule in one place, but they are hidden: whoever reads the program's code does not see them. That is why they are used sparingly, for things that must always happen, whatever writes to the database. The stock here is such a thing, and sending an e-mail is not.

Try it

A new column

The bakery wants to call the customers when the order is ready. The table has data, so it is not made again: ALTER TABLE adds a column to it.

Add to customers a column phone of type TEXT that may be missing.

SQLempty database

With NOT NULL it does not work:

SQLempty database
More precisely, if you are curious

ALTER TABLE in SQLite can add, rename and (since 3.35) drop columns. It cannot add a constraint like CHECK or a foreign key to an existing column: then the table is made again (a new table, INSERT … SELECT, DROP of the old one and a rename). In PostgreSQL ALTER TABLE … ADD CONSTRAINT does it directly.

The idea

What comes next

The database is full, fast and keeps its rules on its own. The last stage is the end: a report, a copy of the database and DROP of everything in the right order.

Test

Check yourself

1. Does the view order_totals keep the sums of the orders?
2. The trigger lowers the stock, and the product runs out. What happens to the row in order_items?
3. Why does ALTER TABLE customers ADD COLUMN phone TEXT NOT NULL give an error?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·