Thursday, April 2, 2026

Beyond the Basics: Master Repeatable DDL in Oracle Database 23ai

The introduction of the IF [NOT] EXISTS clause in Oracle Database 23ai is more than a syntax update it’s a fundamental shift in how we approach Schema as Code.

In my recent exploration of this feature, I found that while the "Table" examples are the most common, the real power lies in how it simplifies the management of all schema objects. Let's dive deeper into some advanced examples and how they solve daily deployment hurdles.

1. Handling Supporting Objects: Indexes and Sequences

In any production environment, a table rarely exists in a vacuum. You usually have sequences for primary keys and indexes for performance. Before 23ai, if a migration script failed halfway through, you’d have to manually check which indexes were created and which weren't.

Now, you can ensure your entire environment is ready in a single, repeatable block:

-- Safely create a sequence for Employee IDs

SQL> CREATE SEQUENCE IF NOT EXISTS emp_seq START WITH 100 INCREMENT BY 1;

-- Safely create a performance index

SQL>CREATE INDEX IF NOT EXISTS idx_emp_name ON EMPLOYEE(EMP_NAME);

Why this matters: If your deployment tool (like Jenkins or GitLab CI/CD) retries a failed job, these statements won't cause the "Object already exists" error that usually stops a pipeline in its tracks.

2. Simplifying Application Logic: Synonyms

If you manage multi-tenant environments or applications that use synonyms to point to different schema versions, you know the struggle of "cleaning up" old pointers.

-- Ensure the public pointer exists without checking metadata

SQL> CREATE SYNONYM IF NOT EXISTS emp_public FOR HR_DATA.EMPLOYEE;

-- Or, if you are decommissioning a module:

SQL> DROP SYNONYM IF EXISTS old_emp_ref;

3. The "Gotcha" Deep Dive: Understanding Object vs. Attribute

As I mentioned in my recent Oracle Sprint, there is a subtle distinction to keep in mind: Object-level existence vs. Attribute-level existence.

Consider the ALTER TABLE command.
 
  • The Success: ALTER TABLE IF EXISTS employee ADD (department_id NUMBER); — This works perfectly because Oracle checks if the table "employee" exists.

  • The Failure: If you run that same command again, it will fail.

The Reason: Even though the table exists, the column "department_id" also now exists. The IF EXISTS clause doesn't currently look inside the table to see if the column is already there. For column-level idempotency, you still need to be strategic with your migration scripts.

4. Avoiding the Conflict: "REPLACE" vs. "IF NOT EXISTS"

This is a frequent point of confusion. You might be tempted to write: CREATE OR REPLACE VIEW IF NOT EXISTS emp_v AS SELECT...

Oracle will throw ORA-11541. * Use OR REPLACE when you want the object to be updated with new logic (common for Views, Procedures, and Functions).
Use IF NOT EXISTS when you want to ensure you don't overwrite something that is already there (common for Tables, Sequences, and Indexes).

Strategic Summary

The "Idempotent DDL" approach in 23ai removes the "fear of the second run." By incorporating these clauses into your standard SQL scripts, you:
  1. Reduce Boilerplate: No more 10-line PL/SQL blocks for a 1-line DDL.
  2. Increase Pipeline Uptime: Fewer "false positive" failures in CI/CD.
  3. Improve Readability: Your intent is clear to any developer reading the code.

Final Thought for the Community

As we move toward more automated, AI-driven database management, these small syntax changes are what make large-scale automation possible. Are you planning to refactor your legacy migration scripts to use these new clauses, or are you saving them for new projects only?

Let's discuss in the comments!

Thanks & Regards
https://oracleracexpert.com

Wednesday, April 1, 2026

Using Assertions to Prevent Scheduling Conflicts in Oracle 23ai and beyond

Hospitals manage hundreds of surgeries every day across multiple operating rooms. To ensure patient safety, efficient use of resources, and smooth hospital operations, having a reliable scheduling system is critical.

However, conflicts often occur when multiple staff members or systems try to book the same operating room at overlapping times. Left unchecked, these conflicts can cause delays, coordination issues, and even risks to patients.

A reliable solution is to use assertions in the database. With Oracle Database 23c, hospitals can enforce complex scheduling rules directly at the database level, ensuring no two surgeries are scheduled in the same operating room at the same time. This eliminates human error and protects hospital operations.
The Problem: Double Booking Operating Rooms

