SQL Server TINYINT Data Type

TINYINT is one of the data types used to store integer values in SQL Server, and it can store integers between 0 and 255. This data type only takes up 1 byte of storage space and can be used as a column data type in a table.

Syntax

The syntax for TINYINT is as follows:

TINYINT

Usage

The TINYINT data type is commonly used to store binary flags or status codes because it occupies very little storage space and is space-saving. For example, the TINYINT data type can be used to store gender and age range information, or it can be used instead of the INT data type when space needs to be saved.

Examples

Here are two examples of using the TINYINT data type:

Example 1

CREATE TABLE Sales (
    SalesID INT PRIMARY KEY,
    SalesPerson NVARCHAR(50),
    SaleDate DATE,
    SaleAmount MONEY,
    Status TINYINT
);

INSERT INTO Sales (SalesID, SalesPerson, SaleDate, SaleAmount, Status)
VALUES (1, 'John Doe', '2022-01-01', 100.00, 1);

INSERT INTO Sales (SalesID, SalesPerson, SaleDate, SaleAmount, Status)
VALUES (2, 'Jane Smith', '2022-01-02', 200.00, 0);

SELECT * FROM Sales;

In the above example, we created a table named Sales and set the data type of the Status column to TINYINT. Then, we inserted two sales records into the table and specified the values of Status as 1 and 0. Finally, we selected all columns from the table and output the results.

Example 2

CREATE TABLE Students
(
    StudentID INT PRIMARY KEY,
    StudentName NVARCHAR(50),
    Age TINYINT,
    Gender TINYINT
);

INSERT INTO Students (StudentID, StudentName, Age, Gender)
VALUES (1, 'John Doe', 20, 1);

INSERT INTO Students (StudentID, StudentName, Age, Gender)
VALUES (2, 'Jane Smith', 22, 0);

SELECT * FROM Students;

In the above example, we created a table named Students and set the data types of the Age and Gender columns to TINYINT. Then, we inserted two student records into the table and specified their ages and genders. Finally, we selected all columns from the table and output the results.

Conclusion

TINYINT is one of the data types used to store integer values in SQL Server, and it can store integers between 0 and 255, occupying only 1 byte of storage space. The TINYINT data type is commonly used to store binary flags or status codes and can be used instead of the INT data type when space needs to be saved.