You are reading the article Learn The Usage And Example Of Mysql Tinyint updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Learn The Usage And Example Of Mysql Tinyint
Introduction to MySQL TINYINTTINYINT 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.
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
Range and Storage Space for Tinyint datatype in MySQLTINYINT 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 datatypeMySQL 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 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. 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 –
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 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 the records of the subjects table by using the same select query that gives the following output –
select * from subjects;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 –
And after selecting the records of the table,
select * from subjects;it shows the following content –
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 –
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);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 –
Display width and ZEROFILL AttributeWhen 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 –
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 –
ConclusionThe 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 ArticlesWe hope that this EDUCBA information on “MySQL TINYINT” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Learn The Usage And Example Of Mysql Tinyint
Update the detailed information about Learn The Usage And Example Of Mysql Tinyint on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!