Database Tables  «Prev  Next»

Lesson 5 Basic elements of a Transact-SQL statement, part 1
Objective Use Transact-SQL to create a basic database..

Transact-SQL Statement

To create a database, use the Transact-SQL statement CREATE DATABASE. The only thing that is required along with the statement is a name for the database, such as "employee." You can create a database with all the default values using a Transact-SQL statement as simple as:
CREATE DATABASE employee

Without specifying any additional parameters, the following defaults are created:
  • The filename is employee_Data.MDF and it is located in the SQL Server data directory.
  • The initial size of the database is equal to the size of primary file in themodel database. If you have not changed this value since you installed SQL Server 7, the size is one megabyte.
  • The database size will grow automatically in 10% increments with no maximum.
  • The file location of the transaction log is employee_Log.LDF (in your SQL Server data directory).
  • The initial size of the transaction log is one megabyte.
  • The transaction log size will grow automatically in 10% increments with no maximum.
The series of images below reviews the available syntax for the CREATE DATABASE statement:

create-database1
The diagram above contains Transact-SQL (T-SQL) syntax for creating a database in SQL Server. Below is the extracted T-SQL code:
CREATE DATABASE database_name
[ ON [PRIMARY] 
    [ <filespec> [,...n] ]
    [, <filegroup> [,...n] ]
]
[ LOG ON { <filespec> } ]
[ FOR LOAD | FOR ATTACH ]

Explanation of the Code:
  1. CREATE DATABASE database_name
    • This command creates a new database.
    • Replace database_name with the actual name of your database.
  2. ON [PRIMARY]
    • Specifies that the primary data file should be created.
    • [ <filespec> [,...n] ] allows defining multiple files for the database.
    • [ <filegroup> [,...n] ] lets you assign database files to different filegroups.
  3. LOG ON { <filespec> }
    • Specifies where the transaction log files will be stored.
  4. FOR LOAD | FOR ATTACH
    • FOR LOAD: Used when restoring a database from a backup.
    • FOR ATTACH: Used to attach an existing MDF file to SQL Server.

Example: Creating a Database in SQL Server
CREATE DATABASE SalesDB
ON PRIMARY 
( NAME = SalesData,
  FILENAME = 'C:\SQLData\SalesDB.mdf',
  SIZE = 10MB,
  MAXSIZE = 50MB,
  FILEGROWTH = 5MB )
LOG ON 
( NAME = SalesLog,
  FILENAME = 'C:\SQLData\SalesDB_log.ldf',
  SIZE = 5MB,
  MAXSIZE = 25MB,
  FILEGROWTH = 5MB );

  • Creates a SalesDB database.
  • Stores the data file (.mdf) in C:\SQLData\.
  • Stores the log file (.ldf) in C:\SQLData\.
  • Specifies initial size, max size, and file growth.

  create-database2
  create-database3
  create-database4
  create-database5
  create-database6
  create-database6

create-database6

In the next lesson, we will take a look at control-of-flow logic, which enables you to test the value of a variable and take an action based on that value.

SEMrush Software Target 5SEMrush Software Banner 5