Search code examples
databaseforeign-keysprojectmysql-error-1064

I got error #1064 while making a database table with Foreign key


I'm making a table for my school project and got a #1064 error on line 2

CREATE TABLE markah (
   kodKursus int(4),
   FOREIGN KEY REFERENCES kursus(kodKursus),
   kodAkaun int(4) not null,
   FOREIGN KEY REFERENCES akaun(kodAkaun),
   jumlahMarkah int(10) not null,
   jumlahJawabBetul int(10) not null,
   jumlahJawabKesalahan int(10) not null,
   jumlahMasaGuna time(6) not null,
   FOREIGN KEY REFERENCES masa(jumlahMasaGuna),
   kodItemDapat int(4),
   FOREIGN KEY REFERENCES item(kodItemDapat)
);

Solution

  • You are missing the foreign key on every FOREIGN KEY REFERENCES. Modify the following according to your needs:

    CREATE TABLE markah (
      kodKursus int(4),
      FOREIGN KEY (foreignKeyToComplete) REFERENCES kursus(kodKursus),
      kodAkaun int(4) not null,
      FOREIGN KEY (foreignKeyToComplete) REFERENCES akaun(kodAkaun),
      jumlahMarkah int(10) not null,
      jumlahJawabBetul int(10) not null,
      jumlahJawabKesalahan int(10) not null,
      jumlahMasaGuna time(6) not null,
      FOREIGN KEY (foreignKeyToComplete) REFERENCES masa(jumlahMasaGuna),
      kodItemDapat int(4),
      FOREIGN KEY (foreignKeyToComplete) REFERENCES item(kodItemDapat)
    );