Oracle DB quote column names

后端 未结 4 1222
悲哀的现实
悲哀的现实 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 16:09

    [TL;DR] The simplest thing to do is to never use double quotes around object names and just let oracle manage the case-sensitivity in its default manner.

    Oracle databases are, by default, case sensitive; however, they will also, by default, convert everything to upper-case so that the case sensitivity is abstracted from you, the user.

    CREATE TABLE Test ( column_name NUMBER );
    

    Then:

    SELECT COUNT(column_name) FROM test;
    SELECT COUNT(Column_Name) FROM Test;
    SELECT COUNT(COLUMN_NAME) FROM TEST;
    SELECT COUNT(CoLuMn_NaMe) FROM tEsT;
    SELECT COUNT("COLUMN_NAME") FROM "TEST";
    

    Will all give the same output and:

    DESCRIBE test;
    

    Outputs:

    Name        Null Type
    ----------- ---- ------
    COLUMN_NAME      NUMBER
    

    (Note: Oracle's default behaviour is to convert the name to upper case.)

    If you use double quotes then oracle will respect your use of case in the object's name (and you are then required to always use the same case):

    CREATE TABLE "tEsT" ( "CoLuMn_NaMe" NUMBER );
    

    (Note: Both the table and column name are surrounded in double quotes and now require you to use exactly the same case, and quotes, when you refer to them.)

    Then you can only do (since you need to respect the case sensitivity):

    SELECT COUNT("CoLuMn_NaMe") FROM "tEsT";
    

    And

    DESCRIBE "tEsT";
    

    Outputs:

    Name        Null Type
    ----------- ---- ------
    CoLuMn_NaMe      NUMBER
    

    (Note: Oracle has respected the case sensitivity.)

提交回复
热议问题