似乎无法调试MySQL错误“#1064

时间:2016-08-01 06:52:06

标签: mysql foreign-keys

我确信这个问题已被多次询问过。 如果有人能看一眼就会很棒

  

我得到#1064 - 你的SQL语法有错误;检查与MySQL服务器版本对应的手册,以获得正确的语法   在使用interests.interest_id作为定义外键的参考创建category_interests和event_attendees表时,'REFERENCES interest(interests_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 interests_id REFERENCES `interests`(interests_id)
);

SET foreign_key_checks = 1;

1 个答案:

答案 0 :(得分: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
  3. 试试这个:

    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