MySql Composite外键引用相同的列

时间:2014-02-21 20:03:28

标签: mysql sql foreign-keys foreign-key-relationship

我正在尝试在 MySQL 中创建复合外来键,但这两个字段都引用另一个表中的同一列。我不确定这是否是准确的方法,因为sql没有执行。 Under是SQL语句

SQL

ALTER TABLE tableA ADD CONSTRAINT `comp_fk`
    FOREIGN KEY (`a_id` , `b_id` )
    REFERENCES `tabelB` (`p_id` , `p_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;

错误

MySQL Database Error: Can't create table 'sep.#sql-984_8' (errno: 150)

1 个答案:

答案 0 :(得分:1)

单独应用约束

尝试此操作
ALTER TABLE `comp_fk`  
  ADD CONSTRAINT `test` FOREIGN KEY (`a_id`) REFERENCES `tabelB`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
  ADD CONSTRAINT `test2` FOREIGN KEY (`b_id`) REFERENCES `tabelB`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION