Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the maximum length of a varchar column?

People also ask

What is the maximum length of VARCHAR column?

The size of the maximum size (m) parameter of a VARCHAR column can range from 1 to 255 bytes. If you are placing an index on a VARCHAR column, the maximum size is 254 bytes. You can store character strings that are shorter, but not longer, than the m value that you specify.

How do I change the column length?

In generic terms, you use the ALTER TABLE command followed by the table name, then the MODIFY command followed by the column name and new type and size. Here is an example: ALTER TABLE tablename MODIFY columnname VARCHAR(20) ; The maximum width of the column is determined by the number in parentheses.

How do I increase the size of an existing column in SQL Server?

SQL - Modify Column Data Type and SizeALTER TABLE Employee ALTER COLUMN FirstName VARCHAR(50); The following will change the size in the Oracle database. ALTER TABLE Employee MODIFY (FirstName VARCHAR2(50)); The following will change the size in the PostgreSQL database.


You need

ALTER TABLE YourTable ALTER COLUMN YourColumn <<new_datatype>> [NULL | NOT NULL]

But remember to specify NOT NULL explicitly if desired.

ALTER TABLE YourTable ALTER COLUMN YourColumn VARCHAR (500) NOT NULL;

If you leave it unspecified as below...

ALTER TABLE YourTable ALTER COLUMN YourColumn VARCHAR (500);

Then the column will default to allowing nulls even if it was originally defined as NOT NULL. i.e. omitting the specification in an ALTER TABLE ... ALTER COLUMN is always treated as.

ALTER TABLE YourTable ALTER COLUMN YourColumn VARCHAR (500) NULL;

This behaviour is different from that used for new columns created with ALTER TABLE (or at CREATE TABLE time). There the default nullability depends on the ANSI_NULL_DFLT settings.


Increasing column size with ALTER will not lose any data:

alter table [progennet_dev].PROGEN.LE 
    alter column UR_VALUE_3 varchar(500) 

As @Martin points out, remember to explicitly specify NULL | NOT NULL


For MySQL or DBMSes other than MSSQL, you may need to use modify instead of alter for the column value:

ALTER TABLE `table name` 
modify COLUMN `column name` varchar("length");

ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME VARCHAR(40);

I am using Oracle SQL Developer and @anonymous' answer was the closest, but kept receiving syntax errors until I edited the query to this. I changed alter to modify and there's no need to define column_name as column.


Using Maria-DB and DB-Navigator tool inside IntelliJ, MODIFY Column worked for me instead of Alter Column


In Oracle SQL Developer

ALTER TABLE car_details MODIFY torque VARCHAR(100);