Updated May 22, 2023
Introduction to PostgreSQL BIGINT
PostgreSQL BIGINT is the numeric data type used in PostgreSQL to store integer type of values, we can store the integer type of value using bigint data type in PostgreSQL. The size of the bigint data type in PostgreSQL is 8 bytes, and the range of the bigint data type is -9223372036854775808 to 9223372036854775807. Bigint data type is very useful and important in PostgreSQL to store the large number of integer. Numeric data type in PostgreSQL contains the smallint, integer, and bigint, there are different storage size structure for every data type in PostgreSQL, smallint and integer contains different storage size.
Syntax
Below is the syntax of the BIGINT data type:
Create table name_of_table (name_of_column1 data_type, name_of_column2 data_type, name_of_column3 data_type, …, name_of_columnN BIGINT);
Name_of_variable or name_of_column BIGINT;
Alter table name_of_tablealter columnname_of_column type bigint;
Below is the parameter description syntax of BIGINT data type in PostgreSQL:
- Create table: We define the creation of a table in PostgreSQL by using the “CREATE TABLE” statement and specifying the column’s data type as “BIGINT”. We can create any table and define data type as bigint to the column.
- Name of table: In PostgreSQL, we define the table name by specifying the column’s data type as “BIGINT”. We can define bigint data type to the column at the time of table creation. Also, we have defined the data type as bigint after table creation using alter command.
- Name of column 1 to name of column N: This is defined as creating a column on the table and defined the bigint data type for the same.
- Data type: We assign a data type to the table column at the time of table creation. We can define data type as per which data we are storing in the table.
- Bigint: The data type “BIGINT” is used for the column at the time of table creation. Using bigint data type, we can store a larger number of integer in PostgreSQL.
- Name of variable: This is nothing but the column name which we used at the time of table creation.
- Alter table: The ALTER TABLE command in PostgreSQL is used to alter a table. With the ALTER command, we can change the data type of a column from one type to another.
- Alter column: The ALTER COLUMN command in PostgreSQL is used to modify a column. By using the ALTER command, we can change the data type of the column from one type to another.
- Type: When changing the data type using the ALTER TABLE command in PostgreSQL, we define the new data type for the column.
How BIGINT Data Type works in PostgreSQL?
- If we want to store a large integer type value into the table, at the same time, we are using bigint data type in PostgreSQL.
- Performance of the bigint data type is less compared to using smallint and integer data type. So while using bigint data type in PostgreSQL, we have a strong reason before using the same in our application.
- We can change the data type from smallint or integer to bigint. But we cannot change the varchar data type into bigint directly it will issue an error that we cannot automatically cast the data type into bigint.
- Below example shows that we can change the data type smallint or integer to bigint but cannot change from varchar data type to bigint directly.
- In below first example, we have alter the column that we are changing the data type form int to bigint, we have successfully changed it to bigint because it is possible in PostgreSQL to change the numeric data type into the bigint.
- In the second example, changing the data type is impossible because we cannot convert the varchar data type into the bigint data type directly.
Code:
ALTER TABLE test_int ALTER COLUMN id type bigint;
ALTER TABLE test_int ALTER COLUMN name type bigint;
\d+ test_int;
Output:
- Bigint data type will consume large storage to store the data into the table. We can store the number of galaxy star or scientific constant using bigint data type in PostgreSQL.
Examples
Given below are the examples mentioned:
Example #1
Create a table using bigint data type.
- Below example shows that create a table by using bigint data type in PostgreSQL.
- We are creating a table name as bigint_test and define the bigint data type to the column name as id.
Code:
create table bigint_test (id bigint, name varchar, phone integer, addreess varchar);
\d+ bigint_test;
Output:
Example #2
Insert value into bigint data type column.
- Below example shows that insert a value into the bigint data type column.
Code:
insert into bigint_test values (123456789012345, 'ABC', 1234567890, 'Mumbai');
insert into bigint_test values (1234567890123456, 'PQR', 1234567890, 'Mumbai');
select * from bigint_test;
Output:
Example #3
Alter column to change the data type from integer to bigint.
- Below is the example of alter column to change the data type from integer to bigint.
- We are changing the phone column data type from integer to bigint.
Code:
ALTER TABLE bigint_test ALTER COLUMN phone type bigint;
\d+ bigint_test
Output:
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL BIGINT” was beneficial to you. You can view EDUCBA’s recommended articles for more information.