w3resource

SQL drop

Remove an object from the database

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 :


-- Dropping the database named 'test'
DROP DATABASE test;

Explanation:

  • This SQL code drops the database named 'test'.

  • The DROP DATABASE statement is used to remove a database and all its associated files from the server's file system.

  • In this case, the database named 'test' will be permanently deleted.

  • Dropping a database will remove all tables, views, stored procedures, and other objects stored within it.

  • Once executed, this SQL statement will irreversibly delete the 'test' database and all its contents. It's essential to use this command with caution as it cannot be undone.

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:


-- Dropping the table named 'agents'
DROP TABLE agents;

Explanation:

  • This SQL code drops the table named 'agents'.

  • The DROP TABLE statement is used to remove a table from the database schema.

  • In this case, the table named 'agents' will be permanently deleted.

  • Dropping a table will remove all data and metadata associated with it.

  • Once executed, this SQL statement will irreversibly delete the 'agents' table and all its contents. It's essential to use this command with caution as it cannot be undone.

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':


-- Creating a view named myview1
CREATE VIEW myview1
-- Defining the view's query to select all columns from the agents table
AS SELECT * 
-- Selecting all columns (*) from the agents table
FROM agents;

Explanation:

  • This SQL code creates a view named "myview1".

  • Views are virtual tables that represent the result of a stored query.

  • The CREATE VIEW statement is used to define a new view.

  • In this case, the view "myview1" is defined by selecting all columns (*) from the "agents" table.

  • Once created, the view "myview1" will contain all columns from the "agents" table. This view can be queried like a regular table, providing a convenient way to access the data in the "agents" table.

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


-- Dropping the view named myview1
DROP VIEW myview1;

Explanation:

  • This SQL code drops the view named "myview1".

  • The DROP VIEW statement is used to remove a view from the database schema.

  • In this case, the view named "myview1" will be permanently deleted.

  • Dropping a view will remove its definition from the database, but it won't affect the underlying tables or data.

  • Once executed, this SQL statement will irreversibly delete the "myview1" view. It's essential to use this command with caution as it cannot be undone.

See our Model Database

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

Previous: Drop Index
Next: SQL Procedure - Create, Alter, Drop



Follow us on Facebook and Twitter for latest update.