Add Check constraints in SQL Server: ADD CONSTRAINT vs ADD CHECK - sql-server

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!

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.

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.

combining UNIQUE and EXCLUSION constraints in postgresql

I am real beginner when it comes to databases, so any advice is appriciated.
There must be a way to combine a UNIQUE constraint and a EXCLUSION constraint in PostgreSQL.
.
The one below written by me is incorrect. (Syntactically for sure, and possible logically, too)
I'm trying to achieve the following:
a single authenticator can belong to 1 useridx at a given time. (so a UNIQUE constraint is needed)
however the authenticator can belong to a different useridx at a different time (so some kind of EXCLUSION constraint is needed)
different time means outside of the interval of validform, and validuntil.
What I've tried:
ALTER TABLE authentication
ADD CONSTRAINT lenient_constraint UNIQUE (useridx, authenticator)
EXCLUDE USING gist(validfrom WITH =, validuntil WITH &&);
If you are asking if you can do it in a single alter table statement the answer is no. If you are asking if you can do it against the same table, the answer is yes. See below:
mydb# alter table test add unique(test);
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "test_test_key" for table "test"
ALTER TABLE
mydb# alter table test add exclude (test with =);
NOTICE: ALTER TABLE / ADD EXCLUDE will create implicit index "test_test_excl" for table "test"
ALTER TABLE

SQL Server check constraint

I have a table in my database and I did a: Script table as > Create to > query editor and some of my columns have this code:
ALTER TABLE [dbo].[TableName] WITH CHECK ADD CONSTRAINT [ConstraintName] FOREIGN KEY([ColumnName])
REFERENCES [dbo].[ForeignKeyTable] ([ColumnName])
GO
ALTER TABLE [dbo].[TableName] CHECK CONSTRAINT [ConstraintName2]
GO
What does the second constraint do? Other columns in the table only have the first constraint.
The second constraint turns on (activates) the check constraint. It is just template code from SSMS
It is possible to generate the create check constraints snippet with or without activating it (e.g. nocheck) - so that is itself one of 2 snippets. Because you opted for the constraints to end up activated, SSMS follows with the activation, not really caring if it was activated already or not. It doesn't really hurt and is just something SSMS does

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