I'm trying to truncate some tables in a database. Some of these tables have foreign keys. So when I try to truncate them I get the following error.
Cannot truncate table 'IDN_OAUTH2_ACCESS_TOKEN' because it is being referenced by a FOREIGN KEY constraint.
Is there a way to disable FOREIGN KEY constraints in Azure SQL (Microsoft SQL Azure (RTM) - 12.0.2000.8) and re-enable them? In MySQL, I have done the same using the following script.
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE IDN_OAUTH2_ACCESS_TOKEN;
SET FOREIGN_KEY_CHECKS=1;
I think we can write some T-SQL scripts to achieve that. Using T-SQL to splice T-SQL(add & drop FK index) commands.
add constraint
todrop constraint
. As follows:So you can drop FKs and after truncate some tables, then rebuid FKs.