Introduction to PostgreSQL Data Types
PostgreSQL Data Types are the predefined data categories used when creating database tables. PostgreSQL offers a wide range of data types, including numeric, monetary, character, binary, Boolean, date/time, and enumerated types, each designed for specific data storage and processing purposes. These data types encompass both single-value types, such as integers, characters, floats, and Booleans, as well as multi-value or complex types like arrays, JSON, and intervals. Enumerated data types are particularly useful for defining fixed sets of values, such as months of the year.
In any application that deals with data, a database is essential for storing and managing the data related to the application. Data types serve as categories or classifications for different kinds of data. Put simply, a variable assigned a specific data type can only store values of that particular data type. For example, if a variable is intended to store integer values, it will only accept and store integer values. Attempting to input values of different data types into that variable will result in an error.
Different PostgreSQL Data Types
Given below are different PostgreSQL Types:
- Numeric data types
- Monetary data types
- Character data types
- Binary data types
- Date/Time data types
- Boolean data types
- Enumerated data types
1. Numeric Data Types
It mainly provides two distinct types of numeric data types.
- Integer
- Floating point numbers
Integer Data Type
The variable defined with the integer data type can only store the integer value. The value must be the integer value else; it will end up with throwing the error. For example, if a variable named i is supposed to store the integer value, it will only hold the integer value.
Example: – 1,200,459,354 etc.
In PostgreSQL, basically, three kinds of integer present are as follows:
- Small Integer: The storage size of the small integer is 2 bytes.
The range of small integer is -32768 to +32767
- Integer: The storage size of the integer is 4 bytes.
The range of integer is -2147483648 to +2147483647
- Big Integer: The storage size of big integer is 8 bytes.
The range of big integer is -9223372036854775808 to 9223372036854775807
Code:
# create table num_test (id smallint, number int, salary bigint);
Output:
Floating point number
Floating-point numbers mainly divide into three types are as follows:
- Float
- Numeric
- Real or float8
Float: This is floating point number precision in PostgreSQL. Range if floating point is at least n and a maximum of 8 bytes. It is also called double-precision data types.
Numeric: This is a real number data type in PostgreSQL. Numeric p and s are the exact numbers in numeric data types.
Real: It is a 4-byte floating point no in PostgreSQL data types.
Code:
# create table num_test1 (id real, number numeric, salary float);
Output:
2. Monetary Data Types
Monetary data types in PostgreSQL stores the current amount with a fractional fixed precision number. The range of monetary data type in PostgreSQL is -92233720368547758.08 to +92233720368547758.07
Code:
# create table mon_test (salary money, currency money);
Output:
3. Character Data Types
In this, there are mainly three distinct character data types available.
- Char
- Varchar
- Text
Char Data Type: The char data type stores a single character value and is typically preferred when only a single character needs to be stored. If the user tries to put more than one character in this, it will lead to an error. The variable storing the char values occupies very little space of the storage.
Example: ‘A’, ‘a’, ‘m’ etc
Text Data Type: The variable with data type as the text can store the long string values. In all the cases where the length of the text that has to be stored is unknown, one can use the text data type. PostgreSQL refers to this as a variable-length character string. In this data type, character strings with an unlimited number of records are referred to as text data.
Example: “Hello”, “Congrats” etc
Varchar(n) Data Type: Varchar data type with some number written along with it denotes that it can store or hold only the number of characters written next to it. In these data types, we can store n number of characters.
For example, if we write varchar(9), it means that the variable will only be able to hold the maximum of nice characters.
Example: “ABCDEFGHI”, “Hello Hey”
Code:
# create table char_test (first_name char(10), middle_name varchar(20), last_name text);
Output:
4. Binary Data Types
PostgreSQL binary data types allow the storage of binary string in PostgreSQL. Its variable length is binary data types.
Code:
# create table bin_test (id bytea, dept_no bytea, salary bytea);
Output:
5. Date/Time data Types
Variables meant to store only time values are assigned the time data type. There should be some particular format of time that has to be stored in the time data type variable. It is one of the most important data types in PostgreSQL as it is used to keep track of transactions.
Example: 12:00:36, 01:06:56
Below is the date/time data types available in PostgreSQL are as follows:
- Date
- Time
- Timestamp
- Timestamptz
- Interval
Date: This data type only stores date. It will not store any time. The low value of this data type is 4713 BC, and the highest value is 294276 AD.
Time: This data type only stores time. It will not store any date. The low value of this data type is 4713 BC, and the highest value is 5874897 AD.
Timestamp: This data type stores the date and time. It will not store any timestamp. The low value of this data type is 4713 BC, and the highest value is 294276 AD.
Timestamptz: This data type stores the date and time with the timestamp. The low value of this data type is 4713 BC, and the highest value is 294276 AD.
Interval: The storage size of these data types is 12 bytes. It will describe the time interval value.
Code:
testing=# create table dt_test (start_date date, start_time time, completion_time timestamp, completion_date timestamptz, no_of_year interval);
Output:
6. Boolean Data Types
PostgreSQL includes support for the Boolean data type, which is capable of storing only two values: "True" and "False." Typically, Boolean values are employed to confirm the accuracy of a statement. When a statement is accurate, it returns "True," otherwise, it returns "False." This data type is particularly valuable in decision-making processes, enabling programs to make choices based on one of these two values.
Example: “True”, “False”
Code:
# create table boolean_test (status boolean, flag boolean, state boolean);
Output:
7. Enumerated Data Types
It comprises a static order set of values.This closely resembles the enum data type supported in programming languages. Enumerated data type in PostgreSQL creates using create type command. In this, if we create enumerated data types once, it will use in any other type.
Code:
# CREATE TYPE year AS ENUM ('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');
Output:
Some other Data Types in PostgreSQL
Below is a list of additional data types available in PostgreSQL.
- Interval Data Type: The interval data type can be assigned to a variable and is capable of storing a specific time interval. It focuses on time and can be used to measure time in various scenarios. The database diligently records which transaction occurred at what interval, and this data type aids in managing the intervals.Example – ‘7 months ago, ‘2 years 5 hours 40 minutes.
- Array: We use arrays to store a set of strings or any values, with the restriction that all array values must be of the same data type. Using arrays makes the program more convenient to understand for those who were not involved in its development Example – ARRAY [ 408)-589-5846′,'(408)-589-5555′ ]
- UUID Data Type: The UUID data type is responsible for storing Universally Unique Identifiers, which programs use to identify anything uniquely. High-level programming languages do not support this special type of data. Example – 0e37df36-f698-11e6-8dd4–cb9ced3df976, a81bc81b-dead-4e5d-abff-90865d1e13b1
- JSON Data Type: Variables that store JSON values are assigned the JSON data type, which is essential in PostgreSQL and capable of handling complex JSON data. Example – { “client”: “Doe”, “items”: {“product”: “Application”,”qty”: 7}}
Conclusion
PostgreSQL, a powerful database management system, proves to be a valuable asset for any data-driven application. It offers robust support for a range of fundamental data types, including time, date, and interval, which are crucial for tracking transactions. In certain operating systems like Kali Linux, PostgreSQL comes pre-installed, simplifying the integration process for developers. This ensures seamless connectivity between applications and the database. Users also have the advantage of utilizing the diverse array of data types that PostgreSQL offers, enhancing the versatility of the platform.
No comments:
Post a Comment