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, constants are immutable values used in SQL queries. These constants can be of various types, such as strings, numbers, and special values like NULL
. Here's a look at the different types of constants in PostgreSQL:
String Constants:
Strings are sequences of characters, and in SQL, they are typically surrounded by single quotes ('
). For example:
SELECT 'Hello, World!';
If you need to include a single quote inside a string, you can do so by typing two single quotes in succession, like ''This is a string''
.
Integer Constants:
Integers are whole numbers without any decimal point. They can be positive, negative, or zero. For example:
SELECT 12345;
Floating-Point Constants:
Floating-point numbers are numbers with a decimal point. They can be represented in standard or exponential notation. For example:
SELECT 123.45; SELECT 1.23e4; -- This is equivalent to 12300
Bit-String Constants:
Bit-strings are binary values, and they are often preceded by a B
or X
. For example:
SELECT B'11001'; SELECT X'4D'; -- Represents the byte value of 4D in hexadecimal (M in ASCII)
Boolean Constants:
PostgreSQL supports the boolean data type with values TRUE
, FALSE
, and NULL
. For example:
SELECT TRUE;
Date and Time Constants:
Date and time values are typically represented as strings, but PostgreSQL interprets them based on context. For example:
SELECT '2021-09-01'::date; SELECT '15:30:00'::time;
The ::
is a type-cast operator, which explicitly converts a constant to a particular data type.
Special Constants:
NULL
is a special constant in SQL that represents an unknown or missing value.
SELECT NULL;
UUID Constants:
PostgreSQL supports the UUID data type, which represents Universally Unique Identifiers. They can be used as constants in a similar fashion:
SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid;
Array Constants:
PostgreSQL supports array data types, and you can create constant arrays in queries:
SELECT '{1,2,3,4}'::int[];
Constants can be used in various parts of SQL queries, from the SELECT
clause to the WHERE
clause, and combined with various operators and functions to form more complex expressions.
Defining and using constants in PostgreSQL:
-- Defining a constant DO $$ DECLARE my_constant CONSTANT INTEGER := 42; BEGIN -- Using the constant RAISE NOTICE 'The constant value is: %', my_constant; END $$;
Global vs. local constants in PostgreSQL:
-- Define a global constant SET my_global_constant = 10;
-- Define a local constant in a PL/pgSQL block DO $$ DECLARE my_local_constant CONSTANT INTEGER := 20; BEGIN -- Using the local constant RAISE NOTICE 'The constant value is: %', my_local_constant; END $$;
Constants in PL/pgSQL functions in PostgreSQL:
CREATE OR REPLACE FUNCTION my_function() RETURNS VOID AS $$ DECLARE my_constant CONSTANT INTEGER := 30; BEGIN -- Using the constant within the function RAISE NOTICE 'The constant value is: %', my_constant; END; $$ LANGUAGE plpgsql;
Handling constant values in PostgreSQL queries:
SELECT * FROM my_table WHERE column1 = 40;
Constants in PostgreSQL stored procedures:
CREATE OR REPLACE PROCEDURE my_procedure() AS $$ DECLARE my_constant CONSTANT INTEGER := 50; BEGIN -- Using the constant within the procedure RAISE NOTICE 'The constant value is: %', my_constant; END; $$ LANGUAGE plpgsql;
Immutable vs. mutable constants in PostgreSQL:
CREATE FUNCTION my_immutable_function() RETURNS INTEGER AS $$ DECLARE my_immutable_constant CONSTANT INTEGER := 60; BEGIN RETURN my_immutable_constant; END; $$ LANGUAGE plpgsql IMMUTABLE;
CREATE FUNCTION my_mutable_function() RETURNS INTEGER AS $$ DECLARE my_mutable_constant CONSTANT INTEGER := 70; BEGIN my_mutable_constant := my_mutable_constant + 1; RETURN my_mutable_constant; END; $$ LANGUAGE plpgsql;
Using constants for indexing in PostgreSQL:
CREATE INDEX idx_my_table_column1 ON my_table(column1, my_constant);
Constants in PostgreSQL views and triggers:
Description: Constants can be used in view definitions or trigger functions to represent fixed values.
Code (View):
CREATE VIEW my_view AS SELECT * FROM my_table WHERE column1 = my_constant;
Code (Trigger):
CREATE OR REPLACE FUNCTION my_trigger_function() RETURNS TRIGGER AS $$ BEGIN NEW.column1 := NEW.column1 + my_constant; RETURN NEW; END; $$ LANGUAGE plpgsql;