MySQL - 如何在不破坏现有数据的情况下增加数据库中现有列的 varchar 大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10184086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL - How to increase varchar size of an existing column in a database without breaking existing data?
提问by Genadinik
I have a column that is currently varchar(100)
and I want to make it 10000
.
我有一个当前的专栏,varchar(100)
我想制作它10000
。
is it as simple as
是不是很简单
alter table table_name set column col_name varchar (10000);
I am afraid to corrupt the exiting data. Will I be ok if I run this query? Or should I do I alter
the column another way?
我害怕破坏现有数据。如果我运行这个查询,我会好吗?或者我应该alter
以另一种方式做我的专栏?
Thanks!
谢谢!
采纳答案by hjpotter92
I normally use this statement:
我通常使用这个语句:
ALTER TABLE `table_name`
CHANGE COLUMN `col_name` `col_name` VARCHAR(10000);
But, I think SET will work too, never have tried it. :)
但是,我认为 SET 也会起作用,从来没有尝试过。:)
回答by Lynn Crumbling
It's safe to increase the size of your varchar column. You won't corrupt your data.
增加 varchar 列的大小是安全的。你不会破坏你的数据。
If it helps your peace of mind, keep in mind, you can always run a database backup before altering your data structures.
如果它有助于您安心,请记住,您始终可以在更改数据结构之前运行数据库备份。
By the way, correct syntax is:
顺便说一句,正确的语法是:
ALTER TABLE table_name MODIFY col_name VARCHAR(10000)
Also, if the column previously allowed/did not allow nulls, you should add the appropriate syntax to the end of the alter table statement, after the column type.
此外,如果该列以前允许/不允许空值,则应在更改表语句的末尾,在列类型之后添加适当的语法。
回答by Mike Causer
I'd like explain the different alter table syntaxes - See the MySQL documentation
我想解释不同的更改表语法 - 请参阅MySQL 文档
For adding/removing defaults on a column:
添加/删除列上的默认值:
ALTER TABLE table_name
ALTER COLUMN col_name {SET DEFAULT literal | DROP DEFAULT}
For renaming a column, changing it's data type and optionally changing the column order:
要重命名列,更改其数据类型并可选择更改列顺序:
ALTER TABLE table_name
CHANGE [COLUMN] old_col_name new_col_name column_definition
[FIRST|AFTER col_name]
For changing a column's data type and optionally changing the column order:
用于更改列的数据类型和可选地更改列顺序:
ALTER TABLE table_name
MODIFY [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
回答by Mike Causer
For me worked this one:
对我来说,这是一个:
ALTER TABLE tablename MODIFY fieldname VARCHAR(128) NOT NULL;
ALTER TABLE tablename MODIFY fieldname VARCHAR(128) NOT NULL;
回答by sam
I am using mysql and below syntax worked well for me,
我正在使用 mysql 和以下语法对我来说效果很好,
ALTER TABLE table_name MODIFY col_name VARCHAR(12);
回答by Tejas
For me this has worked-
对我来说,这奏效了——
ALTER TABLE table_name ALTER COLUMN column_name VARCHAR(50)
ALTER TABLE table_name ALTER COLUMN column_name VARCHAR(50)
回答by Amichai Ungar
use this syntax: alter table table_name modify column col_name varchar (10000);
使用以下语法:alter table table_name modify column col_name varchar (10000);