SQL Server supports DIE syntax(drop table if exists):
Multiple tables can be dropped in any database. If a table being dropped references the primary key of another table that is also being dropped, the referencing table with the foreign key must be listed before the table holding the primary key that is being referenced.
Example:
CREATE TABLE t1(i INT PRIMARY KEY);
CREATE TABLE t2(i INT PRIMARY KEY);
CREATE TABLE t3(i INT PRIMARY KEY, t2_id INT REFERENCES t2(i));
The proper way of droping objects is: t1/t3, and finally t2.
Let's say we run:
DROP TABLE IF EXISTS t1,t2,t3;
-- Could not drop object 't2' because it is referenced by a FOREIGN KEY constraint.
Everything is as expected, the DDL cannot complete successfully because there is FK holding.
Now, I was expecting that for DDL should hold all-or-nothing behaviour. So I should still have my 3 tables intact. But it is not the case, tables t1 and t3 were dropped.
SELECT * FROM t1;
-- Invalid object name 't1'.
SELECT * FROM t2;
SELECT * FROM t3;
-- Invalid object name 't3'.
It looks that internally it is executed as 3 independent drop statements which is odd.
Is there any reason why tables t1 and t3 are gone or am I missing something obvious?