CREATE TABLE IF NOT EXISTS `nm`.`list_activities` (
`activity_id` INT NOT NULL COMMENT '',
`activity_name` VARCHAR(45) NULL COMMENT '',
`activity_type_id` INT NOT NULL COMMENT '',
PRIMARY KEY (`activity_id`, `activity_type_id`) COMMENT '',
CONSTRAINT `fk_list_activities_log`
FOREIGN KEY (`activity_id`)
REFERENCES `nm`.`log` (`activity_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `nm`.`log` (
`date_id` INT NOT NULL COMMENT '',
`activity_id` INT NOT NULL COMMENT '',
PRIMARY KEY (`date_id`, `activity_id`) COMMENT '')
ENGINE = InnoDB;
无法创建list_activities
表,它显示错误消息
错误代码:1215。无法添加外键约束
在mysql中
答案 0 :(得分:1)
那是因为你有复合主键,而你只在第一列创建FK,所以只创建部分功能依赖。
PRIMARY KEY (`date_id`, `activity_id`)
FOREIGN KEY (`activity_id`)
REFERENCES `nm`.`log` (`activity_id`)
您必须创建一个FK引用两个关键列来解决此问题。将您的FK定义更改为
FOREIGN KEY (`activity_id`, `activity_type_id`)
REFERENCES `nm`.`log` (`activity_id`, `date_id`)