0
CREATE TABLE casting 
(
   idFilms INT,
   idActeur INT,
   roles varchar (50) NOT NULL,
   primary key (idFilms, idActeur),
   FOREIGN KEY (idFilms) REFERENCES films(id),    
   FOREIGN KEY (idActeur) REFERENCES acteur(id)  
);

I keep getting this error

#1005 - Can't create table fabrizioestrellatp1.casting (errno: 150 "Foreign key constraint is incorrectly formed"

Need some help please

  • 5
    What is your RDBMS ? (oracle, mysql, sqlserver, postgres...) ? – Jorge Campos Feb 12 '21 at 02:45
  • 2
    also you are missing the constraint word `CONSTRAINT fk_somename FOREIGN KEY (idFilms) REFERENCES films (id)` also make sure the types of the columns matches – Jorge Campos Feb 12 '21 at 02:49
  • 1
    Have you looked at the answers here? https://stackoverflow.com/q/8434518/73226 – Martin Smith Feb 12 '21 at 02:51
  • 2
    @JorgeCampos You can declare a foreign key without a name if it's directly on a column `idFilms INT FOREIGN KEY (idFilms) references films(id),` – Charlieface Feb 12 '21 at 03:01
  • 1
    @Charlieface yeah, you are right, I should not have said missing, i meant more like that the constraint will be created with a random name, it is better to define one... – Jorge Campos Feb 12 '21 at 03:05
  • 1
    @Charlieface: you can also declare the foreign key without a name at the end of the CREATE statement (at least in standard SQL): https://dbfiddle.uk/?rdbms=postgres_13&fiddle=d6c7e15b6e7b2fcf0802e252d996eb33 Note that MySQL silently ignores inline foreign keys. So if you use `idfilms int references ...` in MySQL you are left **without** a foreign key. –  Feb 12 '21 at 07:41
  • This CREATE TABLE is valid, as long as the films and acteur tables both have the corresponding keys required. – jarlh Feb 12 '21 at 07:42
  • Your code works fine: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=122686efffc095e2a6239115aced9062. I'm voting to close. – Gordon Linoff Feb 12 '21 at 13:06

0 Answers0