oracle SQL代码中的语法错误,包含“无效的表名”和“缺少右parthensis”

llycmphe  于 2022-12-11  发布在  Oracle
关注(0)|答案(1)|浏览(103)

今晚我有一个学校项目要交,我和我的团队正在努力解决这个问题。我们比较了教授的代码,但我们找不到错误。我们不断得到“无效的表名”和“缺少右括号”。我们知道错误在代码的CONSTRAINT部分的某个地方。用户表成功创建,并且没有抛出错误,尽管所有其他表都抛出错误。有人能帮忙指出这个错误吗?我知道这是语法的问题。在创建表时,所需的结果不会有任何错误。

DROP TABLE "User";
DROP TABLE "UserFeed";
DROP TABLE "Post";
DROP TABLE "Story";
DROP TABLE "FactCheck";
DROP TABLE "Archive";

CREATE TABLE "User" (
    UserID  INT NOT NULL PRIMARY KEY ,
    Username VARCHAR(30) NOT NULL,
    FirstName VARCHAR(30) NOT NULL,
    LastName VARCHAR(30) NOT NULL,
    DateOfBirth DATE NOT NULL,
    Email   VARCHAR(50) NOT NULL);

CREATE TABLE UserFeed (
        UserFeedID      CHAR(10)    NOT NULL PRIMARY KEY,
        PostCount       INT         NOT NULL,
        FollowerCount   INT     NOT NULL,
        FollowingCount  INT     NOT NULL,
        UserID          INT     NOT NULL,
        CONSTRAINT  FK_UserUserFeed
        FOREIGN KEY (UserID)
        REFERENCES User(UserID)
    );

CREATE TABLE "FactCheck" (
    RevisionID  INT     PRIMARY KEY NOT NULL,
    TrueFalse   VARCHAR(1)  NOT NULL,
    UserFeedID  CHAR(10)    NOT NULL,
    CONSTRAINT  FK_UserFeedID
    FOREIGN KEY (UserFeedID)
    REFERENCES  UserFeed(UserFeedID)
    );

CREATE TABLE "Post" (
    PostID      CHAR(10)    PRIMARY KEY  NOT NULL,
    Caption     VARCHAR(300)    NULL,
    LikeCount   INT     NOT NULL,
    CommentCount    INT     NOT NULL,
    DatePosted  DATE        NULL,
    UserFeedID  CHAR(10)    NOT NULL,
    RevisionID  INT     NOT NULL,
    CONSTRAINT  FK_UserFeedID
    FOREIGN KEY (UserFeedID)
    REFERENCES  UserFeed(UserFeedID)
    CONSTRAINT  FK_RevisionID
    FOREIGN KEY (RevisionID)
    REFERENCES  FactCheck(RevisionID));

CREATE TABLE "Story" (
    StoryID     CHAR(10)    PRIMARY KEY NOT NULL,
    StoryCaption    VARCHAR(100)    NULL,
    Duration    INT     NOT NULL,
    UserFeedID  CHAR(10)    NOT NULL,
    StoryDate   DATE        NULL,
    RevisionID  INT     NOT NULL,
    CONSTRAINT  FK_UserFeedID
    FOREIGN KEY (UserFeedID)
    REFERENCES  UserFeed(UserFeedID)
    CONSTRAINT  FK_RevisionID
    FOREIGN KEY (RevisionID)
    REFERENCES  FactCheck(RevisionID)
    );

CREATE TABLE    "Archive" (
    ArchiveID   CHAR(10)    PRIMARY KEY NOT NULL,
    PostID      CHAR(10)    NOT NULL,
    StoryID     CHAR(10)    NOT NULL,
    CONSTRAINT  FK_PostID
    FOREIGN KEY (PostID)
    REFERENCES  Post(PostID)
    CONSTRAINT  FK_StoryID
    FOREIGN KEY (StoryID)
    REFERENCES  Story(StoryID)
    );
e0bqpujr

e0bqpujr1#

A few things:

  • You generally don't need quotes around table names, unless you want them
  • User is a reserved word, so you should choose a different table name (or keep it quoted, and then quote it everywhere)
  • You need a comma between constraints, this is why you're getting the missing parentheses error
  • You need separate names for constraints, you can't reuse them

See below example:

CREATE TABLE Users ( --Changed to Users
    UserID  INT NOT NULL PRIMARY KEY ,
    Username VARCHAR(30) NOT NULL,
    FirstName VARCHAR(30) NOT NULL,
    LastName VARCHAR(30) NOT NULL,
    DateOfBirth DATE NOT NULL,
    Email   VARCHAR(50) NOT NULL
);

CREATE TABLE UserFeed (
        UserFeedID      CHAR(10)    NOT NULL PRIMARY KEY,
        PostCount       INT         NOT NULL,
        FollowerCount   INT     NOT NULL,
        FollowingCount  INT     NOT NULL,
        UserID          INT     NOT NULL,
        CONSTRAINT  FK_UserUserFeed
        FOREIGN KEY (UserID)
        REFERENCES Users(UserID) --Changed to Users
    );

CREATE TABLE FactCheck (
    RevisionID  INT     PRIMARY KEY NOT NULL,
    TrueFalse   VARCHAR(1)  NOT NULL,
    UserFeedID  CHAR(10)    NOT NULL,
    CONSTRAINT  FK_UserFeedID
    FOREIGN KEY (UserFeedID)
    REFERENCES  UserFeed(UserFeedID)
    );

CREATE TABLE Post (
    PostID      CHAR(10)    PRIMARY KEY  NOT NULL,
    Caption     VARCHAR(300)    NULL,
    LikeCount   INT     NOT NULL,
    CommentCount    INT     NOT NULL,
    DatePosted  DATE        NULL,
    UserFeedID  CHAR(10)    NOT NULL,
    RevisionID  INT     NOT NULL,
    CONSTRAINT  FK_UserFeedID2 --Added '2' so the name is unique
    FOREIGN KEY (UserFeedID)
    REFERENCES  UserFeed(UserFeedID), --Comma added
    CONSTRAINT  FK_RevisionID
    FOREIGN KEY (RevisionID)
    REFERENCES  FactCheck(RevisionID)
);

CREATE TABLE Story (
    StoryID     CHAR(10)    PRIMARY KEY NOT NULL,
    StoryCaption    VARCHAR(100)    NULL,
    Duration    INT     NOT NULL,
    UserFeedID  CHAR(10)    NOT NULL,
    StoryDate   DATE        NULL,
    RevisionID  INT     NOT NULL,
    CONSTRAINT  FK_UserFeedID3 --Added '3' so the name is unique
    FOREIGN KEY (UserFeedID)
    REFERENCES  UserFeed(UserFeedID), --Added comma
    CONSTRAINT  FK_RevisionID2 --Added '2' so the name is unique
    FOREIGN KEY (RevisionID)
    REFERENCES  FactCheck(RevisionID)
    );

CREATE TABLE    Archive (
    ArchiveID   CHAR(10)    PRIMARY KEY NOT NULL,
    PostID      CHAR(10)    NOT NULL,
    StoryID     CHAR(10)    NOT NULL,
    CONSTRAINT  FK_PostID
    FOREIGN KEY (PostID)
    REFERENCES  Post(PostID),
    CONSTRAINT  FK_StoryID
    FOREIGN KEY (StoryID)
    REFERENCES  Story(StoryID)
    );

相关问题