How to create user database using T-SQL script

Hello Friends Good Evening.. I hope now you are able to create user database using wizard.
Today I will tell you how to create a user database using T-SQL script. Below is the T-SQL code for creating user database.
  1. CREATE DATABASE [Test] ON  PRIMARY 
  2. ( NAME = N'Test', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008\MSSQL\DATA\Test.mdf' , SIZE = 2048KB , FILEGROWTH = 1024KB )
  3.  LOG ON 
  4. ( NAME = N'Test_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008\MSSQL\DATA\Test_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
Lets understand the above T-SQL script in details, I have divided the whole script in four different sections and each section is highlighted with different color. Please see the below description for all colors used in the script.

Green  :- Green color indicates logical file name.

Yellow :- Yellow color indicates physical file name.

Sky :- Sky color indicates size of the database(when created).

Pink :- Pink color indicates file growth rate after reaching its maximum size.

I have numbered the whole script to make you understand about each part of the script.
Lets start to understand all four lines of the script.

First Line :- Create database [Name of the database] On Primary
while creating the database we can put the name of the database in between "create database" and "On primary" stuff. Primary indicates that this database will be created having primary file group as a default file group.

Second Line:-Second line contains all the information about the data file(.mdf) , I have explained all the terms used in the script in my earlier blog. If you are not able to co-related with the previous blog then please let me know.

Third Line :- Log on means where transactional data will be logged by SQL Server.

Fourth line:- Contains all the information about the log file(.ldf).

I hope this article will help you to understand the T-SQL script used for creating user database.
In my next blog we will start learning about basics of creating database objects and metadata stored in the SQL Server. Till then take care and have a great week ahead.

No comments: