Oracle DB quote column names

后端 未结 4 1225
悲哀的现实
悲哀的现实 2020-12-06 15:24

When using regular tables, its fine to use the following Oracle SQL query:

SELECT max(some_primary_key) FROM MyTable

However, when using Da

4条回答
  •  清歌不尽
    2020-12-06 15:56

    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.

    You can use either quoted or nonquoted identifiers to name any database object. However, database names, global database names, and database link names are always case insensitive and are stored as uppercase. If you specify such names as quoted identifiers, then the quotation marks are silently ignored. Refer to CREATE USER for additional rules for naming users and passwords.

    To summarize this

    When you do :

    SELECT max(some_primary_key) FROM MyTable
    

    Oracle assume that your column was declared like this :

    CREATE TABLE MyTable (
        some_primary_key INT,
        ...
    )
    

    Seing the resulting error, it's not the case. You obviously declared it like this :

    CREATE TABLE MyTable (
        "some_primary_key" INT,
        ...
    )
    

    And you should thus always refer to that column using double quotes and proper case, thus :

    SELECT max("some_primary_key") FROM MyTable
    

    The bible : Oracle Database Object Names and Qualifiers

提交回复
热议问题