SQL Query To Alter Column Size

Let a table exist named students that was created using following command:

CREATE TABLE `students`(
  `id` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

After the table is created and some record are inserted, we realized the column size for name is small and we want to extend to 150 characters. Here is the query that you would use to alter column size:

ALTER TABLE `university_db`.`students` 
CHANGE COLUMN `name` `name` VARCHAR(150) NULL DEFAULT NULL;

The null default null means, null is allowed and default value should also be null when no value is inserted while adding a record in the table.

Comments