2

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.

1
  • As a rule of thumb: never use double quotes (") in SQL. Commented Oct 1, 2019 at 7:03

1 Answer 1

5

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');
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thank you for the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.