Domanda

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)
È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top