Search code examples
sqlpostgresqlquoted-identifierstring-constant

Query with the postgreSQL database


I already have a database created by postgresql (provided by another administrator than me), the command is created as shown below.

CREATE TABLE "Districts" (
    "Name" character varying(500),
    "ProvinceCode" text
);

The query Insert value to my table as follows:

Insert into Districts (Name,ProvinceCode) values ("Abc","Xyz"); `ERROR:  relation "districts" does not exist`

I have also tried:

Insert into "Districts" (Name,ProvinceCode) values ("Abc","Xyz"); `ERROR:  column "name" of relation "Districts" does not exist`

And:

Insert into "Districts" ("Name","ProvinceCode") values ("Abc","Xyz"); `ERROR:  column "Abc" does not exist`

I do not know how the syntax is correct. I really need someone to help.


Solution

  • To quote identifiers, you use " (double quotes). (Unquoted characters will also be interpreted as identifiers, just ignoring the case (converted to lower case) and splitting at whitespace.)

    Quotes around a string literal are ' (single quotes). At least this is part of the SQL Standard.

    Thus the correct insert for you is:

    INSERT INTO "Districts" ("Name", "ProvinceCode") VALUES ('Abc', 'Xyz');
    

    Anyways, I would usually not recommend using quoted identifiers (because you always need to carry them around with usually no real gain). Thus, if possible, I would recommend renaming the table and its columns to districts, name, province_code (but since this is in some sense not your table, it might be too late for this). After this, your insert would look like this:

    INSERT INTO districts (name, province_code) VALUES ('Abc', 'Xyz');