在sqlplus中创建表时出错

时间:2012-11-25 05:32:21

标签: database oracle sqlplus

我是sqlplus的新手并且正在尝试运行一个创建一些表的sql脚本,但是一旦我尝试运行它,它就会给我一个错误,说明表或视图不存在而且我不知道如何解决此错误。 我的剧本是:

 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;

2 个答案:

答案 0 :(得分:0)

删除或创建表的顺序很重要,因为如果您有外键引用另一个表,则在删除自己的表之前无法删除该表。 在此示例中,必须在BorrowerType表之前删除Borrower表。

答案 1 :(得分:0)

  

“外键引用的表中的唯一/主键”

数据完整性对于正确运行的数据库至关重要,因此如果其主键由另一个表的外键引用,Oracle将不允许我们删除表。所以它投掷了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. 首先运行drop table t_child:没有子表,没有外键。
  2. 删除阻止外键:alter table t_child drop constraint tc_pc_fk
  3. 上一个变种,让数据库找出外键:   drop table t_parent cascade constraints
  4. 第一个选项是最合适的,因为它使数据库处于有效状态(没有表,没有数据完整性损坏的可能性)。第三种方法的有效用途是从模式中擦除所有表的脚本:从数据字典中生成这样的脚本很容易。