添加沒有默認值:alter table Test add BazaarType char(1)
有默認值的添加列:alter table Test add BazaarType char(1) default(0)
刪除沒有默認值的列:alter table Test drop COLUMN BazaarType
刪除有默認值的列:先刪除約束(默認值)alter table Test DROP CONSTRAINT
DF__Test__BazaarType__3C4ACB5F,然後在刪除列
alter table Test DROP COLUMN
BazaarType
系統自帶的查詢約束條件的存儲過程:exec sp_helpconstraint 表名
添加字段:
允許空字符: alter table 表名 add 新字段 字段類型 NULL
不允許空字符: alter table 表名 add 新字段 字段類型 not NULL
例如:
alter table docdsp add dspcode char(200)
刪除字段 例如:
ALTER TABLE
table_NAME DROP COLUMN column_NAME
修改字段類型 例如
ALTER TABLE table_name ALTER
COLUMN column_name
new_data_type
改名
sp_rename
更改當前數據庫中用戶創建對象(如表、列或用戶定義數據類型)的名稱。
語法
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ]
'new_name'
[ , [ @objtype = ] 'object_type' ]
--假設要處理的表名爲: tb
--判斷要添加列的表中是否有主鍵
if exists(select 1 from sysobjects where
parent_obj=object_id('tb') and xtype='PK')
begin
print
'表中已經有主鍵,列只能做爲普通列添加'
--添加int類型的列,默認值爲0
alter table tb add 列名 int default 0
end
else
begin
print '表中無主鍵,添加主鍵列'
--添加int類型的列,默認值爲0
table tb add 列名 int primary key default 0
end