SQL INT VS BIGINT Datatype
Integer numeric data types INT
and BIGINT
are also available. Let's compare the two in terms of storage space, minimum value, and maximum value, and then look at a few cases.
SQL INT Datatype
Integer values range from -2,147,483,648 to 2,147,483,647 for the INT data type.
INT takes up 4 bytes of memory. In SQL Server, the INT data type is the most often used integer data type.
Example: We have Company table with an INT column
CREATE TABLE Company
(
CompanyName VARCHAR(100),
Number of employees INT
)
GO
INSERT INTO Company VALUES ('Infosys', 22000);
INSERT INTO Company VALUES ('HCL', 12980);
INSERT INTO Company VALUES ('Wipro', 8000);
SELECT * FROM Company;
In this datatype we can't store above the range of INT datatype.
Output: The Output of Above query is
Company Name | Number of employees |
---|---|
Infosys | 22000 |
HCL | 12980 |
Wipro | 8000 |
Example 2: INT out of range value
DECLARE @i INT
SET @i = 2147484567
PRINT @i
Output: The Output will be get an error
Example 3: Try to store Negative value
DECLARE @Neg INT
SET @Neg = -150
PRINT @Neg
We can possible store to negative value in INT
Output
SQL BIGINT Datatype
BIGINTis an integer data type with a range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
The largest integer data type in SQL Server is BIGINT. It takes up 8 bytes of memory. When values can beyond the range of INT, BIGINT should be utilised.
Example: Let's look at Company table with a BIGINT column.
CREATE TABLE Company
(
Idno INT IDENTITY,
Company VARCHAR(100),
NetWorth BIGINT
);
GO
INSERT INTO Company VALUES (1,'Wipro', 2254400000000);
INSERT INTO Company VALUES (3,'Ford', 1965500000000);
INSERT INTO Company VALUES (4,'TATA', 1894200000000);
SELECT * FROM Company;
We can't store Above 2^63-1 Values
Output: The output will be
Company Name | Number of employees | Networth |
---|---|---|
1 | Wipro | 2254400000000 |
3 | Ford | 1965500000000 |
4 | TATA | 1894200000000 |
Example 2: BIGINT out of range value
DECLARE @RAG BIGINT
SET @RAG = 2147454687
PRINT @RAG
Ouput: The Output of bigint out of range value
Example 3: Try to store Negative value
DECLARE @Neg BIGINT
SET @Neg = -145
PRINT @Neg
Output: The output value will be
SQL Difference Between INT And BIGINT Datatype
There are 3 main distinguish between int & bigint datatype:-
INT | BIGINT |
---|---|
INT has a storage size of 4 bytes. | BIGINT's storage size is double that, i.e. 8 bytes. |
INT has a minimum value of -2,147,483,648 (-2^31). | BIGINT has a minimum value of -9,223,372,036,854,775,808 (-2^63). |
INT has a maximum value of 2,147,483,647 (2^31-1) | BIGINT has a maximum value of 9,223,372,036,854,775,807 (2^63-1). |