How to change a datatype for a field How do I change a data type for a field in a table? 

To change the field name we can use db_change_field().
The same function can be used to change the type of the field.

Suppose you are the following field defined for the "foo" schema.

$schema['foo'] = array(
  'fields' =&gt; array(
    'bar' =&gt; array('type' =&gt; 'int', 'not null' =&gt; TRUE)
  ),
  'primary key' =&gt; array('bar')
);


You can change its type to "serial" using the following code:

db_drop_primary_key($ret, 'foo');
db_change_field($ret, 'foo', 'bar', 'bar',
  array('type' =&gt; 'serial', 'not null' =&gt; TRUE),
  array('primary key' =&gt; array('bar')));


The call to db_drop_primary_key() is necessary because the field is a primary key; for compatibility with any database engine, it is suggested to first remove the primary keys, the indexes, and the unique keys before to edit them.

If in the call to db_change_field(), the third and the fourth parameters contain the same value, then you are not changing the field name.

The documentation for the function contains the following text:


  Change a field definition.
  
  IMPORTANT NOTE: To maintain database portability, you have to explicitly recreate all indices and primary keys that are using the changed field.


To notice that the first line doesn't say "change a field name"; it says "change a field definition," and the field name is included in the field definition.