表一:
CREATE TABLE `environment` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`APPLICATION` varchar(5) NOT NULL,
`APPLICATION_ID` varchar(42) DEFAULT NULL,
PRIMARY KEY (`ID`,`APPLICATION`)
) ENGINE=InnoDB
表二:
CREATE TABLE `properties_files` (
`FILE_NAME` varchar(254) COLLATE latin1_bin NOT NULL,
`APPLICATION` varchar(10) COLLATE latin1_bin NOT NULL,
KEY `fk_PROPERTIES_FILES_ENVIRONMENT` (`APPLICATION`),
CONSTRAINT `fk_PROPERTIES_FILES_ENVIRONMENT` FOREIGN KEY (`APPLICATION`) REFERENCES `ENVIRONMENT` (`APPLICATION`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB
现在我的要求是如何在表2中设置外键property_files列名是Application。
你能帮帮我吗?
答案 0 :(得分:2)
您必须具有相同结构(数据类型)的引用列(在这种情况下,您必须决定:
varchar(5)
或varchar(10)
NOT NULL
或COLLATE latin1_bin NOT NULL
这只是一个起点,您还必须将引用列“标记”为键/索引/唯一索引。有关详细信息,请参阅此答案: Can a foreign key reference a non-unique index?
这意味着您需要在ENVIRONMENT
表中添加类似内容:
KEY YOUR_KEY_OR_INDEX_NAME(APPLICATION)
在此处添加了工作示例:
答案 1 :(得分:0)
您的外键列属性在master和child中有所不同,请使用以下语法。
参考列上也缺少索引。
CREATE TABLE environment ( ID INT(11) NOT NULL AUTO_INCREMENT, APPLICATION VARCHAR(5) NOT NULL, APPLICATION_ID VARCHAR(42) DEFAULT NULL, PRIMARY KEY (ID,APPLICATION), KEY idx_ap(APPLICATION) ) ENGINE=INNODB
CREATE TABLE properties_files ( FILE_NAME VARCHAR(254) COLLATE latin1_bin NOT NULL, APPLICATION VARCHAR(5) NOT NULL, KEY fk_PROPERTIES_FILES_ENVIRONMENT (APPLICATION), CONSTRAINT fk_PROPERTIES_FILES_ENVIRONMENT FOREIGN KEY (APPLICATION) REFERENCES ENVIRONMENT (APPLICATION) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=INNODB