SQL Server Database Engine Data Types
This document provides a comprehensive overview of the data types supported by the SQL Server Database Engine. Understanding these data types is crucial for efficient database design, data integrity, and performance optimization.
Introduction to Data Types
Data types define the type of data that a column, variable, expression, or parameter can hold. Each data type has a specific set of allowable values, storage size, and behavior. SQL Server offers a rich set of data types to accommodate various data storage and manipulation needs.
Categories of Data Types
SQL Server data types can be broadly categorized as follows:
- Exact Numerics: Integers and monetary values.
- Approximate Numerics: Floating-point numbers.
- Date and Time: For storing date and time values.
- Character Strings: For storing textual data (Unicode and non-Unicode).
- Binary Strings: For storing raw binary data.
- Other Data Types: Including XML, spatial data, JSON, and unique identifiers.
Common Data Types and Their Usage
Integer Data Types
| Data Type | Description | Storage | Range |
|---|---|---|---|
TINYINT |
Unsigned integer. | 1 byte | 0 to 255 |
SMALLINT |
Signed integer. | 2 bytes | -32,768 to 32,767 |
INT |
Signed integer. | 4 bytes | -2,147,483,648 to 2,147,483,647 |
BIGINT |
Signed integer. | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Approximate Numeric Data Types
| Data Type | Description | Storage | Precision |
|---|---|---|---|
FLOAT |
Floating-point number. | 4 or 8 bytes | Approximately 7 or 15 digits |
REAL |
Floating-point number. | 4 bytes | Approximately 7 digits |
Date and Time Data Types
| Data Type | Description | Storage | Range |
|---|---|---|---|
DATE |
Stores date only. | 3 bytes | 0001-01-01 to 9999-12-31 |
TIME |
Stores time only. | 3 to 5 bytes | 00:00:00.0000000 to 23:59:59.9999999 |
DATETIME |
Stores date and time. | 8 bytes | January 1, 1753, through December 31, 9999 |
DATETIME2 |
Stores date and time with higher precision. | 6 to 8 bytes | January 1, 0001, through December 31, 9999 |
Character String Data Types
| Data Type | Description | Storage |
|---|---|---|
CHAR(n) |
Fixed-length non-Unicode character data. | n bytes |
VARCHAR(n) |
Variable-length non-Unicode character data. | n + 2 bytes |
NCHAR(n) |
Fixed-length Unicode character data. | 2 * n bytes |
NVARCHAR(n) |
Variable-length Unicode character data. | 2 * n + 2 bytes |
Choosing the Right Data Type
Selecting the appropriate data type for your columns is a critical design decision. Consider the following:
- Data Integrity: Use data types that accurately represent the data being stored to prevent invalid entries.
- Storage Efficiency: Choose the smallest data type that can accommodate the full range of possible values to minimize disk space and memory usage.
- Performance: Data types can impact query performance. For example, numeric types are generally faster for calculations than string types.
- Application Requirements: Ensure the data type is compatible with the programming languages and applications that will interact with the database.
Advanced Data Types
SQL Server also supports more specialized data types:
UNIQUEIDENTIFIER: Stores a 128-bit globally unique identifier (GUID).XML: Stores XML data.JSON: Stores JSON data (supported from SQL Server 2016 onwards).- Spatial types (
GEOMETRYandGEOGRAPHY): For storing location-based data. VARBINARY(max): For storing large binary objects (BLOBs).VARCHAR(max)/NVARCHAR(max): For storing large character strings.
(max) variants of VARCHAR, NVARCHAR, and VARBINARY.
Further Reading
For more detailed information and specific syntax examples, please refer to the official Microsoft SQL Server documentation: