- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - DROP or DELETE Database
The DROP DATABASE statement in SQL is used to delete a database along with all the data such as tables, views, indexes, stored procedures, and constraints.
The Following are the most important points to remember before we are trying to delete the database −
It is important to make sure that we have to perform the backup of the database that we are going to delete because once the “DROP DATABASE” statement is executed, all the data and database objects in the database will be permanently deleted and cannot be recovered.
It is also important to ensure that no other user or apppcation is currently connected to the database that we want to delete. If we try to delete the database while others users are connected to it, then it can cause data corruption or other issues.
Note − Make sure you have the necessary privilege before deleting any database using the DROP DATABASE statement.
Syntax
Following is the syntax to delete a database in SQL −
DROP DATABASE DatabaseName;
Here, the “DatabaseName” is the name of the database that we want to delete.
Always the database name should be unique within the RDBMS.
Example
First of all, let us create multiple databases into database system using the following query −
SQL> CREATE DATABASE testDB1; CREATE DATABASE testDB2; CREATE DATABASE testDB3; CREATE DATABASE testDB4;
Let us verify whether the databases are created or not using the following query −
SQL> SELECT * FROM SYS.DATABASES +--------------------+ | Database | +--------------------+ | master | | tempdb | | model | | msdb | | testDB1 | | testDB2 | | testDB3 | | testDB4 | +--------------------+ 8 rows in set (0.00 sec)
Now, let us try to delete a single existing database <testDB> using the DROP DATABASE statement −
SQL> DROP DATABASE testDB1;
Once we have deleted the <testDB> database, we can verify whether it is deleted or not using the following query −
SQL> SELECT * FROM sys.databases; +--------------------+ | Database | +--------------------+ | master | | tempdb | | model | | msdb | | testDB2 | | testDB3 | | testDB4 | +--------------------+ 7 rows in set (0.00 sec)
That s it! we have successfully deleted a database in SQL.
Example
Now, let us try to delete a database that doesn t exist in the database system −
SQL> DROP DATABASE testDB1;
The above query returns an error statement because the database we are trying to delete doesn t exist in the database system −
Cannot drop the database testDB1 , because it does not exist or you do not have permission.
DROP DATABASE IF EXISTS Statement
The DROP DATABASE IF EXISTS statement in SQL includes a condition to check whether the database exists before trying to delete it. If the database does not exist in the database system, the "DROP DATABASE IF EXISTS" statement does not raise an error, but it simply terminates without taking any action.
Syntax
Following is the syntax of the DROP DATABASE IF EXISTS statement in SQL −
DROP DATABASE IF EXISTS DatabaseName;
Here, the “DatabaseName” is the name of the database that we want to delete.
Example
Let us try to delete an existing database <testDB2> in the database system using the following query statement −
SQL> DROP DATABASE IF EXISTS testDB2;
On executing the given program, the output is displayed as follows −
Commands completed successfully.
Let us verify whether the database <testDB2> is deleted or not using the following query −
SQL> SELECT * FROM SYS.DATABASES +--------------------+ | Database | +--------------------+ | master | | tempdb | | model | | msdb | | testDB3 | | testDB4 | +--------------------+ 6 rows in set (0.00 sec)
Dropping the database that doesn t exist
If you try to drop the database that doesn t exist
Example
Let us try to delete a database <testDB2> that doesn t exist in the database system using the following query statement −
SQL> DROP DATABASE IF EXISTS testDB2;
When we execute the program above, the output is obtained as follows −
Commands completed successfully.
Deleting multiple databases
You can also drop multiple databases at once using the DROP DATABASE statement.
Example
Let us try to delete multiple databases from the database system using the following query −
SQL> DROP DATABASE testDB3, testDB4;
Once we have deleted the databases, we can verify whether they are deleted in the database system or not using the following query −
SQL> SELECT * FROM sys.databases; +--------------------+ | Database | +--------------------+ | master | | tempdb | | model | | msdb | +--------------------+ 4 rows in set (0.00 sec)Advertisements