Login

Changing field type in production

Author:
lawgon
Posted:
April 17, 2007
Language:
SQL
Version:
Not specified
Tags:
schema_evolution
Score:
2 (after 2 ratings)

Assume a model called 'Child' with a field called 'schoolstd' whic h is of integer type and not null. You need to change it to char type and not null and the same time preserve the data. The above snippet does this in postgresql.

1
2
3
4
5
6
7
begin;
alter table web_child add column strnum varchar(10);
update web_child set strnum = to_char(schoolstd,'999');
alter table web_child alter column strnum set not null;
alter table web_child drop column schoolstd;
alter table web_child rename column strnum to schoolstd;
commit;

More like this

Comments

Please login first before commenting.