How to add a default value to an already existing column? - sql

I have an existing column in my SQL Server database. I have tried about everything I can think of but can not get a default value to be added to the column. What works in every other database is
alter table mytable
alter column mycolumn set default(now()) --mycolumn is a datetime
How do I do this in SQL Server?
The error I get for that exact syntax is incorrect syntax near the keyword 'set'

Use:
ALTER TABLE dbo.mytable
ADD CONSTRAINT def_mycolumn DEFAULT GETDATE() FOR mycolumn
For more info, see: Working with Default Constraints

If you want to change the default value of an already existing column. You need to first drop the constraint and then add the constraint all over again as below
ALTER TABLE <TABLE>
DROP CONSTRAINT <CONSTRAINT NAME>
ALTER TABLE <TABLE>
ADD CONSTRAINT <CONSTRAINT NAME> DEFAULT <VALUE> for <COLUMN>
If you are not having the Constraint details of the table, you can use the below query
sp_helpconstraint <TABLE>

Related

Altering constraint in Derby

I don't understand the logic in Derby syntax.
I'm new to SQL, I'm learning now.
I needed to alter a column constraint. I needed to add "not null".
Since I already did an alter on a column for the constraint unique, I thought the syntax will be the same since they are both constraints.
I finally found for not null this link Cannot alter column in existing table in java Derby database
and as I say the same syntax doesn't work for adding a unique constraint to the same column, so my question is: how is the syntax changed for each constraint?
This works:
ALTER TABLE WALLETUSER ALTER COLUMN WALLETUSERNAME NOT NULL;
So why does this not work?
ALTER TABLE WALLETUSER ALTER COLUMN WALLETUSERNAME unique;
And why does this work:
alter table customer add constraint cu1 unique (cust_name);
but this does not:
alter table customer add constraint not null (cust_name);

SQL - Add Unique Constraint Failure

Trying to alter a table in SQL Server. I want to add a unique constraint to a column called Names in table ReportingItemNames:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([ReportingItemNames,Name])
But I am getting this error:
Column name 'ReportingItemNames,Name' does not exist in the target table or view
Where am I going wrong?
Use this:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames UNIQUE ([Name])
You can refer to ALTER TABLE (Transact-SQL) documentation for more information.
Shouldn't it be:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([Name])

Help for solving drop column default value of a table error

I have a table and I want to drop or set the default value of one of columns. I use below scripts :
ALTER TABLE AccAccountGroup ALTER COLUMN Name DROP DEFAULT
ALTER TABLE AccAccountGroup ALTER COLUMN Name SET DEFAULT 'default value'
when I run the scripts, below errors appears :
Incorrect syntax near the keyword 'DEFAULT'. => for drop script
Incorrect syntax near the keyword 'SET'. => for add script
these scripts are from msdn.
What is the problem? I use SQL Server 2008 R2.
I believe that your reference is for SQL Server Compact Edition. Use this one instead.
You need to use the CONSTRAINT syntax and you need to use the default's name. Even though you didn't assign a name (and I suggest that you do in the future as it's a good practice), SQL Server will assign one, which you can find using EXEC sp_help AccAccountGroup.
Try these syntaxes:
ALTER TABLE AccAccountGroup
DROP CONSTRAINT <default name>
ALTER TABLE AccAccountGroup
ADD CONSTRAINT DF_AccAccountGroup_name DEFAULT 'default value' FOR name
The link you provided refers only to SQL Server Compact Edition, not the 'full' SQL Server. The above statements are only valid in SQL Server Compact Edition.
In the 'full' SQL Server, default values for columns are implemented using constraints. You can add a default constraint to a table using something like
ALTER TABLE AccAccountGroup ADD CONSTRAINT AccAcctGrp_Name_Def DEFAULT 'default value' FOR name;
and drop it using
ALTER TABLE AccAccountGroup DROP CONSTRAINT AccAcctGrp_Name_Def;
Note that you need to provide the name of the constraint when you drop it. If you don't know the name of the constraint, the following query will look it up. Change the schema name dbo, and also the table and column names, if/as necessary:
SELECT object_name(default_object_id)
FROM sys.columns
WHERE object_id = object_id('dbo.AccAccountGroup')
AND name = 'Name';
Once you've got the name of the constraint, you can drop it. (Acknowledgement: this query was taken from a comment posted here.)

Command for adding a default constraint

There appears to be at least two ways to add a default constraint using straight T-SQL. Am I correct that the only difference between the two below is that the second method specifically creates a name for the constraint, and the first method has one generated by SQL Server?
ALTER TABLE [Common].[PropertySetting] ADD DEFAULT ((1)) FOR [Active];
ALTER TABLE [Common].[PropertySetting] ADD CONSTRAINT [DF_PropertySetting_Active) DEFAULT ((1)) FOR [Active];
Pretty much, yes for an ALTER TABLE
You can add a columnn with default in one step for CREATE or ALTER too.
ALTER TABLE dbo.TableName
ADD bar varchar(100) CONSTRAINT DF_Foo_Bar DEFAULT ('bicycle');
ALTER TABLE dbo.TableName
ADD bar varchar(100) DEFAULT ('bicycle');
As you noted, the system generates a name if one is not supplied. CONSTRAINT constraint_name is optional says MSDN. The same applies to any column or table CONSTRAINT
If the column was already created, and you only want to add a (named) DEFAULT constraint, then use:
ALTER TABLE dbo.TableName
ADD CONSTRAINT DF_Foo_Bar DEFAULT 'bicycle' FOR FieldName;
To have the system generate the DEFAULT constraint name (which will be of the form DF_{TableName}_{Column}_{8RandomChars}, e.g. DF_TableName_FieldName_12345678) then omit the CONSTRAINT <name> part, like so:
ALTER TABLE dbo.TableName
ADD DEFAULT 'bicycle' FOR FieldName;

Removing a default value from a Sql Server 2005 table

I have a tabel in an Sql Server 2005 database. I have the following column
IndPL INT DEFAULT 0 NULL
I want to change the column to be of the type NVARCHAR but I receive a constraint violation due to the fact that the column has a default value constraint attached to it.
I need to find out how to remove a default value constraint from a table column or how to change the column type without impeding the constraint.
Added :
I need to do it by using T-SQL in a script that will later be executed on an another machine.
You need to remove the constraint first. In Management Studio open the table, open the Contraints and click on the contraint in question. Delete it. Change the data-type and then add any necessary constraints.
Script:
ALTER TABLE YourTable
DROP CONSTRAINT Your_Contraint
ALTER TABLE YourTable
ALTER COLUMN IndPL NVARCHAR(150)