Add integer column to existing mysql table based on existing column

I want to add a new column to an existing mysql table, where the new column is a unique integer for each unique value of an existing column in the table. For example, if an existing column has unique values ​​A and B (and there may be 50 rows of each of A and B), then add a new column with values ​​1 and 2 in each row where there are A and B, respectively.

+6
source share
1 answer

well, it takes two teams

ALTER TABLE your_table ADD COLUMN your_column INTEGER UNIQUE; 

And then for each column entry you create instructions for updating for this, for example:

 UPDATE your_table SET your_column = 1 WHERE column = 'A' UPDATE your_table SET your_column = 2 WHERE column = 'B' 

Or you create a procedure for this if there are a lot of records.

+10
source

Source: https://habr.com/ru/post/903931/


All Articles