简体   繁体   English

收到错误“ ORA-00942:表或视图不存在”

[英]Getting error “ORA-00942: table or view does not exist”

I am using Oracle APEX platform, when I run my script code, I got the same error again and again 我正在使用Oracle APEX平台,当我运行脚本代码时,一次又一次遇到相同的错误

ORA-00942: table or view does not exist ORA-00942:表或视图不存在

Kindly help me with find the cause of the error. 请帮助我找出错误的原因。

create table roles
(
    "role_id" number(5,0) not null,
    "name" varchar2(20) not null,
    constraint "rle_pk" primary key ("role_id")
);

create table users
(
    "user_id" number(5,0) not null,
    "first_name" varchar2(15) not null,
    "last_name" varchar2(15) not null,
    "contact_number" number(15,0) not null,
    "address" varchar2(30) not null,
    "RLE_id" number(5,0) not null,
    constraint "usr_pk" primary key ("user_id")
);

alter table "users" 
      add constraint "usr_rle_fk" 
          foreign key ("RLE_id") references "roles"("role_id");

When you write a CREATE TABLE ... statement WITHOUT using "double quotes" for the name of the table, then the table's name will be stored using all UPPERCASE letters. 当你写一个CREATE TABLE语句...请不使用“双引号”为表的名称 ,然后表的名称将全部使用大写字母存储。

When you are using "double quotes", then you are enforcing case-sensitivity. 当您使用“双引号”时,您将强制区分大小写。 That's why the ALTER TABLE statement fails (users <> USERS, roles <> ROLES). 这就是为什么ALTER TABLE语句失败(用户<>用户,角色<> ROLES)的原因。 You would have to write: 您将必须编写:

alter table "USERS"  
add constraint "usr_rle_fk" foreign key ("RLE_id")
references "ROLES"("role_id");

Better: avoid double quotes when writing DDL code eg 更好:编写DDL代码时避免双引号,例如

SQL> create table roles
  2  (
  3   role_id number(5,0) not null,
  4      name varchar2(20) not null,
  5      constraint rle_pk primary key (role_id)
  6  );

Table ROLES created.  -- notice: table name all upper case

SQL> 
SQL> create table users
  2  (
  3      user_id number(5,0) not null,
  4      first_name varchar2(15) not null,
  5      last_name varchar2(15) not null,
  6      contact_number number(15,0) not null,
  7      address varchar2(30) not null,
  8      RLE_id number(5,0) not null,
  9      constraint usr_pk primary key (user_id)
 10  );

Table USERS created. -- notice: table name all upper case

SQL> 
SQL> alter table users add constraint usr_rle_fk foreign key (RLE_id)
  2  references roles(role_id);

Table USERS altered.

When working with APEX (version 19, using SQL Workshop/SQL Commands) you only see the message "Table created" when creating a table, but not the table name as such. 使用APEX(版本19,使用SQL Workshop / SQL命令)时,在创建表时只会看到消息“表已创建”,而不会看到表名。

在此处输入图片说明

Consider your CREATE TABLE statement ... 考虑一下您的CREATE TABLE语句...

create table users

... with your ALTER TABLE statement: ...使用您的ALTER TABLE语句:

alter table "users" 

Your use of the double-quotes in the second statement is the cause of the ORA-00942. 您在第二条语句中使用双引号是造成ORA-00942的原因。 Double-quotes make the identifier case sensitive . 双引号使标识符区分大小写 Consequently USERS does not equal "users" . 因此, USERS不等于"users" So remove those double-quotes and the problem will disappear. 因此,删除那些双引号,问题将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM