Introduction to MySQL BigInt
BIGINT is the MySQL data type that can be assigned to the columns of the table in which we want to store the whole numbers and we are aware that the range of the numbers that we will store in that column will be huge and not exceed the range of the BIGINT data type. In this article, we will learn about the BIGINT datatype of MySQL, its range and storage size, and also learn about certain attributes related to BIGINT datatype like signed, unsigned, auto_increment, ZEROFILL, and display width. We will also discuss where and in which scenarios the BIGINT data type is mostly used.
Range and storage space for BigInt Datatype in MySQL
BIGINT 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 BIGINT data type, by default, it is signed BIGINT in its functionality. It takes 8 bytes to store the value of the BIGINT data type. The range of the signed BIGINT datatype from minimum to the maximum value is -9223372036854775808 to 9223372036854775807 that includes almost 20 characters! while for unsigned BIGINT datatype, it is 0 to 18446744073709551615.
Usage of BigInt Datatype
This data type is most commonly used to store the integral value that will be too huge and big. Another usage of BIGINT datatypes is used to declare the primary key of the table that will store the autoincremented value and this table will contain a lot many records even more that the range of INT i.e 4294967295 and you are sure that the range of the value stored in this column will not exceed the range of the BIGINT data type. That means in case if your table is going to contain only too many 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 BIGINT.
Example
Let us create a table that will contain the column as the BIGINT data type that will be the primary key and one more column that will be of BIGINT 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 educba database for which we will execute the following query –
use educba;
that will give following output –
Further, we will create the table named subjects that will contain subject_id as the primary key column of BIGINT datatype and one more unsigned BIGINT column named pages wing the following query –
CREATE TABLE subjects (
subject_id BIGINT AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(255),
pages BIGINT UNSIGNED
);
that gives the following output –
Whenever a null or zero value is put in the auto_increment column then the sequence maintained for it automatically inserts the value incremented by 1 from the last maximum value inserted. It begins by inserting 1 value. In case, if a non-null and non-zero value is inserted in 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',13600),
('Angular',20000),
('Java',96560);
that gives the following output –
Let us check the inserted records by firing the command –
select * from subjects;
that gives 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
(9223372036854775806,'Maven',156);
that gives 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 9223372036854775806 value is inserted into the subject_id column. Now, when we enter the record without mentioning the subject_id value, it will consider 9223372036854775807 as its next value as the sequence is set to that value after inserting 9223372036854775806 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 9223372036854775807 id is duplicated because the range f the BIGINT datatype of signed type by default exceeds and the output is as follows –
4.5 (5,653 ratings)
View Course
Let us see what happens if we specify the value of pages column of unsigned BIGINT type greater than 9223372036854775807 say 10223372036854775807 using the following insert query –
INSERT INTO
subjects(subject_id,description,pages)
VALUES
(4,'Typescript',10223372036854775807);
that gives following output –
and works completely fine because the range of unsigned BIGINT is 255 while of 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 that should be used for displaying 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 we alter the pages column of subjects table to ZEROFILL attribute and specify the display width as 20 using the following command –
ALTER TABLE subjects MODIFY COLUMN pages BIGINT(20) 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 20-digit format with blank spaces replaced with 0.
The select query gives the following output –
select * from subjects;
Conclusion
The BIGINT data type is most often used to store the integral value that will be too huge and big. 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 BigInt. Here we discuss Introduction, Usage of BigInt Datatype, and Examples with code implementation. You can also go through our other related articles to learn more –