II.5.5. Deleting a Table

When the time comes to bid farewell to a table, follow these simple steps and it will be gone from your life forever (unless you've made a backup, in which case the table has achieved immortality — and you still have a copy of the backup).

  1. Launch SQL Server Management Studio.

  2. Connect to the appropriate SQL Server instance.

  3. Expand the connection's entry in the Object Explorer view.

  4. Expand the Databases folder.

  5. Expand the Tables folder.

  6. Highlight the table you want to delete.

  7. Press Delete.

If the table is part of a relationship, and removing the table violates the terms of the relationship, SQL Server reports an error and blocks the deletion of this table. For example, take a look at the error message we received when we erroneously tried to delete a table that others depended upon:

Drop failed for Table 'dbo.students'. (Microsoft.SqlServer.Smo)
An exception occurred while executing a Transact-SQL statement or batch.
(Microsoft.SqlServer.ConnectionInfo)
Could not drop object 'dbo.students' because it is referenced by a FOREIGN KEY
constraint. (Microsoft SQL Server, Error: 3726)

Before deleting a table, try viewing its dependencies to see what impact your change might have. We describe how to do that a little earlier in this chapter. Conveniently, SQL Server puts a handy button on the dialog box just before you actually delete the table.

Get Microsoft® SQL Server™ 2008 All-In-One Desk Reference For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.