I have a SQLServer database which I want to drop a table from. The table has FK constraints, but in this case it doesn't matter because when I repopulate the table, the FKs will be replaced correctly.
I've done EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all" which gives a message: sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINTS all" succeded, but when I try to drop my table I get the following SQL error: SQL Error: Could not drop object 'myTable' because ti is referenced by a FOREIGN KEY constraint.
Should the command not have affected my entire database and allowed me to drop the table without an issue? I've also tried doing EXEC sp_msforeachtable "ALTER TABLE myTable NOCHECK CONSTRAINT all" which results in the same error.
NOCheck disable the constraints so they won't be enforced. This would allow you to delete the data without violating the constraint.
Dropping the table would make the constraint definition invalid. You can't have a constraint that references a table that doesn't exist. You wouldn't be able to drop the referenced column from the table either while the constraint exists.
If you will be repopulating the table, just delete the table data and reload it. If you absolutely must drop and recreate the table, you'll need to include the drop & create statements for your foreign key constraints as well.
Note that if you disable the constraint, you'll need to enable using WITH CHECK CHECK (yes twice). The first check turns the constraint on for new data, the second tells SQL to validate the existing data. If you only do one, new data will be checked, but the existing data will not be 'TRUSTED', which can affect how SQL will leverage the FK reference in queries.
Should the command not have affected my entire database and allowed me
to drop the table without an issue?
No. It doesn't matter what the NOCHECK state is; if there are FK constraints that reference a target table, that target table cannot be dropped.
The only way to drop the table is to first drop the FK constraints that reference it.
Related
I have the following:
DELETE FROM ContactBase
DELETE FROM AccountBase
It errors with:
The DELETE statement conflicted with the REFERENCE constraint "account_primary_contact". The conflict occurred in database "BMBLANK_MSCRM", table "dbo.AccountBase", column 'PrimaryContactId'.
The DELETE statement conflicted with the REFERENCE constraint "account_contacts". The conflict occurred in database "BMBLANK_MSCRM", table "dbo.ContactBase", column 'AccountId'.
I understand because of the constraint I need to delete the data in a particular order, but if I reverse it:
DELETE FROM AccountBase
DELETE FROM ContactBase
It just reverses the error messages:
The DELETE statement conflicted with the REFERENCE constraint "account_contacts". The conflict occurred in database "BMBLANK_MSCRM", table "dbo.ContactBase", column 'AccountId'.
The DELETE statement conflicted with the REFERENCE constraint "account_primary_contact". The conflict occurred in database "BMBLANK_MSCRM", table "dbo.AccountBase", column 'PrimaryContactId'.
How do I empty these tables?
Thanks
First drop the constraints , delete the data and add them again:
ALTER TABLE AccountBase
DROP CONSTRAINT account_contacts;
ALTER TABLE ContactBase
DROP CONSTRAINT account_primary_contact;
DELETE FROM ContactBase;
DELETE FROM AccountBase;
ALTER TABLE AccountBase
ADD FOREIGN KEY (account_contacts)
REFERENCES ContactBase(PrimaryContactId);
ALTER TABLE ContactBase
ADD FOREIGN KEY (account_primary_contact)
REFERENCES AccountBase(AccountId);
Maybe I mixed them, its confusing without the tables DDL's , so if I did just adjust it.
You can generate a script of the constraints, drop the constraints, delete from tables, then use the script to recreate the constraints.
How to generate all constraints scripts
Drop the constraints and then delete the data from the tables. Then, if needed, add the constraints again.
I love foreign keys, but I'm running into one problem with them. I have a conversion program where I am disabling foreign keys to tables. The reason I'm doing this is so that I can reconvert all records in the main table, but leave the other tables dependent on that one untouched without having to reconvert them every time because they are HUGE.
I'm using these commands to disable and re-enable the foreign keys:
ALTER TABLE MyTable NOCHECK CONSTRAINT MyConstraint
ALTER TABLE MyTable CHECK CONSTRAINT MyConstraint
However, after I re-enable the constraint "Check Existing Data on Creation or Re-Enabling" is still set to No. I understand that it is set to No because I disabled the constraint, but by doing this it altered my database schema, which I don't like. I thought this would be considered re-enabling the constraint and would check the existing data, but apparently not.
Is there no way to change this with the ALTER TABLE command? I know I can if I drop the constraint and recreate it, but I'm not about to write the script to recreate every foreign key I have and maintain that.
I'm using SQL Server 2008 R2.
To re-enable a constraint:
-- Enable the constraint
ALTER TABLE MyTable
WITH CHECK CHECK CONSTRAINT MyConstraint
GO
Note: you have to specify CHECK twice to force a check that all foreign key values are valid.
FOREIGN KEY and CHECK constraints that are disabled are marked
is_not_trusted.These are viewable in the sys.check_constraints and
sys.foreign_keys catalog views. This means that the constraint is no
longer being verified by the system for all rows of the table. Even
when you re-enable the constraint, it will not reverify the existing
rows against the table unless you specify the WITH CHECK option of
ALTER TABLE. Specifying WITH CHECK marks the constraint as trusted
again.
Ref.: Guidelines for Disabling Indexes and Constraints
As noted in comments (for search engines), this corresponds to
sys.foreign_keys.is_not_trusted
in the catalog view
I have a table in SQL Server 2005 with a foreign key and it was disable for huge data loading, and then re-enabled:
Example:
alter table table1 nocheck constraint fk_1
go
lots of inserts...
go
alter table table1 check constraint fk_1
go
Now, the question: is there a way to re-check this just inserted data?
The syntax looks a little silly with the word "check" repeated, but what you want is:
alter table table1 with check check constraint fk_1
go
Adding the "with check" option will validate existing data against the constraint. Doing this will also prevent the constraint from becoming untrusted.
If any existing data violates the constraint, you'll get an error that will look like this:
The ALTER TABLE statement conflicted with the CHECK constraint "fk_1".
I need to update the data and schema from dev to staging dbs where I want to DROP/CREATE a group of tables. I need to over-ride the FK constraint checks. Looking at the MS's ALTER TABLE syntax tree - i know it's there but i can't identify the correct syntax.
#Rup: It looks like the hangup is from other tables' FKs. Is there a way to turn all constraint checking off or do i need to produce the list of tables/FKs?
ALTER TABLE yourtable
NOCHECK CONSTRAINT ALL
and a variation on this theme is to disable all the constraints of all the tables delete all the data, and then add all the constraints back again.
exec sp_MSforeachtable #command1='alter table ? nocheck constraint all', #whereand='and substring(o.name,1,1) <> ''_'''
exec sp_MSforeachtable #command1='delete from ?'
exec sp_MSforeachtable #command1='alter table ? check constraint all', #whereand='and substring(o.name,1,1)<> ''_'''
The nice thing here is that with all the constraints disabled, you can delete the data in any order
You can drop the constraints, drop the tables and re-create. It should also be possible to drop the tables in the correct order and re-create if there are no circular constraints.
ALTER TABLE DROP CONSTRAINT
I have a row in a table. This row has an ID column referenced in a few other tables with millions of rows. The SQL statement to delete the row always times out. From my design, I know the row I wish to delete is never referenced any where else. Hence I would like SQL to ignore having to check all other tables for a foreign key reference to this row and delete the row immediately. Is there a quick way to do this in SQL 2008?
Perhaps something along the lines of:
DELETE FROM myTable where myTable.ID = 6850 IGNORE CONSTRAINTS
Or something along those lines.
You can set the constraints on that table / column to not check temporarily, then re-enable the constraints. General form would be:
ALTER TABLE TableName NOCHECK CONSTRAINT ConstraintName
Then re-enable all constraints with
ALTER TABLE TableName CHECK CONSTRAINT ConstraintName
I assume that this would be temporary though? You obviously wouldn't want to do this consistently.
Yes, simply run
DELETE FROM myTable where myTable.ID = 6850
AND LET ENGINE VERIFY THE CONSTRAINTS.
If you're trying to be 'clever' and disable constraints, you'll pay a huge price: enabling back the constraints has to verify every row instead of the one you just deleted. There are internal flags SQL keeps to know that a constraint is 'trusted' or not. You're 'optimization' would result in either changing these flags to 'false' (meaning SQL no longer trusts the constraints) or it has to re-verify them from scratch.
See Guidelines for Disabling Indexes and Constraints and Non-trusted constraints and performance.
Unless you did some solid measurements that demonstrated that the constraint verification of the DELETE operation are a performance bottleneck, let the engine do its work.
Do not under any circumstances disable the constraints. This is an extremely stupid practice. You cannot maintain data integrity if you do things like this. Data integrity is the first consideration of a database because without it, you have nothing.
The correct method is to delete from the child tables before trying to delete the parent record. You are probably timing out because you have set up cascading deltes which is another bad practice in a large database.
I know this is an old thread, but I landed here when my row deletes were blocked by foreign key constraints. In my case, my table design permitted "NULL" values in the constrained column. In the rows to be deleted, I changed the constrained column value to "NULL" (which does not violate the Foreign Key Constraint) and then deleted all the rows.
I wanted to delete all records from both tables because it was all test data. I used SSMS GUI to temporarily disable a FK constraint, then I ran a DELETE query on both tables, and finally I re-enabled the FK constraint.
To disable the FK constraint:
expand the database object [1]
expand the dependant table object [2]
expand the 'Keys' folder
right click on the foreign key
choose the 'Modify' option
change the 'Enforce Foreign Key Constraint' option to 'No'
close the 'Foreign Key Relationships' window
close the table designer tab
when prompted confirm save changes
run necessary delete queries
re-enable foreign key constraint the same way you just disabled it.
[1] in the 'Object Explorer' pane, can be accessed via the 'View' menu option, or key F8
[2] if you're not sure which table is the dependant one, you can check by right clicking the table in question and selecting the 'View Dependencies' option.
This is the way to disable foreign key checks in MySQL. Not relevant to OP's question since they use MS SQL Server, but google search results do turn this up so here's for reference:
SET FOREIGN_KEY_CHECKS = 0;
/ Run your script /
SET FOREIGN_KEY_CHECKS = 1;
See if this helps, This is for ignoring the foreign key checks.
But deleting disabling this is very bad practice.
On all tables with foreign keys pointing to this one, use:
ALTER TABLE MyOtherTable NOCHECK CONSTRAINT fk_name
You can disable all of the constaints on your database by the following line of code:
EXEC sp_MSforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
and after the runing your update/delete command, you can enable it again as the following:
EXEC sp_MSforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
You could maybe disable and re-enable constraints:
http://sqlforums.windowsitpro.com/web/forum/messageview.aspx?catid=60&threadid=48410&enterthread=y
For the testing purpose only, I used the following command in MySQL to delete only one record from a table that has foreign key references.
SET FOREIGN_KEY_CHECKS = 0; -- Disabling foreign key checks before running the following query.
DELETE FROM table_name WHERE id = id_to_delete; -- Deleting a record from a table that has foreign key reference.
SET FOREIGN_KEY_CHECKS = 1; -- Enabling foreign key checks after running the above query.
Temporarily disable constraints on a table T-SQL, SQL Server
MSSQL
ALTER TABLE TableName NOCHECK CONSTRAINT ALL
ALTER TABLE TableName CHECK CONSTRAINT ALL
ALTER TABLE TableName NOCHECK CONSTRAINT FK_Table_RefTable
ALTER TABLE TableName CHECK CONSTRAINT FK_Table_RefTable
ref
DELETE FROM TableName
DBCC CHECKIDENT ('TableName', RESEED, 0)
MySql
SET FOREIGN_KEY_CHECKS = 0; -- Disable foreign key checking.
TRUNCATE TABLE [YOUR TABLE];
SET FOREIGN_KEY_CHECKS = 1;