Creating table via SQL Command Line, invalid identifier

橙三吉。 提交于 2021-02-17 03:31:18

问题


I'm currently learning SQL and I've installed oracle 11g express on my system. I'm trying to create a table however when I try to run the below command I get the following Error Message:

ERROR at line 3: ORA-00904 : invalid identifier

CREATE TABLE PROJECTS (
    proID       NUMBER(4) NOT NULL,
    Desc        CHAR(20),
    sDate       DATE,
    eDate       DATE,
    Budget      NUMBER(7,2),
    maxStaff    NUMBER(2)
);

Can anybody please tell me what I'm doing wrong?

Thanks for all the replies, I ran this command succesfully:

CREATE TABLE PROJECTS (
    proID       NUMBER(4) NOT NULL,
    description CHAR(20),
    sDate       DATE,
    eDate       DATE,
    Budget      NUMBER(7,2),
    maxStaff    NUMBER(2)
);

Really Appreciate the fast replies!

Chris


回答1:


You have DESC in as a column name. While you can use it you will have to encompass it in quotes:

CREATE TABLE PROJECTS (
    proID       NUMBER(4) NOT NULL,
    "Desc"        CHAR(20),
    sDate       DATE,
    eDate       DATE,
    Budget      NUMBER(7,2),
    maxStaff    NUMBER(2)
);

You will also have to use quotes every time you call it in a query. I recommend just changing that column to something else (maybe DESCRIPTION?)




回答2:


Since DESC is a reserved word, you would have to enclose it in double quotes.

However, I would not recommend using reserved words for fields names, perhaps change to description or something similar




回答3:


As already said several times, the error is caused here by the use of a reserved keyword unquoted as an identifier. For sake of completeness:

  • Oracle has an impressive list of reserved keywords.
  • Unquoted identifiers are internally converted upper-case by Oracle.
  • Quoted identifiers are case-sensitive

So:

CREATE TABLE T (DESC INT);

ORA-00904: : invalid identifier as DESC is a keyword

CREATE TABLE T (Desc INT);

ORA-00904: : invalid identifier same reason as unquoted identifiers are converted all upper-case

CREATE TABLE T ("DESC" INT);

Table created by using quotes, "DESC" is no longer recognized as a reserved keyword

INSERT INTO T("Desc") VALUES (1);

ORA-00904: "Desc": invalid identifier Quoted identifiers are case-sensitive. "DESC" is not the same columns as "Desc"

INSERT INTO T("DESC") VALUES (1);

1 row(s) inserted

That being said, you should avoid using a keyword as an identifier...



来源:https://stackoverflow.com/questions/27604027/creating-table-via-sql-command-line-invalid-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!