DROP DATABASE command in SQL

How is a database drop used in SQL? This article will guide you in detail on how to delete a database in SQL.

How is a database drop used in SQL ? This article will guide you in detail on how to delete a database in SQL .

 

Picture 1 of DROP DATABASE command in SQL

The DROP DATABASE statement in SQL is a crucial command used to permanently delete a database from a Database Management System (DBMS). When executed, this command deletes the database and all associated objects, including tables, views, stored procedures, and other entities.

 

What is DROP DATABASE in SQL?

The DROP DATABASE command in SQL is used to completely delete a database and all its objects, such as tables, indexes, views, and other dependent objects. Once executed, this operation cannot be undone, so it is important to back up the database before deleting it.

This command is commonly used when a database is no longer needed or needs to be deleted for reorganization. Before running the command, ensure you have sufficient privileges to delete the database, as typically only administrators have the necessary permissions.

  1. Purpose: To permanently delete a database from the DBMS.
  2. Result: All database data, schemas, and objects are deleted.

Syntax of the DROP DATABASE command in SQL

DROP DATABASE ten_co_so_du_lieu;

The database_name is always unique in an RDBMS. Learn more about RDBMS in SQL .

Example of a DROP DATABASE command in SQL

 

If you want to delete an existing database, for example named TipsMake , the DROP DATABASE command would be written as follows:

DROP DATABASE TipsMake.com;

Note: Please be careful when performing this action, as deleting an existing database may completely erase all information stored within it.

Make sure you have administrator privileges before deleting any databases. Once a database is deleted, you can check the database list again using the command SHOW DATABASES;below:

SQL> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | CONGNGHE | | LANGCONGNGHE | | WINDOWS | | mysql | | ANDROID | | iOS | +--------------------+ 6 rows in set (0.00 sec)

The DROP DATABASE command is quite simple, isn't it? In the next lesson, we'll learn about the USE command, used to select a database.

Key points to remember about the SQL DROP DATABASE statement

  1. Deleting a database: Deleting a database is a permanent action and will result in the loss of all information stored in the database.
  2. Backup: It's always a good practice to back up your database before performing any deletion operations on it.
  3. Permissions: Ensure you have the necessary permissions to delete the database. Typically, administrators can delete databases.
  4. Database state: A database can be deleted regardless of its state: offline, read-only, doubtful, etc.
  5. Database snapshot: Deleting a database snapshot will remove the database snapshot from the SQL Server instance and delete the sparse files of the physical NTFS file system that were snapshotted.
  6. Copying: To delete a database that has been published for transactional replication, or that has been published or registered for merge replication, you must first delete the copy from the database.
  7. System database: Users cannot directly delete or edit the system database.
« PREV POST
READ NEXT »