更改表不添加外键MYSQL

时间:2017-03-11 21:29:43

标签: mysql

当我执行下面的操作时,看起来它设置了常规KEY,而不是外键:

ALTER TABLE `dev1_IMGStoreTmpComponents`
ADD CONSTRAINT `con_fk_templates`
FOREIGN KEY `fk_templates`(`templateId`)
REFERENCES `dev1_IMGStoreTemplates`(`templateId`)

SQL成功运行。

然后我运行:

SHOW CREATE TABLE `dev1_IMGStoreTmpComponents`;

 CREATE TABLE `dev1_IMGStoreTmpComponents` (
 `componentId` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(100) COLLATE utf8mb4_unicode_520_ci NOT NULL,
 `description` varchar(250) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
 `templateId` int(10) unsigned NOT NULL,
 `type` char(3) COLLATE utf8mb4_unicode_520_ci NOT NULL,
 `layer` tinyint(3) unsigned DEFAULT NULL,
 `posx` mediumint(8) unsigned NOT NULL,
 `posy` mediumint(8) unsigned NOT NULL,
 `width` mediumint(8) unsigned NOT NULL,
 `height` mediumint(8) unsigned NOT NULL,
 `settings` text COLLATE utf8mb4_unicode_520_ci,
 PRIMARY KEY (`componentId`),
 UNIQUE KEY `componentId` (`componentId`,`templateId`),
 KEY `con_fk_templates` (`templateId`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci COMMENT='Template Components'

为什么我在Create表中看不到CONSTRAINT?

1 个答案:

答案 0 :(得分:1)

MyISAM存储引擎不支持外键约束。

ENGINE=MYISAM 

InnoDB存储引擎支持参照完整性约束。 (需要使用InnoDB存储引擎定义引用表和引用表。)

ENGINE=INNODB

MySQL确实接受MyISAM表的外键约束语法,但MySQL忽略了外键。接受语法的最可能原因是使DDL从其他数据库迁移更容易。

相关问题