Updated September 12, 2023
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 specific attributes related to BIGINT datatypes like signed, unsigned, auto_increment, ZEROFILL, and display width. Additionally, we will explore the scenarios and use cases where the BIGINT data type is primarily utilized
Table of Contents
Range and storage space for BigInt Datatype in MySQL
BIGINT datatype is the extension of the standard SQL integer type. MySQL allows the declaration of each integral data type as either signed or unsigned. Signed data types enable storage of both positive and negative integral values, while unsigned data types exclusively store positive integer values. By default, integral data types in MySQL are considered signed. The same goes for 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 maximum value is -9223372036854775808 to 9223372036854775807, which includes almost 20 characters! While for unsigned BIGINT datatype, it is 0 to 18446744073709551615.
Usage of BigInt Datatype
We commonly use the BIGINT data type to store large integral values. Another scenario for using BIGINT is declaring the primary key of a table to store auto-incremented values. This is especially valuable when the table is expected to contain a substantial number of records, surpassing the range of INT (4294967295). With BIGINT, you can ensure that the stored values in this column will not exceed the data type’s range. 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 containing the column as the BIGINT data type that will be the primary key and one more column that will be of BIGINT datatype but not 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 –
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 –
For an auto_increment column, if a null or zero value is inserted, the sequence automatically increments the value by 1 from the last maximum value. The initial value inserted is 1. However, if a non-null and non-zero value is provided, it is accepted and used as the inserted value. The sequence value is then set as that inserted value plus 1 for subsequent inserts.
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 provides 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
(9223372036854775806,'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;
Upon inserting the value 9223372036854775806 into the subject_id column, the sequence automatically adjusts to set 9223372036854775807 as the next value. As a result, if we insert a record without specifying a subject_id value, it will default to 9223372036854775807 as the next value in the sequence, following the insertion of columns with subject_id values of 9223372036854775806. Executing the following command –
INSERT INTO
subjects(description,pages)
VALUES
('Hibernate',99);
gives the 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 the next –
INSERT INTO subjects(description,pages) VALUES ('javascipt',105);
gives the error saying the 9223372036854775807 ids are duplicated because the range f the BIGINT 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 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 the 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 the specification of display width for a column by including the desired width within () brackets after the data type. It’s essential to understand that this display width pertains to the formatting of values, not the storage size. Using the ZEROFILL attribute results in filling any empty spaces within the specified display width with zeroes when displaying the number. For example, if we alter the pages column of the subjects table to the 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 –
Assigning the ZEROFILL property to a column automatically designates it as an 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 finds its frequent application in storing extremely large integral values. It offers the flexibility to assign attributes such as AUTO_INCREMENT and ZEROFILL. Additionally, the display width of a BIGINT column can be specified using () brackets.
MySQL’s BIGINT data type is a versatile choice for storing very large integer values, offering a wide range while considering potential storage and performance implications. It’s crucial to use BIGINT judiciously and only when necessary to optimize database efficiency and resource utilization.
Frequently Asked Questions (FAQs)
Q1. Can I store non-integer values in a BIGINT column?
Answer: While BIGINT is primarily for integers, you can store non-integer values in a BIGINT column. However, keep in mind that the decimal part of non-integer values will be truncated.
Q2. Can I convert data from other numeric types to BIGINT?
Answer: Yes, you can convert data from other numeric types to BIGINT using explicit casting or conversion functions like CAST() or CONVERT(). Be cautious about potential data loss if the value exceeds the BIGINT range.
Q3. What’s the difference between BIGINT and INT?
Answer: BIGINT can store larger values than INT. While INT typically uses 4 bytes and has a range of -2,147,483,648 to 2,147,483,647, BIGINT uses 8 bytes and has a much larger range.
Recommended Articles
MySQL BIGINT explore its advantages for storing large integer data, performance considerations, indexing, and comparisons with other numeric types. These articles provide insights into when and how to use BIGINT effectively in database design and application development. You can view EDUCBA’s recommended articles for more information.