I am about to run a massive data insert into my DB. I have managed to work out how to enable and rebuild non-clustered indexes on my tables but I also want to disable/enable primary keys as I believe this will speed up the insertion process.
NOTE: This is over numerous tables and so I assume I need some loop to get the primary key information and run the following to drop it but I'm not sure about recreating it:
ALTER TABLE Table1
DROP CONSTRAINT PK_Table1_Col1
IIRC clustered indexes cannot be disabled as they govern where the actual data is stored in the pages.
I'm pretty sure you would have to drop the key and re-create it after your insert. Depending on the size of the tables, indexes and insert this may not save you any time.
Run This :
//For disable all constraint of your all tables
exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
//Your insert query here ......................
//After Insert Enable all the constraint
exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
Related
I have a table with some bad data in SQL. this table has a lot of relationship with other tables and other tables have a lot of data so when I want to delete bad data it's very slowly and take lots of time to do. I think the cause of this problem is foreign key constraints. the main problem is how can disable all of the foreign key constraints from one table.
You have to disable check constraint:
SET FOREIGN_KEY_CHECKS=0;
Make sure to turn it back after your commands:
SET FOREIGN_KEY_CHECKS=1;
Disable constraint for one table:
alter table
table_name
DISABLE constraint
constraint_name;
Here is an example:
select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints;
In my table I have primary key on 3 columns (name, dept, MobNo).
Now I want to change it to be on two columns (Name, MobNo).
Is there any way I can alter the primary key constraint without dropping it?
I know I can drop old constraint and can create new but without dropping old constraint it is possible to alter it?
The only and one way would be to drop the constraint with an Alter table then recreate it.
ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>
ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (<Column1>,<Column2>)
If you (probably) have dependencies on that PK, you will also have to drop them and recreate them. To have all this done automagically, it's easier from SSMS to right-click on the table, choose Design, and from there you click in the top left toolbar the button called Manage indexes and Keys. From there you make your changes, and at the end, you have 2 options:
you close and save your changes (and it works fine but you don't learn anything)
instead you click on the Generate change script so you can examine and execute the script later
(at least it works like this with my version 2014 of SSMS)
When I have issued a
ALTER TABLE table DISABLE CONSTRAINT fk1
and when I then try to drop the table
DROP TABLE table
That the constraint is still checked even though it is disabled.
Have I missed something?
You have to drop the constraints as well in order drop a table. Try the following:
DROP TABLE someTable CASCADE CONSTRAINTS;
DISABLE CONSTRAINT works for update/insert statements.
See oracle help.
Disabling Constraints
To enforce the rules defined by integrity constraints, the constraints
should always be enabled. However, consider temporarily disabling the
integrity constraints of a table for the following performance
reasons:
When loading large amounts of data into a table
When performing batch operations that make massive changes to a table
(for example, changing every employee's number by adding 1000 to the
existing number)
When importing or exporting one table at a time
You are trying to drop your table. It is not designed to for this. You need to DROP CONSTRAINTs.
I'd like to clear the database altogether and reset the data. What's the quickest way to do that? Or, what's the command that will delete all the rows of a table (and I'll repeat that process for all tables)?
Thanks
This approach will enable you to delete content from all tables, even those referenced by a foreign key constraint. You can enhance it to make it check for the absence of foreign key constraints and do a TRUNCATE TABLE in those cases.
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_msforeachtable 'DELETE FROM ?'
EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL'
If you don't want to script and drop the tables, there are a number of ways to do this with a loop. Here's probably the easiest:
sp_MsForEachTable 'TRUNCATE TABLE ?'
Drop the database and recreate it.
Use SQL Server Management Studio to script the DROP and CREATE statements for the tables and then run the script. Right click on the database and select Tasks --> Generate Scripts. Run through the wizard (make sure to check the Script Drop option on the "Choose Script Options" step) and select all of the tables. The script that is generated should drop all the tables and then recreate them.
Is there a way to truncate tables with constraints ?
I tried to DEACTIvATE with this:
DECLARE #SQLtxt varchar(max)
SET #SQLtxt = '-- DESACTIVER LES CONTRAINTES' + CHAR(10)
SELECT #SQLtxt = #SQLtxt + 'ALTER TABLE [' + name + '] NOCHECK CONSTRAINT ALL;' FROM sys.tables
PRINT #SQLtxt
Of course, it didn't worked. I have to drop the constraint then recreate them ! The only way I could make it work is by extracting the script to drop and recreate the contraint.
Is there another way ? BTW I don't want to delete because it would use the Transaction Log.
Here is a script that may help you get going scripting out FK's. Script out your foreign keys.
I use a modified version to dump the constraint definitions into a temp table, then do the TRUNCATE magic and then recreate the constraints from the temp table. However, this is only for my own convenience when restoring the production database onto a non-production environment to get rid of most of the data. Not sure, I would use it in a production scenario though. I would rather prefer deleting in small batches knowing that everything is fully logged.
Btw, womb's reference to the SQL Server 2000 Books Online is a bit misleading. TRUNCATE TABLE has always been a minimally logged operation.
TRUNCATE TABLE removes the data by deallocating the data pages used to store the table's data, and only the page deallocations are recorded in the transaction log.
This is has specified more precisely in later versions of Books Online.
The TRUNCATE command will not work on tables with FK references. Straight from the documentation:
You cannot use TRUNCATE TABLE on a
table referenced by a FOREIGN KEY
constraint; instead, use DELETE
statement without a WHERE clause.
Because TRUNCATE TABLE is not logged,
it cannot activate a trigger.
You sort of answered the question yourself - if there's a foreign key referencing your table, SQL Server needs that information in the transaction log. Since TRUNCATE TABLE effectively bypasses the log, it's not allowed on tables referenced by foreign keys.
You'll either have to DROP the foreign key constraint, or use a DELETE statement.
It's only referencing (eg the REFERENCES bit) foreign keys you need to drop.
This should make it easier...