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+------------+----------------------+--------------------+------------+-----------------+---------+ | AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION | PHONE_NO | COUNTRY | +------------+----------------------+--------------------+------------+-----------------+---------+ | A007 | Ramasundar | Bangalore | 0.15 | 077-25814763 | | | A003 | Alex | London | 0.13 | 075-12458969 | | | A008 | Alford | New York | 0.12 | 044-25874365 | | | A011 | Ravi Kumar | Bangalore | 0.15 | 077-45625874 | | | A010 | Santakumar | Chennai | 0.14 | 007-22388644 | | | A012 | Lucida | San Jose | 0.12 | 044-52981425 | | | A005 | Anderson | Brisban | 0.13 | 045-21447739 | | | A001 | Subbarao | Bangalore | 0.14 | 077-12346674 | | | A002 | Mukesh | Mumbai | 0.11 | 029-12358964 | | | A006 | McDen | London | 0.15 | 078-22255588 | | | A004 | Ivan | Torento | 0.15 | 008-22544166 | | | A009 | Benjamin | Hampshair | 0.11 | 008-22536178 | | +------------+----------------------+--------------------+------------+-----------------+---------+Sample table: orders
ORD_NUM ORD_AMOUNT ADVANCE_AMOUNT ORD_DATE CUST_CODE AGENT_CODE ORD_DESCRIPTION
---------- ---------- -------------- --------- --------------- --------------- -----------------
200114 3500 2000 15-AUG-08 C00002 A008
200122 2500 400 16-SEP-08 C00003 A004
200118 500 100 20-JUL-08 C00023 A006
200119 4000 700 16-SEP-08 C00007 A010
200121 1500 600 23-SEP-08 C00008 A004
200130 2500 400 30-JUL-08 C00025 A011
200134 4200 1800 25-SEP-08 C00004 A005
200108 4000 600 15-FEB-08 C00008 A004
200103 1500 700 15-MAY-08 C00021 A005
200105 2500 500 18-JUL-08 C00025 A011
200109 3500 800 30-JUL-08 C00011 A010
200101 3000 1000 15-JUL-08 C00001 A008
200111 1000 300 10-JUL-08 C00020 A008
200104 1500 500 13-MAR-08 C00006 A004
200106 2500 700 20-APR-08 C00005 A002
200125 2000 600 10-OCT-08 C00018 A005
200117 800 200 20-OCT-08 C00014 A001
200123 500 100 16-SEP-08 C00022 A002
200120 500 100 20-JUL-08 C00009 A002
200116 500 100 13-JUL-08 C00010 A009
200124 500 100 20-JUN-08 C00017 A007
200126 500 100 24-JUN-08 C00022 A002
200129 2500 500 20-JUL-08 C00024 A006
200127 2500 400 20-JUL-08 C00015 A003
200128 3500 1500 20-JUL-08 C00009 A002
200135 2000 800 16-SEP-08 C00007 A010
200131 900 150 26-AUG-08 C00012 A012
200133 1200 400 29-JUN-08 C00009 A002
200100 1000 600 08-JAN-08 C00015 A003
200110 3000 500 15-APR-08 C00019 A010
200107 4500 900 30-AUG-08 C00007 A010
200112 2000 400 30-MAY-08 C00016 A007
200113 4000 600 10-JUN-08 C00022 A002
200102 2000 300 25-MAY-08 C00012 A012
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+------------+----------------------+--------------------+------------+-----------------+---------+ | AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION | PHONE_NO | COUNTRY | +------------+----------------------+--------------------+------------+-----------------+---------+ | A007 | Ramasundar | Bangalore | 0.15 | 077-25814763 | | | A003 | Alex | London | 0.13 | 075-12458969 | | | A008 | Alford | New York | 0.12 | 044-25874365 | | | A011 | Ravi Kumar | Bangalore | 0.15 | 077-45625874 | | | A010 | Santakumar | Chennai | 0.14 | 007-22388644 | | | A012 | Lucida | San Jose | 0.12 | 044-52981425 | | | A005 | Anderson | Brisban | 0.13 | 045-21447739 | | | A001 | Subbarao | Bangalore | 0.14 | 077-12346674 | | | A002 | Mukesh | Mumbai | 0.11 | 029-12358964 | | | A006 | McDen | London | 0.15 | 078-22255588 | | | A004 | Ivan | Torento | 0.15 | 008-22544166 | | | A009 | Benjamin | Hampshair | 0.11 | 008-22536178 | | +------------+----------------------+--------------------+------------+-----------------+---------+
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+------------+----------------------+--------------------+------------+-----------------+---------+ | AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION | PHONE_NO | COUNTRY | +------------+----------------------+--------------------+------------+-----------------+---------+ | A007 | Ramasundar | Bangalore | 0.15 | 077-25814763 | | | A003 | Alex | London | 0.13 | 075-12458969 | | | A008 | Alford | New York | 0.12 | 044-25874365 | | | A011 | Ravi Kumar | Bangalore | 0.15 | 077-45625874 | | | A010 | Santakumar | Chennai | 0.14 | 007-22388644 | | | A012 | Lucida | San Jose | 0.12 | 044-52981425 | | | A005 | Anderson | Brisban | 0.13 | 045-21447739 | | | A001 | Subbarao | Bangalore | 0.14 | 077-12346674 | | | A002 | Mukesh | Mumbai | 0.11 | 029-12358964 | | | A006 | McDen | London | 0.15 | 078-22255588 | | | A004 | Ivan | Torento | 0.15 | 008-22544166 | | | A009 | Benjamin | Hampshair | 0.11 | 008-22536178 | | +------------+----------------------+--------------------+------------+-----------------+---------+
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.
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Previous: Drop Index
Next: SQL Procedure - Create, Alter, Drop
