EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MySQL Tutorial MySQL TINYINT
 

MySQL TINYINT

Updated June 1, 2023

MySQL TINYINT

 

 

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 specific attributes related to TINYINT datatypes like signed, unsigned, auto_increment, ZEROFILL, and display width. We will also discuss the main scenarios and places where developers use the TINYINT data type.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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 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 bits, to store the value of the TINYINT data type. The range of the signed TINYINT datatype from minimum to maximum value is -128 to 127, while for unsigned TINYINT datatype, it is 0 to 255.

Usage of TINYINT datatype

MySQL often uses the TINYINT(1) datatype to store boolean values when declaring the column as boolean or bool in the table. Additionally, one can use the TINYINT datatype to declare the primary key for a table and prevent the autoincremented value from exceeding the range of the TINYINT datatype. 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 containing 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 on my 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 –

mysql tinyint output 1

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 –

mysql tinyint output 2

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. 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 sequence value 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 –

mysql tinyint output 3

Let us check the inserted records by firing the command –

select * from subjects;

that gives the following output –

mysql tinyint output 4

We can see that the subject_id column has 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 –

mysql tinyint output 5

Let us check the records of the subjects table by using the same select query that gives the following output –

select * from subjects;

that gives the following output –

mysql tinyint output 6

The system has inserted the value 126 in the subject_id column. If you add a record without specifying a subject_id value, the system will automatically use 127 as the next value in the sequence since the column already has 126 as its highest value. Executing the following command –

INSERT INTO
subjects(description,pages)
VALUES
('Hibernate',99);

gives the following output –

mysql tinyint output 7

And after selecting the records of the table,

select * from subjects;

it shows the following content –

mysql tinyint output 8

After inserting the record in the subjects table without subject_id specification, such as the following –

INSERT INTO subjects(description,pages) VALUES ('javascipt',105);

gives the error saying the 127 ids are duplicated because the range f the TINYINT datatype of signed type by default exceeds, and the output is as follows –

output 9

Let us see what happens if we specify the value of the 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 –

output 10

The maximum value for an unsigned tinyint is 255, while for a signed tinyint, it is only 127.

select * from subjects;

After selecting the records, we see the following output –

output 11

Display width and ZEROFILL Attribute

When working with Mysql, choose the appropriate data type and specify the width within parentheses by entering an integral value inside the parentheses to select the display width of a column. This does not tell about the storage size; instead, it stands for the format to display the values. When you apply the ZEROFILL attribute to a column, it fills any empty spaces in the specified display width with zeroes and displays the number. For example, if I alter the pages column of the subjects table to the 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 –

output 12

If you give a column the ZEROFILL property, it will default to being considered unsigned. 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.

select * from subjects;

The select query gives the following output –

output 13

Conclusion

The TINYINT data type typically stores boolean or small-range values, such as positive integers less than 255 and signed integers less than 127. You can assign AUTO_INCREMENT, and ZEROFILL attributes to it and specify its display width using () brackets.

Recommended Articles

We hope that this EDUCBA information on “MySQL TINYINT” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. MySQL Interval
  2. GROUP BY in MySQL
  3. MySQL Average
  4. MySQL Binlog

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW