更改 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
Altering column size in SQL Server
提问by Sree
How to change the column size of the salary
column in the employee
table from numeric(18,0)
to numeric(22,5)
如何salary
将employee
表中列的列大小从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 COLUMN
without mentioning attribute NOT NULL
will 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
回答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 ROW
level, then ALTER TABLE ALTER COLUMN
is metadata only operation.
当表在ROW
级别上压缩时,则ALTER TABLE ALTER COLUMN
是仅元数据操作。