Frage

I am attempting to create a composite foreign key in MySQL however both fields are referencing the same column in another table. I am not sure if this is the accurate approach since the sql is not executing. Under is the SQL statement

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;

Error

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

Lösung

Try this by individually applying constraint

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top