SQL 判斷要新增列的表中是否有主鍵

2021-04-15 13:57:29 字數 369 閱讀 2413

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

endelse

begin

print '表中無主鍵,新增主鍵列'

--新增int型別的列,預設值為0

alter table tb add 列名 int primary key default 0

end

判斷mysql中列是否要新增索引的標準

show index from table name 檢視該錶的索引資訊 table 索引所在表名 non unique 非唯一的索引,可以看到primary key是0,因為必須是唯一的 key name 索引的名字 seq in index 索引中該列的位置,針對聯合索引比較直觀 column ...

sql新增 刪除表中的列

新增沒有預設值 alter table test add bazaartype char 1 有預設值的新增列 alter table test add bazaartype char 1 default 0 刪除沒有預設值的列 alter table test drop column bazaar...

sql新增 刪除表中的列

新增沒有預設值 alter table test add bazaartype char 1 有預設值的新增列 alter table test add bazaartype char 1 default 0 刪除沒有預設值的列 alter table test drop column bazaar...