2012-11-25 103 views
0

我是新來的sqlplus,並試圖運行一個sql腳本,創建幾個表,但一旦我嘗試運行它,它給了我一個錯誤,說表或視圖不存在我不知道如何解決這個錯誤。 我的腳本是:在sqlplus中創建表時出錯

drop table Borrower; 
create table Borrower (
    bid char(100) not null, 
    password char(100) not null, 
    name char(100) null, 
    address char(100) null, 
    phone char(100) null, 
    emailAddress char(100) null, 
    sinOrStNo char(100) null, 
    expiryDate date null, 
    --type ENUM('student','faculty','staff'), 
    type char(100) not null, 
    --CONSTRAINT Btype_check CHECK (type IN ('student','faculty','staff')), 
    FOREIGN KEY (type) references BorrowerType(type), 
    PRIMARY KEY (bid)); 
grant select on Borrower to public; 
+0

可能是'drop table borrower'命令 - 你試圖刪除一個不存在的表 –

+0

不,我試過了。它仍然無法創建數據庫。 – BBB

+1

數據庫或表?如果只運行'create'命令會發生什麼? –

回答

0

「唯一/主表鍵的外鍵引用」

數據的完整性是一個正常運行的數據庫至關重要因此甲骨文不會讓我們刪除表,如果它的主鍵被另一個引用表的外鍵。所以它拋出了ORA-02449。

因此,鑑於此設置:

create table t_parent (
    id number not null 
    , constraint tp_pk primary key (id) 
); 
create table t_child (
    id number not null 
    , p_id number not null 
    , constraint tc_pk primary key (id) 
    , constraint tc_tp_fk foreign key (p_id) 
     references t_parent (id) 
); 

有三種方式來刪除表t_parent

  1. run drop table t_child第一:沒有子表,沒有外鍵。
  2. 刪除阻止外鍵:alter table t_child drop constraint tc_pc_fk
  3. 上一個變體,讓數據庫找出外鍵: drop table t_parent cascade constraints

第一個選項是最合適的,因爲它使數據庫保持有效狀態(沒有表,不存在數據完整性損壞的可能性)。第三種方法的有效用途是一個腳本,用於壓縮模式中的所有表:從數據字典中生成這樣的腳本很容易。

0

你刪除或創建表是很重要的,因爲如果你有外鍵引用另一個表,你不能刪除自己的表之前刪除該表中的順序。 在這個例子中,Borrower表必須在BorrowerType表之前被刪除。