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 :
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;
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 [8 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
- Introduction
- 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.
Previous: Drop Index
Next: SQL Procedure - Create, Alter, Drop
SQL: Tips of the Day
SQL Server SELECT into existing table.
INSERT INTO dbo.TABLETWO SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:
INSERT INTO dbo.TABLETWO (col1, col2) SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
Database: SQL Server
Ref: https://bit.ly/3y6tpA3
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework