SQL 更改 Oracle 表名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13292652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:01:49  来源:igfitidea点击:

Changing Oracle table name

sqloracle11galter-tabletable-rename

提问by Antarr Byrd

I'm trying to change a table name in oracle. I first run this script to declare the table

我正在尝试更改 oracle 中的表名。我首先运行这个脚本来声明表

CREATE TABLE CUSTOMER
(
  C_ID NUMBER(6,0),
  C_LAST VARCHAR2(25),
  C_FIRST varchar2(25),
  C_MI char(1),
  C_DOB DATE,
  C_ADDRESS varchar2(100),
  C_CITY varchar2(35),
  C_STATE char(2),
  C_ZIP varchar2(10),
  C_DPHONE varchar2(12),
  C_EPHONE varchar2(12),
  C_USERID varchar2(20),
  C_PASSWORD VARCHAR2(30),
  CONSTRAINT CUSTOMER_PK PRIMARY KEY(C_ID)
);

CREATE TABLE ORDER_SOURCE
(
  OS_ID NUMBER(6),
  OS_DESC varchar2(255),
  CONSTRAINT ORDER_SOURCE_PK PRIMARY KEY(OS_ID)
);

CREATE TABLE ORDERS
(
  O_ID NUMBER(6),
  O_DATE DATE,
  O_METHPMT varchar2(25),
  C_ID NUMBER(6),
  OS_ID NUMBER(6),
  CONSTRAINT ORDERS_PK PRIMARY KEY(O_ID),
  CONSTRAINT ORDERS_CUSTOMER_FK FOREIGN KEY(C_ID) REFERENCES CUSTOMER(C_ID),
  CONSTRAINT ORDERS_ORDER_SOURCE_FK FOREIGN KEY(OS_ID) REFERENCES ORDER_SOURCE(OS_ID)
);

It runs correctly, I then try to run

它运行正确,然后我尝试运行

alter table ORDERS
rename to ORDER;

I get this error:

我收到此错误:

Error starting at line 1 in command: alter table ORDERS rename to ORDER Error report: SQL Error: ORA-00903: invalid table name 00903. 00000 - "invalid table name" *Cause:
*Action:

从命令中的第 1 行开始出错:alter table ORDERS rename to ORDER 错误报告:SQL 错误:ORA-00903:无效的表名 00903。00000 -“无效的表名”*原因:
*操作:

采纳答案by Shepherdess

order is a reserved word in oracle so you cannot use it as a table name. You can try escaping with double quotes ("order") but it's not a good practice

order 是 oracle 中的保留字,因此您不能将其用作表名。您可以尝试使用双引号(“order”)进行转义,但这不是一个好习惯

回答by Vladimir

Syntax "RENAME TABLE tab_old TO tab_new" is not correct one.
Correct syntax: "RENAME tab_old TO tab_new".
Word "TABLE" shouldn't be in the statement.

语法“ RENAME TABLE tab_old TO tab_new”是不正确的。
正确的语法:“ RENAME tab_old TO tab_new”。
语句中不应出现“TABLE”一词。

回答by djadmin

RENAME TABLE table-Name TO new-Table-Name

If there is a view or foreign key that references the table, attempts to rename it will generate an error. In addition, if there are any check constraints or triggers on the table, attempts to rename it will also generate an error.

如果存在引用该表的视图或外键,尝试重命名它会产生错误。此外,如果表上有任何检查约束或触发器,尝试重命名它也会产生错误。

And in your case, the "ORDER" table-name is RESERVED so please try to change the name

在您的情况下,“ORDER”表名是 RESERVED 所以请尝试更改名称