tags:

views:

335

answers:

5

I have made a database and 10 tables in mysql(table type NONE) and i can't create foreign keys. An alert message says

SQLyog The selected table does not support foreign keys. Table engine must be InnoDB, PBXT or SolidDB

Will i must alter all of my tables now? thanks for any hint or suggestion

+4  A: 

Yes. The default storage engine (MyISAM) does not support foreign key constraints.

Ramon
+8  A: 

The engine you're using doesn't support foreign keys (as the error message tells you obviously). You can change the engine you're using with

ALTER TABLE `tableName` ENGINE = newEngine;
+9  A: 

Typically, InnoDB is used to support foreign keys, and transactions:

http://dev.mysql.com/doc/refman/5.1/en/innodb.html

You can change the table engine on the fly:

ALTER TABLE tableName ENGINE = InnoDB;

You can also change the default -- for newly created tables -- as shown here:

http://dev.mysql.com/doc/refman/5.1/en/storage-engine-setting.html

wsorenson
"Or change the default as shown here" isn't quite right because changing the default won't change the format of table that have already been created.
longneck
A: 

The MyISAM engine DOES support foreign keys. What it doesn't support is foreign key constraints.

You can go ahead and create as many foreign keys as you like - but don't expect the database to enforce integrity for you.

Don't change engines in your application without significant testing; changing engine will affect behaviour and is not recommended without great care and a lot of QA work. In particular, switching from MyISAM to InnoDB will break your application if:

  • It doesn't handle deadlocks appropriately (Deadlocks do not happen on MyISAM)
  • It expects table-level locking
  • It relies on (mostly dubious) MyISAM features
  • It is high performance and you have not understood how to tune InnoDB correctly, but did tune MyISAM well (or ok)
  • You have not assessed the disc space impact of switching engines
MarkR
@MarkR: MyISAM *DOES NOT* support foreign keys. If you attempt to add a foreign key constraint to a MyISAM table, MySQL allows the syntax but silently ignores the foreign key addition. That hardly counts as support for foreign keys in my book.
Asaph
It does in a certain way; the syntax being supported but not the feature still allows you to run your standard SQL script on the engine. But it sucks for someone unaware of it not to be warned about unsupported key constraints when executing such scripts.
JP
A: 

thanks for all the answers. So the only way to use foreign key constraints that the database will protect me from unwanted entries is changing from MYISAM to InnoDB?

For example i have 2 tables reservations [id] transactions [id,resv_id(foreign key to reservations table)]

InnoDB will protect me from inserting a record in transactions table with resv_id that don't have an entry in reservations table?

thodoris
Yes. InnoDB should normally be your database engine of choice, falling back to MyISAM only when you need fulltext search (which isn't available in InnoDB), or a particular, unusual case where MyISAM's performance characteristics are required.
bobince