Search code examples
postgresqlsql-insert

Postgres - Data insertion into a table - Wrong Visualization


I want to insert a subset of values into a table, called "Regions", made by just two column, "ID_Region" and "Region"

So, I've written this piece of code:

INSERT INTO public."Regions" ("ID_Region", "Region")
VALUES
    (0, 'Africa'),
    (1, 'Asia'),
    (2, 'Central and North America'),
    (3, 'Europe'),
    (4, 'Middle East'),
    (5, 'Nordic countries'),
    (6, 'Oceania'),
    (7, 'South America'),
    (9999, 'Missing')

And as output I have this, not the result expected:

ID_Region Region
0 "A"
1 "C"
2 "8"
3 "E"
4 "M"
5 "N"
6 "O"
7 "S"
9999 "M"

ID_Region is set as integer Region is set as Char

Why the result is different from the sql statement?


Solution

  • You must have used the special type "char" (Note the quotes!). That type only holds a single character, and will truncate its over-length input rather than throwing an error like other character types do. (Also, your example is wrong as your rows with ids 1 and 2 do not show what they actually would).

    You should not use the special type "char", it is for internal use in system catalogs or things that must interact with them.

    It is often handy to read the documentation for the tool you are using.