trying to create a column but it also creates a default constraint with useless name - sql-server

I am using the alter command to add a column with not null and default value to the sql,
but when i run it it also creates the constraint with some silly words, how can i use the alter command to add the column also and also add the constraint with it with the name i specify
my alter is like this
ALTER TABLE users
ADD role bit not null default 0;
Should the combined constraint with same alter command will work, not sure how to add it

You can include the constraint name within the statement as follows:
ALTER TABLE users ADD role bit NOT NULL CONSTRAINT DF_Users__Role DEFAULT (0);
There are examples in the docs online, albeit a long way down the page: B. Adding a column with a constraint

Related

What is the use of generated CHECK CONSTRAINT sql

In SSMS, if I open a table with constraints, right click on one and select "Script Constraint As" > "CREATE TO", these are the lines that are generated automatically:
ALTER TABLE [dbo].[table] DROP CONSTRAINT [CK_table_col]
GO
ALTER TABLE [dbo].[table] WITH CHECK ADD CONSTRAINT [CK_table_col] CHECK (([col] IN('SUP', 'ADM')))
GO
ALTER TABLE [dbo].[table] CHECK CONSTRAINT [CK_table_col]
GO
I get what the first two lines do, that's not a problem. However, the CHECK CONSTRAINT at the end seems unclear. If I remove it, it still checks that the existing records match the new constraint. If I run the CHECK CONSTRAINT on it's own, it just tells me that the query succeeded... Why is it needed? What does it do?
A CONSTRAINT can be disabled, using NOCHECK. This cannot be done when you CREATE the CONSTRAINT, but has to be done explicitly afterwards, such as with the following:
ALTER TABLE dbo.MyTable NOCHECK CONSTRAINT chk_MyTable;
If you disable a constraint and then use the CREATE TO script option, then that would be reflected too. As such SSMS explicitly defines whether the constraint is to be checked or not afterwards. Although yes enforcing the CHECK is redundant, it creates consistent scripts.

Add Check constraints in SQL Server: ADD CONSTRAINT vs ADD CHECK

I want use a check constraint for a column in any table in SQL Server 2008.
I would like give a qualified name to the check constraint.
I have seen several syntax version on how to create it:
ALTER TABLE [dbo].[Roles2016.UsersCRM] WITH CHECK
ADD CHECK (([Estado]=(4) OR [Estado]=(3) OR [Estado]=(2) OR [Estado]=(1)))
ALTER TABLE [dbo].[Roles2016.UsersCRM] WITH CHECK
ADD CONSTRAINT [CK_UsuariosCRM_Estado]
CHECK (([Estado]=(4) OR [Estado]=(3) OR [Estado]=(2) OR [Estado]=(1)))
What's difference ADD CHECK and ADD CONSTRAINT for a check constraint?
It is possible, but a very bad habit to add constraints without a name:
CREATE TABLE tbl(SomeColumn VARCHAR(10) DEFAULT('test'))
will create a CONSTRAINT with a random name. Better use this
CREATE TABLE tbl(SomeColumn VARCHAR(10) CONSTRAINT DF_YourTable_SomeColumm DEFAULT('test'))
This will do the same, but will name the constraint like you want it.
This is extremely important if you run upgrade scripts in deployed environments. Just imagine, you want to change a constraint later and the name of this constraint is all different on your customers machines... That's a real pain!
So: Always name your constraints!

Drop a Unique Constraint

I need to drop a Unique constraint on a previously existing table and create a new unique constraint that will include extra column. Can I use the name of the constraint to drop it? or will the name of the unique constraint change based on Datasource. I need to execute the script on multiple instances of same DB (eg. dev,test,prod)
Yes, you can use the name of the constraint, provided that you previously deleted and it was a named contraint.

Add a default constraint to an existing field with values

I'm trying to add a default constraint to an existing field in SQL Server using the following T-SQL:
alter table Extra add constraint DF_Extra_ExternalRef DEFAULT ('*') for
ExternalRef with values
This adds the default constraint but fails to update existing records with null values.
I'm using SQL Server 2005.
I've only ever seen WITH VALUES used this way when adding a new column (and this is all that is documented). If you're adding a constraint to an existing column, I think WITH VALUES is a no-op. Therefore:
ALTER TABLE dbo.Extra ADD CONSTRAINT DF_Extra_ExternalRef
DEFAULT ('*') FOR ExternalRef;
UPDATE dbo.Extra SET ExternalRef = '*' WHERE ExternalRef IS NULL;

SQL Server - How to change table column only having some specific values?

TABLE Family(
BrothersName varchar(30)
);
I have added some names (values) into BrothersName, but now I want it to only have 2 specific names 'Alex' and 'Tom'. However later it should also accept other names. What is the best way to handle this problem?
Add a check constraint stating BrothersName should accept only 'Alex' and 'Tom'. In future when you don't need it you can drop the constraint
ALTER TABLE Family
ADD CONSTRAINT chk_BrothersName CHECK (BrothersName in ('Alex','Tom'))
To Drop the Check Constraint
ALTER TABLE Family
DROP CONSTRAINT chk_BrothersName

Resources