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;
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join