GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||
GeezerVison is showing www.tutorialspoint.com — SQL DROP DATABASE Statement
Selected Reading
SQL DROP DATABASE StatementThe SQL DROP DATABASE StatementThe DROP DATABASE statement in SQL is used to permanently delete an existing database and all of its contents. This operation removes the entire database, including all tables, views, stored procedures, and the data stored inside it. You need to be very careful when using the DROP DATABASE command. It will permanently delete all data in the database and cannot be undone. Always make sure you have a backup before running this command. SyntaxFollowing is the syntax of the DROP DATABASE statement in SQL: DROP DATABASE database_name; Here,
ExampleFollowing is the command to delete a database named testDB: DROP DATABASE testDB; After executing the above command in MySQL database, we get the following output: Query OK, 0 rows affected (0.04 sec) How to Verify Whether a Database Is DroppedTo confirm that a database has been dropped successfully, you can list all databases using the following SQL command: SHOW DATABASES; After executing the above command on the MySQL server, if the database named testdb no longer appears in the list, it means it has been successfully deleted:
Since the database testdb is not listed, it has been successfully dropped. SQL DROP DATABASE with IF EXISTS ClauseIf we try to delete a database that doesn't exist, SQL will throw an error. To avoid this, we can use the IF EXISTS clause with the DROP DATABASE command. This way, SQL will only try to delete the database if it actually exists. Following is the syntax to create a database only if it does not already exist, using the IF NOT EXISTS clause in SQL: SyntaxFollowing is the syntax to delete a database only if it exists, to avoid errors: DROP DATABASE IF EXISTS database_name; Example: If Database ExistsThe following command deletes the testDB database only if it already exists: DROP DATABASE IF EXISTS testDB; Following is the output of the above code: Query OK, 0 rows affected (0.05 sec) Example: If Database Does Not ExistHere, we are trying to delete the database testDB, which has already been deleted, and then run the same command again: DROP DATABASE IF EXISTS testDB; We get the following output: Query OK, 0 rows affected, 1 warning (0.02 sec) This warning indicates that the database did not exist. However, no error is thrown due to the IF EXISTS clause. Deleting Multiple DatabasesIn SQL, you can delete multiple databases in a single command by separating their names with commas in the DROP DATABASE statement.
SyntaxFollowing is the syntax to delete multiple databases using the DROP DATABASE statement in SQL Server: DROP DATABASE db1, db2, db3; ExampleIn this example, we are deleting three databases named testDB1, testDB2, and testDB3 at once in SQL Server: DROP DATABASE testDB1, testDB2, testDB3; We get the output as shown below: Commands completed successfully. When Should You Drop a Database?You can use the DROP DATABASE command in the following situations:
Advertisements
| ||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |