PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, BIGINT
is one of several integer data types, and it's used to store large integer values.
BIGINT
:Creating a table with a BIGINT
column:
CREATE TABLE large_numbers ( id BIGINT PRIMARY KEY, description TEXT );
Inserting data into the BIGINT
column:
INSERT INTO large_numbers (id, description) VALUES (9223372036854775807, 'Maximum BIGINT value');
Querying data from a BIGINT
column:
SELECT * FROM large_numbers WHERE id > 9000000000000000000;
Use BIGINT
only when you expect the values to exceed the range provided by the smaller integer types. Otherwise, it's more efficient to use INTEGER
or SMALLINT
as they require less storage.
Converting between integer types and floating-point types can result in rounding errors, so always be cautious when performing such operations.
When inserting values into a BIGINT
column, ensure the values are within the allowable range. Attempting to insert values outside the range will result in an error.
Indexing a BIGINT
column will take up more disk space than indexing a smaller integer type. If you're working with a large dataset and are concerned about storage efficiency, ensure you're using the most appropriate data type for your use case.
In situations where you need a large range but also need to store fractional values, you'd need to consider using other data types like DECIMAL
or NUMERIC
.
In essence, BIGINT
provides a way to store very large integer values, and it's especially useful in scenarios like large-scale analytics, unique identifiers that have a vast range, or other cases where extremely large numbers are used.
Defining columns with BIGINT in PostgreSQL:
BIGINT
is a numeric data type in PostgreSQL that can store 8-byte signed integers. It's used when you need to handle very large integer values.CREATE TABLE my_table ( my_column BIGINT );
Inserting and updating BIGINT values in PostgreSQL:
Description: You can insert and update BIGINT
values using standard SQL INSERT
and UPDATE
statements.
Code:
INSERT INTO my_table (my_column) VALUES (123456789012345);
UPDATE my_table SET my_column = 987654321098765 WHERE some_condition;
Range of values for BIGINT in PostgreSQL:
BIGINT
in PostgreSQL has a range of -9223372036854775808 to 9223372036854775807 for signed integers.CREATE TABLE my_numbers ( big_number BIGINT ); INSERT INTO my_numbers (big_number) VALUES (-9223372036854775808); INSERT INTO my_numbers (big_number) VALUES (9223372036854775807);
Using BIGINT in arithmetic and mathematical operations in PostgreSQL:
BIGINT
can be used in various arithmetic and mathematical operations just like any other numeric type.SELECT big_column * 2 FROM my_table WHERE some_condition;
Converting data types to and from BIGINT in PostgreSQL:
Description: You can convert data types to and from BIGINT
using explicit casting or conversion functions.
Code:
SELECT CAST(some_column AS BIGINT) FROM some_table;
SELECT my_column::TEXT FROM my_table;
Indexing and querying on columns with BIGINT in PostgreSQL:
Description: Indexing BIGINT
columns can improve query performance, especially when searching and sorting based on these values.
Code (Indexing):
CREATE INDEX idx_big_number ON my_numbers(big_number);
SELECT * FROM my_numbers WHERE big_number = 123456789012345;
Handling overflow and precision with BIGINT in PostgreSQL:
BIGINT
.INSERT INTO my_table (my_column) VALUES (9999999999999999999); -- This will result in an error