Professional Documents
Culture Documents
Basics
This week we are going to learn about the files that make up
a database and we will also create our first database. The
topics for this article include:
- Database Files
- Database Filegroups
- Creating a Database
(I)
Database Files
Databases in SQL Server have an underlying data storage
structure made up of two or more system files. Each
database must have at least one data file, used for storing
database objects (tables, views, stored procedures, etc), and
one log file, used for storing transaction information. In
addition, each database must have its own files; you can't
share files between databases. (II)
Data files store objects and the data inside objects. The first
data file you create is known as the "Primary data file." If you
need to create additional data files for your database, they
must be created as "Secondary data files." (III)
Log files, on the other hand, are used to track changes in the
database and have only one file type. In the event of a
system failure, the log files can be used to repair the
database. We will look at how SQL Server tracks database
modifications when we start talking about transactions. (IV)
The following table summarizes the three file types.
File Type
Primary data
file
Description
Each database must
have one, and only one,
Primary data file. This file
type stores the system
tables, data, and also
keeps track of the other
files that makeup the
database. Primary data
Secondary
data files
Transaction log
files
(V)
A simple database may have all its data stored in the
Primary data file and may only have one log file. On the
other hand, a more complex database may have the Primary
data file, three Secondary data files, and two log files. By
using multiple files a more complex database can spread its
load across as many physical disks as possible. (VI)
Now that we know about the files that makeup our database,
let's look at how our data is being stored inside the files.
Database Filegroups
Creating a Database
Ok, enough with that stuff...let's create our first database!
Start by opening Enterprise manager and expand the tree
view until you see the "Databases" folder of your server.
The top portion of the Data Files tab shows you all the data
files that make up this database. As you can see Enterprise
Manager has already added the Primary Data file for us. The
logical file name is FirstDB_Data and the physical file is
named FirstDB_Data.MDF located in the Data folder that
was specified when we installed SQL Server. The logical file
name is the name that SQL Server uses internally to
reference the file, whereas the physical file name is the
name of the operating system file.
In addition to the logical file name and physical
name/location, we also can set the Initial size for each file. If
you know you will be loading data into your database and
have a general idea of its size, you can set this value to
avoid automatic file growth (more on this in a second). The
last column allows the selection and creation of file groups.
The lower half of the Data Files tab allows you to set the
Automatic file growth for each file that makes up the
database. When a database has used all available space in
a data file, you can have SQL Server automatically expand
the file as needed. The File Growth option allows you to set
how much SQL Server will expand the data file. You can
select a fixed amount in megabytes or enter a percentage of
the current file size to grow the file by. You can also set a
maximum file size for the data file. Once a file reaches its
maximum size, no more automatic file growth will take
place.
As a general rule, it is best to have files expand as few times
as possible because expanding files causes a performance
hit to SQL Server.
Click the "Transaction Log" tab.