0

I'm trying to run a script which is just a series of drop tables and create tables. It compiles but when it runs I get the following error: ORA-00942 table or view does not exist. They shouldn't exist I'm trying to create them! Anyone have any idea as to why this might be happening? I'm using the Oracle interface, not the command line. I've created the code in Notepad++ and uploaded it to Oracle. I then save and run the script within Oracle. When I click view results the error appears.

EDIT: I've now noticed that it's only the tables that have foreign keys that are not being created!

This is an excerpt from my code:

CREATE TABLE PricePlans (
plan_id NUMBER(3) NOT NULL,
plan_name VARCHAR2(15) NOT NULL,
plan_desc VARCHAR2(30) NOT NULL,
plan_duration_months NUMBER(2) NOT NULL,
plan_cost NUMBER(4) NOT NULL,
CONSTRAINT pk_planid PRIMARY KEY (plan_id)
);

DROP TABLE Classes;
CREATE TABLE Classes (
class_id NUMBER(3) NOT NULL,
class_name VARCHAR2(30) NOT NULL,
class_desc VARCHAR2(30) NOT NULL,
class_facility NUMBER(2) NOT NULL,
class_instructor NUMBER(5) NOT NULL,
class_cost NUMBER(2) NOT NULL,
CONSTRAINT pk_classid PRIMARY KEY (class_id),
CONSTRAINT fk_classfacility 
    FOREIGN KEY (class_facility) 
    REFERENCES Facilities(facility_id),
CONSTRAINT fk_classinstructor 
    FOREIGN KEY (class_instructor) 
    REFERENCES Employees(emp_id)
);

DROP TABLE Facilities;
CREATE TABLE Facilities (
facility_id NUMBER(2) NOT NULL,
facility_name VARCHAR(15) NOT NULL,
facility_manager NUMBER(4) NOT NULL,
CONSTRAINT pk_facilityid PRIMARY KEY (facility_id);
CONSTRAINT fk_facilitymanager 
    FOREIGN KEY (facility_manager) 
    REFERENCES Manager(manager_id)
);
adohertyd
  • 2,689
  • 19
  • 58
  • 78

1 Answers1

2

I believe that DROP TABLE statements in your query are trying to drop the tables that don't exist.

Remove the following lines from your script.

DROP TABLE Classes;

DROP TABLE Facilities;

You can check if the table exists or not before executing the DROP TABLE statements. Here are few SO answers that might help you with that.

Oracle: If Table Exists

Check if table exists in the database - PL SQL

UPDATE

In the CREATE TABLE statement of Classes, the foreign key fk_classfacility is referencing the table Facilities, which is being created later. Try to switch the CREATE TABLE statements. Create the Facilities table first and then create the Classes table.

I assume that all the other tables referenced in the constraints already exist.

Community
  • 1
  • 1