I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 years. I like video games and reading books.
This article shows how we create an SQL Server 2005 database. Here, we will use SQL Server Management Studio to generate the SQL Script that creates the database. This article also explains, how storage is allocated for the database the files involved in creating the database.
2. Steps to Create a Database
1) Launch the SQL Server 2005 Management Studio by using the 'sa' login.
2) In the left side pane, right-click the Database node and select New Database from the Context menu.
3) The New Database option will open a dialog where we can provide database creation parameters. The screenshot shows a portion of the dialog:
4) In the Database Name field, we type Sample. This will fill the Data and Log file names for us in the Database files grid. But, we can override these default file names supplied by the dialog by our own Database file names. The screenshot below shows how other parameters are auto-filled after typing the Sample in the Database name field:
In the above picture, the blue box shows the files created by the dialog based on what we supplied on the database name field. In the Grid, File Type column denotes that Sample.mdf is a database file name. The Table Data and all other database data which we create goes and sits here. The database Logs are written to the file Sample_Log.LDF. MDF is the Main Database File, and LDF is used for logging purpose.
5) Scroll the database files grid to the left to see the path in which these files are stored. If required, you can provide a different path by clicking the ellipsis button (marked in red) shown below:
6) If we click the OK button on the dialog, the database will be created for us. But, we have no need to click the OK button now. Instead, we will click on the Down Arrow button next to Script and select the First option “Script Action to New Query Window” as shown below:
7) Now we can use this script to create the database. This script will take care of all the actions we did in the dialog. Also, it is useful when we deploy the database application on our client machine. We can run this script through a setup program will create the database on the client machine. Now, we click the Execute Button to create the database. If the database is not displayed under the database folder, right-click the database folder and select the refresh.
3. How the Data Is Organized in the MDF File
The database Sample is now ready. When we create a table and insert data into it, the size of the Sample.mdf gets increased as all our table data goes to this file. The data is stored in the form of something called Extents. A database file has multiple Extends when the data is increased. Maximum of 8 Pages constitutes a single extent. A Page is a block of storage, which has a maximum of 8 KB in size. Database row can be a maximum of 8 KB in size. This excludes the large data type columns say Text, Image, Varchar(max), etc. This means, a database row excluding the large data type can fit into a single page. Also, note that large data type values are stored in a separate page and location offset is stored in the page which has normal data like integer and char. Below picture shows how the storage in physical media is organized:
© 2018 sirama