patternsqlCritical
What is a WITH CHECK CHECK CONSTRAINT?
Viewed 0 times
withcheckwhatconstraint
Problem
I have some auto-generated T-SQL, which is probably valid, but I don't really understand.
I know what a foreign key constraint is, but what's the
ALTER TABLE [dbo].[MyTable]
WITH CHECK
CHECK CONSTRAINT [My_FORIEGN_KEY];I know what a foreign key constraint is, but what's the
CHECK CHECK?Solution
The MSDN documentattion page about
(and some of the possible actions/modifications are):
There are also additional, optional steps to do while creating/enabling/disabling a constraint:
In their words:
...
Specifies whether the data in the table is or is not validated against a newly added or re-enabled
If you do not want to verify new
The query optimizer does not consider constraints that are defined
...
Specifies that constraint_name is enabled or disabled. This option can only be used with
Test in dbfiddle:
GO
✓
GO
3 rows affected
GO
✓
GO
4 rows affected
GO
Msg 547 Level 16 State 0 Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "My_FORIEGN_KEY". The conflict occurred in database "fiddle_792fce5de09f42908c3a0f91421f3522", table "dbo.a", column 'aid'.
Msg 3621 Level 0 State 0 Line 1
The statement has been terminated.
GO
aid | bid
--: | --:
1 | 11
1 | 12
2 | 21
3 | 31
GO
✓
GO
2 rows affected
GO
aid | bid
--: | --:
1 | 11
1 | 12
2 | 21
3 | 31
4 | 41
4 | 42
GO
✓
GO
aid | bid
--: | --:
1 | 11
1 | 12
2 | 21
3 | 31
4 | 41
4 | 42
GO
Msg 547 Level 16 State 0 Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "My_FORIEGN_KEY". The conflict occurred in database "fiddle_792fce5de09f42908c3a0f91421f3522", table "dbo.a", column 'aid'.
Msg 3621 Level 0 State 0 Line 1
The statement has been terminated.
GO
aid | bid
--: | --:
1 | 11
1 | 12
2 | 21
3 | 31
4 | 41
4 | 42
GO
Msg 547 Level 16 State 0 Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "My_FORIEGN_KEY". The conflict occurred in database "fiddle_792fce5de09f42908c3a0f91421f3522", table "dbo.a", column 'aid'.
ALTER TABLE explains these:ALTER TABLE: modify the table's structure
(and some of the possible actions/modifications are):
CHECK CONSTRAINT ..: enable the constraint
NOCHECK CONSTRAINT ..: disable the constraint
There are also additional, optional steps to do while creating/enabling/disabling a constraint:
WITH CHECK: check the constraint as well
WITH NOCHECK: do not check the constraint
In their words:
| [ WITH { CHECK | NOCHECK } ] { CHECK | NOCHECK } CONSTRAINT
{ ALL | constraint_name [ ,...n ] }...
WITH CHECK | WITH NOCHECKSpecifies whether the data in the table is or is not validated against a newly added or re-enabled
FOREIGN KEY or CHECK constraint. If not specified, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.If you do not want to verify new
CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We do not recommend doing this, except in rare cases. The new constraint will be evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that does not comply with the constraint.The query optimizer does not consider constraints that are defined
WITH NOCHECK. Such constraints are ignored until they are re-enabled by using ALTER TABLE table WITH CHECK CHECK CONSTRAINT ALL....
{ CHECK | NOCHECK } CONSTRAINTSpecifies that constraint_name is enabled or disabled. This option can only be used with
FOREIGN KEY and CHECK constraints. When NOCHECK is specified, the constraint is disabled and future inserts or updates to the column are not validated against the constraint conditions. DEFAULT, PRIMARY KEY, and UNIQUE constraints cannot be disabled.Test in dbfiddle:
CREATE TABLE a (aid INT PRIMARY KEY);GO
✓
INSERT INTO a (aid)
VALUES (1), (2), (3) ;GO
3 rows affected
CREATE TABLE b
( aid INT,
bid INT PRIMARY KEY,
CONSTRAINT [My_FORIEGN_KEY]
FOREIGN KEY (aid) REFERENCES a (aid)
) ;GO
✓
INSERT INTO b (aid, bid)
VALUES
(1, 11),
(1, 12),
(2, 21),
(3, 31) ;GO
4 rows affected
INSERT INTO b (aid, bid)
VALUES
(6, 61),
(6, 62) ;GO
Msg 547 Level 16 State 0 Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "My_FORIEGN_KEY". The conflict occurred in database "fiddle_792fce5de09f42908c3a0f91421f3522", table "dbo.a", column 'aid'.
Msg 3621 Level 0 State 0 Line 1
The statement has been terminated.
SELECT * FROM b ;GO
aid | bid
--: | --:
1 | 11
1 | 12
2 | 21
3 | 31
ALTER TABLE b NOCHECK CONSTRAINT [My_FORIEGN_KEY]; --disableGO
✓
INSERT INTO b (aid, bid)
VALUES
(4, 41),
(4, 42) ;GO
2 rows affected
SELECT * FROM b ;GO
aid | bid
--: | --:
1 | 11
1 | 12
2 | 21
3 | 31
4 | 41
4 | 42
ALTER TABLE b WITH NOCHECK CHECK CONSTRAINT [My_FORIEGN_KEY];
-- enable constraint without checking existing dataGO
✓
SELECT * FROM b ;GO
aid | bid
--: | --:
1 | 11
1 | 12
2 | 21
3 | 31
4 | 41
4 | 42
INSERT INTO b (aid, bid)
VALUES
(6, 61),
(6, 62) ;GO
Msg 547 Level 16 State 0 Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "My_FORIEGN_KEY". The conflict occurred in database "fiddle_792fce5de09f42908c3a0f91421f3522", table "dbo.a", column 'aid'.
Msg 3621 Level 0 State 0 Line 1
The statement has been terminated.
SELECT * FROM b ;GO
aid | bid
--: | --:
1 | 11
1 | 12
2 | 21
3 | 31
4 | 41
4 | 42
ALTER TABLE b WITH CHECK CHECK CONSTRAINT [My_FORIEGN_KEY];
-- check existing data and enable constraintGO
Msg 547 Level 16 State 0 Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "My_FORIEGN_KEY". The conflict occurred in database "fiddle_792fce5de09f42908c3a0f91421f3522", table "dbo.a", column 'aid'.
Code Snippets
| [ WITH { CHECK | NOCHECK } ] { CHECK | NOCHECK } CONSTRAINT
{ ALL | constraint_name [ ,...n ] }CREATE TABLE a (aid INT PRIMARY KEY);INSERT INTO a (aid)
VALUES (1), (2), (3) ;CREATE TABLE b
( aid INT,
bid INT PRIMARY KEY,
CONSTRAINT [My_FORIEGN_KEY]
FOREIGN KEY (aid) REFERENCES a (aid)
) ;INSERT INTO b (aid, bid)
VALUES
(1, 11),
(1, 12),
(2, 21),
(3, 31) ;Context
StackExchange Database Administrators Q#167861, answer score: 54
Revisions (0)
No revisions yet.