Oracle table 中缺少右括号错误

Missing right parenthesis error in Oracle table

CREATE TABLE Countries
    (CountryID INT NOT NULL PRIMARY KEY,
    CountryName VARCHAR2(100) NOT NULL,
    RegionID INT FOREIGN KEY REFERENCES Regions(RegionID)
    );

一直给我扔这个: ORA-00907: missing right parenthesis

我做错了什么?

内联外键语法不带 FOREIGN KEY 关键字。

考虑:

CREATE TABLE Countries (
    CountryID INT NOT NULL PRIMARY KEY,
    CountryName VARCHAR2(100) NOT NULL,
    RegionID INT REFERENCES Regions(RegionID)
);

或者,您可以在另一行声明外键,如下所示:

CREATE TABLE Countries (
    CountryID INT NOT NULL PRIMARY KEY,
    CountryName VARCHAR2(100) NOT NULL,
    RegionID INT,
    CONSTRAINT fkRegionID FOREIGN KEY (RegionID) REFERENCES Regions(RegionID)
);

这也有效:

CREATE TABLE Countries (
    CountryID INT NOT NULL PRIMARY KEY,
    CountryName VARCHAR2(100) NOT NULL,
    RegionID INT,
    FOREIGN KEY (RegionID) REFERENCES Regions(RegionID)
);

Demo on DB Fiddle