ORA-00907:缺少右括号和ORA-00942:表或视图不存在

时间:2017-12-26 16:32:16

标签: sql oracle oracle-apex ddl

我遇到的问题是

  • ORA-00907:缺少右括号和
  • ORA-00942:表或视图不存在。

错误的屏幕截图为enter image description here enter image description here

DROP TABLE Organizer CASCADE CONSTRAINT;
DROP TABLE festival CASCADE CONSTRAINT;
DROP TABLE staff CASCADE CONSTRAINT;
DROP TABLE act CASCADE CONSTRAINT;
DROP TABLE equipment CASCADE CONSTRAINT;
DROP TABLE stage CASCADE CONSTRAINT;
DROP TABLE band CASCADE CONSTRAINT;
DROP TABLE agent CASCADE CONSTRAINT;
DROP TABLE venue CASCADE CONSTRAINT;
DROP TABLE band_agent CASCADE CONSTRAINT;

表格创建

  • 模型上的每个实体都由一个需要在数据库中创建的表来表示。
  • 在SQL中,使用CREATE TABLE命令创建新表。
  • 创建表时,会定义其名称及其属性。
  • 其值来自模型中指定的值。
  • 有时也会指定某些约束,例如识别主键。
-- Create a Database table to represent the "Organizer" entity.
    CREATE TABLE Organizer(
        OrganizerId INTEGER NOT NULL,
        oranizerName    VARCHAR(55) NOT NULL,
        organizerAddress    VARCHAR(55),
        organizerPhone  INTEGER,
        -- Specify the PRIMARY KEY constraint for table "Organizer".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_Organizer PRIMARY KEY (OrganizerId)
    );

    -- Create a Database table to represent the "festival" entity.
    CREATE TABLE festival(
        festvalId   INTEGER NOT NULL,
        festivalName    VARCHAR(55) NOT NULL,
        festvalLocation VARCHAR(55) NOT NULL,
        festivalPeriod  INTEGER,
        fk1_OrganizerId NUMBER NOT NULL,
        -- Specify the PRIMARY KEY constraint for table "festival".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_festival PRIMARY KEY (festvalId)
    );

    -- Create a Database table to represent the "staff" entity.
    CREATE TABLE staff(
        staffId INTEGER NOT NULL,
        fk1_venueId INTEGER NOT NULL,
        fk2_staffId INTEGER NOT NULL,
        -- Specify the PRIMARY KEY constraint for table "staff".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_staff PRIMARY KEY (staffId)
    );

    -- Create a Database table to represent the "equipment" entity.
    CREATE TABLE equipment(
        equipmentmodel  INTEGER NOT NULL,
        equipmentname   VARCHAR(55),
        equipmenttype   VARCHAR(55),
        fk1_stageId INTEGER NOT NULL,
        -- Specify the PRIMARY KEY constraint for table "equipment".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_equipment PRIMARY KEY (equipmentmodel));

    -- Create a Database table to represent the "act" entity.
    CREATE TABLE act(
        actId   INTEGER NOT NULL,
        typeOfAct   VARCHAR(55) NOT NULL,
        actName VARCHAR(55),
        fk1_bandId  INTEGER NOT NULL,
        fk2_festvalId   INTEGER NOT NULL,
        fk3_stageId INTEGER NOT NULL,
        -- Specify the PRIMARY KEY constraint for table "act".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_act PRIMARY KEY (actId)
    );

    -- Create a Database table to represent the "stage" entity.
    CREATE TABLE stage(
        stageId INTEGER NOT NULL,
        -- Specify the PRIMARY KEY constraint for table "stage".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_stage PRIMARY KEY (stageId)
    );

    -- Create a Database table to represent the "band" entity.
    CREATE TABLE band(
        bandId  INTEGER NOT NULL,
        bandName    VARCHAR(55),
        bandAddress VARCHAR(55),
        bandPhone   INTEGER,
        -- Specify the PRIMARY KEY constraint for table "band".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_band PRIMARY KEY (bandId)
    );

    -- Create a Database table to represent the "agent" entity.
    CREATE TABLE agent(
        agentId INTEGER NOT NULL,
        agentName   VARCHAR(55),
        agentAddress    VARCHAR(55),
        agentPhone  INTEGER,
        -- Specify the PRIMARY KEY constraint for table "agent".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_agent PRIMARY KEY (agentId)
    );

    -- Create a Database table to represent the "venue" entity.
    CREATE TABLE venue(
        venueId INTEGER NOT NULL,
        venueName   VARCHAR(55) NOT NULL,
        venueLocation   VARCHAR(55),
        venueType   VARCHAR(55),
        fk1_festvalId   INTEGER NOT NULL,
        -- Specify the PRIMARY KEY constraint for table "venue".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_venue PRIMARY KEY (venueId)
    );

    -- Create a Database table to represent the "band_agent" entity.
    CREATE TABLE band_agent(
        fk1_bandId  INTEGER NOT NULL,
        fk2_agentId INTEGER NOT NULL
    );

    -- Create a Database table to represent the "first aider" entity.
    -- This table is representing a sub-type entity, so the primary key will be the same as that
    -- defined for table "staff" which represents the super-type entity.
    CREATE TABLE first_aider(
        staffId INTEGER NOT NULL,
        name    VARCHAR(25),
        -- Specify the PRIMARY KEY constraint for table "first_aider".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_first_aider PRIMARY KEY (staffId),
        -- Specify a FOREIGN KEY constraint to indicate that this table's primary key (staffId)
        -- references the super-type table's primary key. In this case the key of table "staff"
        -- The ON DELETE CASCADE and ON UPDATE CASCADE ensure that if the super-type key data is
        -- deleted or updated then the changes will be cascaded down to this sub-type.
        --  i.e. if the value of the super-type key is changed the value of this table's key is also changed.
        FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE ON UPDATE CASCADE
    );

    -- Create a Database table to represent the "security" entity.
    -- This table is representing a sub-type entity, so the primary key will be the same as that
    -- defined for table "staff" which represents the super-type entity.
    CREATE TABLE security(
        staffId INTEGER NOT NULL,
        name    VARCHAR(55),
        -- Specify the PRIMARY KEY constraint for table "security".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_security PRIMARY KEY (staffId),
        -- Specify a FOREIGN KEY constraint to indicate that this table's primary key (staffId)
        -- references the super-type table's primary key. In this case the key of table "staff"
        -- The ON DELETE CASCADE and ON UPDATE CASCADE ensure that if the super-type key data is
        -- deleted or updated then the changes will be cascaded down to this sub-type.
        --  i.e. if the value of the super-type key is changed the value of this table's key is also changed.
        FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE ON UPDATE CASCADE
    );

    -- Create a Database table to represent the "supervisor" entity.
    -- This table is representing a sub-type entity, so the primary key will be the same as that
    -- defined for table "staff" which represents the super-type entity.
    CREATE TABLE supervisor(
        staffId INTEGER NOT NULL,
        name    VARCHAR(8),
        -- Specify the PRIMARY KEY constraint for table "supervisor".
        -- This indicates which attribute(s) uniquely identify each row of data.
        CONSTRAINT  pk_supervisor PRIMARY KEY (staffId),
        -- Specify a FOREIGN KEY constraint to indicate that this table's primary key (staffId)
        -- references the super-type table's primary key. In this case the key of table "staff"
        -- The ON DELETE CASCADE and ON UPDATE CASCADE ensure that if the super-type key data is
        -- deleted or updated then the changes will be cascaded down to this sub-type.
        --  i.e. if the value of the super-type key is changed the value of this table's key is also changed.
        FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE ON UPDATE CASCADE
    );


    --------------------------------------------------------------
    -- Alter Tables to add fk constraints --

    -- Now all the tables have been created the ALTER TABLE command is used to define some additional
    -- constraints.  These typically constrain values of foreign keys to be associated in some way
    -- with the primary keys of related tables.  Foreign key constraints can actually be specified
    -- when each table is created, but doing so can lead to dependency problems within the script
    -- i.e. tables may be referenced before they have been created.  This method is therefore safer.

    -- Alter table to add new constraints required to implement the "organizer_to_festival" relationship

    -- This constraint ensures that the foreign key of table "festival"
    -- correctly references the primary key of table "Organizer"

    ALTER TABLE festival ADD CONSTRAINT fk1_festival_to_Organizer FOREIGN KEY(fk1_OrganizerId) REFERENCES Organizer(OrganizerId);

    -- Alter table to add new constraints required to implement the "stage_to_equipment" relationship

    -- This constraint ensures that the foreign key of table "equipment"
    -- correctly references the primary key of table "stage"

    ALTER TABLE equipment ADD CONSTRAINT fk1_equipment_to_stage FOREIGN KEY(fk1_stageId) REFERENCES stage(stageId);

    -- Alter table to add new constraints required to implement the "band_to_acts" relationship

    -- This constraint ensures that the foreign key of table "act"
    -- correctly references the primary key of table "band"

    ALTER TABLE act ADD CONSTRAINT fk1_act_to_band FOREIGN KEY(fk1_bandId) REFERENCES band(bandId);

    -- Alter table to add new constraints required to implement the "festival_to_act" relationship

    -- This constraint ensures that the foreign key of table "act"
    -- correctly references the primary key of table "festival"

    ALTER TABLE act ADD CONSTRAINT fk2_act_to_festival FOREIGN KEY(fk2_festvalId) REFERENCES festival(festvalId);

    -- Alter table to add new constraints required to implement the "festival_to_venue" relationship

    -- This constraint ensures that the foreign key of table "venue"
    -- correctly references the primary key of table "festival"

    ALTER TABLE venue ADD CONSTRAINT fk1_venue_to_festival FOREIGN KEY(fk1_festvalId) REFERENCES festival(festvalId);

    -- Alter table to add new constraints required to implement the "band_to_band_agent" relationship

    -- This constraint ensures that the foreign key of table "band_agent"
    -- correctly references the primary key of table "band"

    ALTER TABLE band_agent ADD CONSTRAINT fk1_band_agent_to_band FOREIGN KEY(fk1_bandId) REFERENCES band(bandId);

    -- Alter table to add new constraints required to implement the "agent_to_band_agent" relationship

    -- This constraint ensures that the foreign key of table "band_agent"
    -- correctly references the primary key of table "agent"

    ALTER TABLE band_agent ADD CONSTRAINT fk2_band_agent_to_agent FOREIGN KEY(fk2_agentId) REFERENCES agent(agentId);
    -- Alter table to add new constraints required to implement the "supervises" relationship

    -- This constraint ensures that the foreign key of table "staff"
    -- correctly references the primary key of table "supervisor"

    ALTER TABLE staff ADD CONSTRAINT fk2_staff_to_supervisor FOREIGN KEY(fk2_staffId) REFERENCES supervisor(staffId);
    -- Alter table to add new constraints required to implement the "staff_to_venue" relationship

    -- This constraint ensures that the foreign key of table "staff"
    -- correctly references the primary key of table "venue"

    ALTER TABLE staff ADD CONSTRAINT fk1_staff_to_venue FOREIGN KEY(fk1_venueId) REFERENCES venue(venueId);

    -- Alter table to add new constraints required to implement the "act_stage" relationship

    -- This constraint ensures that the foreign key of table "act"
    -- correctly references the primary key of table "stage"

    ALTER TABLE act ADD CONSTRAINT fk3_act_to_stage FOREIGN KEY(fk3_stageId) REFERENCES stage(stageId);



        enter code here

    --------------------------------------------------------------
    -- End of DDL file auto-generation
    --------------------------------------------------------------

