Thursday, August 1, 2013

How do you create a database in sql server?

You can use either SSMS or TSQL. But I prefer TSQL.
Syntax: Please do refer online for full syntax
CREATE DATABASE database_name
[ CONTAINMENT = { NONE | PARTIAL } ]
[ ON
      [ PRIMARY ] <filespec> [ ,...n ]
      [ , <filegroup> [ ,...n ] ]
      [ LOG ON <filespec> [ ,...n ] ]
]
There are three key things are important while creating a database.
·         Databasename
·         Datafile
·         Transactionlog
For both data and Transaction log files you need to specify logical and physical names.
Logical name is used with in the sqlserver and physical name specifies the actual path and name on the OS level.
Example:
1) Open SSMS run the following script
Create Database Wineworld
The above code will execute and and create wineworld.mdf data file and wineworld.ldf log file.
Both files will be created under specified default directories Of Sql server during installation.
But this is not a good practice to create database. In the following steps you will know how a database will be created.
1) Create a folder C:\Wineworld\Data for data C:\Wineworld\Log.
2) Launch SSMS run the following script
USE [master]
GO
/****** Object: Database [Wineworld] Script Date: 8/1/2013 9:16:14 PM ******/
CREATE DATABASE [Wineworld] ON PRIMARY
( NAME = N'Wineworld', FILENAME = N'C:\wineworld\Data\Wineworld.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'Wineworld_log', FILENAME = N'C:\wineworld\Log\Wineworld_log.ldf' , SIZE = 39936KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO


No comments:

Post a Comment