首页 > 代码库 > sql 修改字段默认值

sql 修改字段默认值

1、查出该字段的约束名称

SELECT c.name FROM sysconstraints a 
INNER JOIN syscolumns b on a.colid=b.colid 
INNER JOIN sysobjects c on a.constid=c.id 
WHERE a.id=object_id(TClass) 
AND b.name=PhoneIsOpen 

技术分享

2、删除该约束

ALTER TABLE TClass DROP CONSTRAINT DF__TClass__PhoneIsO__51BA1E3A

3、修改默认值约束

ALTER TABLE TClass ADD CONSTRAINT DF__TClass__PhoneIsO__51BA1E3A DEFAULT(1) FOR PhoneIsOpen

4、获取表列的默认值

SELECT SO.NAME AS "Table Name", SC.NAME AS "Column Name", SM.TEXT AS "Default Value"
FROM dbo.sysobjects SO INNER JOIN dbo.syscolumns SC ON SO.id = SC.id
LEFT JOIN dbo.syscomments SM ON SC.cdefault = SM.id
WHERE SO.xtype = U and so.name=TClass
ORDER BY SO.[name], SC.colid

技术分享

 

sql 修改字段默认值