如何更改SQL Server中的列的数据类型?

时间:2022-01-16 17:00:05

I am trying to change a column from a varchar(50) to a nvarchar(200). What is the SQL command to alter this table?

我正在尝试将一个列从varchar(50)改为nvarchar(200)。更改此表的SQL命令是什么?

7 个解决方案

#1


471  

ALTER TABLE TableName 
ALTER COLUMN ColumnName NVARCHAR(200) [NULL | NOT NULL]

EDIT As noted NULL/NOT NULL should have been specified, see Rob's answer as well.

按照注释的NULL/NOT NULL进行编辑应该已经被指定,请参见Rob的答案。

#2


159  

Don't forget nullability.

不要忘记nullability。

ALTER TABLE <schemaName>.<tableName>
ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL]

#3


19  

Use the Alter table statement.

使用Alter table语句。

Alter table TableName Alter Column ColumnName nvarchar(100)

#4


9  

The syntax to modify a column in an existing table in SQL Server (Transact-SQL) is:

在SQL Server (Transact-SQL)中修改现有表中的列的语法是:

ALTER TABLE table_name
    ALTER COLUMN column_name column_type;

For example:

例如:

ALTER TABLE employees
    ALTER COLUMN last_name VARCHAR(75) NOT NULL;

This SQL Server ALTER TABLE example will modify the column called last_name to be a data type of VARCHAR(75) and force the column to not allow null values.

这个SQL Server ALTER TABLE示例将把名为last_name的列修改为VARCHAR(75)的数据类型,并强制列不允许空值。

see here

在这里看到的

#5


2  

As long as you're increasing the size of your varchar you're OK. As per the Alter Table reference:

只要你增加varchar的尺寸就可以了。根据附表:

Reducing the precision or scale of a column may cause data truncation.

降低列的精度或比例可能导致数据截断。

#6


2  

For changing data type

改变数据类型

alter table table_name 
alter column column_name datatype [NULL|NOT NULL]

For changing Primary key

修改主键

ALTER TABLE table_name  
ADD CONSTRAINT PK_MyTable PRIMARY KEY (column_name)

#7


-10  

Try this:

试试这个:

ALTER TABLE "table_name"
MODIFY "column_name" "New Data Type";

#1


471  

ALTER TABLE TableName 
ALTER COLUMN ColumnName NVARCHAR(200) [NULL | NOT NULL]

EDIT As noted NULL/NOT NULL should have been specified, see Rob's answer as well.

按照注释的NULL/NOT NULL进行编辑应该已经被指定,请参见Rob的答案。

#2


159  

Don't forget nullability.

不要忘记nullability。

ALTER TABLE <schemaName>.<tableName>
ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL]

#3


19  

Use the Alter table statement.

使用Alter table语句。

Alter table TableName Alter Column ColumnName nvarchar(100)

#4


9  

The syntax to modify a column in an existing table in SQL Server (Transact-SQL) is:

在SQL Server (Transact-SQL)中修改现有表中的列的语法是:

ALTER TABLE table_name
    ALTER COLUMN column_name column_type;

For example:

例如:

ALTER TABLE employees
    ALTER COLUMN last_name VARCHAR(75) NOT NULL;

This SQL Server ALTER TABLE example will modify the column called last_name to be a data type of VARCHAR(75) and force the column to not allow null values.

这个SQL Server ALTER TABLE示例将把名为last_name的列修改为VARCHAR(75)的数据类型,并强制列不允许空值。

see here

在这里看到的

#5


2  

As long as you're increasing the size of your varchar you're OK. As per the Alter Table reference:

只要你增加varchar的尺寸就可以了。根据附表:

Reducing the precision or scale of a column may cause data truncation.

降低列的精度或比例可能导致数据截断。

#6


2  

For changing data type

改变数据类型

alter table table_name 
alter column column_name datatype [NULL|NOT NULL]

For changing Primary key

修改主键

ALTER TABLE table_name  
ADD CONSTRAINT PK_MyTable PRIMARY KEY (column_name)

#7


-10  

Try this:

试试这个:

ALTER TABLE "table_name"
MODIFY "column_name" "New Data Type";