15

I am rather confused by the hurt-mongering here.

I know how to do them, see below, but no idea why? What are they for?

create table orders (order_no int not null auto_increment, FK_cust_no int not null, 
foreign key(FK_cust_no) references customer(cust_no), primary key(order_no)) type=InnoDB;


create table orders (order_no int not null auto_increment, FK_cust_no int not null, 
foreign key(FK_cust_no) references customer(cust_no), primary key(order_no));
4
  • 1
    your example is doing foreign keys, not innodb. You should have asked what foreign keys are for.
    – J-16 SDiZ
    Jul 4, 2009 at 13:35
  • 1
    What "hurt-mongering"? That article is about how to recover from an inadvertent DROP DATABASE. Jul 4, 2009 at 14:27
  • Jim Ferrans: I refered to the sentence: "if you're not a strongly experienced programmer, you're going to be hurting." Jul 4, 2009 at 15:36
  • @J-16 SDiZ: Uhhh... did you notice this at the end of the first query?: type=InnoDB Jul 20, 2009 at 16:37

10 Answers 10

18

InnoDB is a storage engine in MySQL. There are quite a few of them, and they all have their pros and cons. InnoDB's greatest strengths are:

  • Support for transactions (giving you support for the ACID property).
  • Row-level locking. Having a more fine grained locking-mechanism gives you higher concurrency compared to, for instance, MyISAM.
  • Foreign key constraints. Allowing you to let the database ensure the integrity of the state of the database, and the relationships between tables.
12

Always. Unless you need to use MySQL's full-text search or InnoDB is disabled in your shared webhost.

2
  • that's the most simple answer and it's true!
    – JohnB
    Jul 20, 2010 at 18:33
  • 1
    FYI InnoDB now supports full-text search. Also, InnoDB is much faster than it used to be. So, there is rarely any reason to use MyISAM anymore (which is why InnoDB is now the default engine in MySQL) May 31, 2013 at 20:32
4

InnoDB:

The InnoDB storage engine in MySQL. InnoDB is a high-reliability and high-performance storage engine for MySQL. Key advantages of InnoDB include:

  • Its design follows the ACID model, with transactions featuring commit, rollback, and crash-recovery capabilities to protect user data.
  • Row-level locking (without escalation to coarser granularity locks) and Oracle-style consistent reads increase multi-user concurrency and performance.
  • InnoDB tables arrange your data on disk to optimize common queries based on primary keys. Each InnoDB table has a primary key index called the clustered index that organizes the data to minimize I/O for primary key lookups
  • To maintain data integrity, InnoDB also supports FOREIGN KEY referential-integrity constraints.
  • You can freely mix InnoDB tables with tables from other MySQL storage engines, even within the same statement. For example, you can use a join operation to combine data from InnoDB and MEMORY tables in a single query.

InnoDB Limitations:

  • No full text indexing (Below-5.6 mysql version)

  • Cannot be compressed for fast, read-only

More Details:

Refer this link

3

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 theorder_itemstable that isn't in theitemstable, or from adding aid_order_itemstoordersthat isn't in theorder_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.**

1
  • Adding 1 for opposing innoDB to myIsam, When we just dont know mySQL and innoDB, we cant know there are many engines.
    – pdem
    Jan 28, 2015 at 14:22
2

In your example, you create foreign keys. Foreign keys are only supported for InnoDB tables, not for MyISAM tables.

2

You may be interested in this article from Database Journal which discusses the InnoDB table type in MySQL.

Excerpt:

Last month we looked at the HEAP table type, a table type which runs entirely in memory. This month we look at setting up the InnoDB table type, the type of most interest to serious users. The standard MyISAM table type is ideal for website use, where there are many reads in comparison to writes, and no transactions. Where these conditions do not apply (and besides websites, they do not apply often in the database world), the InnoDB table is likely to be the table type of choice. This article is aimed at users who are familiar with MySQL, but have only used the default MyISAM table type.

I wouldn't be put off by the other question. Keep proper backups of your database, of any type -- and don't drop tables by accident ;-) -- and you'll be ok whatever table type you choose.

0
1

In general for me the most important point is that InnoDB offers per row locking, while MyISAM does look per table. On big tables with a lot of writes this might make a big performance issue.

On the otherhand MyISAM table have a easier file structure, copying and repairing table on file level is way easier.

2
  • How much is considered a lot? one write per second is lot?
    – Evan Lee
    Apr 5, 2014 at 7:07
  • @EvanLee It depends :) Specially how much data you write, respectively how long the write operation takes. But in general: No!
    – leo
    Apr 6, 2014 at 14:21
1

A comment has a command to convert your databases to InnoDB here.

0

Everywhere! Deprecate myisam, innodb is the way to go. Is not only about performance, but data integrity and acid transactions.

1
  • ACID transactions are not always needed so in a lot of cases MyISAM is very good. There are different solutions for different problems.
    – Nin
    Aug 27, 2012 at 19:18
0

A supplement to Machine and knoopx's answer about transactions:

The default MySQL table type, MyISAM, does not support transactions. BerkeleyDB and InnoDB are the transaction-safe table types available in open source MySQL, version 3.23.34 and greater.

The definition of the transaction and an banking example

A transaction is a sequence of individual database operations that are grouped together. -- A good example where transactions are useful is in banking.

Source of the citations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.