Java Developers often use MySQL to store, retrieve and manipulate their data efficiently. Both Java and MySQL support a wide range of data types to store data. Some of them are the same in MySQL and Java while a few of them are different. For instance, Java offers the “Long” data type for storing large numeric data while MySQL does not support the stated data type.
To overcome this situation, developers can use the “BIGINT” data type in MySQL as an alternative to Java’s Long data type.
This post will illustrate the equivalent of Java Long in MySQL.
What is the Equivalent of Java Long in MySQL?
The equivalent of Java long in MySQL is “BIGINT”, to understand how it works, first, you need to understand Java long data type.
What is Java Long Data Type?
In Java, long is a frequently used data type that can store really big whole numbers. It can hold numbers as small as “-9,223,372,036,854,775,808” and as large as “9,223,372,036,854,775,807”. To use a long data type, you just need to write the word “long” followed by the name you want to give your variable, as shown below in the example:
It’s important to remember that you need to put an “L” at the end of the number you assign to your long variable, otherwise, Java will think it’s a different type of number known as int.
BIGINT – Java Long Equivalent in MySQL
In MySQL, the “BIGINT” is the data type that is equivalent to a Java long data type, utilized to store a really large positive or negative number. It supports a signed and unsigned range. Where the signed range varies between “-9,223,372,036,854,775,808” to “9,223,372,036,854,775,807” and the unsigned range varies between “0” to “9,223,372,036,854,775,807”.
Let’s move to the working of the “BIGINT” to understand it in a better way.
Example: Working With BIGINT in MySQL
Working with “BIGINT” includes different steps, such as creating a table, inserting the data, etc. Given below are the steps to work with a “BIGINT”:
Step 1: Create a Table
Let’s create a table using the “CREATE TABLE” command and declare a column with ‘BIGINT” data type as shown in the given below example:
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
number BIGINT,
PRIMARY KEY (id)
);
In the above example, “lh_table_1” is the table name, and the “number” variable is declared as “BIGINT”.
Output
The output depicts that the table has been created.
Step 2: Insert Data
After creating a table, let’s insert the data into the “lh_table_1” with the large value that will be stored in the “number” column. An example to insert data using the “INSERT” command is given below:
VALUES (1, 'Frank Lee', 9223372036854775807);
In the above example, the value “9223372036854775807” is being stored in the number variable that was declared as “BIGINT”.
Output
The output showed that the value has been inserted.
Step 3: Retrieve the Data
Let’s retrieve the data from the “lh_table_1” table using the “SELECT” statement to confirm if the value “9223372036854775807” (a really big number) was inserted or not. The command is given below:
In the above example, only one row is being retrieved using the “WHERE” clause.
Output
This way, a large numeric value can be stored or fetched using the “BIGINT” in MySQL.
Conclusion
The equivalent of the Java long data type in MySQL is the “BIGINT” data type. Java’s Long data type can store large whole numbers, while the “BIGINT” data type in MySQL also allows the storage of extremely large positive or negative numbers. The range of values for both types is the same i.e., “-9,223,372,036,854,775,808” to “9,223,372,036,854,775,807”. This guide has illustrated in-depth information on the equivalent of the Java Long data type in MySQL.