Why PostgreSQL does not like UPPERCASE table names?

前端 未结 3 2080
[愿得一人]
[愿得一人] 2020-12-09 03:42

I have recently tried to create some tables in PostgreSQL all in uppercase names. However in order to query them I need to put the table name inside the \"TABLE_NAME\". Is t

3条回答
  •  有刺的猬
    2020-12-09 04:16

    put table name into double quotes if you want postgres to preserve case for relation names.

    Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)

    from docs (emphasis mine)

    example with quoting:

    t=# create table "UC_TNAME" (i int);
    CREATE TABLE
    t=# \dt+ UC
    
    t=# \dt+ "UC_TNAME"
                          List of relations
     Schema |   Name   | Type  |  Owner   |  Size   | Description
    --------+----------+-------+----------+---------+-------------
     public | UC_TNAME | table | postgres | 0 bytes |
    (1 row)
    

    example without quoting:

    t=# create table UC_TNAME (i int);
    CREATE TABLE
    t=# \dt+ UC_TNAME
                          List of relations
     Schema |   Name   | Type  |  Owner   |  Size   | Description
    --------+----------+-------+----------+---------+-------------
     public | uc_tname | table | postgres | 0 bytes |
    (1 row)
    

    So if you created table with quotes, you should not skip quotes querying it. But if you skipped quotes creating object, the name was folded to lowercase and so will be with uppercase name in query - this way you "won't notice" it.

提交回复
热议问题