2016-11-06 109 views
-3

我正在試圖製作3個帶有外鍵的表格2。當我嘗試向表格中插入數據時,我不斷收到錯誤代碼,所以我想我會隔離創建表並嘗試在那裏修復。代碼如下:SQL無效標識符代碼

-- 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 

這就是呼應後,我嘗試運行SQL文件:

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 

任何幫助將是巨大的感謝。

+0

您刪除表電影,公司,製片人;您創建表Customer,Sale_Vehicle,Sale - 並獲知Customer已存在。爲什麼要刪除你沒有創建的表,爲什麼不刪除你創建的表?並且使用關鍵字「日期」作爲列名稱不太合適。您需要將其標記爲'分隔標識符'(用雙引號括起來,或者放在特定於平臺的替代方法中,例如方括號中),或者使用不同的名稱。 –

+0

你是否設置了你的數據庫案例不敏感?您的創建命令具有大寫的表名,而您的DROP小寫。不知道這是你的問題,但它值得嘗試。 – FDavidov

+0

那麼在我的課上,我正在教授說,進入刪除表是一個好習慣,這樣每次運行腳本時都不會收到錯誤消息表。 –

回答

0

我想通了這裏就是答案:

-- 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 
+0

輝煌!現在你可能想要刪除整個線程 - 你沒有暴露任何其他人可能從中學到什麼的「常見問題」。謝謝! – mathguy