PostgreSQL – Copying Data Types
Last Updated :
21 Aug, 2024
When working with PostgreSQL, you can define a variable that directly references the data type of a column in a table or the data type of another variable. This feature is useful when you want to maintain consistency and avoid repetitive changes to your code whenever the data type of a column is altered.
Let us better understand this concept in PostgreSQL along with syntax and examples.
Syntax to Reference Column Data Types
The below syntax refers to the data type of a column:
variable_name table_name.column_name%TYPE;
This syntax allows you to automatically copy the data type of the specified column into the variable.
Syntax to Reference Variable Data Types
The below syntax refers to the data type of another variable:
variable_name variable%TYPE;
This is useful when you need multiple variables of the same type, ensuring that any future changes in data type only need to be updated in one place.
Example
In this example, we will build a table (say, ‘City’) with a column (say, ‘name’) that has a CHAR data type and create a variable (say, ‘city_name’) and refer to the data type of the column as the data type of the variable.
city_name city.name%TYPE := 'Delhi';
Now let’s check the data type of the variable
SELECT pg_typeof(city_name);
This will establish that the variable is of type CHAR.
By using copying type feature, you receive the following advantages:
- First, you don’t need to care about the data type of the column. You declare a variable to just hold the values of that column in a query.
- Second, when the data type of the column changes, you don’t need to change the variable declaration in the function to adapt to the new changes.
- Third, you can refer to the type of variables to the data type of function arguments to create polymorphic functions since the type of internal variables can change from one call to the next.
Assigning aliases to variables
PostgreSQL also allows you to assign aliases to variables, which can be particularly helpful in trigger procedures where variables have predetermined names such as NEW or OLD. You can create aliases to improve code readability:
new_name ALIAS FOR old_name;
This feature is mainly used in triggers to give more meaningful names to variables that represent data before or after an update.
Example: Using %TYPE and Aliases in a Trigger
CREATE OR REPLACE FUNCTION log_city_changes()
RETURNS TRIGGER AS $$
DECLARE
old_name ALIAS FOR OLD.name;
new_name ALIAS FOR NEW.name;
city_name City.name%TYPE;
BEGIN
city_name := new_name;
IF city_name IS DISTINCT FROM old_name THEN
INSERT INTO City_Logs (old_name, new_name, change_time)
VALUES (old_name, city_name, NOW());
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER city_update_trigger
AFTER UPDATE ON City
FOR EACH ROW EXECUTE FUNCTION log_city_changes();
In this example:
- The ‘log_city_changes’ function uses aliases to rename the ‘OLD.name’ and ‘NEW.name’ variables, making the code more readable.
- We also use the ‘%TYPE’ feature to ensure that ‘city_name’ always matches the data type of the ‘name’ column in the ‘City’ table.
Similar Reads
PostgreSQL - Data Types
PostgreSQL is a robust open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column, affecting storage, access, and manipulation. In this article, We will learn about the P
5 min read
PostgreSQL - CHAR Data Type
The CHAR data type in PostgreSQL is one of the essential character data types for storing fixed-length strings. Unlike VARCHAR, which stores variable-length data, CHAR is used when we need to store a fixed-length string. This article will explain the CHAR data type in PostgreSQL, its syntax, common
5 min read
PostgreSQL - Boolean Data Type
PostgreSQL's Boolean data type supports three states: TRUE, FALSE, and NULL. It uses a single byte to store Boolean values and can be abbreviated as BOOL. In this article, we will explain the PostgreSQL BOOLEAN data type and its implementation in database table design, highlighting its usage through
4 min read
PostgreSQL - Array Data Type
PostgreSQL provides an advanced and flexible feature known as the Array Data Type, which allows us to store multiple values in a single column. Arrays in PostgreSQL can be used with all built-in data types and even user-defined types, enabling a wide range of use cases. In this article, we will expl
4 min read
PostgreSQL - Copy Database
Copying a PostgreSQL database is essential for tasks like testing, backup, and database migration. PostgreSQL offers efficient tools for duplicating databases either within the same server or across different servers, ensuring data consistency and integrity. This guide explains step-by-step methods
4 min read
PostgreSQL - TEXT Data Type
PostgreSQL provides a highly flexible character data type known as TEXT, designed to store character strings of virtually unlimited length. Unlike the VARCHAR data type, which can be limited to a specified length, the TEXT data type offers the same efficiency and performance without the length const
3 min read
PostgreSQL - Date Data Type
PostgreSQL offers powerful DATE data type and date functions to efficiently handle date and time information. PostgreSQL DATE data type allows for storing and manipulating calendar dates while its robust set of date functions enables users to perform operations like date arithmetic and formatting. I
4 min read
PostgreSQL - TIME Data Type
In PostgreSQL, the TIME data type is essential for applications that require precise time tracking, such as scheduling systems and event logging. This data type allows for accurate time-based entries without storing date information. PostgreSQLâs TIME data type also supports fractional seconds for u
4 min read
PostgreSQL - JSON Data Type
JSON (JavaScript Object Notation) is a widely used format for storing data in the form of key-value pairs. Its popularity comes from being easy for humans to read and understand, making it ideal for communication between servers and clients. This readability and ease of use have made JSON a standard
5 min read
PostgreSQL - Copy Table
In PostgreSQL, the copy table functionality is a powerful feature that allows us to efficiently duplicate existing tables, including their structure and data. This capability is essential for various database management tasks such as backing up data, migrating tables, or testing modifications in a s
4 min read