Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When dealing with deleting data from tables which have foreign key relationships - which is basically the case with any properly designed database - we can disable all the constraints, delete all the data and then re-enable constraints</p> <pre><code>-- disable all constraints EXEC sp_MSForEachTable "ALTER TABLE ? NOCHECK CONSTRAINT all" -- delete data in all tables EXEC sp_MSForEachTable "DELETE FROM ?" -- enable all constraints exec sp_MSForEachTable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all" </code></pre> <p>More on disabling constraints and triggers <a href="https://stackoverflow.com/questions/123558/sql-server-2005-t-sql-to-temporarily-disable-a-trigger#123966">here</a></p> <p>if some of the tables have identity columns we may want to reseed them</p> <pre><code>EXEC sp_MSForEachTable "DBCC CHECKIDENT ( '?', RESEED, 0)" </code></pre> <p>Note that the behaviour of RESEED differs between brand new table, and one which had had some data inserted previously from <a href="http://msdn.microsoft.com/en-us/library/aa258817(SQL.80).aspx" rel="noreferrer">BOL</a>:</p> <blockquote> <p><strong>DBCC CHECKIDENT ('table_name', RESEED, newReseedValue)</strong></p> <p>The current identity value is set to the newReseedValue. If no rows have been inserted to the table since it was created, the first row inserted after executing DBCC CHECKIDENT will use newReseedValue as the identity. Otherwise, the next row inserted will use newReseedValue + 1. If the value of newReseedValue is less than the maximum value in the identity column, error message 2627 will be generated on subsequent references to the table.</p> </blockquote> <p>Thanks to <a href="https://stackoverflow.com/users/23566/robert-claypool">Robert</a> for pointing out the fact that disabling constraints does not allow to use truncate, the constraints would have to be dropped, and then recreated</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload