TINYINT
TINYINT is a numeric data type in SQL used for storage of numeric values. It allows for a smaller range of numbers, specifically from -128 to 127 or 0 to 255 if unsigned. It uses a minimum storage footprint of only 1 byte, making it an efficient choice for columns with small numeric values.
Example
CREATE TABLE ExampleTable ( Column1 TINYINT);
INSERT INTO ExampleTable (Column1)VALUES (5), (10), (15);
SELECT * FROM ExampleTable;
Output
Column1 |
---|
5 |
10 |
15 |
Explanation
The TINYINT
type is used in the creation of the ExampleTable
, specifically for Column1
. The INSERT INTO
command is then used to populate Column1
with values 5, 10, and 15. The SELECT * FROM ExampleTable;
command then retrieves all information from ExampleTable
, displaying the inputted TINYINT
values in Column1
. The demonstrated records show that TINYINT
can store small integer values.
Example
Here is an example of creating a table using the TINYINT data type and an insert statement to add a record to the table:
CREATE TABLE Employees( EmployeeId TINYINT PRIMARY KEY, EmployeeName NVARCHAR(100));
INSERT INTO Employees(EmployeeId, EmployeeName)VALUES (1, 'John Doe');
Output
The output does not provide much visual insight as it is the backend execution of the SQL Server. However, when you try to run a select statement such as the one below:
SELECT * FROM Employees;
You can expect the following output:
+------------+--------------+| EmployeeId | EmployeeName |+------------+--------------+| 1 | John Doe |+------------+--------------+
Explanation
In the code example above, a table named Employees
is created with a primary key field EmployeeId
of type TINYINT
, and another field EmployeeName
of type NVARCHAR(100)
.
Then a new record is inserted into the Employees
table with EmployeeId
as 1
and EmployeeName
as John Doe
.
In SQL Server, TINYINT
data type is an integer that supports values from 0 to 255.
Example
CREATE TABLE TestTable ( ID TINYINT);INSERT INTO TestTable(ID) VALUES (8);SELECT * FROM TestTable;
Output
ID---8
Explanation
In the example, a table named ‘TestTable’ is created with a single column ‘ID’ with data type as TINYINT. An integer value ‘8’ is inserted into the table and displayed by selecting all rows from the table.