Query to delete database

DROP DATABASE (Transact-SQL)
February 19, 2021 – 11:44 am
Mysql - phpmyadmin enable drop database statement - Stack Overflow
THIS TOPIC APPLIES TO: SQL Server (starting with 2008)Azure SQL DatabaseAzure SQL Data Warehouse Parallel Data Warehouse

Removes one or more user databases or database snapshots from an instance of SQL Server.

Transact-SQL Syntax Conventions

Syntax

- SQL Server Syntax DROP DATABASE [ IF EXISTS ] { database_name | database_snapshot_name } [...n ] [;]

- Azure SQL Database and Parallel Data Warehouse Syntax DROP DATABASE database_name [;]

Arguments

IF EXISTS
Applies to: SQL Server ( SQL Server 2016 through current version).

Conditionally drops the database only if it already exists.

database_name
Specifies the name of the database to be removed. To display a list of databases, use the sys.databases catalog view.

database_snapshot_name
Applies to: SQL Server 2008 through SQL Server 2017.

Specifies the name of a database snapshot to be removed.

General Remarks

A database can be dropped regardless of its state: offline, read-only, suspect, and so on. To display the current state of a database, use the sys.databases catalog view.

A dropped database can be re-created only by restoring a backup. Database snapshots cannot be backed up and, therefore, cannot be restored.

Dropping a database deletes the database from an instance of SQL Server and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped, the disk files are not deleted. These files can be deleted manually by using Windows Explorer. To remove a database from the current server without deleting the files from the file system, use sp_detach_db.

Warning

Dropping a database that has FILE_SNAPSHOT backups associated with it will succeed, but the database files that have associated snapshots will not be deleted to avoid invalidating the backups referring to these database files. The file will be truncated, but will not be physically deleted in order to keep the FILE_SNAPSHOT backups intact. For more information, see SQL Server Backup and Restore with Microsoft Azure Blob Storage Service. Applies to: SQL Server 2016 through current version.

SQL Server

Dropping a database snapshot deletes the database snapshot from an instance of SQL Server and deletes the physical NTFS File System sparse files used by the snapshot. For information about using sparse files by database snapshots, see Database Snapshots (SQL Server). Dropping a database snapshot clears the plan cache for the instance of SQL Server. Clearing the plan cache causes a recompilation of all subsequent execution plans and can cause a sudden, temporary decrease in query performance. For each cleared cachestore in the plan cache, the SQL Server error log contains the following informational message: " SQL Server has encountered %d occurrence(s) of cachestore flush for the '%s' cachestore (part of plan cache) due to some database maintenance or reconfigure operations". This message is logged every five minutes as long as the cache is flushed within that time interval.

To drop a database published for transactional replication, or published or subscribed to merge replication, you must first remove replication from the database. If a database is damaged or replication cannot first be removed or both, in most cases you still can drop the database by using ALTER DATABASE to set the database offline and then dropping it.

Limitations and Restrictions

The DROP DATABASE statement must run in autocommit mode and is not allowed in an explicit or implicit transaction. Autocommit mode is the default transaction management mode.

You cannot drop a database currently being used. This means open for reading or writing by any user. To remove users from the database, use ALTER DATABASE to set the database to SINGLE_USER.

Any database snapshots on a database must be dropped before the database can be dropped.

Dropping a database enable for Stretch Database does not remove the remote data. If you want to delete the remote data, you have to remove it manually.

Azure SQL Database

You must be connected to the master database to drop a database.

The DROP DATABASE statement must be the only statement in a SQL batch and you can drop only one database at a time.

Requires the CONTROL permission on the database, or ALTER ANY DATABASE permission, or membership in the db_owner fixed database role.

Only the server-level principal login (created by the provisioning process) or members of the dbmanager database role can drop a database.

Parallel Data Warehouse

A. Dropping a single database

The following example removes the Sales database.

DROP DATABASE Sales;

B. Dropping multiple databases

Applies to: SQL Server 2008 through SQL Server 2017.

The following example removes each of the listed databases.

DROP DATABASE Sales, NewSales;

C. Dropping a database snapshot

The following example removes a database snapshot, named sales_snapshot0600, without affecting the source database.

Source: docs.microsoft.com
Related Posts