DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Advanced Database Constraints: Don’t Look for a Second

Advanced Database Constraints: Don’t Look for a Second

Jens Schauder user avatar by
Jens Schauder
·
Apr. 14, 12 · Interview
Like (0)
Save
Tweet
Share
5.38K Views

Join the DZone community and get the full member experience.

Join For Free

One of the powers of RDBMS as we use them today are constraints. I use unique indexes, not null constraints and foreign keys on a regular basis and if you do any work with RDBMSs you probably do as well.

From time to time one comes at a point where you think “it would be nice to enforce this with some kind of constraint”, but you can’t. Well maybe you just didn’t try hard enough. This is the second part of the mini series about somewhat special constraints for (Oracle) databases.

Let’s assume you want to model a system of superheroes an villain. Each superhero has an arch_villain and each villain has an arch superhero. Thats simple, isn’t it?

    CREATE TABLE super_hero (
      id number(10),
      name varchar2(200) NOT NULL UNIQUE,
      arch_villain_id number(10) NOT NULL,
      CONSTRAINT super_hero_pk PRIMARY KEY (id)
    );
     
    CREATE TABLE villain   (
      id number(10),
      name varchar2(200) NOT NULL UNIQUE,
      arch_hero_id number(10) NOT NULL,
      CONSTRAINT villain_pk PRIMARY KEY (id)
    );
     
    ALTER TABLE super_hero
    ADD constraint arch_villain_fk
    FOREIGN KEY(arch_villain_id)
    REFERENCES villain(id);
     
    ALTER TABLE villain
    ADD constraint arch_hero_fk
    FOREIGN KEY(arch_hero_id)
    REFERENCES super_hero(id);

Not so fast. Try to insert a row into these tables. It doesn’t work. Before you can insert the first super hero you have to insert a first villain to serve as a arch villain for our super hero. But this arch villain needs a super hero first for use as arch super hero. A vicious circle.

Looks like we have to drop at least one of our constraints. Actually no. We can make it a deferred constraint like so:

    CREATE TABLE super_hero (
      id number(10),
      name varchar2(200) NOT NULL UNIQUE,
      arch_villain_id number(10) NOT NULL,
      CONSTRAINT super_hero_pk PRIMARY KEY (id)
    );
     
    CREATE TABLE villain   (
      id number(10),
      name varchar2(200) NOT NULL UNIQUE,
      arch_hero_id number(10) NOT NULL,
      CONSTRAINT villain_pk PRIMARY KEY (id)
    );
     
    ALTER TABLE super_hero
    ADD constraint arch_villain_fk
    FOREIGN KEY(arch_villain_id)
    REFERENCES villain(id)
    deferrable initially deferred;
     
    ALTER TABLE villain
    ADD constraint arch_hero_fk
    FOREIGN KEY(arch_hero_id)
    REFERENCES super_hero(id)
    deferrable initially deferred;

Now we can enter our data in a pretty natural way:

    INSERT INTO super_hero(id, name, arch_villain_id) VALUES (1, 'Spider-Man', 2);
     
    INSERT INTO villain(id, name, arch_hero_id) VALUES(2, 'Green Goblin', 1);
     
    commit;

What happened? Are the constraints still there? Yes they are:

    INSERT INTO super_hero(id, name, arch_villain_id) VALUES (1, 'Spider-Man', 2);
     
    INSERT INTO villain(id, name, arch_hero_id) VALUES(3, 'Green Goblin', 1);
     
    commit; – fails with ORA-02091: transaction rolled back ORA-02291: integrity constraint (USER_1DA56.ARCH_VILLAIN_FK) violated – parent key not found : commit

But they are only enforced on commit, not after every DML statement like it is done normaly.

Many Java developers know this feature due to a bug in hibernate that causes illegal intermediate inserts/updates. Most database developers know this feature more as a performance tuning tool. When you insert or update millions of rows Oracle has to executes additional statementes in order to verify constraints. This can cost a considerable amount of performance and often can be done much more efficient when one waits until all the data has its final state.

 

 

 

 

Database

Published at DZone with permission of Jens Schauder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Web Testing Tutorial: Comprehensive Guide With Best Practices
  • Creating a Personal ReadMe for Scrum Masters With ChatGPT
  • Chaos Data Engineering Manifesto: 5 Laws for Successful Failures

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: