Without Overlaps Constraints


BigQueryDb2 (LUW)DuckDBH2MariaDBMySQLOracle DBPostgreSQLSQL ServerSQLite200920112013201520172019202120232025⊘ 3.5.7 - 3.51.0⊘ 2008R2 - 2025✓ 18+c⊘ 9.2 - 12a⊘ 8.3 - 9.1⊘ 11gR1 - 23.26.0⊘ 5.0 - 9.5.0✓ 10.5 - 12.1.2⊘ 5.1 - 10.4⊘ 1.4.191 - 2.4.240⊘ 1.4.0✓ 10.5 - 12.1.2b⊘ 9.7⊘ 2.0
  1. Functionality available with exclusion constraints
  2. Without keyword for: PERIOD FOR BUSINESS_TIME (…)
  3. Using a range: BUSINESS_TIME tsrange GENERATED ALWAYS AS (tsrange(start_ts, end_ts)) STORED

SQL supports temporal constraints that prevent rows with overlapping time ranges. These constraints are typically used to prevent double bookings in a reservation system.

Such a primary key or unique constraint uses the keywords without overlaps on the period that combines the begin and end of the time frame.0

CREATE TABLE t (
  id INTEGER NOT NULL,
  
  start_ts TIMESTAMP(6) NOT NULL,
  end_ts   TIMESTAMP(6) NOT NULL,
  PERIOD FOR BUSINESS_TIME (start_ts, end_ts),

  PRIMARY KEY (id, BUSINESS_TIME WITHOUT OVERLAPS)
)

Now consider these insert statements.

INSERT INTO t (id, start_ts, end_ts)
VALUES (1, TIMESTAMP'2018-05-16 19:12:50', TIMESTAMP'9999-12-31 23:59:59')
INSERT INTO t (id, start_ts, end_ts)
VALUES (1,  TIMESTAMP'2019-05-16 19:11:50', TIMESTAMP'2019-05-16 19:12:50')

Given the without overlaps constraint the second insert is rejected as the period in 2019 falls within the range of the first row that was already inserted (2018-9999).

  • Standard Features

    • T180, “System-versioned tables”

    • T181, “Application-time period tables”

    • T200, “Trigger DDL”

    • F521, “Assertions”

    • F671, “Subqueries in CHECK constraints”,

Normative References

Without overlaps constraints are defined in ISO/IEC 9075-2:2023 as part of the optional feature T181, “Application-time period tables”.

You can’t catch up on 20 years of SQL evolution in one day. Subscribe the newsletter via E-Mail, Bluesky or RSS to gradually catch up and to keep modern-⁠sql.com on your radar.

About the Author

Photo of Markus Winand

Markus Winand provides insights into SQL and shows how different systems support it at modern-sql.com. Previously he made use-the-index-luke.com, which is still actively maintained. Markus can be hired as trainer, speaker and consultant via winand.at.

Buy the Book

Cover of “SQL Performance Explained”: Squirrel running on grass

The essence of SQL tuning in 200 pages

Buy now!
(paperback and/or PDF)

Paperback also available at Amazon.com.

Hire Markus

Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »

Footnotes

  1. Closed/Open or Inklusive/Exklusive.

Connect with Markus Winand

Subscribe mailinglistsSubscribe the RSS feedMarkus Winand on LinkedInMarkus Winand on XINGMarkus Winand on TwitterMarkus Winand on Bluesky
Copyright 2015-2025 Markus Winand. All righs reserved.
Legal | Contact | NO WARRANTY | Trademarks | Privacy and GDPR