SQL SERVER – Trigger on Database to Prevent Table Creation

A Trigger on Database scope can reject CREATE_TABLE events. A customer required this rule, though I preferred ordinary permissions.

A removable local doorway stop blocks a new shelf assembly from one specific work area.

-- In a disposable test database, separate from business workloads:
-- CREATE TRIGGER StopTableCreation ON DATABASE FOR CREATE_TABLE AS
-- BEGIN
--     PRINT 'This database does not allow new tables.';
--     ROLLBACK TRANSACTION;
-- END;
-- GO
-- CREATE TABLE dbo.T1 (ID int); -- Expected rejection by that trigger.
-- GO
-- DROP TRIGGER StopTableCreation ON DATABASE; -- Lab cleanup.
SELECT name,is_disabled,parent_class_desc FROM sys.triggers WHERE parent_class=0;

I previously called this prevention of database creation. CREATE_DATABASE requires a different server scope. The shown trigger handles table creation. ROLLBACK rejects that operation, producing the historical message 3609.

Administrators can disable or remove the trigger. It can obstruct intended deployments and application operations. Prefer least privilege when it expresses the requirement. Review ownership and an operational escape process for any trigger.

The reproduction and cleanup are disabled lab instructions. Test rejection in a disposable database with a client that handles the aborted batch. Confirm the original trigger state after testing.

Reference: Database DDL trigger scope.

Related reading

A DDL trigger is not an immutable administrator boundary, it is event-driven logic with scope and operational consequences.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Scripts, SQL Server, SQL Trigger
Previous Post
Reading the Connectivity Ring Buffer for Dropped Connections
Next Post
Prevent Index Changes in SQL Server With a DDL Trigger

Related Posts

1 Comment. Leave new

  • can i do this on model db to trap developers creating db’s without permission ? (i cannot remove their sysadmin)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.