1

This is my first post on here. I am still very new to MySQL. I am having issues debugging some code involving Foreign Keys. Whenever I un-comment the foreign keys and try to run the code (using XAMPP and Netbeans 8.2) I get errors saying 'the foregin key constriant is incorrectly formed'? I have checked over the data types between the tables as well as the foreign key syntax and tried all kinds of things but I am all out of ideas (no it is not the commas, I have previously corrected that and the problem persists) and completely fed up with it. Why is this happening and how do I fix this? Thanks

DROP TABLE IF EXISTS Staff;
DROP TABLE IF EXISTS Subjects;
DROP TABLE IF EXISTS Results;
DROP TABLE IF EXISTS Students;


CREATE TABLE Students
(
    StudentID       INT NOT NULL,
    Forename        VARCHAR(20) NOT NULL,
    Surname         VARCHAR(20) NOT NULL,
    Gender          CHAR(1) NOT NULL,
    DateOfBirth     DATE NOT NULL,
    PRIMARY KEY (StudentID)
);


CREATE TABLE Results
(
    StudentID       INT NOT NULL,
    SubjectID       CHAR(6) NOT NULL,
    Result          INT,
    PRIMARY KEY (StudentID, SubjectID),
    FOREIGN KEY (StudentID)REFERENCES Students(StudentID)
    /*FOREIGN KEY (SubjectID)REFERENCES Subjects(SubjectID)*/
);


CREATE TABLE Subjects
(
    SubjectID       CHAR(6) NOT NULL,
    SubjectTitle    VARCHAR(50) NOT NULL,
    StaffID         INT NOT NULL,
    PRIMARY KEY (SubjectID)
    /*FOREIGN KEY (StaffID) REFERENCES Staff(StaffID)*/
);


CREATE TABLE Staff
(
    StaffID         INT NOT NULL,
    Forename        VARCHAR(20) NOT NULL,
    Surname         VARCHAR(20) NOT NULL,
    Post            VARCHAR(20) NOT NULL,
    PRIMARY KEY (StaffID)
);
13008620
  • 23
  • 3

0 Answers0