There are several different
methods for dropping (or deleting) a table. You can right-click the
table in the SSMS Object Explorer and select Delete, you can right-click
a table in a database diagram and choose Delete Tables from Database,
or you can use the old-fashioned method of utilizing T-SQL. Here’s an
example of the T-SQL DROP TABLE statement:
DROP TABLE [HumanResources].[Department]
You can reference multiple tables in a single DROP TABLE
command by separating the table names with commas. Any triggers and
constraints associated with the table are also dropped when the table is
dropped.
A big consideration when
dropping a table is the table’s relationship to other tables. If a
foreign key references the table that you want to drop, the referencing
table or foreign key constraint must be dropped first. In a database
that has many related tables, dropping elements can get complicated.
Fortunately, a few tools can help you through this. The system stored
procedure sp_helpconstraint is one of these tools. This procedure lists all the foreign key constraints that reference a table. Listing 1 shows an execution of this stored procedure for the Sales.Store table in the AdventureWorks2008
database. The procedure results include information about all the
constraints on the table. The results to focus on are those that follow
the heading Table Is Referenced by Foreign Key. The partial results
shown in Listing 1 for the Sales.Store table indicate that FK_StoreContact_Store_CustomerID must be dropped first before you can drop the Sales.Store table.
Listing 1. Using sp_helpconstraint to Find Foreign Key References
sp_helpconstraint [Sales.Store]
/*partial results of sp_helpconstraint execution
Table is referenced by foreign key
-------------------------------------------------------------------
AdventureWorks2008.Sales.StoreContact: FK_StoreContact_Store_CustomerID
*/
|
Two other approaches are
useful for identifying foreign key references prior to dropping a table.
The first is using a database diagram. You can create a new database
diagram and add the table that you are considering for deletion. After
the table is added, you right-click the table in Object Explorer and
select Add Related Tables. The related tables, including those that have
foreign key references, are then added. You can then right-click the
relationship line connecting two tables and select Delete Relationships
from Database. When you have deleted all the foreign key relationships
from the diagram, you can right-click the table you want to delete and
select Generate Change Script to create a script that can be used to
remove the foreign key relationship(s).
The other approach is to
right-click the table in Object Explorer and choose View Dependencies.
The dialog that appears gives you the option of viewing the objects that
depend on the table or viewing the objects on which the table depends.
If you choose the option to view the objects that depend on the table,
all the dependent objects are displayed, but you can focus on the
objects that are tables.
Creating Temporary Tables
A temporary table
is a special type of table that is automatically deleted when it is no
longer used. Temporary tables have many of the same characteristics as
permanent tables and are typically used as work tables that contain
intermediate results.
You designate a table as temporary in SQL Server by prefacing the table name with a single pound sign (#) or two pound signs (##). Temporary tables are created in tempdb;
if a temporary table is not explicitly dropped, it is dropped when the
session that created it ends or the stored procedure it was created in
finishes execution.
If a table name is prefaced with a single pound sign (for example, #table1), it is a private temporary table, available only to the session that created it.
A table name prefixed with a double pound sign (for example, ##table2) indicates that it is a global
temporary table, which means it is accessible by all database
connections. A global temporary table exists until the session that
created it terminates. If the creating session terminates while other
sessions are accessing the table, the temporary table is available to
those sessions until the last session’s query ends, at which time the
table is dropped.
A common way of creating a temporary table is to use the SELECT INTO method as shown in the following example:
SELECT* INTO #Employee2 FROM Employee
This method creates a
temporary table with a structure like the table that is being selected
from. It also copies the data from the original table and inserts it
into this new temporary table. All of this is done with this one simple
command.
Note
Table variables are a
good alternative to temporary tables. These variables are also temporary
in nature and have some advantages over temporary tables. Table
variables are easy to create, are automatically deleted, cause fewer
recompilations, and use fewer locking and logging resources. Generally
speaking, you should consider using table variables instead of temporary
tables when the temporary results are relatively small. Parallel query
plans are not generated with table variables, and this can impede
overall performance when you are accessing a table variable that has a
large number of rows.
Tables created without the # prefix but explicitly created in tempdb
are also considered temporary, but they are a more permanent form of a
temporary table. They are not dropped automatically until SQL Server is
restarted and tempdb is reinitialized.