Database Fundamentals #3: What's in a Database?
Get an introduction to the guts of the file storage for databases and learn why it’s a good idea to understand what this stuff is and how it works.
Join the DZone community and get the full member experience.Join For Free
it’s worth noting that a lot of people will never need to create their own database. you may never create your own tables or other data structures, either. you may only ever run backups and restores and manipulate the security on the system and let application installs create databases for you. that’s completely understandable and perfectly in line with the needs of many businesses and many accidental dbas. however, it’s a good idea to understand what this stuff is and how it works as part of understanding sql server.
a database is actually files
you need to store information that you want to be able to retrieve later. it’s necessary that you organize that information. if you were working with a word processing program, you store different documents in different files. you really wouldn’t put all of your documents into a single, large file. sql server functions in a very similar manner. while you have a server, you’re not going to simply store all the various types of information necessary to run your business in one large pile within that server. instead, you’re going to organize that information. the initial organizational mechanism for sql server is the database. a database allows you to keep sets of information in separate storage areas. further, it allows you to isolate the security for those different sets of information so that you can control who gets to see or modify that data.
within a database, there are these things called tables. tables are how the relational storage gets defined. the information that you’ve organized for storage in a given database will be added to these tables. you will be able to add or remove data to the database by way of these tables. you’ll also be able to retrieve the information from the database from these tables.
in addition to tables in the database, there are a number of different constructs that will help you manage the information within your database that are also stored in the database. these include a number of different types of objects that help you manipulate the data such as views, stored procedures, and functions. you also get a full set of security objects such as roles and users.
databases are actually made up of files that are stored somewhere on the windows file system. these files are in a proprietary, binary format and cannot be read directly except through sql server. writing data to these files is one of the most expensive and time-consuming operations within sql server (although in most cases, the time involved is measured in microseconds). because of this expense, it’s important to understand that your databases are defined by files and that it matters where they are stored as part of your understanding of the database.
two types of files
you could simply store your data in a text file or a spreadsheet and plenty of people do. but when you need multiple people to access it at the same time, updating some of the dates, deleting some, inserting new information, all at the same time, those other storage mechanisms become very problematic. that’s why you need to use a database. because sql server has to do so much different kinds of work with the data that it’s storing, it’s come up with some different mechanisms for performing those actions. databases are structured around two different types of files that store different kinds of information. you can refer to this figure as we go through the two types.
note : there are actually several other file types that can be added to a database, but we’re talking fundamentals here, so we’ll keep it straight forward and simple for now.
1. data file
the first type of file needed to define your database is the data file. the data file is easy to understand and explain. this type of file is where the information that you write to your database is going to be stored. any given database can be made up of multiple data files. the data files can be placed on more than one hard drive with your system. if you have more advanced storage mechanisms, such as storage area networks (san), you may have other constructs than simple hard drives on a server, but they will be mapped to the windows operating system as drives and sql server can use those for storage of the data files as well.
2. log file
the second type of file needed to create a database is the log file. a log file is a slightly more complex thing to understand than the data file. a log file records every transaction that takes place within the database. a transaction occurs when data is manipulated in some way within the system. these manipulations can be updates to existing data, adding new data by inserting it into a table, or deletes of data from tables. all these actions cause information to be written to the log file. there are a number of other functions also associated with the log file. these files are usually much smaller than the data files because the entries into the log file only need to be kept around until all the data has been successfully written to the data file. because writing to the data file is subject to different kinds of failures, log files are kept to help deal with those failures. once information has been written to the data file, the log entry can be removed. the process to clean up log files will be discussed in detail in another post.
when you’re initially creating the files you can adjust their size. you can adjust that size, both up and down, after creating the files as well. each file can also be set to grow automatically and this is complicated enough so it’s worth spending a little more time understanding exactly what it means.
managing files on databases can be a lot of work if you have a lot of databases. you’ll have to check for the available space and then set the files to larger sizes when there is enough. one way around all this manual work is to use the autogrow property settings on the database.
a word of caution : be very careful using this setting. you can fill a drive and cause your server to go offline.
setting a database to autogrow means that the database will automatically adjust the file size upwards when it starts to run out of room. many people use this setting and many applications set it to on when they’re installed. you should set an upper growth limit on the files in order to avoid that problem. you can set the files to grow by a percentage of the database or in fixed sizes. for smaller databases, growing by percentages can work, but as the database expands in size, growing by percentages becomes a longer and longer process. the best practice is to set the growth to a particular value instead of a percentage. the details on how to set all these will be covered when databases get created.
where to place your files
when you install sql server, you have the option of defining where your database files will be placed. you can also adjust this through the server properties window. it’s always best to know where you are going to be placing files when you create a database in order to be sure that you have enough space on that drive. to see this location, connect to your server as was outlined in database fundamentals #2 . once connected, in the object explorer window, right-click on the server name itself. this will bring up a context menu. select the properties menu choice at the bottom of that menu. this will open the server properties window and you will be in the default general tab. select the database settings tab and you will see something very similar to this:
the interesting area to look at here is at the bottom of the window in the section titled database default locations. you’ll see three different directories listed, one each for data, log and backup. by clicking on the ellipsis at the right side of the entry you can bring up a default file browser window to make a change to the default location of your log or data files. you can also modify them by typing directly into the text box with either a physical path as shown above or using a universal naming convention (unc) path.
the goal for placing files should be to attempt to separate functionality as much as you can. if you have the capability, you’re better off separating your operating system files from your data and log files. this means placing the data and log files on something other than the c:\ drive, if possible. further, you should at least consider storing the data and log files on completely separate drives as well. this allows for the maximum throughput of data to the files on the drives.
once you’ve determined that the storage location is appropriate, if you’ve made changes, click on the ok button to save those changes. if you haven’t made any changes, or you don’t wish to save the changes you made, click the cancel button. either of these clicks will close the server properties window.
each of the two file types has several properties in addition to the physical location that will matter when you create your databases. part of the physical location will be the file name and extension. these can be any valid windows operating system name and extensions. by default, the extensions will usually be
for the data file and
for the log file. you can change these if you want, but it could lead to confusion since most people use the defaults as a best practice. there is a logical file name that allows you to refer to the file for operations within the database without having to refer back to the full file location including the drive. the logical name does not have to match the physical name at all, but usually, they do.
that introduces the guts of the file storage for databases. next post we’re going to actually create a database on the server, which is a heck of a lot easier than this post makes it feel like.
Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.