Create and delete the MSSQL Server Database

Learn how to use SQL Server Management Studio and Transact-SQL on an MSSQL Server to create and delete an MSSQL database.

Create and delete an MSSQL the database

  • You can create a database using the UI Tool with SQL Server Management Studio (SSMS).
  • There is a way to create a new database using Transact-SQL.
  • There is a way to create a command in the CMCD window.
  • There is a way to create a database as a backup file.
  • You can also delete the database you are using with the UI Tool.
  • Transact-SQL can be used to delete the database in use.
  • I will explain how to create a database using the UI Tool and Transact-SQL.

How to create and delete an MSSQL database

How to create a database with the UI Tool with SQL Server Management Studio (SSMS)

1-1. Select Object explorer - click database - right mouse - new database. new database

1-2. In the new Database window - Database name, click the Enter Name - ok button. new Database window

1-3. You can see that a new database has been created in the Object explorer. Object explorer.

1-4. Let me check the location of the file where the new database was created.

  • Select database and right-click - Properties. the new database was created.

1-5. Check the path of the file. Check the path of the file.

1-6. Check the MDF, LDF files on my computer through the file explorer. Check the MDF

1-7. Select from the new database and check. the new database

How to delete database with the UI tool with SQL Server Management Studio (SSMS)

  • Run SSMS and connect to the SQL Server.
  • Please work carefully when deleting the database.

2-1. Select Object explorer - click database - right mouse - Delete. Select Object explorer

2-2. In the Database Delete window, check Close existing connections, and click the OK button. In the Database Delete window

How to create a new database using Transact-SQL

3-1. Select a new questionnaire from the standard toolbar.

  • Copy Curry at the bottom and create it. File path can be changed.
USE master;
CREATE DATABASE [samDBsql]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'samDBsql', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\samDBsql.mdf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
 LOG ON 
( NAME = N'samDBsql_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\samDBsql_log.ldf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
 WITH LEDGER = OFF

File path can be changed.

3-2. Click Object explorer - Refresh. Click Object explorer

3-3. You can check the new database. the new database.

3-4. Check the MDF, LDF files on my computer through Windows File Explorer. Check the MDF, LDF files

How to delete database using Transact-SQL.

4-1.

USE [master];

ALTER DATABASE [samDBsql] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE;

DROP DATABASE [samDBsql];

DROP DATABASE

4-2. Confirm database deletion.

Confirm database deletion.

Finish the MS SQL database

This is how to deal with errors

Msg 3702, Level 16, State 4, Line 3 database “samDBsql” is currently in use and cannot be deleted.
🔔 Answer: Database cannot be deleted if another session is connected.

USE [master];
ALTER DATABASE [samDBsql] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE;

MSSQL database precautions

  • Be sure to get a backup before deleting the database.
  • There are many ways to back up, but I will post it later.
  • It is recommended that you work with T-SQL for data backup and deletion.

Categories:

Updated:

Leave a comment