is there any difference between the follow exemples?
ProjNum int
CONSTRAINT nn_ProjNum NOT NULL
CONSTRAINT C_ProjNum CHECK (ProjNum >= 10)
and
ProjNum int NOT NULL
CHECK (ProjNum >= 10)
Thanks.
is there any difference between the follow exemples?
ProjNum int
CONSTRAINT nn_ProjNum NOT NULL
CONSTRAINT C_ProjNum CHECK (ProjNum >= 10)
and
ProjNum int NOT NULL
CHECK (ProjNum >= 10)
Thanks.
There is no difference in SQL Server between those two statements. Each results in NOT NULL column (not a constraint) with a single check constraint. The only difference is that second one creates a system-named check constraint, something like 'CK__t__ProjNum__4AB81AF0'.
CONSTRAINT name NOT NULL syntax, though (for compatibility with other products, perhaps). Of course, given that NOT NULL is a column property rather than a constraint in SQL Server, the name is just ignored.
– Andriy M
May 29 '18 at 09:19
Both are same.
In my understanding ,CONSTRAINT CHECK can be define when you add new columns.
ALTER TABLE dbo.DocExc
ADD ColumnD int NULL
CONSTRAINT CHK_ColumnD_DocExc
CHECK (ColumnD > 10 AND ColumnD < 50);
GO
Also user can provide own constraint name.
But if we have to put check in existing column then
ALTER TABLE dbo.DocExc
ADD CHECK (ExistsColumn > 10 AND ExistsColumn < 50);
GO
system will provide constraint name here.
'CK__t__ProjNum__4AB81AF0is likely to be waiting a while ("How many underscores... err... spell that again please...") whereas with an error like "stock_gt_zero_ck" is much more likely to get a fast (and correct) response! Ahh... the good old days with Oracle system named constraints... <nostalgic tear runs down cheek... :-) ). – Vérace May 25 '18 at 05:09