2 个答案:

答案 0 :(得分:1)

外键定义在这里是错误的:

CREATE TABLE first_aider(
    ......
    ......
    FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE ON UPDATE CASCADE
);

Oracle并不知道ON UPDATE CASCADE条款,根据the documentation仅允许ON DELETE CASCADEON DELETE SET NULL。您必须删除ON UPDATE CASCADE

enter image description here

同样的错误在这里:

CREATE TABLE security(
    ......
    ......
    FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE ON UPDATE CASCADE
);

也在这里:

CREATE TABLE supervisor(
    ......
    ......
    FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE ON UPDATE CASCADE
);

答案 1 :(得分:0)

ON UPDATE CASCADE可能存在时,没有条款ON DELETE CASCADE

删除以下条款:

CREATE TABLE first_aider(
    staffId INTEGER NOT NULL,
    name    VARCHAR(25),
    CONSTRAINT  pk_first_aider 
    PRIMARY KEY (staffId),
    FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE 
);

<强>

CREATE TABLE security(
    staffId INTEGER NOT NULL,
    name    VARCHAR(55),
    CONSTRAINT  pk_security 
    PRIMARY KEY (staffId),
    FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE 
);

<强>

CREATE TABLE supervisor(
    staffId INTEGER NOT NULL,
    name    VARCHAR(8),
    CONSTRAINT  pk_supervisor 
    PRIMARY KEY (staffId),    
    FOREIGN KEY(staffId) REFERENCES staff(staffId) ON DELETE CASCADE 
);