RateM
RateM

Reputation: 291

Unable to create table in mysql while referencing

CREATE TABLE `DB`.`ORDER` (
    `oid`               INT NOT NULL UNIQUE AUTO_INCREMENT,
    `createdAt`         TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `oValue`            FLOAT NOT NULL,
    `no`                INT NOT NULL,
    `qty`               INT NOT NULL,
    `status`            VARCHAR(20) NOT NULL,
    `refundStatus`      VARCHAR(10),
    `refundId`          INT,
    `paymentStatus`     VARCHAR(10),
    `paymentId`         INT,
    `aid`               INT,
    PRIMARY KEY(`oid`),
    FOREIGN KEY `aid` REFERENCES ADDR(`aid`)
)
ENGINE = InnoDB;

ADDR table with key aid do exist in the db but I am getting error while running this command.

Error:

CREATE TABLE `DB`.`ORDER` (
    `oid`               INT NOT NULL UNIQUE AUTO_INCREMENT,
    `createdAt`         TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `oValue`            FLOAT NOT NULL,
    `no`                INT NOT NULL,
    `qty`               INT NOT NULL,
    `status`            VARCHAR(20) NOT NULL,
    `refundStatus`      VARCHAR(10),
    `refundId`          INT,
    `paymentStatus`     VARCHAR(10),
    `paymentId`         INT,
    `aid`               INT,
    PRIMARY KEY(`oid`),
    FOREIGN KEY `aid` REFERENCES ADDR(`aid`)
)
ENGINE = InnoDB
MySQL said: Documentation

#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 'REFERENCES ADDR(`aid`)
)
ENGINE = InnoDB' at line 14

I am not able to figure out why in this case it is happening and not in other case.

Upvotes: 0

Views: 27

Answers (1)

The Impaler
The Impaler

Reputation: 48750

Replace:

FOREIGN KEY `aid` REFERENCES ADDR(`aid`)

with:

FOREIGN KEY (`aid`) REFERENCES ADDR(`aid`)

or with the official standard SQL syntax:

constraint fk1 FOREIGN KEY (`aid`) REFERENCES ADDR (`aid`)

Upvotes: 1

Related Questions