Introduction
In the previous lessons, you became familiar with the SQL Server environment and the SSMS interface. In this lesson, you will create a working SQL Server database and examine its basic storage structure. A database is not only a logical container for tables; its data is stored in physical files, while database changes are tracked through the transaction log.
By the end of this lesson, you will be able to use CREATE DATABASE and ALTER DATABASE, explain the difference between data files and log files, add a filegroup, configure file growth settings, and change basic database options.
The basic physical structure of a database
A SQL Server database contains at least one data file and one transaction log file. Data files commonly use the .mdf or .ndf extension, while log files commonly use the .ldf extension.
- Primary data file: The main database file, usually using the
.mdfextension. System objects and objects assigned to the default filegroup may be stored there. - Secondary data files: Additional
.ndffiles that can be used to expand capacity or organize data across different storage locations. - Transaction log file: Records database changes and supports transaction rollback and recovery operations. It usually uses the
.ldfextension. - Filegroup: A logical collection of one or more data files. Log files do not belong to data filegroups.
The distinction between a file and a filegroup is important. A file is a physical storage unit, while a filegroup is a logical management layer for data files. Tables and indexes can be assigned to a specific filegroup.
Creating a database with SSMS
To create a database in SSMS, right-click the Databases node in Object Explorer and select New Database. After entering the database name, you can edit the properties of the data and log files.
- Connect to the SQL Server instance through Object Explorer.
- Right-click the Databases node.
- Select New Database.
- Enter
TrainingStorein the Database name field. - Review the initial size, maximum size, and autogrowth values for the data and log files.
- Open the Options page and review basic settings such as the recovery model.
- Click OK to create the database.
SSMS generates an appropriate CREATE DATABASE operation behind the interface. Understanding the corresponding T-SQL makes graphical configuration easier to verify, document, and repeat.
Creating a simple database with T-SQL
The following statement creates a simple database using the default data and log directories configured for the SQL Server instance.
USE master;
GO
CREATE DATABASE TrainingStore;
GOAfter the statement completes, refresh the Databases node in Object Explorer to display the new database. You can also verify the result with T-SQL:
SELECT name, database_id, state_desc, recovery_model_desc
FROM sys.databases
WHERE name = N'TrainingStore';Defining file properties explicitly
In practical environments, explicitly defining the file path, initial size, maximum size, and growth increment provides more predictable administration. Before running the following example, create the C:\SQLData and C:\SQLLog folders and verify that the SQL Server service account can access them.
USE master;
GO
CREATE DATABASE TrainingStore_Configured
ON PRIMARY
(
NAME = TrainingStore_Configured_Data,
FILENAME = 'C:\SQLData\TrainingStore_Configured.mdf',
SIZE = 100MB,
MAXSIZE = 1GB,
FILEGROWTH = 64MB
)
LOG ON
(
NAME = TrainingStore_Configured_Log,
FILENAME = 'C:\SQLLog\TrainingStore_Configured.ldf',
SIZE = 64MB,
MAXSIZE = 512MB,
FILEGROWTH = 64MB
);
GONAME is the logical file name inside SQL Server. FILENAME is the physical Windows path. These names do not have to be identical. SIZE defines the initial size, MAXSIZE defines the upper limit, and FILEGROWTH defines how much space is added when the file needs to expand.
Creating a filegroup
Filegroups can be useful in larger databases when data must be organized across storage locations, capacity must be expanded, or specific objects should be placed on a designated disk. The following example creates a database, adds an ArchiveGroup filegroup, and adds a data file to that filegroup.
USE master;
GO
CREATE DATABASE TrainingStore_Files
ON PRIMARY
(
NAME = TrainingStore_Files_Data,
FILENAME = 'C:\SQLData\TrainingStore_Files.mdf',
SIZE = 100MB,
MAXSIZE = 1GB,
FILEGROWTH = 64MB
)
LOG ON
(
NAME = TrainingStore_Files_Log,
FILENAME = 'C:\SQLLog\TrainingStore_Files.ldf',
SIZE = 64MB,
MAXSIZE = 512MB,
FILEGROWTH = 64MB
);
GO
ALTER DATABASE TrainingStore_Files
ADD FILEGROUP ArchiveGroup;
GO
ALTER DATABASE TrainingStore_Files
ADD FILE
(
NAME = TrainingStore_Files_Archive,
FILENAME = 'C:\SQLData\TrainingStore_Files_Archive.ndf',
SIZE = 100MB,
MAXSIZE = 2GB,
FILEGROWTH = 64MB
)
TO FILEGROUP ArchiveGroup;
GOWhen creating a table, you can assign it to this filegroup by adding ON ArchiveGroup to the table definition. Tables and file placement will be covered in more detail in later lessons.
USE TrainingStore_Files;
GO
CREATE TABLE dbo.ArchiveNote
(
ArchiveNoteID int IDENTITY(1,1) NOT NULL,
NoteText nvarchar(200) NOT NULL
) ON ArchiveGroup;
GOInspecting files and configuration
You can inspect logical names, physical paths, sizes, and growth settings through the database catalog views:
USE TrainingStore_Files;
GO
SELECT
name AS logical_name,
physical_name,
type_desc,
size * 8.0 / 1024 AS size_mb,
max_size,
growth,
is_percent_growth
FROM sys.database_files;
GOThe size value is stored as a number of 8 KB pages, so size * 8.0 / 1024 converts it to megabytes. If is_percent_growth is 1, the growth setting is percentage-based; if it is 0, the value represents a fixed amount.
You can also view general database information with:
EXEC sys.sp_helpdb N'TrainingStore_Files';Changing file settings with ALTER DATABASE
After a database has been created, its file settings can be changed with ALTER DATABASE. For example, the following statement sets the data file growth increment to 128 MB and the maximum size to 2 GB:
ALTER DATABASE TrainingStore_Files
MODIFY FILE
(
NAME = TrainingStore_Files_Data,
FILEGROWTH = 128MB,
MAXSIZE = 2GB
);
GOYou can also add another data file to an existing filegroup:
ALTER DATABASE TrainingStore_Files
ADD FILE
(
NAME = TrainingStore_Files_Secondary,
FILENAME = 'C:\SQLData\TrainingStore_Files_Secondary.ndf',
SIZE = 128MB,
MAXSIZE = 2GB,
FILEGROWTH = 128MB
)
TO FILEGROUP PRIMARY;
GOBasic database options
SQL Server databases include options that control important behavior. At an introductory level, it is especially useful to understand the recovery model and the purpose of automatic shrinking.
- Recovery model: Controls transaction log behavior and the recovery scenarios supported by the database. The available models are
SIMPLE,FULL, andBULK_LOGGED. - Compatibility level: Defines the compatibility behavior associated with a SQL Server version.
- AUTO_SHRINK: Attempts to reduce files when unused space is available. In general-purpose systems, repeated shrinking and growth can cause fragmentation and performance problems, so this option should be used carefully.
To set the recovery model of a training database to simple:
ALTER DATABASE TrainingStore_Files
SET RECOVERY SIMPLE;
GOTo check the current recovery model:
SELECT name, recovery_model_desc
FROM sys.databases
WHERE name = N'TrainingStore_Files';Before changing production database options, evaluate backup, recovery, and business continuity requirements. Production settings should be selected as part of a complete backup and recovery plan rather than for convenience alone.
Common mistakes
- Missing folders: SQL Server does not always create the Windows folder specified in
FILENAME. Check that the folder exists and that the service account has access. - Confusing logical and physical names:
NAMEis the logical SQL Server name;FILENAMEis the physical path. - Trying to place a log file in a filegroup: Log files are independent of data filegroups.
TO FILEGROUPapplies to data files. - Using percentage growth without planning: As a file becomes larger, each percentage-based growth event also becomes larger. Fixed MB increments are often more predictable.
- Treating automatic shrinking as a general solution: Repeated shrinking can lead to repeated growth and fragmentation. Plan initial size, growth increments, and disk capacity instead.
- Running commands in the wrong database: Run database creation and alteration statements in the appropriate context, usually
master, and explicitly select the target database before creating objects.
Short practice exercise
Complete the following exercise in SSMS:
- Create a database named
CourseLab. - Configure its primary data file with an initial size of 64 MB and a growth increment of 64 MB.
- Configure its log file with an initial size of 64 MB and a growth increment of 64 MB.
- Create a filegroup named
HistoryGroup. - Add a data file named
CourseLab_History.ndfto the filegroup, with an initial size of 128 MB. - Use
sys.database_filesto list the logical names, physical paths, and growth settings of all files. - Query the recovery model and record the result.
If you receive a file path error, check whether the folders exist and whether the SQL Server service account has the required permissions.
Conclusion
In this lesson, you learned that a SQL Server database consists of logical objects as well as physical data and log files. You created databases through SSMS and T-SQL, defined file properties explicitly, added a filegroup, and modified existing settings with ALTER DATABASE.
Choosing the initial size, growth increment, maximum size, and file locations deliberately helps reduce future capacity and performance problems. In the next stage, you will create tables inside this database, select appropriate data types, and define basic constraints.
Sources
- https://learn.microsoft.com/sql/t-sql/statements/create-database-transact-sql?view=sql-server-ver16
- https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-file-and-filegroup-options?view=sql-server-ver17
- https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-properties-files-page?view=sql-server-ver17
- https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/database-file-operations/considerations-autogrow-autoshrink