简体   繁体   English

如何使用语法和外键解决SQL错误

[英]how to resolve SQL error with syntax and foreign key

I'm getting a sql error in the following table, but i'm unsure where it's occuring. 下表显示了SQL错误,但是我不确定它在哪里发生。 I"m guessing it's something wrong with my foreign key? 我猜我的外键有问题吗?

Error Code: 1064. You have an error in your SQL syntax; 错误代码:1064。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY REFERENCES account(id) ON DELETE CASCADE 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'FOREIGN KEY REFERENCES account(id)附近使用DELETE CASCADE

CREATE TABLE account (
    id INT PRIMARY KEY AUTO_INCREMENT,
    email VARCHAR(254) NOT NULL UNIQUE,
    passwordHash VARCHAR(60) NOT NULL,
    created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    validationCode VARCHAR(10) NOT NULL,
    isValidated BOOL NOT NULL,
    isDisabled BOOL NOT NULL
);

CREATE TABLE accountSession (
    id INT PRIMARY KEY AUTO_INCREMENT,
    accountID INT FOREIGN KEY REFERENCES account(id) ON DELETE CASCADE,
    sessionKey VARCHAR(100) NOT NULL,
    loggedIn TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    lastSeen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

If you use FOREIGN KEY you have to specify a constraint name. 如果使用FOREIGN KEY ,则必须指定约束名称。 If you don't want to name the constraint remove the FOREIGN KEY : 如果您不想命名约束,请删除FOREIGN KEY

So either this: 所以这:

CREATE TABLE accountSession (
    id INT PRIMARY KEY AUTO_INCREMENT,
    accountID INT REFERENCES account(id) ON DELETE CASCADE,
    sessionKey VARCHAR(100) NOT NULL,
    loggedIn TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    lastSeen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

or this: 或这个:

CREATE TABLE accountSession (
    id INT PRIMARY KEY AUTO_INCREMENT,
    accountID INT FOREIGN KEY fk_accountsession2 account REFERENCES account(id) ON DELETE CASCADE,
    sessionKey VARCHAR(100) NOT NULL,
    loggedIn TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    lastSeen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

However: in neither of the above cases, the foreign key will be created because MySQL will silently ignore the definition. 但是:在以上两种情况下,都不会创建外键,因为MySQL将默默地忽略该定义。

Quote from the manual 引用手册

Furthermore, MySQL parses but ignores “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification 此外,MySQL解析但忽略了“内联引用规范”(如SQL标准所定义),其中引用被定义为列规范的一部分。

So if you do want the foreign key, you have to move it to the end: 因此,如果您确实想要外键,则必须将其移到末尾:

CREATE TABLE accountSession (
    id INT PRIMARY KEY AUTO_INCREMENT,
    accountID INT,
    sessionKey VARCHAR(100) NOT NULL,
    loggedIn TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    lastSeen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (accountid) REFERENCES account(id) ON DELETE CASCADE
);

You can not create default on TIMESTAMP Datatype,so you can remove default value or change datatype 您无法在TIMESTAMP数据类型上创建默认值,因此可以删除默认值或更改数据类型

CREATE TABLE account (
id INT PRIMARY KEY IDENTITY(1,1),
email VARCHAR(254) NOT NULL UNIQUE,
passwordHash VARCHAR(60) NOT NULL,
created DateTime NOT NULL DEFAULT GETDATE(),
validationCode VARCHAR(10) NOT NULL,
isValidated bit NOT NULL,
isDisabled bit NOT NULL);


CREATE TABLE accountSession (
    id INT PRIMARY KEY IDENTITY(1,1),
    accountID INT FOREIGN KEY REFERENCES account(id) ON DELETE CASCADE,
    sessionKey VARCHAR(100) NOT NULL,
    loggedIn DateTime NOT NULL ,
    lastSeen DateTime NOT NULL  
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM