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.
DROP DATABASE [database name];
|database_name||Name of the database.|
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.
DROP TABLE [table name];
|table_name||Name of the table.|
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.
DROP VIEW [view_name];
|view_name||Name of the view or virtual table.|
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;
Practice SQL Exercises
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [7 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.
New Exercises:R Programming