Error trying to rename columns with space in oracle table. Error - SQL Error : ORA- 00946 : missing TO keyword

Error trying to rename columns with space in oracle table. Error - SQL Error : ORA- 00946 : missing TO keyword

我有 table 名为 employee。其中,我希望将列名从 Employee Name 更改为 Employee_Name(请注意,现有列名有一个 space!)

当我使用以下命令时:

Alter table employee rename column Employee Name to Employee_Name

我收到这个错误:

SQL Error : ORA- 00946 : missing TO keyword

如何解决这个问题?

由于 Oracle 不知道 space 是列名的一部分,因此您必须将其括在引号中以确保其正确识别:

Alter table employee rename column "Employee Name" to Employee_Name

I wish to alter the column name from 'Employee Name' to 'Employee_Name'

问题在于您在创建 table 时将 quoted identifier 用于 列名称 .

来自docs,

Database Object Naming Rules

Every database object has a name. In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier.

  • A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.

  • A nonquoted identifier is not surrounded by any punctuation.

您需要在现有列名周围使用双引号来解决问题:

SQL> CREATE TABLE employee("employee name" VARCHAR2(10));

Table created.

SQL> ALTER TABLE employee RENAME column "employee name" TO employee_name;

Table altered.