SQL语句增加列、修改列、删除列

SQL语句增加列、修改列、删除列 

1.增加列: 
1)alter table tableName add columnName varchar(30)  
2)ALTER TABLE dbo.doc_exa ADD column_b VARCHAR(20) NULL, column_c INT NULL ;

2. 修改列类型: 
1)alter table tableName alter column columnName varchar(4000)
3.修改列的名称: 语法:sp_rename [ @objname = ]'object_name' , [ @newname = ] 'new_name' [ , [ @objtype =] 'object_type' ] 如:EXEC sp_rename 'oldname','PartStock' 1)EXEC sp_rename 'tableName.column1' , 'column2' (把表名为tableName的column1列名修改为column2) 4.删除列:
1)alter table tableName drop column columnName
2)alter table tableName drop column columnName1,columnName2
5.指定表中某列默认数据
1)alter table dbo.doc_exz add constraint col_b_def default 50 for column_b

下面的示例将 TerritoryID 表中的 Sales.SalesTerritory 列重命名为 TerrID。 将以下示例复制并粘贴到查询窗口中,然后单击“执行”

USE AdventureWorks2012;  
GO  
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';  
GO 

 

出处:https://www.cnblogs.com/net-sky/p/11076256.html

https://blog.csdn.net/suntanyong88/article/details/6546913

=======================================================================================

SQL代码示例
use asm    
——–删除字段    
if exists (select * from syscolumns where id =object_id(‘tb_log’) and name=‘COLUMN1′)    
  ALTER TABLE [tb_log ] DROP COLUMN [COLUMN1]    
   
if exists (select * from syscolumns where id =object_id(‘tb_log’) and name=‘COLUMN2′)    
  ALTER TABLE [tb_log ] DROP COLUMN [COLUMN2]   
   
———添加字段    
alter   table   tb_log       
  add    COLUMN5   nvarchar(200), COLUMN6  int   

 

出处:https://database.51cto.com/art/201009/223863.htm

posted on 2021-04-14 16:24  jack_Meng  阅读(20773)  评论(0编辑  收藏  举报

导航