SQL外键错误?

时间:2018-04-12 04:34:15

标签: mysql sql mariadb

  DROP TABLE IF EXISTS userRole;
  DROP TABLE IF EXISTS account;

  CREATE TABLE account (
     lionID VARCHAR(50) NOT NULL,
     firstName VARCHAR(50) NOT NULL,
     hashedpass VARCHAR(255) NOT NULL,
     PRIMARY KEY (lionID)
   );


   CREATE TABLE roles (
     roleID INT UNSIGNED AUTO_INCREMENT NOT NULL,
     lionID VARCHAR(50) NOT NULL,
     administrator BOOLEAN NOT NULL DEFAULT False,
     qRole VARCHAR(255) NOT NULL,
     PRIMARY KEY (roleID),
     FOREIGN KEY (lionID) REFERENCES account(lionID)
   );

我无法理解为什么会出现错误..角色中的外键lionID引用了帐户中的主键lionID,但它似乎并不喜欢它。非常感谢任何帮助。

ERROR 1064 (42000) at line 76: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FOREIGN KEY lionID REFERENCES account lionID'
)' at line 8

1 个答案:

答案 0 :(得分:0)

我认为问题出在create statement:

您需要使用语法:

CONSTRAINT `fk_account`
        FOREIGN KEY (lionID) REFERENCES account(lionID)