如何将employee
表中的salary
列从numeric(18,0)
更改为numeric(22,5)
如何将employee
表中的salary
列从numeric(18,0)
更改为numeric(22,5)
ALTER TABLE [Employee]
ALTER COLUMN [Salary] NUMERIC(22,5) NOT NULL
ALTER TABLE [table_name] ALTER COLUMN [column_name] varchar(150)
alter table Employee alter column salary numeric(22,5)
如果在运行 ALTER COLUMN
时没有提及属性 NOT NULL
,则会导致该列变为可空列(如果它尚未为空)。因此,您需要首先检查列是否可为空,如果不是,则指定属性 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
这里可以找到有趣的方法:如何在不停机的情况下扩大列(How To Enlarge Your Columns With No Downtime),作者为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.
当表在ROW
级别上被压缩时,ALTER TABLE ALTER COLUMN
是仅元数据操作。
ALTER
命令来修改表结构。ALTER table table_name modify COLUMN column_name varchar (size);
修改表 "Employee" 中字段 "Salary" 的数据类型为 NUMERIC(22,5)。
针对Oracle数据库:
ALTER TABLE table_name MODIFY column_name VARCHAR2(255 CHAR);
NOT NULL
属性,那么在查询中必须明确指出,否则默认会将其设置为NULL
。翻译的SQL语句为:ALTER TABLE [Employee] ALTER COLUMN [Salary] NUMERIC(22,5) NOT NULL
- Suvendu Shekhar Giri