开发者

Alter column's default value

开发者 https://www.devze.com 2022-12-13 22:56 出处:网络
Ihave a table which has a column \'CompanyID int not null\' and its default开发者_StackOverflow value is set to 10. Now I want to write a query which will alter this default value to 1. How can can I

I have a table which has a column 'CompanyID int not null' and its default开发者_StackOverflow value is set to 10. Now I want to write a query which will alter this default value to 1. How can can I do it?

Any help will be appreciated. I am using SQL server 2000.


I think the best you can do is drop the constraint and create it again:

alter table dbo.yourTable
drop constraint default_value_name_constraint
go
alter table dbo.yourTable
add constraint default_value_name_constraint default YourValue for ColumnName
go


First, find out the name of the 'constraint' on the field which is used to set the default. You can do this by running this query:

EXEC sp_helpconstraint 'MyTable'

Then, you can just drop and re-add the constraint.

ALTER TABLE dbo.MyTable
DROP CONSTRAINT def_MyTable_CompanyID
GO

ALTER TABLE dbo.MyTable
ADD CONSTRAINT def_MyTable_CompanyID DEFAULT (1) FOR CompanyID
GO


ALTER TABLE tablename ALTER COLUMN CompanyID SET DEFAULT 1; 


ALTER TABLE tablename ALTER COLUMN CompanyID SET DEFAULT Pending;

I have tried this in mysql and it was giving error near Pending

Then I tried

 ALTER TABLE tablename ALTER COLUMN CompanyID SET DEFAULT 'Pending';

which worked fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消