Introduction to PostgreSQL BIGINT
PostgreSQL BIGINT is 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 bigint data type in PostgreSQL is 8 bytes and range of 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 of every data type in PostgreSQL, smallint and integer contains the different storage size.
Syntax of PostgreSQL BIGINT
Below is the syntax of BIGINT data type in PostgreSQL:
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: This is defined as the create table in PostgreSQL by defining data type as bigint to the column. We can create any table and define data type as bigint to the column.
- Name of table: This is defined as table name in PostgreSQL by defining data type as bigint to the column. We can define bigint data type to the column at the time of table creation. Also we have defined data type as bigint after table creation using alter command.
- Name of column 1 to name of column N: This is defined as create column on table and defined bigint data type for the same.
- Data type: This is defined as assign data type to the table column at the time of table creation. We can define data type as per which data we are storing into the table.
- Bigint: This is defined as data type which we have used to the column at the time of table creation. Using bigint data type we can store larger number of integer in PostgreSQL.
- Name of variable: This is nothing but column name which we have used at the time of table creation.
- Alter table: This command is used to alter table in PostgreSQL, using alter command we are changing the data type of column from one data type to other data type.
- Alter column: This command is used to alter column in PostgreSQL, using alter command we are changing the data type of column from one data type to other data type.
- Type: This is defined as data type which we are changing at the time of alter table command in PostgreSQL.
How BIGINT Data Type works in PostgreSQL?
- If we want to store large integer type value into the table, same time we are using bigint data type in PostgreSQL.
- Performance of bigint data type is less as compare to using smallint and integer data type. So while using bigint data type in PostgreSQL we have strong reason before using the same in our application.
- We can change the data type of smallint or integer to bigint. But we cannot change varchar data type into bigint directly it will issue 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 second example it is not possible to change the data type because we cannot convert 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 table using bigint data type.
- Below example shows that create table by using bigint data type in PostgreSQL.
- We are creating table name as bigint_test and define 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 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 data type from integer to bigint.
- Below is the example of alter column to change 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
This is a guide to PostgreSQL BIGINT. Here we discuss how BIGINT data type works in PostgreSQL with query examples for better understanding. You may also have a look at the following articles to learn more –