2016-08-01 164 views
1

我相信這個問題已經被問了很多次了。 如果有人可以看看它會很棒似乎無法調試MySQL錯誤「#1064

我得到#1064 - 你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,在正確的語法中使用 'REFERENCES interests(interests_id)'''),同時使用interests.interest_id創建category_interests和event_attendees表作爲定義外鍵的參考。

我似乎無法找到它的原因。

SET foreign_key_checks = 0; 

DROP table IF EXISTS categories; 
CREATE table categories(
    category_id int AUTO_INCREMENT, 
    category_name VARCHAR(40), 
    category_desc TEXT, 
    CONSTRAINT pk_categories PRIMARY KEY (category_id) 
); 

DROP table IF EXISTS interests; 
CREATE table interests(
    interest_id int AUTO_INCREMENT, 
    interest_name varchar(40), 

    CONSTRAINT pk_interests PRIMARY KEY (interest_id) 
); 


-- ERROR - query does not run 
DROP table IF EXISTS category_interests; 
CREATE table category_interests(
    interest_id int, 
    category_id int, 
    CONSTRAINT fk_interests FOREIGN KEY interest_id REFERENCES interests(interest_id), 
    CONSTRAINT fk_categories FOREIGN KEY category_id REFERENCES categories(category_id) 
); 

-- ERROR - query does not run 
DROP table IF EXISTS event_atendees; 
CREATE table event_atendees(
    attendee_id int AUTO_INCREMENT, 
    interest_id int, 
    lat double, 
    lon double, 
    name varchar(115), 
    dob date, 
    gender CHAR(1), 

    CONSTRAINT pk_event_atendees PRIMARY KEY (attendee_id), 
    CONSTRAINT fk_intrests_ea FOREIGN KEY interests_id REFERENCES `interests`(interests_id) 
); 

SET foreign_key_checks = 1; 

回答

0

的錯誤,我發現:

  1. 你忘了通過()圍住外鍵列。它應該是 CONSTRAINT fk_interests FOREIGN KEY (interest_id) REFERENCES interests(interest_id)。它不應該是CONSTRAINT fk_interests FOREIGN KEY interest_id....(不帶圓括號)
  2. 某處您使用了interests_id。但你應該使用 interest_id

試試這個:

SET foreign_key_checks = 0; 

DROP table IF EXISTS categories; 
CREATE table categories(
    category_id int AUTO_INCREMENT, 
    category_name VARCHAR(40), 
    category_desc TEXT, 
    CONSTRAINT pk_categories PRIMARY KEY (category_id) 
); 

DROP table IF EXISTS interests; 
CREATE table interests(
    interest_id int AUTO_INCREMENT, 
    interest_name varchar(40), 

    CONSTRAINT pk_interests PRIMARY KEY (interest_id) 
); 


-- ERROR - query does not run 
DROP table IF EXISTS category_interests; 
CREATE table category_interests(
    interest_id int, 
    category_id int, 
    CONSTRAINT fk_interests FOREIGN KEY (interest_id) REFERENCES interests(interest_id), 
    CONSTRAINT fk_categories FOREIGN KEY (category_id) REFERENCES categories(category_id) 
); 

-- ERROR - query does not run 
DROP table IF EXISTS event_atendees; 
CREATE table event_atendees(
    attendee_id int AUTO_INCREMENT, 
    interest_id int, 
    lat double, 
    lon double, 
    name varchar(115), 
    dob date, 
    gender CHAR(1), 

    CONSTRAINT pk_event_atendees PRIMARY KEY (attendee_id), 
    CONSTRAINT fk_intrests_ea FOREIGN KEY (interest_id) REFERENCES `interests`(interest_id) 
); 

SET foreign_key_checks = 1; 

WORKING DEMO

+0

我已經提到我所做的更改。這是您的模式的工作演示。 http://sqlfiddle.com/#!9/d6c965/2/0。 @NishonTandukar – 1000111

+0

我修復了已識別的錯誤 現在我得到了#1215 - 無法添加外鍵約束' 而我嘗試創建** event_atendess **表 –

+0

它不是應該是'event_atendees'嗎?請檢查工作演示。它成功執行所有查詢。 – 1000111