Should I Create Tables in Sql to Drop Them Again

This commodity gives an overview of the SQL Drib TABLE argument to remove one or more tables from a database.

In my earlier commodity, Difference between SQL Truncate and SQL Delete statements in SQL Server, nosotros explored to delete data from an existing data. We might delete whole information using both SQL Delete and SQL Truncate statements. We might also delete specific data from the SQL Server tables using SQL Delete argument. SQL Delete and Truncate do non move the object structure from the database.

Sometimes, nosotros practice want to perform database clean up by removing unnecessary tables. Let'due south say yous want to make majority changes to a tabular array. Most of the DBA'due south take table-level backup before making any alter to it. It involves creating another backup table in a similar database with a different proper name.

In the post-obit table, we want to delete multiple records.

Earlier removing the data, take a backup using SELECT INTO control.

Information technology created some other tabular array with existing cavalcade structure and copies the data into information technology. We need to perform regular clean-up of these backup tables. It takes unnecessary disk space and if you do index maintenance for all indexes, it might add actress overhead to the organisation.

Let's explore the SQL DROP Table in the adjacent action.

Overview of SQL Driblet TABLE

Nosotros use the SQL Drib Tabular array command to driblet a table from the database. Information technology completely removes the table construction and associated indexes, statistics, permissions, triggers and constraints. You might have SQL Views and Stored procedures referencing to the SQL table. SQL Server does not remove these stored procedures and views. We demand to drop them explicitly. Nosotros should check object dependencies before removing a SQL table.

The syntax for SQL DROP Table

It uses the post-obit parameters.

  • Database_name: Specify the database name in which table exists. We can skip this parameter if we execute the drib command in the current database context
  • Schema_name: Specify the schema name for which the object exists. If the object belongs to the default schema DBO, we can skip this parameter. SQL Server automatically uses dbo schema. Nosotros must specify the schema proper name if the object belongs other than the default schema
  • Table proper name: Specify the table that we want to remove

Instance 1: Drib a unmarried table using the SQL DROP Table statement

Execute the post-obit query to drop HumanResources.Employee13072019 table from the AdventureWorks2017 database.

It gives the following error bulletin. We go this error because the object belongs to the default schema dbo.

: Drop a single table using the SQL DROP Table statement

To gear up this, we demand to specify schema name forth with the table name.

Alternatively, we tin can utilise the following query to driblet a SQL tabular array.

Instance 2: Driblet multiple tables together using the SQL Drop Table statement

We tin drop multiple tables together using a single Drib Table statement too.

Let's create three tables and later nosotros will drop information technology.

At present, we can employ the post-obit drib table statement and specify all tabular array names together to drop it.

Instance 2: Drop a SQL tabular array having a foreign key constraint using the SQL Driblet Table statement

In SQL Server, we tin use a strange cardinal between multiple table columns to link data between these tables. We cannot drib the table directly in this instance.

Let's understand this using an example. Nosotros will create ii SQL tables Section and Employee. In the database diagram, you tin meet that nosotros accept a foreign key constraint for on the Dept_id column

database diagram

Execute the following script to create both tables.

Let's effort to drop departments table using SQL DROP TABLE statement.

You lot get the following fault message.

error while dropping a table

Strange fundamental relationships are like a parent-kid human relationship. We cannot delete a parent table that is referenced by a foreign key constraint. We demand to either remove the foreign key relationship or drop the child table first.

In my instance, we demand to driblet the Employee1 table first because it has a foreign cardinal relationship with the section table.

We tin use a unmarried SQL Drop Table statement as well in such case with a circumspection to drop the referencing table first.

Example iii: Drib a temp table using the SQL DROP Table statement

We tin too drop a temp tabular array in a way similar to the regular table. The following example creates a temp table and drops information technology.

Instance 4: Dropping a table using IF EXISTS

Usually, developers check for the being of any database object in the database and drib the object if information technology exists.

If we endeavour to drop a tabular array that does non be, we get the following fault message.

Dropping a table using IF EXISTS

We do not want whatever error in executing queries, especially during the execution of a bunch of code. Before SQL Server 2016, developers use the IF EXISTS statement and check for the object existence before dropping it.

For example, in the following query, we check the section tabular array in the sys .objects.

If the object exists, execute the drop table argument else, no actions required.

Alternatively, we can cheque the object id of the SQL tabular array and execute the drop table argument if it is non NULL.

These approaches work fine. However, the issue is that you need to write a long transact SQL code. Starting from SQL Server 2016, we tin can use the new syntax of SQL DROP Table. It drops a SQL table if it already exists.

It is a short version of the lawmaking we executed before. Let'south try to drop the department table using this new code. It is a small and easy way to driblet a table.

Example five: Dropping a table having a reference in the stored procedures, views

SQL Server does not give any error bulletin if you driblet a SQL table that is being used in the stored procedure, views. We can use the SCHEMABINDING option, just it is not in the scope of this commodity.

Nosotros can check the dependencies of an object using SSMS. Right-click on a table and click on View Dependencies. It opens a dissever window and displays the dependencies.

View Dependencies

You can look at the dependencies and resolve it so that the procedures and views tin can function correctly after dropping this object.

Conclusion

In this article, nosotros explored the SQL Driblet Table statement for removing the objects from the SQL database. You should be careful before dropping whatsoever object in the production database.

  • Author
  • Contempo Posts

Rajendra Gupta

costigancornind.blogspot.com

Source: https://www.sqlshack.com/an-overview-of-a-sql-drop-table-statement/

0 Response to "Should I Create Tables in Sql to Drop Them Again"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel