I have a table of many columns in Postgresql database. Some of these columns are in text type and have several rows of values. The values also recur. I would like to change these text values with unique integer values.
This is my table column:
Country_Name
------------
USA
Japan
Mexico
USA
USA
Japan
England
and the new column I want is:
Country_Name
------------
1
2
3
1
1
2
4
Each country name is assigned (mapped) to a unique integer and all the recurrences of the text is replaced with this number. How can I do this?
Edit 1: I want to replace my column values on the fly if possible. I don't actually need another column to keep the names but it would be nice to see the actual values too. Is it possible to do:
- Create a column
country_idwith the same values ofcountry_namecolumn in the same table - And for
country_idreplace each name with a unique integer with an update statement or procedure without requiring a new table or dictionary or map.
I don't know if this is possible but this will speed up things because I have a total of 220 columns and millions of rows. Thank you.
SELECT DISTINCT. Then update while joining to the mapping table.