How to drop tables with no constraint checking - sql-server

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

Related

How to truncate all tables of a given database in SQL Server

I have to execure in test environment the truncate of all tables of three different test databases. I need sql query that considers the table constraints because all the codes that I tested on AdventureWorks database are raising the following error:
Cannot TRUNCATE TABLE 'Person.CountryRegion' because it is being referenced by object 'vStateProvinceCountryRegion'
You could try something like:
-- Disable all FK constraints...
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL";
GO
-- TRUNCATE TABLES...
-- Enable all FK constraints...
EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL";
GO

SQLServer Foreign Key Checks Disabled But Still Can't Drop Table

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.

how to disable constraint in sybase

What is the syntax to enable/disable the constraint? I tried google
in oracle I write it like this
alter table OPT DISABLE constraint FK_param
how to do that in sybase?
You simply can't disable foreign keys constraints in sybase. But you can drop and recreate
them.
Drop constraint:
alter table table_name drop constraint constraint_name
Add constraint:
alter table table_name add constraint constraint_name
I know that in other DB's, there is something like a CHECK and NOCHECK on these constraints. So you could just NOCHECK the constraint you want to disable.
ALTER TABLE a1 NOCHECK CONSTRAINT a1_TestConstraint
You can't disable constraints in sybase. You can drop/add constraints only.

How to recheck primary/foreign key constraint for data already in the table in sql server?

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".

Deleting a SQL row ignoring all foreign keys and constraints

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;

Resources