Validating email addresses, URIs and CSV in Oracle with the grammars from the RFCs

Every few months I need to answer a question that sounds simple: “is this string valid?” Is it an email address, an IPv6 address, a URI, a CSV file? The usual answer is a regular expression, and the usual regular expressions you find online are wrong in some edge case nobody tested. In my case I wanted to parse email addresses accurately, strictly and according to the agreed rules.

The standards bodies already wrote these rules down, precisely, in a notation designed for exactly this job called Augmented Backus Naur Form (ABNF). So I wrote an engine that reads that notation. It’s an ABNF implementation in Oracle object types and PL/SQL, and the code is in the ABNF repo on my GitHub.

Continue reading →

Grapheme Clusters in Oracle PL/SQL

Ask Oracle how long this string is:

πŸ‘©πŸ½β€β€οΈβ€πŸ’‹β€πŸ‘¨πŸ½

To a person reading it, it is one thing: a couple kissing, with a skin tone applied. To Oracle it is 10 characters, 15 UTF-16 code units and 35 bytes. Call SUBSTR(txt, 1, 1) and you get half of a person. This post is about why that happens, what a grapheme cluster is, and a PL/SQL package I wrote to give Oracle the missing support.

Continue reading →

Oracle External Tables – Part 2

Advanced Techniques, Performance, and Error Handling

In Part 1, we covered the foundational mechanics of external tables to stream flat files into Oracle.

In this follow-up, I will explore some more advanced execution patterns for high-throughput data pipelines, dynamic file parsing without catalogue overhead, OS-level pre-processing, fixed-width parsing strategies, and robust error management.

Continue reading →

Routines for Indenting and Unindenting Text

Introduction: Why Would I Need These Routines?

If, like me, you’ve ever worked with dynamic SQL generation, code formatting tools, or text processing in PL/SQL, you’ve likely encountered the need to adjust indentation programmatically. For example, when you’re:

  • Building formatted SQL statements on the fly
  • Processing configuration files or structured text
  • Generating code or documentation programmatically
  • Reformatting nested structures with variable indentation levels
  • Creating readable log output or debug traces

In any case, you need a reliable way to add or remove indentation while preserving the structure and line endings of your text. Our modern development tool editors seem to do it without issue and so it sounds quite simple, doesn’t it? Exactly as I initially thought, but during development of PL/SQL functions to do it I found it wasn’t as simple as it sounds.

This article walks through the design and implementation of the final two companion functions which peform Indenting and Unindenting: IndentString for VARCHAR2 data and IndentClob for CLOB data, capable of handling text of any size. The source is found at the end of the article.

Continue reading →

Unique Index Versus Unique Constraint

A question I’ve seen asked time and again in forums and from DB developers is “What’s the difference between a unique constraint and a unique index?”. So I thought I’d do a quick write up explaining the differences.

  • A Unique Constraint can be referred to by a Foreign Key**, a Unique Index can’t
  • A Unique Constraint can be used to check for uniqueness BUT allows nulls, a Unique Index doesn’t allow nulls.
  • A Unique Constraint creates a Unique Index, but not vice vera.
Β  Foreign Keys Allows Nulls Also Creates…
Unique Constraint βœ”οΈ βœ”οΈ Unique Index
Unique Index ❌ ❌  

** Many developers think a Foreign Key is a link between a child table and a Primary Key, but that is not correct, it’s actually a link between a child table and a Candidate Key (a key which uniquely identifies a record). You can have multiple Candidate Keys on a record, but the Primary Key is the primary one, the others are just a Unique Constraint.

Taming the Nulls: Column Shifting Values Left in SQL

Imagine you’re working with a table full of data – say, employee bonuses stored across five columns: bonus1, bonus2, bonus3, bonus4, and bonus5. Each row represents an employee, and the bonuses are scattered across these columns, with some entries missing (i.e., NULL). Your boss walks in and says, β€œI want all the actual bonus values pushed to the left, in order, with nulls filling the gaps on the right.” For example, taking this data…:

create table test (id number, bonus1 number, bonus2 number, bonus3 number, bonus4 number, bonus5 number);
insert into test values (1, 10, null, null, 20,30);
insert into test values (2, null, null, 20,30,40);
insert into test values (3, 10, 20, null, null, 30);
insert into test values (4, null, null, null, null, 50);
insert into test values (5, null, null, null, null, null);
insert into test values (6, 1, 2, 3, 4, 5);

select * from test;

        ID     BONUS1     BONUS2     BONUS3     BONUS4     BONUS5
---------- ---------- ---------- ---------- ---------- ----------
         1         10                               20         30
         2                               20         30         40
         3         10         20                               30
         4                                                     50
         5                                                       
         6          1          2          3          4          5

… and turning it into the following, but how?…

        ID     BONUS1     BONUS2     BONUS3     BONUS4     BONUS5
---------- ---------- ---------- ---------- ---------- ----------
         1         10         20         30                      
         2         20         30         40                      
         3         10         20         30                      
         4         50                                            
         5                                                       
         6          1          2          3          4          5
Continue reading →

Script for Exporting Access Control Entries (ACEs)

Access Control Entries (ACEs) control which principals (users or roles) can access different resources, primarily network services, but also Oracle Wallets – which store credentials like private keys, certificates, and other sensitive data needed for secure communication and authentication. ACEs were introduced in Oracle Database 12c and supersede the older Access Control Lists (ACLs) by offering more granular and flexible security controls. ACEs are easier to use – they are clearer, more flexible and more focused, so are simpler and faster. This lets database admins set exact permissions, making it quicker to manage and ensure only the right people or systems can reach sensitive info or services.

There are occasions where you might want to migrate ACEs to another instance (such as a clone) or generate a script for source control or testing purposes. Whilst DBMS_METADATA is typically used to extract DDL for many database objects, it does not include functionality for ACEs. Tools like Data Pump Export and Import can include ACEs and be used for migration, but these methods can be slow, are resource hungry and not particularly user friendly. To address these limitations, I’ve written a custom lightweight script for generating the DB calls which would “replay” the ACE entries.

Continue reading →

JSON Sibling Arrays – With Ordinal Association

Oracle’s JSON_TABLE provides a facility to un-nest arrays using the nested path clause, but if you try to use that with sibling arrays you can end up with unexpected results. In my previous post JSON Sibling Arrays – Without Ordinal Association, I covered the situation where sibling arrays have no connection through ordinal position, they are separate aspects under the parent object. In this post I cover the other situation where array items are linked across the sibling arrays by ordinal position. So the nth array object in one array is linked in some way to the nth array object in the sibling array.

Continue reading →

URLEscape and URLUnescape for CLOBs

URL escaping (encoding), is a method to encode arbitrary data in a Uniform Resource Identifier (URI) using only the limited US-ASCII characters legal within a URI. Certain characters are reserved in URLs, so are replaced with percent encoding, which is a % + two hex digits which represent the character’s ASCII code. For multibyte characters such as extended characters in UTF-8, codepoints are encoded as multiple escaped values – one for each octet. You can read more about URL encoding here or see the standard’s document relating to it : RFC 3986.

Oracle provides package functions for escaping (encoding) URLs using UTL_URL.ESCAPE and unescaping (decoding) using UTL_URL.UNESCAPE, however, these only support VARCHAR2 inputs up to 4000 bytes. I needed equivalents for CLOB inputs, so I wrote functions which leveraged the UTL_URL functions. Here’s what I came up with. Note the input parameters to both functions match the UTL_URL equivalents.

Continue reading →