Oracle 26ai: Easy Data Validation with Assertions
What You Will Learn
Discover the new Assertion feature in Oracle 26ai. You will learn what it is, why it helps, and how to use it in real-world scenarios.
The Problem with Check Constraints
The Solution: Assertions
Prerequisites
Oracle 26ai Database.
Run the below command to grant you access from ADMIN:
GRANT CREATE ASSERTION TO DBX_ASSERTION;
Step 1: Create the Tables
CREATE TABLE ASSERTION_DEMO( ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY NOCACHE, NAME VARCHAR2(200 CHAR), STATUS VARCHAR2(20 CHAR) DEFAULT 'ACTIVE' NOT NULL CHECK (STATUS IN('ACTIVE','INACTIVE')), NEW_STATUS VARCHAR2(20));
CREATE TABLE ASSERTION_STATUS_LOOKUPS( ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY NOCACHE, STATUS VARCHAR2(200 CHAR), DESCRIPTION VARCHAR2(2000 CHAR));
Step 2: Add Lookup Data
INSERT INTO ASSERTION_STATUS_LOOKUPS (STATUS, DESCRIPTION)VALUES ('ACTIVE', 'Active');
INSERT INTO ASSERTION_STATUS_LOOKUPS (STATUS, DESCRIPTION)VALUES ('INACTIVE', 'Inactive');
Step 3: Create the Assertion
CREATE ASSERTION check_new_status_validCHECK ( ALL ( SELECT NEW_STATUS FROM ASSERTION_DEMO WHERE NEW_STATUS IS NOT NULL ) demo_row SATISFY ( EXISTS ( SELECT 1 FROM ASSERTION_STATUS_LOOKUPS l WHERE l.STATUS = demo_row.NEW_STATUS AND l.STATUS IS NOT NULL ) ));
Step 4: Test the Setup
-- 1. Valid insert (default STATUS = ACTIVE)INSERT INTO ASSERTION_DEMO (NAME)VALUES ('Record A');
-- 2. Valid insert with allowed STATUSINSERT INTO ASSERTION_DEMO (NAME, STATUS)VALUES ('Record B', 'INACTIVE');
-- 3. Valid NEW_STATUS (exists in lookup)INSERT INTO ASSERTION_DEMO (NAME, STATUS, NEW_STATUS)VALUES ('Record C', 'ACTIVE', 'INACTIVE');
-- 4. Valid NEW_STATUSINSERT INTO ASSERTION_DEMO (NAME, STATUS, NEW_STATUS)VALUES ('Record D', 'INACTIVE', 'ACTIVE');
-- 5. Invalid STATUS (fails because of the old CHECK constraint)INSERT INTO ASSERTION_DEMO (NAME, STATUS)VALUES ('Record E', 'DELETED');
-- 6. Invalid NEW_STATUS (fails because 'PENDING' is not in our lookup table)INSERT INTO ASSERTION_DEMO (NAME, STATUS, NEW_STATUS)VALUES ('Record F', 'ACTIVE', 'PENDING');
--7. Insert PENDING into lookupINSERT INTO ASSERTION_STATUS_LOOKUPS (STATUS, DESCRIPTION)VALUES ('PENDING', 'Pending');
--8. Try again step 6 - SuccessINSERT INTO ASSERTION_DEMO (NAME, STATUS, NEW_STATUS)VALUES ('Record F', 'ACTIVE', 'PENDING');
--9. If the child table having data, then the lookup table row cannot be updated or deletedUPDATE ASSERTION_STATUS_LOOKUPS SET STATUS = 'DELETED'WHERE STATUS = 'PENDING';commit;
--10. Removing the child table record and update lookup table rowDELETE FROM ASSERTION_DEMO WHERE NEW_STATUS = 'PENDING';
UPDATE ASSERTION_STATUS_LOOKUPS SET STATUS = 'DELETED'WHERE STATUS = 'PENDING';
--11. Update child table - status not available in lookup - (fails because 'ARCHIEVED' is not in our lookup table)UPDATE ASSERTION_DEMO SET NEW_STATUS = 'ARCHIEVED'WHERE NEW_STATUS = 'ACTIVE';
Conclusion
In short, Oracle 26ai Assertions make checking your data much easier. Here is why they are helpful:
No fixed rules: You do not have to lock rules directly into your table design.
Fast updates: Need to add a new status? Just add a new row to your lookup table. The database does the rest.
Less risk: You avoid changing live tables just to update a simple rule.
Cleaner setup: Your main tables stay clean, and your rules are easy to find and manage.
With Assertions, your database can easily grow with your business needs without extra coding.
Hope you learned something new. 😊
Comments
Post a Comment