Issue with truncation due to FOREIGN KEY constraint.

  • I want to truncate data.

    1. So i do ALTER TABLE table1 NOCHECK CONSTRAINT ALL

    2. Then i do TRUNCATE TABLE table1

    3. Then i get an error java.sql.SQLException: Cannot truncate table 'table1 ' because it is being referenced by a FOREIGN KEY constraint.

    The table has cyclic reference.

    i.e it has an primary key id which is an foreign key in the next row.

    Any help is greatly appreciated.

  • SQL Server will not allow you to use the TRUANCATE TABLE command on a table referenced by a foreign key. You have 2 options.

    1. Drop foreign key, truncate table, re-create foreign key.

    2. Leave foreign key alone, use DELETE instead.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply