Reputation: 2821
For example, suppose I have a TINYINT
column that I want to change into an ENUM
. Is it possible to write a MySQL query that changes the datatype of the column while mapping the existing data so that (for example) 0
becomes No
, 1
becomes Yes
, and 2
becomes Maybe
?
Upvotes: 7
Views: 2965
Reputation: 11
This needs a sequence of 4 steps- add new column, set data in new column, drop old column, rename new column
Use [Databasename]
Go
ALTER table [tableName]
Add [newColumnname] datatype
GO
Update [tableName] SET [newColumnname] = [oldColumnname]
GO
ALTER table [tableName] Drop Column [oldColumnname]
GO
EXEC sp_RENAME 'tableName.newColumnname' , 'oldColumnname', 'COLUMN'
GO
Upvotes: 1
Reputation: 839
I'm not sure if this is possible in MySQL, but an alternative solution would be to:
Upvotes: 9
Reputation: 1216
create the new column next to the old column, use an update to move the data over, then delete the original column.
Upvotes: 1