简体   繁体   中英

Double quote in the name of table in select query of PostgreSQL

I am running following simple select query in PostgreSQL:

SELECT * FROM "INFORMATION_SCHEMA.KEY_COLUMN_USAGE"

It gives me following error report:

ERROR:  relation "INFORMATION_SCHEMA.KEY_COLUMN_USAGE" does not exist
LINE 1: SELECT * FROM "INFORMATION_SCHEMA.KEY_COLUMN_USAGE"
                      ^
********** Error **********

ERROR: relation "INFORMATION_SCHEMA.KEY_COLUMN_USAGE" does not exist
SQL state: 42P01
Character: 15

But when I am running the following query it runs successfully:

SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE

Again when I select from a table created by me the situation is reversed. Following one fails:

SELECT * FROM countryTable

while following one runs successfully.

SELECT * FROM "countryTable"

Why is it happening? What is the problem?

You probably created your table so:

CREATE TABLE "countryTable" (
  id SERIAL NOT NULL,
  country TEXT NOT NULL,
  PRIMARY KEY(id)
);

Which create a tablespace wrapped in "" , you shouldn't use double quote in general in postgres for table names or columns, try without double quotes:

CREATE TABLE countryTable (
  id SERIAL NOT NULL,
  country TEXT NOT NULL,
  PRIMARY KEY(id)
);

An then you can use this query you already have SELECT * FROM countryTable

While my personal advice is to use legal, lower-case names exclusively and never use double-quote, it is no problem per se.

When you look at the table definition in psql ( \\d tbl ), or at table names in the system catalog pg_class or column names in pg_attributes or any of the information schema views, you get identifiers in their correct spelling (and with all other oddities that may have been preserved by double-quoting them). You can use quote_ident() to quote such names automatically as needed - it only adds double quotes if necessary.

Postgres itself isn't foolish enough to use CaMeL case names. All objects in the information schema or in the system catalog are lower-cased (the names of the system tables and columns, not the names of user tables they carry as data).

Start at the basics, read the manual about identifiers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM