Disables a foreign key constraint and defers constraints during replication. Constraints can be deferred without being dropped.
For additional syntax information for the ALTER TABLE statement, see the Microsoft SQL Server Transact-SQL Reference.
ALTER TABLE [database.[owner.]]table_name
[WITH {CHECK | NOCHECK}]
{{CHECK | NOCHECK} CONSTRAINT {constraint_name | ALL}
|
[ADD
{col_name column_properties [column_constraints]
| [[, ] table_constraint]}
[, {next_col_name | next_table_constraint}]...]
|
[DROP CONSTRAINT]
constraint_name [, constraint_name2]...]}
where
You can defer constraints for the table, including CHECK and FOREIGN KEY constraints, by using the ALL keyword. However, you cannot defer primary and unique key constraints; these constraints must be dropped.
Important Deferring constraints on a table does not defer constraints on other tables that refer to the altered tables. Updates to the table can still raise constraint violation errors.
For a table-level constraint:
[CONSTRAINT constraint_name]
{ PRIMARY KEY [CLUSTERED | NONCLUSTERED]
(col_name [, col_name2[..., col_name16]])
| UNIQUE [CLUSTERED | NONCLUSTERED]
(col_name [, col_name2[..., col_name16]])
[WITH FILLFACTOR = fillfactor]
| FOREIGN KEY (col_name[, col_name2[..., col_name16]])
REFERENCES [owner.] ref_table (ref_col[, ref_col2[..., ref_col16]])
[NOT FOR REPLICATION]
| CHECK [NOT FOR REPLICATION] (expression)}
When the data satisfies the checks, the SORTED_DATA option will always be faster than the SORTED_DATA_REORG because the data is not copied and nonclustered indexes will not be rebuilt.
SORTED_DATA_REORG differs from SORTED_DATA because it physically reorganizes the data. This option is useful when a FILLFACTOR is specified to compact or expand the pages on which a table is stored. The effects of these options change slightly if used with the ON segment_name option.
Reorganizing the data is a good idea when a table becomes fragmented. To determine whether or not a table is contiguous, use the DBCC statement's SHOW_CONTIG. For details about what causes table fragmentation and recommended solutions, see the DBCC statement.
For a column-level constraint:
[CONSTRAINT constraint_name]
{ PRIMARY KEY [CLUSTERED | NONCLUSTERED] [(col_name)]
| UNIQUE [CLUSTERED | NONCLUSTERED] [(col_name)]
[WITH FILLFACTOR = fillfactor]
| FOREIGN KEY [(col_name)]
REFERENCES [(ref_col)]
[NOT FOR REPLICATION]
| CHECK [NOT FOR REPLICATION] (expression)}
The ALTER TABLE NOCHECK statement allows replication to take place without permanently removing referential integrity constraints. Replication needs deferred constraints because constraints are sometimes violated by modifying a database with INSERT and DELETE statements. Use the ALTER TABLE statement to temporarily suspend constraints checking.
Two conditions cause an error to be returned: when ALL is specified and no constraints exist and when the specified constraint does not exist.
Once all data modifications are made, you can use the ALTER TABLE statement to reestablish constraint checking.
For more information about foreign keys or the ALTER TABLE statement, see the Microsoft SQL Server Transact-SQL Reference.
This example reviews adding a constraint. The foreign key constraint pub_id is added to the titles table.
ALTER TABLE titles ADD CONSTRAINT FK_pub_id FOREIGN KEY (pub_id) REFERENCES publishers(pub_id)
This example disables the ssn_constraint in the authors table.
ALTER TABLE authors NOCHECK CONSTRAINT ssn_constraint
This example enables the ssn_constraint in the authors table.
ALTER TABLE authors CHECK CONSTRAINT ssn_constraint
Only the system administrator, the database owner, and the table owner can use the ALTER TABLE statement.