I think you are confused about two different issues, when to use InnoDB instead of MyISAM, and when to use foreign key (FK) constraints.
As for the first issue, there have been many answers that do a great job of explaining the differences between MyISAM and InnoDB. I'll just reiterate that, in tvanfosson's quote from an article, MyISAM is better suited for system with mostly reads. This is because it uses table level locking instead of row level like InnoDB, so MyISAM can't handle high concurrency as well, plus it's missing features that help with data integrity such as transactions and foreign keys (again, already mentioned by others).
You don't have to use FK constraints in your data model. If you know what the relationships between your tables is, and your application is free of bugs, then you'll get by without FKs just fine. However, using FKs gives you extra insurance at the database layer because then MySQL won't let your application insert bad data based on the constraints that you created.
In case you aren't clear on why to use primary keys (PK), making a column such as id_order
for example the PK of the orders
table means that MySQL won't let you INSERT
the same value of id_order
more than once because every row in a PK column must be unique.
A FK would be used on a table that has a dependency on another table, for example, order_items
would have a dependency on orders
(below). id_order_items
is the PK of order_items
and you could make id_order_items
the FK of the orders
table to establish a one-to-many relationship between orders
and order_items
. Likewise, id_item
could be a FK in the order_items
table and a PK in the items
table to establish a one-to-many relationship between order_items
and items
.
**Then, what the FK constraint does is prevent you from adding an id_item value to the
order_itemstable that isn't in the
itemstable, or from adding a
id_order_itemsto
ordersthat isn't in the
order_items `table.
All a FK does is insure data integrity, and it also helps convey relationships among your tables to other developers that didn't write the system (and yourself months later when you forget!), but mainly it's for data integrity.**
Extra credit: so why use transactions? Well you already mentioned a quote that says that they are useful for banking system, but they are useful in way more situations than that.
Basically, in a relational database, especially if it's normalized, a routine operation such as adding an order, updating an order, or deleting an order often touches more than 1 table and/or involves more than one SQL statement. You could even end up touching the same table multiple times (as the example below does). Btw Data Manipulation Language (DML) statements (INSERT
/UPDATE
/DELETE
) only involve one table at a time.
An example of adding an order:
I recommend an orders
table and an order_items
table. This makes it so can you can have a PK on the id_order
in the orders
table, which means id_order
cannot be repeated in orders
. Without the 1-to-many orders
- order_items
relationship, you'd have to have multiple rows in the orders
table for every order that had multiple items associated with it (you need an items
table too for this e-commerce system btw). This example is going to add an order and touch 2 tables in doing so with 4 different INSERT
statements.
(no key constraints for illustration purposes)
-- insert #1
INSERT INTO orders (id_order, id_order_items, id_customer)
VALUES (100, 150, 1)
-- insert #2
INSERT INTO order_items (id_order_items, id_item)
VALUES (4, 1)
-- insert #3
INSERT INTO order_items (id_order_items, id_item)
VALUES (4, 2)
-- insert #4
INSERT INTO order_items (id_order_items, id_item)
VALUES (4, 3)
So what if the insert #1 and insert #2 queries run successfully, but the insert #3 statement did not? You'd end up with an order that was missing an item, and that would be garbage data. If in that case, you want to rollback all the queries so the database is in the same state it was before adding the order and then start over, well that's exactly what transactions are for. **You group together queries that you either want all of them done, or in case of an exception, then none at all, into a transaction.
So like PK/FK constraints, transactions help insure data integrity.**