How to Create Database using T-SQL command?

Below are the T-SQL commands to create database

1. Default command:
This command will create database files in default directory.

USE [master]
GO
IF DB_ID(N'TestDB') IS NULL
CREATE DATABASE [TestDB]
GO

2. Command to specify File name, path, and size:
This command will create database files in the directory mentioned mentioned in the T-SQL command.


USE [master]
GO
IF DB_ID(N'TestDB') IS NULL
BEGIN
CREATE DATABASE [TestDB] ON PRIMARY
(
   NAME = N'TestDB'
   ,FILENAME = N'C:\MSSQL\Data\TestDB.mdf'
   ,SIZE = 1024KB
   ,MAXSIZE = 2048GB
   ,FILEGROWTH = 1024KB
)
LOG ON
(
   NAME = N'TestDB_log'
   ,FILENAME = N'C:\MSSQL\Data\TestDB.ldf'
   ,SIZE = 1024KB
   ,MAXSIZE = 2048GB
   ,FILEGROWTH = 10%
)
END
GO

No comments:

Post a Comment