Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Oracle – constraint to check a value exists in another column

I need to check if the value of ‘montant’ exists in the column ‘prix’ of the table ‘service’ in order to add this value in the table.

This is what I tried so far :

ALTER TABLE abonnement ADD CONSTRAINT montant_inclus CHECK(montant in (select prix from service));

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the error I got :
ORA-02251: subquery not allowed here

>Solution :

As per error it’s clear Oracle is not allowing to do
Subqueries in CHECK constraints.

This is doable by JOIN.

ALTER TABLE abonnement ADD CONSTRAINT montant_inclus CHECK(EXISTS (SELECT 1 FROM service WHERE service.prix = abonnement.montant));

It’s also possible using Trigger and other ways.

  1. similar question answer 1
  2. how to add constraint via trigger
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading