Given a database with many tables, I have a script populated with several delete and truncate statements. I do not have a delete statement or truncate statement for each table in my database. The script looks like the following (I have changed names and shortened the length):
TRUNCATE table db.dbo.table1
TRUNCATE table db.dbo.table2
TRUNCATE table db.dbo.table3
--cannot truncate table because it is being referenced by a FK constraint
--TRUNCATE table db.dbo.table4
--TRUNCATE table db.dbo.table5
--TRUNCATE table db.dbo.table6
--TRUNCATE table db.dbo.table7
DELETE FROM db.dbo.table4
DBCC CHECKIDENT ('db.dbo.table4',RESEED, 0)
DELETE FROM db.dbo.table5
DBCC CHECKIDENT ('db.dbo.table5',RESEED, 0)
DELETE FROM db.dbo.table6
DBCC CHECKIDENT ('db.dbo.table6',RESEED, 0)
DELETE FROM db.dbo.table7
DBCC CHECKIDENT ('db.dbo.table7',RESEED, 0)
DELETE FROM db.dbo.table8
DELETE FROM db.dbo.table9
DELETE FROM db.dbo.table10
DELETE FROM db.dbo.table11
DELETE FROM db.dbo.table12
DELETE FROM db.dbo.table13
DELETE FROM db.dbo.table14
DELETE FROM db.dbo.table15
After executing the script, I am finding that data is being removed from tables that are not included in the script.
To verify which tables have data, I am using the following query provided in a SO answer found here.
SELECT
t.NAME AS TableName,
SUM(p.rows) AS [RowCount]
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
WHERE
i.index_id <= 1
GROUP BY
t.NAME, i.object_id, i.index_id, i.name
ORDER BY
SUM(p.rows) DESC
I have a backup of the database. Next I:
- restore the database
- run 'table data' query: to double check that data does really exists in tables not included in the delete/truncate script
- execute delete/truncate script
- run 'table data' query
My findings are as such: data is being removed from tables that are not defined in my delete/truncate script.
My question: Given I am deleting or truncating from more than one and not all tables in a database, how is data being removed from the tables that are not defined in a delete or truncate statement?