更改 SQL Server 中的列大小

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

Altering column size in SQL Server

sqlsql-serverdatabasetsqlalter-table

提问by Sree

How to change the column size of the salarycolumn in the employeetable from numeric(18,0)to numeric(22,5)

如何salaryemployee表中列的列大小从numeric(18,0)更改为numeric(22,5)

回答by Darren

ALTER TABLE [Employee]
ALTER COLUMN [Salary] NUMERIC(22,5) NOT NULL

回答by Priyank Patel

alter table Employee alter column salary numeric(22,5)

回答by Durgesh Pandey

ALTER TABLE [table_name] ALTER COLUMN [column_name] varchar(150)

回答by Hamid Heydarian

Running ALTER COLUMNwithout mentioning attribute NOT NULLwill result in the column being changed to nullable, if it is already not. Therefore, you need to first check if the column is nullable and if not, specify attribute NOT NULL. Alternatively, you can use the following statement which checks the nullability of column beforehand and runs the command with the right attribute.

ALTER COLUMN不提及属性NOT NULL而运行将导致该列更改为可为空(如果它已经不是)。因此,您需要首先检查该列是否可以为空,如果不是,则指定 attribute NOT NULL。或者,您可以使用以下语句预先检查列的可空性并使用正确的属性运行命令。

IF COLUMNPROPERTY(OBJECT_ID('Employee', 'U'), 'Salary', 'AllowsNull')=0
    ALTER TABLE [Employee]
        ALTER COLUMN [Salary] NUMERIC(22,5) NOT NULL
ELSE        
    ALTER TABLE [Employee]
        ALTER COLUMN [Salary] NUMERIC(22,5) NULL

回答by arnav

Select table--> Design--> change value in Data Type shown in following Fig.

选择表格-->设计-->更改数据类型中的值,如下图所示。

enter image description here

在此处输入图片说明

Save tables design.

保存表设计。

回答by Lukasz Szozda

Interesting approach could be found here: How To Enlarge Your Columns With No Downtime by spaghettidba

可以在这里找到有趣的方法:如何在没有停机时间的情况下扩大您的列,由 spaghettidba

If you try to enlarge this column with a straight “ALTER TABLE” command, you will have to wait for SQLServer to go through all the rows and write the new data type

ALTER TABLE tab_name ALTER COLUMN col_name new_larger_data_type;

To overcome this inconvenience, there is a magic column enlargement pill that your table can take, and it's called Row Compression.(...) With Row Compression, your fixed size columns can use only the space needed by the smallest data type where the actual data fits.

如果您尝试使用直接的“ALTER TABLE”命令扩大此列,则必须等待 SQLServer 遍历所有行并写入新的数据类型

ALTER TABLE tab_name ALTER COLUMN col_name new_larger_data_type;

为了克服这种不便,您的表格可以使用一种神奇的列扩大药丸,它称为行压缩。(...) 使用行压缩,固定大小的列只能使用适合实际数据的最小数据类型所需的空间。

When table is compressed at ROWlevel, then ALTER TABLE ALTER COLUMNis metadata only operation.

当表在ROW级别上压缩时,则ALTER TABLE ALTER COLUMN是仅元数据操作。