Skip to Main Content
  • Questions
  • Prevent Duplicate value of column in Interactive Grid in case no Constraint at database level

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Daxesh .

Asked: June 23, 2021 - 10:18 am UTC

Last updated: June 23, 2021 - 12:37 pm UTC

Version: 20.2

Viewed 1000+ times

You Asked

in sample dataset emp and dept.

how to prevent duplicate value of column in interactive grid.

case -1
-------

Requirement of that in emp table emp name should be not duplicate for the same department. for example dept 10 - account i entered sam with employee id 8972 and entered 9872 sam for same dept should be prevent.

how i can do. i tried in process interactive grid - Automatic processing and write plsql to check for insert and update and if okey then i manually insert the record. it prevent the insert. but when i update with same value it done. any alternative solution for that.

at database level i am not be able to enforce constraint.

this is the sample example. in current business application i have in corporate this solution. business application i am unable to explain here so with sample example i explain my problem.

and Chris said...

Any approach which relies on front-end validation to stop duplicates is doomed to fail.

While you can create a process to check if there's already a row in the table for the employee & department like this:

insert into employees ( emp_id, dept_id )
  select :p1_emp_id, :p1_dept_id 
  from   dual
  where  not exists (
    select * from employees
    where  emp_id := :p1_emp_id
    and    dept_id := :p1_dept_id
  );


this fails if two people run the process at the same time.

You can only view changes committed by other transactions. So if the second starts before the first commits, they'll both go "no existing rows for this emp/dept_id combo - it's fine to insert".

So both can insert the row, leaving you with duplicates.

To avoid this you must create a unique constraint over ( emp_id, dept_id ).

If you're looking to do insert-if-not-exists; update-if-exists logic, you can use MERGE. But this still suffers from the concurrency issue above.

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.