SQL script for database creation

Techsavy
Techsavy used Ask the Experts™
on
Hi,

Can anyone help me write a SQL script to create a Database?

Thanks,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The simple SQL Script for creating the database could be:
CREATE DATABASE myDatabaseName

Open in new window

But it will be useful to read a little more, if you want to move further than creating just a database.
Anuradha GoliSystems Development / Support Specialist

Commented:
You can read this link
http://msdn.microsoft.com/en-us/library/ms176061.aspx
Or simply can use to create simpel database
CREATE DATABASE dbname

Open in new window

Commented:
Everything you need about Creating a database can be found here: http://msdn.microsoft.com/en-us/library/ms176061.aspx

What may suffice rather than going through the details is as follows:

USE master;
GO
CREATE DATABASE <database_name>
ON
( NAME = <Database_data_file_logical_name>,
    FILENAME = '<Database_data_file_physical location and name>C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\saledat.mdf' )
LOG ON
( NAME = <Database_log_file_logical_name>,
    FILENAME = '<Database_log_file_physical location and name>C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\salelog.ldf' ) ;
GO

Commented:
It depending on the decision of the DBA how it should be.
Using script then first check is, is it already exist the same database on the server if not then create new database that you want.

USE [master]
GO
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'MarketIndexData')
BEGIN

CREATE DATABASE [TestDatabase] ON  PRIMARY
( NAME = N'TestDatabase',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDatabase.mdf' ,
SIZE = 3072KB , FILEGROWTH = 1024KB ),
FILEGROUP [Secondary]
( NAME = N'TestDatabase_2',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDatabase_2.ndf' ,
SIZE = 3072KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'TestDatabase_log',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDatabase_log.ldf' ,
SIZE = 1024KB , FILEGROWTH = 10%)
END
GO
Commented:
USE [master]
GO
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'TestDatabase')
BEGIN

CREATE DATABASE [TestDatabase] ON  PRIMARY
( NAME = N'TestDatabase',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDatabase.mdf' ,
SIZE = 3072KB , FILEGROWTH = 1024KB ),
FILEGROUP [Secondary]
( NAME = N'TestDatabase_2',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDatabase_2.ndf' ,
SIZE = 3072KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'TestDatabase_log',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDatabase_log.ldf' ,
SIZE = 1024KB , FILEGROWTH = 10%)
END
GO

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial