Tutorial on Creating, Altering, and Deleting Databases in SQL Server
Creating a Database in SQL Server
- Using SQL Server Management Studio
- In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that instance.
- Right-click Databases, and then select New Database.
- In New Database, enter a database name.
- To create the database by accepting all default values, select OK; otherwise, continue with the following optional steps.
- ...
- To create the database, select OK.
- Using Transact-SQL
- Connect to the Database Engine.
- From the Standard bar, select New Query.
- Copy and paste the following example into the query window and select Execute. This example creates the database Sales.
USE master;
GO
CREATE DATABASE Sales ON
(NAME = Sales_dat,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5)
LOG ON
(NAME = Sales_log,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\salelog.ldf',
SIZE = 5 MB,
MAXSIZE = 25 MB,
FILEGROWTH = 5 MB);
GO
Altering a Database in SQL Server
- Using Transact-SQL
- Connect to the Database Engine.
- From the Standard bar, select New Query.
- Copy and paste the following example into the query window and select Execute. This example alters the Sales database to add a new filegroup named Sales_fg1.
USE master;
GO
ALTER DATABASE Sales
ADD FILEGROUP Sales_fg1;
GO
Deleting a Database in SQL Server
- Using SQL Server Management Studio
- In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
- Expand Databases, right-click the database to delete, and then select Delete.
- To confirm you want to delete the database, select Yes.
- Using Transact-SQL
- Connect to the Database Engine.
- From the Standard bar, select New Query.
- Copy and paste the following example into the query window and select Execute. This example removes the Sales database.
USE master ;
GO
DROP DATABASE Sales ;
GO
Follow Up: After deleting a database
Back up the master database. If master must be restored, any database that has been deleted since the last backup of master will still have references in the system catalog views and may cause error messages to be raised.
Related Articles
Coming Soon