Surgery scheduling in hospitals typically follows this workflow:
 
  1. A surgeon requests a procedure.
  2. An operating room is assigned.
  3. Start and end times for the surgery are set.

Even with this workflow, overlapping bookings can happen if the database does not strictly enforce scheduling rules.

Example of a scheduling conflict:

RoomSurgeryStart TimeEnd Time
OR-305Knee Replacement7:009:00
OR-305Hip Replacement8:1510:15

Here, the surgeries overlap by 45 minutes, which can lead to:
  • Delays in procedures
  • Staff coordination problems
  • Risk to patient safety
  • Inefficient use of operating rooms

Relying solely on application logic or manual checks is risky—especially when multiple systems interact with the same database. 

Enforcing Scheduling Rules with Assertions

Assertions are database-level rules that guarantee certain conditions are always true. For hospital scheduling, the main rule is:

An operating room cannot host more than one surgery at the same time.

This ensures that time intervals for surgeries in the same room never overlap.
Example Data Model

Operating Rooms Table:

SQL> CREATE TABLE operating_rooms(
or_id NUMBER PRIMARY KEY,
or_name VARCHAR2(50),
department VARCHAR2(50)
);

Surgeons Table:
 
SQL> CREATE TABLE surgeons(
surgeon_id NUMBER PRIMARY KEY,
surgeon_name VARCHAR2(100),
surgeon_specialty VARCHAR2(50)
);


Surgery Schedule Table:

SQL> CREATE TABLE surgery_schedule(
surgery_id NUMBER PRIMARY KEY,
surgeon_id NUMBER,
or_id NUMBER,
start_time TIMESTAMP,
end_time TIMESTAMP,
CONSTRAINT fk_surgeon FOREIGN KEY (surgeon_id) REFERENCES surgeons(surgeon_id),
CONSTRAINT fk_room FOREIGN KEY (or_id) REFERENCES operating_rooms(or_id)
);

Without assertions, the database could allow conflicting schedules:

RoomSurgeryStart TimeEnd Time
OR-305Knee Replacement7:009:00
OR-305Hip Replacement8:1510:15

How Assertions Detect Conflicts

An assertion prevents overlapping surgeries by checking time intervals:
 
CREATE ASSERTION no_room_schedule_conflict
CHECK (
NOT EXISTS (
SELECT s1.or_id
FROM surgery_schedule s1
JOIN surgery_schedule s2
ON s1.or_id = s2.or_id
AND s1.surgery_id <> s2.surgery_id
WHERE s1.start_time < s2.end_time
AND s2.start_time < s1.end_time
)
);

Logic:

Two surgeries conflict if:
  • Surgery A start < Surgery B end
  • AND Surgery B start < Surgery A end

If a conflict exists, the assertion fails and the database blocks the transaction, preventing overlapping surgeries.

Example:

Valid Schedule:

RoomSurgeryStart TimeEnd Time
OR-402Appendectomy6:307:30
OR-402Gallbladder Removal7:309:00

Conflict Attempt:

RoomSurgeryStart TimeEnd Time
OR-402Appendectomy6:307:30
OR-402Hernia Repair7:008:00

Result: ERROR – assertion NO_ROOM_SCHEDULE_CONFLICT violated.

The conflicting schedule is automatically blocked. 

Why Hospitals Should Use Assertions

Assertions provide several advantages in hospital scheduling systems:
  1. Prevent Critical Operational Errors – Conflicts are blocked immediately.
  2. Protect Patient Safety – Eliminates risks caused by overlapping surgeries or delays.
  3. Ensure Centralized Data Integrity – Works across multiple systems like hospital administration portals, scheduling apps, and emergency systems.
  4. Simplify System Design – Reduces reliance on triggers, manual checks, or complex application validations.
Other Healthcare Applications

Assertions can also help manage:
  • Surgeon availability – Prevents double booking of surgeons.
  • ICU bed allocation – Ensures ICU reservations never exceed capacity.
  • Medical equipment scheduling – Avoids conflicts for MRI, X-ray, or CT scanners.
  • Staff shift compliance – Keeps nursing shifts within legally allowed hours.

Conclusion

Oracle Database 23c’s assertions provide a powerful mechanism for enforcing complex business rules directly in the database. In hospitals, where scheduling mistakes can have serious consequences, assertions ensure no operating room conflicts occur, safeguarding both patients and operations.