Creating a database in Microsoft SQL Server involves using a CREATE DATABASE
statement. Here’s a basic example of how to do it:
Basic Syntax
CREATE DATABASE database_name;
Example
Let's say you want to create a database named MyDatabase
. You would use the following SQL command:
CREATE DATABASE MyDatabase;
Advanced Options
You can also specify additional options such as file locations, sizes, and growth settings. Here’s a more detailed example:
CREATE DATABASE MyDatabase
ON PRIMARY ( NAME = MyDatabase_data, FILENAME = 'C:\Path\To\Your\Folder\MyDatabase_data.mdf', SIZE = 10MB, MAXSIZE = 100MB, FILEGROWTH = 5MB )
LOG ON ( NAME = MyDatabase_log, FILENAME = 'C:\Path\To\Your\Folder\MyDatabase_log.ldf', SIZE = 5MB, MAXSIZE = 50MB, FILEGROWTH = 1MB );
ON PRIMARY ( NAME = MyDatabase_data, FILENAME = 'C:\Path\To\Your\Folder\MyDatabase_data.mdf', SIZE = 10MB, MAXSIZE = 100MB, FILEGROWTH = 5MB )
LOG ON ( NAME = MyDatabase_log, FILENAME = 'C:\Path\To\Your\Folder\MyDatabase_log.ldf', SIZE = 5MB, MAXSIZE = 50MB, FILEGROWTH = 1MB );
Explanation
- NAME: Logical name for the file.
- FILENAME: Physical path to the file on disk.
- SIZE: Initial size of the file.
- MAXSIZE: Maximum size to which the file can grow.
- FILEGROWTH: Increment by which the file will grow when more space is needed.
Steps to Execute
- Open SQL Server Management Studio (SSMS).
- Connect to your SQL Server instance.
- Open a new query window.
- Paste the
CREATE DATABASE
command into the query window. - Execute the query.
This will create a new database with the specified options and file settings. If you don't need to specify advanced options, the basic command is usually sufficient for creating a simple database.