w3resource logo


>Sql drop

SQL drop

Secondary Nav

Description

The SQL DROP command is used to remove an object (table,view,index) from the database. When you drop a table, all the rows from the table will be deleted with the structure from the database.

Once the table is dropped, you can not undo that, so it is better to take a backup of the table before you drop.

The table which is referenced by a FOREIGN KEY constraint can not be dropped by using SQL DROP TABLE command. First, the referencing FOREIGN KEY constraint or the referencing table must be dropped.

When a table is dropped, by default all the constraints or triggers associated with the table will be deleted automatically.

Difference between DROP and TRUNCATE Statement:

  • When a table is dropped, all the relationships related to the tables will no longer be valid i.e., the constraints, grant or access privileges on the table will also be deleted.
  • But, when a table is truncated, only the rows will be deleted without affecting the structure of the table.

SQL drop database

The SQL DROP command can remove the database that means this command will remove all the tables, views, index tables and all other objects related to that database.

Syntax

DROP   DATABASE [database name]; 

Parameters

Name Description
database_name Name of the database.

Example

Sample table : agents

Sample table : orders

Suppose the above tables 'orders' and 'agents' exist in the database 'TEST'.

To remove the database TEST, the following SQL statement can be used :

DROP DATABASE test;

SQL drop table

The SQL DROP TABLE command drops the table and all the relationships related to the tables such as all the constraints or triggers associated with the table will be automatically dropped. A table which has a FOREIGN KEY and referenced by another table can not be dropped and if required to drop, the foreign key constraint or the referencing table should be dropped first.

Syntax

DROP  TABLE [table name]; 

Parameters

Name Description
table_name Name of the table.

Example

Sample table : agents

To remove the table 'agents' from the current database, the following SQL statement can be used :

DROP  TABLE agents;

SQL drop view

The SQL DROP VIEW command drops the virtual table or view of a base table from the current database.

Syntax

DROP  VIEW [view_name]; 

Parameters

Name Description
view_name Name of the view or virtual table.

Example

Sample table :agents

This the statement bellow to create a view 'myview1' :

CREATE VIEW myview1AS SELECT * FROM agents;

To drop the virtual table or view 'myview1' created from the base table 'agents', the following SQL statement can be used :

DROP VIEW myview1;

See our Model Database