我使用以下脚本在 mysql 中创建一个表:
CREATE TABLE IF NOT EXISTS users_x_activities(
id int NOT NULL auto_increment,
id_user int unsigned NOT NULL,
id_attivita int unsigned NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES utente(id),
FOREIGN KEY (id_attivita) REFERENCES attivita(id)
) ENGINE = INNODB;
当我从 phpMyAdmin 导出创建的表时,我获得以下脚本
CREATE TABLE IF NOT EXISTS `users_x_activities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(10) unsigned NOT NULL,
`id_attivita` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `id_user` (`id_user`),
KEY `id_attivita` (`id_attivita`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
所以问题是:我的外键约束在哪里?是KEY
指FK吗?似乎这两个表utente
并attivita
不再在新生成的脚本中引用。我在哪里做错了?
编辑
在 phpMyAdmin 中,配置表的导出我找到了“显示外键关系”选项如果我标记这个选项,我也在脚本中获得了这个代码
--
-- RELATIONS FOR TABLE `users_x_activity`:
-- `id_user`
-- `utente` -> `id`
-- `id_attivita`
-- `attivita` -> `id`
--
--
-- Constraints for dumped tables
--
--
-- Constraints for table `users_x_activity`
--
ALTER TABLE `users_x_activity`
ADD CONSTRAINT `users_x_activities_ibfk_1` FOREIGN KEY (`id_user`) REFERENCES `utente` (`id`),
ADD CONSTRAINT `users_x_activities_ibfk_2` FOREIGN KEY (`id_attivita`) REFERENCES `attivita` (`id`);
这意味着如果我添加选项“显示外键关系”我也会获得 FK 约束?在其他情况下不是?