• subscribe
October 01, 2001 12:00 AM

T-SQL Tutor Quick Tip

SQL Server Pro
InstantDoc ID #22073

I often encounter the complaint that accessing production databases is too easy, especially from Enterprise Manager. To make a system both secure and highly available, you need to have administrative change and maintenance processes in place to minimize direct access to production databases. However, I have a quick tip that can help in the interim.

Recently someone asked me, "How can I prevent a systems administrator (sa) from dropping a production table?" I realized that using declarative referential integrity (DRI) makes inadvertently dropping a table more difficult. However, DRI prevents a drop table only when the table is being referenced with a foreign key constraint. A sales table, for example, often references other tables, but other tables don't always reference it. So how can you prevent an accidental table drop? In SQL Server 2000, consider using a schema-bound view.

T-SQL Tutor quick tip of the month: Using WITH SCHEMABINDING, create a view on any tables you want to protect. This tip prevents an administrator from dropping the tables on which the view depends. You can't use an asterisk (*) when defining schema-bound views, so you must supply at least one column name in the view definition; I recommend the primary key. You can still use T-SQL to modify the table's schema, other than the columns in the schema-bound view, and most of us won't need to change the primary key. However, be careful when using Enterprise Manager to make schema changes; sometimes, the removal of SCHEMABINDING from the view is a side effect of changing the schema. Here's an example of the syntax you could use to manually create a schema-bound view for a table named Sales with a primary key named SalesPK:

CREATE VIEW dbo.Sales_
   PreventDropView
	WITH SCHEMABINDING
AS
SELECT SalesPK FROM dbo.Sales

However, creating a schema-bound view manually for each table would be time-consuming. To help you create a schema-bound view for all your production tables, I've written a stored procedure called sp_CreateSchemaboundViewToPreventTableDrop. This procedure requires a couple of other stored procedures, sp_GetPrimaryKeyList and sp_GenerateUniqueNameForNewObject, to create a uniquely named view for each table that uses their primary key. You can download the SchemaboundViews.sql script (see "Obtaining the Code," page 3, for instructions) to create these stored procedures in the master database. After creating the stored procedures, you can test them within your Pubs database. Begin by using sp_depends to see what views are currently on the authors table:

EXEC sp_depends authors

Then, use sp_CreateSchemaboundViewToPreventTableDrop to create a view on only one table:

EXEC sp_CreateSchemaboundViewTo
   PreventTableDrop 'authors'

or on all the tables within your database:

EXEC sp_CreateSchemaboundViewToPreventTableDrop 'All'

Now, use sp_depends to check the dependencies again:

EXEC sp_depends authors

You'll see a new view called authors_PreventTableDropWithSBView. The authors table can't be dropped unless this view is removed or altered to exclude schemabinding. To remove this view, you can drop it with the DROP VIEW command. Or, you can use sp_DropAllSBPreventTableDropView with the view name to drop it. You can also use 'All' to remove all the PreventTableDropWithSBView views within your database:

EXEC sp_DropAllSBPreventTableDrop
   Views 'authors_PreventTableDrop
	WithSBView'
--or EXEC sp_DropAllSBPreventTable
   DropViews 'All'

Have fun!



ARTICLE TOOLS

Comments
  • DAN
    6 years ago
    Sep 25, 2006

    This tip helped me think of a way to overcome a problem I've been having with instead of triggers defined on views. If the view gets dropped, so does the trigger. This is good when you really want to delete the view. However, a common way to edit a view is with drop view and create view, which will delete the trigger as well. Enterprise Manager (2000 SP4) will make sure to preserve triggers on tables when table design changes are made which drop the and re-create the table, but it doesn't do the same with views. It's difficult to remember to not edit these views with Enterprise Manager or by using the Create option in Query Analyzer.

    So, I'm going to try this technique to protect the views on which I have created triggers for my application.

    Thanks for keeping the older articles on here!

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...