I have Oracle database. I want to create INDEX:
CREATE INDEX indexID ON Employee(id_employee);
But it writes -> SQL Error: ORA-01408: such column list already indexed
So before create index I put:
DROP INDEX indexID;
But it writes -> SQL Error: ORA-01418: specified index does not exist
my Employee table:
CREATE TABLE Employee (
id_employee NUMBER(5) NOT NULL,
name VARCHAR(25) NOT NULL,
surname VARCHAR(25) NOT NULL,
day_of_birth DATE NOT NULL,
salary NUMBER(6) NOT NULL,
PRIMARY KEY(id_employee)
);
Have you some idea? it looks like index does not create.
You have a different index on that column.
Say you create the table like this:
SQL> CREATE TABLE Employee (
2 id_employee NUMBER(5) NOT NULL ,
3 name VARCHAR(25) NOT NULL,
4 surname VARCHAR(25) NOT NULL,
5 day_of_birth DATE NOT NULL,
6 salary NUMBER(6) NOT NULL
7 );
Table created.
Then you add the PK constraint:
SQL> alter table employee add primary key(id_employee);
Table altered.
Now Oracle already created a unique index on the PK field, so you already have it, with no need for manual creation.
SQL> select index_name, column_name
2 from user_ind_columns c
3 inner join user_indexes i
4 using (index_name)
5 where i.table_name = 'EMPLOYEE';
INDEX_NAME COLUMN_NAME
-------------------- --------------------
SYS_C007892 ID_EMPLOYEE
In your example:
SQL> CREATE TABLE Zamestnanec (
2 id_zamestnance NUMBER(5) PRIMARY KEY ,
3 jmeno VARCHAR(25) NOT NULL,
4 prijmeni VARCHAR(25) NOT NULL,
5 datum_narozeni DATE NOT NULL,
6 prava CHAR(3) CHECK(prava IN ('ano', 'ne')) NOT NULL,
7 plat NUMBER(6) NOT NULL
8 );
Table created.
SQL> select index_name, column_name
2 from user_ind_columns c
3 inner join user_indexes i
4 using (index_name)
5 where i.table_name = 'ZAMESTNANEC';
INDEX_NAME COLUMN_NAME
-------------------- --------------------
SYS_C007899 ID_ZAMESTNANCE
I slightly modified your syntax; besides, you can avoid the NOT NULL constraint on a PK field: the PK will force the field to be NOT NULL.
See here for a similar problem.
The CREATE TABLE for ZAMESTNANEC is
CREATE TABLE Zamestnanec (
id_zamestnance NUMBER(5) NOT NULL,
jmeno VARCHAR(25) NOT NULL,
prijmeni VARCHAR(25) NOT NULL,
datum_narozeni DATE NOT NULL,
prava CHAR(3) CHECK(prava IN ('ano', 'ne')) NOT NULL,
plat NUMBER(6) NOT NULL,
PRIMARY KEY(id_zamestnance)
);
You've created a primary key constraint for ID_ZAMESTNANCE, which by default creates an index on ID_ZAMESTNANCE; thus you don't need to create another index on ID_ZAMESTNANCE.
I would suggest a freakish solution, but I guarantee you that it will work.
Drop the primary key constraint from the table.
ALTER TABLE Employee
DROP CONSTRAINT pk_id_employee
Drop the index, then…
Create the primary key constraint
Related
I have a table similar to this.
CREATE TABLE customers (
customer_id NUMBER(7,0) NOT NULL,
customer_name VARCHAR2(50) NOT NULL,
CONSTRAINT customers_pk PRIMARY_KEY (customer_id)
);
with some values in the table.
I want to change the data type of the primary key customer_id to NUMBER(10, 0). So what steps do we follow before executing the ALTER command? (This column is not being referenced as a foreign key in any table)
Specifically,
We have an index on the primary column by default in oracle SQL. So should we drop the primary key constraint and then execute the ALTER command?
And any other considerations we need to make?
For case like yours, you don't have to do anything - just do it:
SQL> create table customers (
2 customer_id number(7, 0),
3 customer_name varchar2(50),
4 constraint customer_pk primary key (customer_id));
Table created.
SQL> insert into customers
2 select 1234566, 'Little' from dual union all
3 select 98876 , 'Foot' from dual;
2 rows created.
SQL> alter table customers modify customer_id number(8, 0);
Table altered.
SQL> select constraint_name from user_constraints where table_name = 'CUSTOMERS';
CONSTRAINT_NAME
------------------------------
CUSTOMER_PK
SQL>
But, if you had to make the column smaller or modify its datatype - that's another story. Lucky you, not yours.
I tried to create several tables, but only the server table was created. Please fix my work!
CREATE TABLE server
(
SERVER_ID varchar(25) NOT NULL,
SERVER_IP varchar(15) NOT NULL,
SERVER_LOCATION varchar(25) NOT NULL,
SERVER_BRAND varchar(15) NOT NULL,
CONSTRAINT server_pk
PRIMARY KEY (SERVER_ID)
);
CREATE TABLE application
(
APP_ID varchar(25) NOT NULL,
ACCOUNT_NUM varchar(25) NOT NULL,
RECORD_ID varchar(10) NOT NULL,
VERSION_ID varchar(10) NOT NULL,
LAST_UPDATED date NOT NULL,
CONSTRAINT application_pk
PRIMARY KEY (APP_ID),
CONSTRAINT application _fk_account
FOREIGN KEY (ACCOUNT_NUM)
REFERENCES account (ACCOUNT_NUM),
CONSTRAINT application _fk_record
FOREIGN KEY (RECORD_ID)
REFERENCES record (RECORD_ID)
);
CREATE TABLE record
(
RECORD_ID varchar(25) NOT NULL,
VIN_NUM varchar(25) NOT NULL,
SERVER_ID varchar(25) NOT NULL,
CONSTRAINT record_pk
PRIMARY KEY (RECORD_ID, VIN_NUM, SERVER_ID),
CONSTRAINT record_fk_vehicle
FOREIGN KEY (VIN_NUM)
REFERENCES vehicle (VIN_NUM),
CONSTRAINT record_fk_server
FOREIGN KEY (SERVER_ID)
REFERENCES server (SERVER_ID)
);
CREATE TABLE vehicle
(
VIN_NUM varchar(25) NOT NULL,
V_MILEAGE int NOT NULL,
V_GASUSED varchar(25) NOT NULL,
V_ELECTRICALMILES int NOT NULL,
DRIVER_ID varchar(25) NOT NULL,
CONSTRAINT vehicle_pk
PRIMARY KEY (VIN_NUM),
CONSTRAINT vehicle_fk_driver
FOREIGN KEY (DRIVER_ID)
REFERENCES driver (DRIVER_ID)
);
CREATE TABLE driver
(
DRIVER_ID varchar(25) NOT NULL,
LICENSE_NUM varchar(25) NOT NULL,
FIRST_NAME varchar(25) NOT NULL,
LAST_NAME varchar(25) NOT NULL,
INSURANCE_POLICY varchar(25) NOT NULL,
ACCOUNT_NUM int NOT NULL,
CONSTRAINT driver_pk
PRIMARY KEY (DRIVER_ID),
CONSTRAINT driver_fk_account
FOREIGN KEY (ACCOUNT_NUM)
REFERENCES account (ACCOUNT_NUM)
);
CREATE TABLE account
(
ACCOUNT_NUM int NOT NULL,
DRIVER_ID varchar(25) NOT NULL,
DEVICE_ID varchar(25) NOT NULL,
DATE_CREATED date NOT NULL,
ACCOUNT_STATUS varchar(10) NOT NULL,
CONSTRAINT account_pk
PRIMARY KEY (ACCOUNT_NUM, DRIVER_ID),
CONSTRAINT account_fk_driver
FOREIGN KEY (DRIVER_ID)
REFERENCES driver (DRIVER_ID)
);
There are several issues here.
You should create tables in order that matches foreign key constraint dependencies, i.e. you can't create detail table first, and then its master table because there's no table (or its primary key) which is to be referenced.
A good workaround for that is to remove foreign key constraints from create table and create them separately (using alter table ... add constraint) after all tables are created.
There's no problem if two tables reference each other (such as driver and account do), if there's a good reason to do it. However, as I previously said - you can't do it within create table statements; at least one foreign key has to be moved out.
If primary key consists of several columns (so it is a composite key), foreign key also must contain the same number & datatypes of columns that reference it. That's what you have for driver and account tables.
account's primary key is (account_num, driver_id)
driver's foreign key can't be just (account_num) - it has to contain driver_id as well
Finally, application table: its foreign keys can't be created because it references
account table whose primary key is (account_num, driver_id), but - application doesn't contain driver_id column at all which might mean that you wrongly set account's primary key (or application table)
record table which has a composite primary key on (record_id, vin_num, server_id), while application has only record_id. Remark I wrote for account is valid here as well
Now, if we fix what I mentioned, tables are created, but application misses all its foreign key constraints.
SQL> CREATE TABLE server
2 (
3 SERVER_ID varchar2(25) NOT NULL,
4 SERVER_IP varchar2(15) NOT NULL,
5 SERVER_LOCATION varchar2(25) NOT NULL,
6 SERVER_BRAND varchar2(15) NOT NULL,
7 CONSTRAINT server_pk
8 PRIMARY KEY (SERVER_ID)
9 );
Table created.
SQL>
SQL> CREATE TABLE driver
2 (
3 DRIVER_ID varchar2(25) NOT NULL,
4 LICENSE_NUM varchar2(25) NOT NULL,
5 FIRST_NAME varchar2(25) NOT NULL,
6 LAST_NAME varchar2(25) NOT NULL,
7 INSURANCE_POLICY varchar2(25) NOT NULL,
8 ACCOUNT_NUM int NOT NULL,
9 CONSTRAINT driver_pk
10 PRIMARY KEY (DRIVER_ID)
11 --CONSTRAINT driver_fk_account
12 --FOREIGN KEY (ACCOUNT_NUM)
13 --REFERENCES account (ACCOUNT_NUM)
14 );
Table created.
SQL>
SQL> CREATE TABLE account
2 (
3 ACCOUNT_NUM int NOT NULL,
4 DRIVER_ID varchar2(25) NOT NULL,
5 DEVICE_ID varchar2(25) NOT NULL,
6 DATE_CREATED date NOT NULL,
7 ACCOUNT_STATUS varchar2(10) NOT NULL,
8 CONSTRAINT account_pk
9 PRIMARY KEY (ACCOUNT_NUM, DRIVER_ID),
10 CONSTRAINT account_fk_driver
11 FOREIGN KEY (DRIVER_ID)
12 REFERENCES driver (DRIVER_ID)
13 );
Table created.
SQL>
SQL> alter table driver add constraint driver_fk_account
2 foreign key (account_num, driver_id)
3 references account (account_num, driver_id);
Table altered.
SQL>
SQL> CREATE TABLE vehicle
2 (
3 VIN_NUM varchar2(25) NOT NULL,
4 V_MILEAGE int NOT NULL,
5 V_GASUSED varchar2(25) NOT NULL,
6 V_ELECTRICALMILES int NOT NULL,
7 DRIVER_ID varchar2(25) NOT NULL,
8 CONSTRAINT vehicle_pk
9 PRIMARY KEY (VIN_NUM),
10 CONSTRAINT vehicle_fk_driver
11 FOREIGN KEY (DRIVER_ID)
12 REFERENCES driver (DRIVER_ID)
13 );
Table created.
SQL>
SQL> CREATE TABLE record
2 (
3 RECORD_ID varchar2(25) NOT NULL,
4 VIN_NUM varchar2(25) NOT NULL,
5 SERVER_ID varchar2(25) NOT NULL,
6 CONSTRAINT record_pk
7 PRIMARY KEY (RECORD_ID, VIN_NUM, SERVER_ID),
8 CONSTRAINT record_fk_vehicle
9 FOREIGN KEY (VIN_NUM)
10 REFERENCES vehicle (VIN_NUM),
11 CONSTRAINT record_fk_server
12 FOREIGN KEY (SERVER_ID)
13 REFERENCES server (SERVER_ID)
14 );
Table created.
SQL>
SQL> CREATE TABLE application
2 (
3 APP_ID varchar2(25) NOT NULL,
4 ACCOUNT_NUM varchar2(25) NOT NULL,
5 RECORD_ID varchar2(10) NOT NULL,
6 VERSION_ID varchar2(10) NOT NULL,
7 LAST_UPDATED date NOT NULL,
8 CONSTRAINT application_pk
9 PRIMARY KEY (APP_ID)
10 --CONSTRAINT application_fk_account
11 --FOREIGN KEY (ACCOUNT_NUM)
12 --REFERENCES account (ACCOUNT_NUM),
13 --CONSTRAINT application_fk_record
14 --FOREIGN KEY (RECORD_ID)
15 --REFERENCES record (RECORD_ID)
16 );
Table created.
SQL>
You have to add a / after every table definition end -
CREATE TABLE server
(
SERVER_ID varchar(25) NOT NULL,
SERVER_IP varchar(15) NOT NULL,
SERVER_LOCATION varchar(25) NOT NULL,
SERVER_BRAND varchar(15) NOT NULL,
CONSTRAINT server_pk
PRIMARY KEY (SERVER_ID)
);
/ ----> Here
CREATE TABLE application
(
APP_ID varchar(25) NOT NULL,
.....
Here is a logical model that I have.
Below are table structures and their constraints
Doctor
CREATE TABLE doctor (
doctor_id NUMBER(4) NOT NULL,
doctor_title VARCHAR2(2) NOT NULL,
doctor_fname VARCHAR2(50),
doctor_lname VARCHAR2(50),
doctor_phone CHAR(10) NOT NULL
);
ALTER TABLE doctor ADD CONSTRAINT doctor_pk PRIMARY KEY ( doctor_id );
Procedure
CREATE TABLE procedure (
proc_code NUMBER(5) NOT NULL,
proc_name VARCHAR2(100) NOT NULL,
proc_description VARCHAR2(300) NOT NULL,
proc_time NUMBER(3) NOT NULL,
proc_std_cost NUMBER(7, 2) NOT NULL
);
ALTER TABLE procedure ADD CONSTRAINT procedure_pk PRIMARY KEY ( proc_code );
ALTER TABLE procedure ADD CONSTRAINT proc_name_unq UNIQUE ( proc_name );
Admission
CREATE TABLE admission (
adm_no NUMBER(6) NOT NULL,
adm_date_time DATE NOT NULL,
adm_discharge DATE,
patient_id NUMBER(6) NOT NULL,
doctor_id NUMBER(4) NOT NULL
);
ALTER TABLE admission ADD CONSTRAINT admission_pk PRIMARY KEY ( adm_no ); --surrogate key
ALTER TABLE admission ADD CONSTRAINT admission_nk UNIQUE ( patient_id,
adm_date_time );
Admission Procedure
CREATE TABLE adm_prc (
adprc_no NUMBER(7) NOT NULL,
adprc_date_time DATE NOT NULL,
adprc_pat_cost NUMBER(7, 2) NOT NULL,
adprc_items_cost NUMBER(6, 2) NOT NULL,
adm_no NUMBER(6) NOT NULL,
proc_code NUMBER(5) NOT NULL,
request_dr_id NUMBER(4) NOT NULL,
perform_dr_id NUMBER(4)
);
ALTER TABLE adm_prc ADD CONSTRAINT adm_prc_pk PRIMARY KEY ( adprc_no ); --surrogate key
ALTER TABLE adm_prc ADD CONSTRAINT adm_prc_nk UNIQUE ( adprc_date_time,
adm_no );
ALTER TABLE adm_prc
ADD CONSTRAINT admission_admprc FOREIGN KEY ( adm_no )
REFERENCES admission ( adm_no );
ALTER TABLE adm_prc
ADD CONSTRAINT doctor_performadmprc FOREIGN KEY ( perform_dr_id )
REFERENCES doctor ( doctor_id );
ALTER TABLE adm_prc
ADD CONSTRAINT doctor_requestadmprc FOREIGN KEY ( request_dr_id )
REFERENCES doctor ( doctor_id );
Item Treatment
CREATE TABLE item_treatment (
adprc_no NUMBER(7) NOT NULL,
item_code CHAR(5) NOT NULL,
it_qty_used NUMBER(2) NOT NULL,
it_item_total_cost NUMBER(8, 2) NOT NULL
);
ALTER TABLE item_treatment
ADD CONSTRAINT item_treatment_pk PRIMARY KEY ( adprc_no,item_code);
ALTER TABLE item_treatment
ADD CONSTRAINT admprc_itemtreatment FOREIGN KEY ( adprc_no )
REFERENCES adm_prc ( adprc_no );
ALTER TABLE item_treatment
ADD CONSTRAINT admprc_itemtreatment FOREIGN KEY ( adprc_no )
REFERENCES adm_prc ( adprc_no );
ALTER TABLE item_treatment
ADD CONSTRAINT item_itemtreatment FOREIGN KEY ( item_code )
REFERENCES item ( item_code );
In a fictitious hospital, every time an admission procedure is completed for a patient admission, the lead doctor who performed the procedure is recorded under the perform_dr_id in adm_prc table. Even if a team of doctors perform the procedure, only the lead doctor is recorded.
The hospital now wishes to record all the doctors who performed in the admission procedure including an Ancillary doctor (assisting doctor).
I have altered the adm_prc table to include ancillary doctors
ALTER TABLE ADM_PRC ADD ANCILLARY_DR_ID NUMBER(4);
ALTER TABLE ADM_PRC
ADD CONSTRAINT DOCTOR_PERFORM_ANCILLARY FOREIGN KEY ( ANCILLARY_DR_ID )
REFERENCES DOCTOR ( DOCTOR_ID );
I need to change the structure of the database is such a way that, there should be multiple records for the same admission procedure as there can be many doctors assisting in a single admission procedure. For example, I should be able to insert the following records in adm_prc table
adprc_no adprc_date_time adprc_pat_cost adprc_items_cost adm_no proc_code
request_dr_id perform_dr_id ancillary_dr_id
-----------------------------------------------------------------------------------------------------------------------------
1 14/03/2019 100 100 1234 1234
10 10 12
1 14/03/2019 100 100 1234 1234
10 10 13
However, this violates primary key constraint adm_prc_pk
I'm stuck at this point and unable to proceed further. Would appreciate if someone could point me in the right direction.
You should create a new satellite table (say ADM_PRC_DR_DTLS) to hold adprc_no and corresponding dr_id details.
adprc_no of ADM_PRC_DR_DTLS would refer to the ADM_PRC's adprc_no as a Foreign Key.
I am getting ORA-00955 error which is name is already used by an existing object. I know it is in the foreign key constraint im trying to use. It happens in both foreign key constraints. I cant figure out why its happening. I tried renaming them to anything possible and I cant understand why it wont work.
DROP TABLE STUDENT;
CREATE TABLE STUDENT
(stuID VARCHAR (7) PRIMARY KEY NOT NULL,
major VARCHAR (15) NOT NULL,
firstName VARCHAR (15) NOT NULL,
lastName VARCHAR (15) NOT NULL,
yr VARCHAR(8) NOT NULL
);
DROP TABLE ENROLL;
CREATE TABLE ENROLL
(stuID VARCHAR (7) NOT NULL,
grade VARCHAR (1) NOT NULL,
courseID VARCHAR(7) NOT NULL,
CONSTRAINT fk_STUDENT
FOREIGN KEY (stuID)
REFERENCES STUDENT(stuid)
);
DROP TABLE CLASS;
CREATE TABLE CLASS
(instructorID VARCHAR(7) NOT NULL,
classRoomNumber VARCHAR(7) NOT NULL,
dateStarts DATE NOT NULL,
courseID VARCHAR(7) PRIMARY KEY NOT NULL,
description CHAR(100) NOT NULL,
CONSTRAINT fk_INSTRUCTOR
FOREIGN KEY (instructorID)
REFERENCES INSTRUCTOR(instructorid)
);
DROP TABLE INSTRUCTOR;
CREATE TABLE INSTRUCTOR
(firstName VARCHAR (15) NOT NULL,
lastName VARCHAR (15) NOT NULL,
departmentName VARCHAR (15) NOT NULL,
instructorID VARCHAR(7) PRIMARY KEY NOT NULL
);
In a comment, you said that there's still an error.
If you create (and/or drop) tables in a correct order, everything is OK. I prefer dropping them separately.
At first, tables don't exist so I'll just create them:
SQL> CREATE TABLE student (
2 stuid VARCHAR(7) PRIMARY KEY NOT NULL,
3 major VARCHAR(15) NOT NULL,
4 firstname VARCHAR(15) NOT NULL,
5 lastname VARCHAR(15) NOT NULL,
6 yr VARCHAR(8) NOT NULL
7 );
Table created.
SQL> CREATE TABLE enroll (
2 stuid VARCHAR(7) NOT NULL,
3 grade VARCHAR(1) NOT NULL,
4 courseid VARCHAR(7) NOT NULL,
5 CONSTRAINT fk_student FOREIGN KEY ( stuid )
6 REFERENCES student ( stuid )
7 );
Table created.
SQL> CREATE TABLE instructor (
2 firstname VARCHAR(15) NOT NULL,
3 lastname VARCHAR(15) NOT NULL,
4 departmentname VARCHAR(15) NOT NULL,
5 instructorid VARCHAR(7) PRIMARY KEY NOT NULL
6 );
Table created.
SQL> CREATE TABLE class (
2 instructorid VARCHAR(7) NOT NULL,
3 classroomnumber VARCHAR(7) NOT NULL,
4 datestarts DATE NOT NULL,
5 courseid VARCHAR(7) PRIMARY KEY NOT NULL,
6 description CHAR(100) NOT NULL,
7 CONSTRAINT fk_instructor FOREIGN KEY ( instructorid )
8 REFERENCES instructor ( instructorid )
9 );
Table created.
Drop tables in reverse order, so that detail is dropped before its master
SQL> DROP TABLE enroll;
Table dropped.
SQL> DROP TABLE student;
Table dropped.
SQL> DROP TABLE class;
Table dropped.
SQL> DROP TABLE instructor;
Table dropped.
SQL>
Table INSTRUCTOR is referenced by table CLASS, hence you heed to create table INSTRUCTOR before you create table CLASS.
Also, you should use DROP TABLE ... CASCADE CONSTRAINTS instead of just DROP TABLE .... This allows foreign and primary keys to be properly dropped at the same time as the table, and might avoid error name already used by existing object that you are currently getting.
I am trying to make 3 tables 2 with foreign keys to the rest. I keep getting error codes when I try to insert data into the table so I figured I would isolate the create tables and attempt to fix there. The code is as follows:
-- Spool the execution output to a text file. This spooled file serves as logs.
spool on
spool hw2.lst
set echo on
-- All the DROP TABLE in the beginning.
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE companies CASCADE CONSTRAINTS;
DROP TABLE producer CASCADE CONSTRAINTS;
-- Creates Table CUSTOMER.
CREATE TABLE CUSTOMER (
cust_ID NUMBER(10),
last_name VARCHAR2(20) NOT NULL,
first_name VARCHAR2(20) NOT NULL,
MI VARCHAR2(1) NOT NULL,
street VARCHAR2(30) NOT NULL,
city VARCHAR2(30) NOT NULL,
state VARCHAR2(30) NOT NULL,
zip_code VARCHAR2(20) NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(cust_ID));
-- Creates Table SALE_VEHICLE.
CREATE TABLE SALE_VEHICLE (
VIN NUMBER(20),
trade_ID NUMBER(10),
make VARCHAR2(30) NOT NULL,
model VARCHAR2(30) NOT NULL,
type VARCHAR2(30) NOT NULL,
where_frome VARCHAR2(30) NOT NULL,
wholesale_cost NUMBER(8,2)NOT NULL,
CONSTRAINT producer_pk PRIMARY KEY(VIN));
-- Creates Table SALE.
CREATE TABLE SALE (
sale_ID NUMBER(6),
cust_ID NUMBER(10),
salesperson_ID NUMBER(10),
VIN NUMBER(20),
date DATE NOT NULL,
vehicle_status VARCHAR2(20) NOT NULL,
milage VARCHAR2(20) NOT NULL,
gross_sale_price NUMBER(8,2)NOT NULL,
CONSTRAINT sale_pk PRIMARY KEY(sale_ID))
CONSTRAINT customers_fk2 FOREIGN KEY(cust_ID) REFERENCES CUSTOMER(cust_ID))
CONSTRAINT VIN_fk FOREIGN KEY(VIN) REFERENCES SALE_VEHICLE(VIN));
-- spool off command to turn off the spool with it you may not get the all the content spooled.
set echo off
spool off
This is what is echoed after I attempt to run the sql file:
SQL>
SQL>
SQL> -- All the DROP TABLE in the beginning.
SQL>
SQL> DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> DROP TABLE companies CASCADE CONSTRAINTS;
DROP TABLE companies CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> DROP TABLE producer CASCADE CONSTRAINTS;
DROP TABLE producer CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL>
SQL> -- Creates Table CUSTOMER.
SQL>
SQL> CREATE TABLE CUSTOMER (
2 cust_ID NUMBER(10),
3 last_name VARCHAR2(20) NOT NULL,
4 first_name VARCHAR2(20) NOT NULL,
5 MI VARCHAR2(1) NOT NULL,
6 street VARCHAR2(30) NOT NULL,
7 city VARCHAR2(30) NOT NULL,
8 state VARCHAR2(30) NOT NULL,
9 zip_code VARCHAR2(20) NOT NULL,
10 CONSTRAINT customer_pk PRIMARY KEY(cust_ID));
CREATE TABLE CUSTOMER (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL> -- Creates Table SALE_VEHICLE.
SQL>
SQL> CREATE TABLE SALE_VEHICLE (
2 VIN NUMBER(20),
3 trade_ID NUMBER(10),
4 make VARCHAR2(30) NOT NULL,
5 model VARCHAR2(30) NOT NULL,
6 type VARCHAR2(30) NOT NULL,
7 where_frome VARCHAR2(30) NOT NULL,
8 wholesale_cost NUMBER(8,2)NOT NULL,
9 CONSTRAINT producer_pk PRIMARY KEY(VIN));
CREATE TABLE SALE_VEHICLE (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL>
SQL> -- Creates Table SALE.
SQL>
SQL> CREATE TABLE SALE (
2 sale_ID NUMBER(6),
3 cust_ID NUMBER(10),
4 salesperson_ID NUMBER(10),
5 VIN NUMBER(20),
6 date DATE NOT NULL,
7 vehicle_status VARCHAR2(20) NOT NULL,
8 milage VARCHAR2(20) NOT NULL,
9 gross_sale_price NUMBER(8,2)NOT NULL,
10 CONSTRAINT sale_pk PRIMARY KEY(sale_ID))
11 CONSTRAINT customers_fk2 FOREIGN KEY(cust_ID) REFERENCES CUSTOMER(cust_ID))
12 CONSTRAINT VIN_fk FOREIGN KEY(VIN) REFERENCES SALE_VEHICLE(VIN));
date DATE NOT NULL,
*
ERROR at line 6:
ORA-00904: : invalid identifier
SQL>
SQL> -- spool off command to turn off the spool with it you may not get the all the content spooled.
SQL>
SQL> set echo off
Any help would be great thanks.
I figured it out here is the answer:
-- Spool the execution output to a text file. This spooled file serves as logs.
spool on
spool hw2.lst
set echo on
-- All the DROP TABLE in the beginning.
DROP TABLE CUSTOMER CASCADE CONSTRAINTS;
DROP TABLE SALE_VEHICLE CASCADE CONSTRAINTS;
DROP TABLE SALE CASCADE CONSTRAINTS;
-- Creates Table CUSTOMER.
CREATE TABLE CUSTOMER (
cust_ID NUMBER(10),
last_name VARCHAR2(20) NOT NULL,
first_name VARCHAR2(20) NOT NULL,
MI VARCHAR2(1) NOT NULL,
street VARCHAR2(30) NOT NULL,
city VARCHAR2(30) NOT NULL,
state VARCHAR2(30) NOT NULL,
zip_code VARCHAR2(20) NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(cust_ID));
-- Creates Table SALE_VEHICLE.
CREATE TABLE SALE_VEHICLE (
VIN NUMBER(20),
trade_ID NUMBER(10),
make VARCHAR2(30) NOT NULL,
model VARCHAR2(30) NOT NULL,
type VARCHAR2(30) NOT NULL,
where_frome VARCHAR2(30) NOT NULL,
wholesale_cost NUMBER(8,2)NOT NULL,
CONSTRAINT producer_pk PRIMARY KEY(VIN));
-- Creates Table SALE.
CREATE TABLE SALE (
sale_ID NUMBER(6),
cust_ID NUMBER(10),
salesperson_ID NUMBER(10),
VIN NUMBER(20),
date_received DATE NOT NULL,
vehicle_status VARCHAR2(20) NOT NULL,
milage VARCHAR2(20) NOT NULL,
gross_sale_price NUMBER(8,2)NOT NULL,
CONSTRAINT sale_pk PRIMARY KEY(sale_ID),
CONSTRAINT customers_fk2 FOREIGN KEY(cust_ID) REFERENCES CUSTOMER(cust_ID),
CONSTRAINT VIN_fk FOREIGN KEY(VIN) REFERENCES SALE_VEHICLE(VIN));
-- spool off command to turn off the spool with it you may not get the all the content spooled.
set echo off
spool off