Home

Creating a Database

     

Visually Creating a Database

To visually create a database, start Microsoft SQL Server and login. In the Object Explorer, right-click Databases and click New Database...

New Database

In the Name text box, type the desired name. Here is an example:

New Database

You can then click OK.

By default, when you create a new database, it would be located in C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA folder. Otherwise, you can change it in the New Database dialog box as follows:

New Database

This would produce:

New Database
 

Creating a Database With SQL Code

The primary command to create a database is:

CREATE DATABASE DatabaseName

Here is an example:

CREATE DATABASE NationalCensus;
GO

You can then execute the code.

If you want to add a primary file, here is an example:

CREATE DATABASE NationalCensus
ON PRIMARY
( NAME = DataRepository, FILENAME = 'C:\Exercises\NationalCensus.mdf')
GO

If you wan to add a log on file, use the following formula:

CREATE DATABASE DatabaseName
ON PRIMARY
( NAME = LogicalName, FILENAME = Path.mdf )
LOG ON
( NAME = LogicalName, FILENAME = Path.ldf )

Here is an example:

CREATE DATABASE NationalCensus
ON PRIMARY
( NAME = DataRepository, FILENAME = 'C:\Exercises\NationalCensus.mdf')
LOG ON
( NAME = DataLog, FILENAME = 'C:\Exercises\NationalCensus.ldf')
GO

  Copyright © 2008-2013 FunctionX