问题
After making a few tests, I found that foreign keys on MEMORY engine miss one of the checks for consistency.
To explain better, here a similar example in InnoDb
CREATE TABLE testInnoDb1 (
c1 int(11) PRIMARY KEY
) ENGINE=InnoDB;
CREATE TABLE testInnoDb2 (
c1 int(11) PRIMARY KEY,
FOREIGN KEY (c1) REFERENCES testInnoDb1(c1) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB;
INSERT INTO testInnoDb1 (c1) VALUES (42);
INSERT INTO testInnoDb2 (c1) VALUES (42);
-- INSERT INTO testInnoDb2 (c1) VALUES (64); -- This will fail
-- UPDATE testInnoDb2 SET c1=128; -- This will fail
And the same with MEMORY
CREATE TABLE testMem1 (
c1 int(11) PRIMARY KEY
) ENGINE=MEMORY;
CREATE TABLE testMem2 (
c1 int(11) PRIMARY KEY,
FOREIGN KEY (c1) REFERENCES testMem1(c1) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=MEMORY;
INSERT INTO testMem1 (c1) VALUES (42);
INSERT INTO testMem2 (c1) VALUES (42);
-- INSERT INTO test2 (c1) VALUES (64); -- This will fail
UPDATE testMem2 SET c1=128; -- This should fail?
My question is: Why the update consistency is not maintained with the memory engine? Is that a bug or it is for any real reason? Any reference to a known bug for this?
As additional info, I am using for development the repository release on my Linux which is still 15.1 (5.1)
回答1:
MySQL does not currently support foreign key constraint checking:
MySQL 5.6 Reference Manual
13.1.17.3 Using FOREIGN KEY Constraints
Foreign Keys and Other MySQL StatementsImportant
For storage engines that do not support foreign keys (such as MyISAM), MySQL Server parses and ignores foreign key specifications.Chapter 15 Alternative Storage Engines
Table 15.1 Storage Engines Feature SummaryFeature MyISAM Memory InnoDB Archive NDB Foreign key support No No Yes No No
15.3 The MEMORY Storage Engine
Table 15.4 MEMORY Storage Engine FeaturesForeign key support No
PS From 15.3 re InnoDB (which has FKs) vs MEMORY (which does not):
Performance Characteristics
Despite the in-memory processing for MEMORY tables, they are not necessarily faster than InnoDB tables on a busy server, for general-purpose queries, or under a read/write workload.
来源:https://stackoverflow.com/questions/34283488/mysql-memory-engine-do-not-check-on-update-foreign-key