2012-03-24 234 views
2

大家好我無法找到在此表中創建位錯誤,似乎真的直截了當地是,這裏就是它給我:MySQL錯誤#1064

錯誤1064第3行:您有一個您的SQL語法錯誤;檢查對應於你的MySQL服務器版本的權利 語法使用近「的外鍵(courses_courseDepartmentAbbv))」在 線 手冊8

DROP TABLE IF EXISTS courses; 

CREATE TABLE courses(
    courses_courseNumber INT NOT NULL AUTO_INCREMENT, 
    courses_courseTitle VARCHAR(25) NOT NULL, 
    courses_courseTeacher VARCHAR(30) NOT NULL, 
    courses_courseCostOfBooks DECIMAL(5,2) NOT NULL, 
    courses_courseDepartmentAbbv CHAR(4) NOT NULL, 
    PRIMARY KEY (courses_courseNumber), 
    FOREIGN KEY (courses_courseTeacher), 
    FOREIGN KEY (courses_courseDepartmentAbbv) 
); 

DROP TABLE IF EXISTS departments; 

CREATE TABLE departments(
    departments_departmentAbbv CHAR(4) NOT NULL, 
    departments_departmentFullName VARCHAR(15) NOT NULL, 
    PRIMARY KEY (departments_departmentAbbv), 
    FOREIGN KEY (departments_departmentAbbv) REFERENCES (courses_courseDepartmentAbbv) 
); 

DROP TABLE IF EXISTS teachers; 

CREATE TABLE teachers(
      teachers_teacherName VARCHAR(20) NOT NULL, 
    teachers_teacherHomeroom SMALLINT(3) NOT NULL, 
    teachers_teacherHomeroomGrade SMALLINT(1) NOT NULL, 
    teachers_teacherFullTime BOOL NOT NULL, 
    PRIMARY KEY (teachers_teacherName), 
    FOREIGN KEY (teachers_teacherName) REFERENCES (courses_courseTeacher) 
); 

回答

2

你需要在每個外鍵後參考文獻。在開始設置課程時你錯過了。這裏是documentation

我認爲這是更多你想要的。您的創作順序不正確。由於這個,你的外鍵位於錯誤的位置。您只能在與PK關係的表上設置外鍵映射。你只需要在其他桌子上設置PK。只要你按照正確的順序創建表格,那麼你可以做到這一點,如下所示。

因此,teachersdepartments的主鍵是courses表中的外鍵。 teachersdepartments不需要擔心外鍵。你離開,多達實際具有參考(courses

DROP TABLE IF EXISTS teachers; 

CREATE TABLE teachers(
      teachers_teacherName VARCHAR(20) NOT NULL, 
    teachers_teacherHomeroom SMALLINT(3) NOT NULL, 
    teachers_teacherHomeroomGrade SMALLINT(1) NOT NULL, 
    teachers_teacherFullTime BOOL NOT NULL, 
    PRIMARY KEY (teachers_teacherName) 
    --FOREIGN KEY (teachers_teacherName) REFERENCES courses (courses_courseTeacher) 
    --This is not where you set up the FK for courses 
); 

DROP TABLE IF EXISTS departments; 

CREATE TABLE departments(
    departments_departmentAbbv CHAR(4) NOT NULL, 
    departments_departmentFullName VARCHAR(15) NOT NULL, 
    PRIMARY KEY (departments_departmentAbbv) 
    --FOREIGN KEY (departments_departmentAbbv) REFERENCES courses (courses_courseDepartmentAbbv) 
    --This is not where you set up the FK for courses 
); 

CREATE TABLE courses(
    courses_courseNumber INT NOT NULL AUTO_INCREMENT, 
    courses_courseTitle VARCHAR(25) NOT NULL, 
    courses_courseTeacher VARCHAR(30) NOT NULL, 
    courses_courseCostOfBooks DECIMAL(5,2) NOT NULL, 
    courses_courseDepartmentAbbv CHAR(4) NOT NULL, 
    PRIMARY KEY (courses_courseNumber), 
    FOREIGN KEY (courses_courseTeacher) 
     REFERENCES teachers (teachers_teacherName) 
    FOREIGN KEY (courses_courseDepartmentAbbv) 
     REFERENCES departments(departments_departmentAbbv) 
); 
+0

試過了,仍然給我同樣的錯誤,雖然 – user1086348 2012-03-24 02:11:36

+0

@ user1086348我更新我的答案的代碼,應該是用作你的參考資料。 – 2012-03-24 02:23:23

+0

真棒我現在明白了,初學者在這裏,謝謝 – user1086348 2012-03-24 02:26:32

0

你需要給一個參考條款爲您foreign key子句

FOREIGN KEY (courses_courseTeacher) 
FOREIGN KEY (courses_courseDepartmentAbbv)