Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a database using T SQL on a specified location

How to create a database using T SQL script on a specified location? Let's say, I want to create a SQL server database on D:\temp\dbFolder. How to do this?

like image 269
Graviton Avatar asked Sep 23 '08 13:09

Graviton


3 Answers

When you create the new database you specify the location. For example:

USE [master]
GO

    CREATE DATABASE [AdventureWorks] ON  PRIMARY 
    ( NAME = N'AdventureWorks_Data', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf' , SIZE = 167872KB , MAXSIZE = UNLIMITED, FILEGROWTH = 16384KB )
     LOG ON 
    ( NAME = N'AdventureWorks_Log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Log.ldf' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 16384KB )
    GO
like image 180
Leah Avatar answered Nov 18 '22 01:11

Leah


From the SQL Server Books an example where database filenames are explicitely defined:

USE master
GO
CREATE DATABASE Sales
ON 
( NAME = Sales_dat,
   FILENAME = 'c:\program files\microsoft sql server\mssql\data\saledat.mdf',
   SIZE = 10,
   MAXSIZE = 50,
   FILEGROWTH = 5 )
LOG ON
( NAME = 'Sales_log',
   FILENAME = 'c:\program files\microsoft sql server\mssql\data\salelog.ldf',
   SIZE = 5MB,
   MAXSIZE = 25MB,
   FILEGROWTH = 5MB )
GO
like image 32
boes Avatar answered Nov 17 '22 23:11

boes


  1. Create folder on your file system: D:\temp\dbFolder\
  2. Run the script:

    USE master; 
    GO 
    CREATE DATABASE TestDB1  
    ON      ( NAME = Sales_dat, FILENAME = 'D:\temp\dbFolder\TestDB1.mdf') 
    LOG ON  ( NAME = Sales_log, FILENAME = 'D:\temp\dbFolder\TestDB1.ldf'); 
    GO
    
like image 3
landrew Avatar answered Nov 17 '22 23:11

landrew