简单的SQL问题,无法弄清楚我做错了什么来获取这些错误 - ORA-00942 / 907/922

时间:2016-07-17 10:51:47

标签: sql oracle

这应该是一个简单的SQL项目,但我坚持第一步。我之前从未使用过SQL,所以我很丢失。有人可以告诉我,我做错了导致这些错误吗?

这是文件:

   SPOOL output.log;

    DROP TABLE rental CASCADE CONSTRAINTS;

    DROP TABLE movie CASCADE CONSTRAINTS;

    DROP TABLE customer CASCADE CONSTRAINTS;

    DROP TABLE distributor CASCADE CONSTRAINTS;

    DROP TABLE rental_store CASCADE CONSTRAINTS;


    CREATE TABLE rental (   
        inventory_id        CHAR(10)        PRIMARY KEY,
        transaction_id      CHAR(10)        NOT NULL UNIQUE,
        late                DECIMAL(6,2),
        damaged             DECIMAL(6,2),
        fail_rewind         DECIMAL(6,2),
        taxes               DECIMAL(6,2)    NOT NULL,
        discount            DECIMAL(6,2),
        customer_id         CHAR(10)        NOT NULL UNIQUE,
    CONTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id));

    CREATE TABLE movie (    
        title_id_number     CHAR(10)        PRIMARY KEY,
        genre               VARCHAR2(20)    NOT NULL,
        actor               VARCHAR2(30)    NOT NULL, 
        director            VARCHAR2(30)    NOT NULL,
        awards              VARCHAR2(30),
        running_length      INTEGER         NOT NULL,
        rating              VARCHAR2(5)     NOT NULL,
        year_released       INTEGER         NOT NULL,
        media_type          CHAR(5)         NOT NULL,
        inventory_id        CHAR(10)        NOT NULL UNIQUE,
        title               VARCHAR2(30)    NOT NULL,
        distrib_serial      INTEGER         NOT NULL UNIQUE,
        cat_mov_id          CHAR(10)        NOT NULL UNIQUE));    

    CREATE TABLE customer ( 
        customer_id     CHAR(10)        PRIMARY KEY,
        name            VARCHAR2(30)    NOT NULL,
        address         VARCHAR2(50)    NOT NULL,
        tele_number     CHAR(10)        NOT NULL UNIQUE,
        rent_history    INTEGER         NOT NULL));

    CREATE TABLE distributor (
          distributor_name      VARCHAR2(30)    PRIMARY KEY,
          catalog               VARCHAR2(30)    NOT NULL,
          genres_offered        VARCHAR2(50)    NOT NULL));

    CREATE TABLE rental_store (
           store_name       VARCHAR2(30)    PRIMARY KEY,
           address          VARCHAR2(50)    NOT NULL,
           owner            VARCHAR2(30)    NOT NULL));

    SPOOL OFF;  

Output from running it in Putty Output part 2

2 个答案:

答案 0 :(得分:1)

DROP语句的错误是预期的,因为表尚不存在。你可以放心地忽略它们。

你的大多数问题来自第二个右括号,例如

CREATE TABLE movie (    
    ...
    cat_mov_id          CHAR(10)        NOT NULL UNIQUE));  
                                                        ^ ---- here

您需要删除它们。由于外键的列列表,它仅在第一个语句中需要。

你得到的第一个(实际)错误是因为你拼写错误CONSTRAINT(你写了CONTRAINT。完整的条款必须是

CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
   ^ -- the "s" was missing here

但是,您无法为不存在的表创建外键约束。因此,您需要更改create语句的顺序,首先创建customer表,然后,您可以创建rental表。

把所有这些放在一起,你的脚本应该是这样的:

DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;

CREATE TABLE customer ( 
    customer_id     CHAR(10)        PRIMARY KEY,
    name            VARCHAR2(30)    NOT NULL,
    address         VARCHAR2(50)    NOT NULL,
    tele_number     CHAR(10)        NOT NULL UNIQUE,
    rent_history    INTEGER         NOT NULL
);

CREATE TABLE rental (   
    inventory_id        CHAR(10)        PRIMARY KEY,
    transaction_id      CHAR(10)        NOT NULL UNIQUE,
    late                DECIMAL(6,2),
    damaged             DECIMAL(6,2),
    fail_rewind         DECIMAL(6,2),
    taxes               DECIMAL(6,2)    NOT NULL,
    discount            DECIMAL(6,2),
    customer_id         CHAR(10)        NOT NULL UNIQUE,
    CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);

CREATE TABLE movie (    
    title_id_number     CHAR(10)        PRIMARY KEY,
    genre               VARCHAR2(20)    NOT NULL,
    actor               VARCHAR2(30)    NOT NULL, 
    director            VARCHAR2(30)    NOT NULL,
    awards              VARCHAR2(30),
    running_length      INTEGER         NOT NULL,
    rating              VARCHAR2(5)     NOT NULL,
    year_released       INTEGER         NOT NULL,
    media_type          CHAR(5)         NOT NULL,
    inventory_id        CHAR(10)        NOT NULL UNIQUE,
    title               VARCHAR2(30)    NOT NULL,
    distrib_serial      INTEGER         NOT NULL UNIQUE,
    cat_mov_id          CHAR(10)        NOT NULL UNIQUE
);    

CREATE TABLE distributor (
      distributor_name      VARCHAR2(30)    PRIMARY KEY,
      catalog               VARCHAR2(30)    NOT NULL,
      genres_offered        VARCHAR2(50)    NOT NULL
);

CREATE TABLE rental_store (
       store_name       VARCHAR2(30)    PRIMARY KEY,
       address          VARCHAR2(50)    NOT NULL,
       owner            VARCHAR2(30)    NOT NULL
);

不相关,但是:您不想使用CHAR数据类型。如果对每个字符列使用VARCHAR,它会更有效率,并且会减少麻烦。

答案 1 :(得分:0)

当然,第一次运行脚本时,DROP TABLE会失败,因为您还没有创建它们。这是预期的,并且在这样的脚本中是可以接受的。

除此之外,如果要创建FK关系,则需要在创建子项之前创建父表,在更正拼写错误的关键字后,您将看到该表。

我会留下它作为练习让学生抓住线索并在第一个CREATE TABLE语句中找到拼写错误的关键词。