Introduction to MySQL TINYINT
TINYINT is the MySQL data type that can be assigned o the columns of the table in which we need to store the whole numbers, and we are aware that the range of the numbers that we will store in that column will not exceed the range of the TINYINT data type. In this article, we will learn about the TINYINT datatype of MySQL, its range and storage size, and also learn about certain attributes related to TINYINT datatype like signed, unsigned, auto_increment, ZEROFILL, and display width. We will also discuss where and in which scenarios the TINYINT data type is mostly used.
Range and Storage Space for Tinyint datatype in MySQL
TINYINT datatype is the extension of the standard SQL integer type. Each integral datatype of MySQL can be declared either signed or unsigned. Signed data types specify that the negative, integral values can also be stored in that column while unsigned always contains the positive integer values. By default, the datatype of any integral nature in MySQL is considered as a signed data type. The same goes with the TINYINT data type; by default, it is signed TINYINT in its functionality. It takes 1 byte that is 8 its to store the value of the TINYINT data type. The range of the signed TINYINT datatype from minimum to the maximum value is -128 to 127, while for unsigned TINYINT datatype, it is 0 to 255.
Usage of TINYINT datatype
This data type is most commonly used to store the boolean values in MySQL. Whenever the datatype of the column is declared and specified as boolean or bool in the table, it is internally automatically converted to the TINYINT(1) datatype.
Another usage of TINYINT datatypes is used to declare the table’s primary key that will store the autoincremented value, and you are sure that the range of the value stored in this column will not exceed the range of the TINYINT data type. That means in case if your table is going to contain only a few records and you want to declare an integral column that will store the autoincremented whole numbers; then instead of using the MySQL int or integer data type, you will declare the datatype of the column as TINYINT.
Example of MySQL TINYINT
Let us create a table that will contain the column as the TINYINT data type that will be the primary key and one more column that will be of the TINYINT datatype but will not be a primary key. For example, we will create a table named subjects inside the educba database that exists on my database server. For this firstly, We will have to use the educba database for which we will execute the following query –
use educba;
that will give the following output –
Further, we will create the table named subjects that will contain subject_id as the primary key column of TINYINT datatype and one more unsigned TINYINT column named pages wing the following query –
CREATE TABLE subjects (
subject_id TINYINT AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(255),
pages TINYINT UNSIGNED
);
that gives the following output –
Whenever a null or zero value is put in the auto_increment column, the sequence maintained automatically inserts the value incremented by 1 from the last maximum value inserted. Thus, it begins by inserting 1 value. In case if a non-null and non-zero value is inserted in the auto_increment value, then that value is accepted and inserted in that column, and the value of the sequence is set to that value +1 for further reference.
Let us insert some values in the table subjects using the following query –
INSERT INTO
subjects(description,pages)
VALUES
('MySQL',136),
('Angular',200),
('Java',96);
that gives the following output –
Let us check the inserted records by firing the command –
select * from subjects;
that gives the following output –
We can see that the subject_id column has got the default autoincremented values as 1,2, and 3. Let us insert one record mentioning the subject_id column value as follows –
INSERT INTO
subjects(subject_id,description,pages)
VALUES
(126,'Maven',156);
that gives the following output –
Let us check records of subjects table by using the same select query that gives the following output –
select * from subjects;
We can see that 126 value is inserted into the subject_id column. Now, when we enter the record without mentioning the subject_id value, it will consider 127 as its next value as the sequence is set to that value after inserting 126 valued columns of subject_id. Executing the following command –
INSERT INTO
subjects(description,pages)
VALUES
('Hibernate',99);
gives following output –
And after selecting the records of the table, it shows the following content –
select * from subjects;
After inserting the record in the subjects table without subject_id specification such as following –
INSERT INTO subjects(description,pages) VALUES ('javascipt',105);
gives the error saying the 127 id is duplicated because the range f the TINYINT datatype of signed type by default exceeds, and the output is as follows –
Let us see what happens if we specify the value of pages column of unsigned TINYINT type greater than 127 say 159 using the following insert query –
INSERT INTO
subjects(subject_id,description,pages)
VALUES
(4,'Typescript',244);
that gives the following output –
and works completely fine because the range of unsigned tinyint is 255 while signed is 127.
After selecting the records, we see the following output –
select * from subjects;
Display width and ZEROFILL Attribute
Mysql allows specifying the display width of the column by specifying the display width in the data type followed by the () brackets inside which the integral value of the width can be specified. This does not tell about the storage size; instead, it stands for the format to display the values. When the ZEROFILL attribute is used for the column, then all the blank spaces in the format specified by the display width are filled with the zeroes, and the number is displayed. For example, if I alter the pages column of subjects table to ZEROFILL attribute and specify the display width as 3 using the following command –
ALTER TABLE subjects MODIFY COLUMN pages TINYINT(3) ZEROFILL;
that gives the following output –
By default, when a column is assigned the ZEROFILL property, it is automatically considered as the unsigned column. Let us now select the records of the subject table and observe the pages column values display format that should be 3-digit format with blank spaces replaced with 0.
The select query gives the following output –
select * from subjects;
Conclusion
The TINYINT data type is most often used to store the boolean values or values that will have a small range of less than 255 in the case of positive integers and less than 127 in the case of signed integers. It can be assigned AUTO_INCREMENT, ZEROFILL attributes, and its display width can be specified by using () brackets.
Recommended Articles
This is a guide to MySQL TINYINT. Here we discuss the Range and Storage Space for Tinyint datatype in MySQL along with the usage and example. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses