Foreign key in mysql - mysql

Does mysql server have the concept of foreign key or it's only in InnoDB?
I know that InnoDB is part of mysql but it's a kind of extension. what if I wanted to use only mysql and still taking advantage of foreign keys?
Thanks

InnoDB supports foreign keys. See http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
InnoDB is the default storage engine in MySQL 5.5 and later. You should not think of InnoDB as "a kind of extension." You should use InnoDB unless you have a specific and testable reason to use some other storage engine.
However, you're right that outside the storage engine layer, MySQL server has no implementation of foreign keys. Even the parsing of foreign key constraint syntax is delegated to the storage engine, whereas all other SQL parsing is handled at a higher level.
The NDB storage engine in MySQL Cluster also supports foreign keys.

The support of foreign keys in mysql depends on the storage engine.
Myisam and innodb are the main storage engines. Innodb does support foreign keys, myisam doesn't.

Related

Geodjango and Innodb, mixed innodb and myisam models

I keep hearing that InnoDB is better for data integrity, unfortunately as of MySQL 5.6 it has yet to support SPATIAL indexes. A fast SPATIAL index is pretty critical to my app, though what's nice about my model that it's pretty much results in a fairly static (write once, read many) table of (ID, POINT), so I could use MyISAM and not care too much.
I'd like to restrict the use of MyISAM to just that table, and migrate it over when InnoDB support for SPATIAL is ready. Problem is, if I ALTER TABLE after my models are migrated (by having an app/sql/app_model.sql) to switch the table to MyISAM, MySQL complains:
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
That makes sense, my other models refer to this one and Django automatically makes FOREIGN KEY constraints between those models and this one.
What's the best strategy here? Should I abandon InnoDB and switch everything back to MyISAM? Can I just drop all the FOREIGN KEY constraints?
I tried automating the FOREIGN KEY drops by looking in INFORMATION_SCHEMA.TABLE_CONSTRAINTS, but that only lists the tables that have the constraints, not the tables referred to by those constraints. I would have to do some fuzzy column name matching which feels very brittle.
To solve this I gave up on using InnoDB by default. Because Amazon RDS makes Inno the default, I did this by adding an init_command in my settings.py:
'default': {
'OPTIONS': {
'init_command' : 'SET storage_engine=MYISAM', # Can't make SPATIAL keys on InnoDB
},
}
Then for all but the table with a SPATIAL index I created a $modelname.sql file under the $appname/sql directory that changes the engine after it's created.
-- Alter to InnoDB so we can make concurrent insertions w/o full table lock.
ALTER TABLE <modeltable> ENGINE=INNODB;
Switching to MYISAM default means Django doesn't automatically create the FOREIGN KEY constraints for you for your Inno tables which isn't ideal. I wish there was a way to make Django create them after-the-fact.

Why doesn't MySQL's MyISAM engine support Foreign keys?

I am writing a web-app for my studies which includes fulltext search and foreign keys.
I have read somewhere, MyISAM engine is suitable for fulltext searching, and InnoDB for foreign keys.
In this situation what engine should I use for the best performance?
Why does MyISAM not support foreign key relationship but InnoDB does?
Why does MyISAM support full text search but InnoDB does not?
Kindly tell me, In this situation what engine I have to use for improve performance?
The performance of each storage engine will depend on the queries you perform. However, be aware that different tables within the same database can use different storage engines.
Why MyISAM engine does not support foreign key relationship and InnoDB does?
As documented under Foreign Key Differences:
At a later stage, foreign key constraints will be implemented for MyISAM tables as well.
Therefore, foreign key constraints have simply not yet been implemented in MyISAM.
EDIT: As that comment is removed from docs, it appears that it is no longer planned to implement foreign key constraints in MyISAM engine.
Why MyISAM engine does support full text search and InnoDB does not?
As documented under What Is New in MySQL 5.6:
You can create FULLTEXT indexes on InnoDB tables, and query them using the MATCH() ... AGAINST syntax.
Therefore, full text search has been implemented in InnoDB as of MySQL 5.6.
I do remember the times when mysql had only myisam and innodedb was in development. MyIsam has no foreign keys because it is old system that does not support relations in database. It will never use foreign keys! To Use it you have innodb. If you don't need all stuff, like relations in DB, use MyISAM to get better performance.

Does MySQL do automatic indexing for foreign keys?

Can anyone tell me if MySQL does indexing for its foreign keys automatically or not?
My MySQL is using MyIsam Engine.
MyISAM does not support foreign keys at all. From the manual:
For storage engines other than InnoDB, MySQL Server parses the FOREIGN
KEY syntax in CREATE TABLE statements, but does not use or store it.
... At a later stage, foreign key constraints will be implemented for
MyISAM tables as well.
This is for MySQL 5.6, the next version, so it is not implemented yet. The text is exactly the same for older versions.
This means that the foreign key construct is not used at all. You can specify it in your CREATE TABLE command but MySQL will silently ignore it. No index will be made out of it, and it won't be stored (so a SHOW CREATE TABLE command will not show that you tried to createa a foreign key).
If you need foreign key support, consider using the InnoDB storage engine instead. InnoDB creates indices automatically for foreign keys.
MyISAM engine doesn't support foreign keys, only the InnoDB engine:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
InnoDB requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later, if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.

What's the safest way to convert table with InnoDB to MyISAM?

My database is currently using the InnoDB engine. Now I want to add the fulltext search feature, which is why I want to convert my tables to MyISAM. But doing so breaks all foreign keys. How can I change my table engines to MyISAM safely?
How can I use SELECT...JOIN after I change my tables to the MyISAM engine?
ALTER TABLE jobs ENGINE = MyISAM;
Cannot delete or update a parent row: a foreign key constraint fails
I'd recommend you to do a dump of the db, change all the text from that file from InnoDB to MyISAM, then load the modified file
As I know, MyISAM doesn't supports foreign keys (compare the features offered by InnoDB vs the features of MyISAM). MySQL tries to tell you that you have to drop every foreign key constraint that references your jobs table before changing its engine to MyISAM.

foreign key problem in mysql

hay i have two tables i want to relate them with a foreign key i made it locally and works fine using innoDB but when i upload it to my server database the type of table automatically change to myISAM. when i checked the type again i found that there is no innoDB engine type instead MyISAM, MEMORY, BLACKHOLE, EXAMPLE, ARCHIVE, CSV,FEDERATED, MRG_MYISAM were there. please help which storage engine out of these can support foreign key.
None of them. Only innodb supports FKs.