Changing the maximum length of a varchar column?

后端 未结 9 1583
[愿得一人]
[愿得一人] 2021-01-30 12:02

I\'m trying to update the length of a varchar column from 255 characters to 500 without losing the contents. I\'ve dropped and re-created tables before but I\'ve never been expo

相关标签:
9条回答
  • 2021-01-30 12:30

    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.

    0 讨论(0)
  • 2021-01-30 12:35

    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

    0 讨论(0)
  • 2021-01-30 12:36

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

    0 讨论(0)
  • 2021-01-30 12:37

    I was also having above doubt, what worked for me is

    ALTER TABLE `your_table` CHANGE `property` `property` 
    VARCHAR(whatever_you_want) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;  
    
    0 讨论(0)
  • 2021-01-30 12:41

    You can use modify:

    ALTER TABLE `table name` 
    modify COLUMN `column name` varchar("length");
    
    0 讨论(0)
  • 2021-01-30 12:42

    As an alternative, you can save old data and create a new table with new parameters.

    In SQL Server Management Studio: "your database" => task => generatescripts => select specific database object => "your table" => advanced => types of data to script - schema and data => generate

    Personally, I did so.

    0 讨论(0)
提交回复
热议问题