Basic Training – Data Types – Part 5


I am continuing my series of talks on fundamental topics like data types. I am proud to be a United States Army Reservist (USAR) Veteran.

Just like boot camp I went to so long ago, I am going nick name the series BASIC TRAINING.

The most basic unit of any database is a TABLE which consists of COLUMNS. A vital decision during the initial database design is to choose the data types that will capture the information you want in the least amount of space.

Today, I am exploring date and time fields.

I am extenting the sample database named [BASIC] that contains a sample schema named [TRAINING]. The snippet below creates a sample table named [DATE_N_TIME] that contains one or more fields for each data type.

Books online describes six data types that are categorized as date, time or the combination of both. I created a field for each type in our new table.

We should test the new created table with values that show the minimum and maximum data points that can be stored.

As a database designer, you should always question the components that make up your database.

One question that you might have is ‘What is the maximum number of bytes that a row can have?’. This is important because data is stored in the *.MDF or *.NDF files as pages ~ 8k. Since a page can only save 8060 bytes, you can figure out how many records can fit on a page and how many bytes are wasted space.

The following code uses the sys.columns table to count the number of fields and calculate the maximum row size.

We can see that 6 columns in the table have a maximum record length of 38 bytes and 212 records will fit into one page. This leaves 4 bytes of wasted space on each data page. The sp_spaceused stored procedure shows us that 1 data and 1 index page has been allocated for the table. This is called a mixed extent.

Last but not least, the sp_help stored procedure displays the details of the table. This includes many different settings that can be choosen as a DDL designer such as computed column, field length, nullabilty, and collation to mention a few.

In summary, when designing a table to use exact date time types, choose the data type that will allow the storage of the information in the least amount of space. Most people choose DATETIME (8 bytes) as the default instead of a SMALLDATETIME (4 bytes). Selecting the correct data type can amount in savings of up to 50%. Next time, I will be going over types classified as binary strings.

Related posts

One Thought to “Basic Training – Data Types – Part 5”

  1. mr pumps

    Itˇs in reality a great and useful piece of information. Iˇm glad that you simply shared this useful info with us. Please keep us informed like this. Thank you for sharing.

Leave a Reply to mr pumps Cancel reply