Exploring Advanced PostgreSQL Data Types – Part 2

Exploring Advanced PostgreSQL Data Types – Part 2

Exploring Advanced PostgreSQL Data Types: Part 2

Mar 8, 2024 by Robert Gravelle

Range types offer a concise way to represent a range of values within a single database field. They find application in various domains, from temporal data to numeric intervals. In this blog article, we’ll be delving into their usage (and benefits!) using both DML/SQL statements and Navicat for PostgreSQL 16.

In PostgreSQL, range types allow for the representation of continuous ranges of values. These ranges can be of different data types such as numeric, date, or timestamp. For example, a range might represent a period of time, a set of temperatures, or a range of product prices.

Let’s consider a scenario where we want to track the duration of rentals in the free dvdrental sample database. We can utilize range types to store rental durations efficiently. Here are the statements to create and populate the new “rentals_with_rental_period” table:


CREATE TABLE rentals_with_rental_period (
    rental_id SERIAL PRIMARY KEY,
    customer_id INT,
    rental_duration INT,
    rental_period DATERANGE
);

INSERT INTO rentals_with_rental_period (customer_id, rental_duration, rental_period)
VALUES
(1, 7, '[2024-02-01, 2024-02-08]'),
(2, 5, '[2024-01-15, 2024-01-20]');
    

In Navicat, we can create our table using the Table Designer:

rentals_with_rental_period_in_table_designer (59K)

After creating the table, we can add data to it. Be sure to prefix the Range values with a square bracket “[” and end them with a parenthesis “)”. That tells Navicat that the values belong to a range:

rentals_with_rental_period_table (24K)

In this example, the “rental_period” column stores ranges representing the start and end dates of each rental. We can easily query rentals that include a specific date using the @> operator:

range_query (40K)

Range types are not limited to temporal data. They can also be used to represent numeric intervals. For instance, imagine a scenario where a product’s price can vary within a specific range based on quantity purchased. We can use range types to model this effectively:


CREATE TABLE product_price (
    product_id SERIAL PRIMARY KEY,
    price_range NUMRANGE
);

INSERT INTO product_price (price_range)
VALUES
('[10.00, 20.00)'),
('[20.00, 30.00)'),
('[30.00, )');
    

In this example, the “price_range” column stores ranges representing the minimum and maximum prices for each product. We can query products within a specific price range using the @> operator:


SELECT * FROM product_price
WHERE price_range @> 25.00;
    

Range types in PostgreSQL offer a powerful way to represent and query continuous ranges of values. Whether dealing with temporal data, numeric intervals, or other continuous values, range types provide a concise and efficient solution. By leveraging range types, developers can enhance the expressiveness and flexibility of their database schemas, paving the way for more sophisticated applications.

Looking for an easy-to-use graphical tool for PostgreSQL database development? Navicat 16 For PostgreSQL has got you covered. Click here to download the fully functioning application for a free 14 day trial!

类似文章

  • 你可以更换Telegram的号码吗?

    # 根据你可以更换Telegram的号码吗?在现代社交软件中,Telegram以其隐私安全和功能丰富而受到广泛欢迎。对于许多用户而言,偶尔更换电话号码是出于隐私保护、生活环境变化或其他多种原因。那么,如何在Telegram上更换电话号码呢?这篇文章将深入探讨这一话题,并提供一些实用的建议

  • Telegram如何繞過審查

    # Telegram的兴起与审查机制在数字时代,信息自由流动成为了社会进步的重要标志。然而,针对某些信息的审查措施在世界许多地方依然存在,尤其是在政治敏感度较高的国家中。Telegram作为一种即时通讯工具,凭借其强大的加密技术和用户隐私保护理念,迅速崛起

  • telegram打开机器人

    # 如何使用Telegram打开机器人在当今数字化时代,社交应用程序已经成为人们交流和共享信息的重要工具。其中,Telegram因其强大的功能和安全性,在全球范围内赢得了大量用户。在众多功能中,Telegram机器人的使用逐渐成为一个重要的话题。本文将深入分析如何在Telegram

  • telegram怎么登录

    # Telegram概述Telegram是一款广受欢迎的即时通讯软件,自2013年发布以来,它以其高速、安全和用户友好的界面赢得了全球数亿用户的青睐。与其他社交平台相比,Telegram提供了如端对端加密、无限云存储和多平台支持等多种功能,这使得它在

  • 网络上的电报

    # 电报的历史与发展电报,这一被称为现代通信先驱的工具,起源于19世纪初。它的出现彻底改变了人类的信息传递方式。最早的电报是利用电信号传输代码,这些代码通常是摩尔斯电码,以点和划的组合表示字母和数字。电报的发明者之一,萨缪尔·摩尔斯(Samuel Morse),不仅设计了这一传输方式,

  • telegram会员

    Telegram 是一个广受欢迎的即时通讯应用程序,它提供了许多功能和特性,吸引了大量用户。本文将从不同角度深度分析 Telegram,并探讨其吸引力所在。**1. Telegram 的功能和特性**Telegram 提供了丰富的功能和