简体   繁体   中英

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. I"m guessing it's something wrong with my foreign key?

Error Code: 1064. You have an error in your SQL syntax; 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

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. If you don't want to name the constraint remove the 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.

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

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

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  
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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