SQL Server中改变表列字段长度的SQL是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4372176/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:29:56  来源:igfitidea点击:

What is the SQL to change the field length of a table column in SQL Server

sqlsql-server

提问by leora

What is the SQL to make a field go from nvarchar(50)to nvarchar(250)?

使字段从nvarchar(50)到的 SQL 是什么nvarchar(250)

When I try to change it through the SQL Server Management Studio, it doesn't allow me to do it, so I figured I would try SQL directly instead of using the GUI.

当我尝试通过 SQL Server Management Studio 更改它时,它不允许我这样做,所以我想我会直接尝试 SQL 而不是使用 GUI。

回答by Martin Smith

Alter table tblname ALTER Column colname nvarchar(250) [NOT] NULL

If NULL/ NOT NULLis not specified the column will become Nullable irrespective of what ever the original specification was.

如果NULL/NOT NULL未指定,则无论原始规范是什么,该列都将变为 Nullable。

回答by Randy Minder

ALTER TABLE MyTable
ALTER COLUMN MyColumn varchar(NewSize)

回答by saravanan

The ALTER TABLE Statement

ALTER TABLE 语句

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. SQL ALTER TABLE Syntax

ALTER TABLE 语句用于添加、删除或修改现有表中的列。 SQL ALTER TABLE 语法

To add a column in a table, use the following syntax:

要在表中添加列,请使用以下语法:

ALTER TABLE table_name
ADD column_name datatype

To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):

要删除表中的列,请使用以下语法(请注意,某些数据库系统不允许删除列):

ALTER TABLE table_name
DROP COLUMN column_name

To change the data type of a column in a table, use the following syntax:

要更改表中列的数据类型,请使用以下语法:

SQL Server / MS Access:

SQL Server / MS 访问:

ALTER TABLE table_name
ALTER COLUMN column_name datatype

My SQL / Oracle (prior version 10G):

我的 SQL/Oracle(10G 之前的版本):

ALTER TABLE table_name MODIFY COLUMN column_name datatype

ALTER TABLE table_name MODIFY COLUMN column_name 数据类型

Oracle 10G and later:

Oracle 10G 及更高版本:

ALTER TABLE table_name
MODIFY column_name datatype

回答by Olaolu Ajose

Its sometimes safer to check if the table exist in the first place...

有时首先检查表是否存在更安全......

IF COL_LENGTH('[tablename]','[tablecolumn]') IS NULL
        BEGIN
        ALTER TABLE tablename
            ALTER COLUMN [tablecolumn] 
            NVARCHAR(500)  
        END

回答by Dillip

To change the datatype of many column but same datatype

更改多列但相同数据类型的数据类型

alter table employee modify (firstname varchar2(9),lastname varchar2(9),email varchar2(9));
-- Table altered.


alter table employee modify (firstname,lastname,email varchar2(9));
-- Table altered.

回答by Javed Ansari

For Oracle SQL Developers

对于 Oracle SQL 开发人员

Alter table tblname MODIFY (colname varchar2(250));

Description : It will increase the length of column. where 250 represent the updated (incremented) length of column.

描述:它会增加列的长度。其中 250 表示列的更新(增加)长度。