Jeremy
Jeremy

Reputation: 5435

Writing data constraints into tables

I want to add something to a table (trigger?) so that, for exactly, exactly 1 row per ID has a specific value for a specific column. So that if a statement was run that makes this not the case, an exception would be thrown and the insert would be rolled back.

Let's take this schema.

ID    Current    Value
1     Y          0
1     N          0
1     N          2
2     Y          2

And the constraint I want is that for each ID, exactly one row has a current of 'Y'.

Therefore, these statements would not be executed and return an appropriate error:

insert into table values (1,'Y',1);
insert into table values (3,'N',2);
update table set current = 'N' where ID = 1;

I have two questions:

  1. Is it a good idea to code this kind of constraint logic into your table, or is that best saved for the applications that manipulate the data? Why?

  2. How can it be done? What kind of tool does oracle provide to create a constraint like this?

Upvotes: 2

Views: 106

Answers (2)

Jeffrey Kemp
Jeffrey Kemp

Reputation: 60262

It's best if you can specify it in a declarative fashion (rather than procedurally, e.g. using triggers). Especially because triggers, without some kind of locking algorithm, will NOT work anyway due to concurrent sessions trying to insert/update the table at the same time.

In this instance, the simplest solution is a unique, function-based index, e.g.:

CREATE UNIQUE INDEX only_one_current ON thetable
  (CASE WHEN Current = 'Y' THEN ID END);

The expression is NULL if Current = 'N', and all-NULL rows in an index are not stored, which means that the uniqueness constraint will only apply to rows where Current = 'Y'.

Upvotes: 2

cooltodinesh
cooltodinesh

Reputation: 225

I think what you are looking for is just a unique constraint. You can add it using below statement so that only unique combination of ID , Current can exist in table.

ALTER TABLE table_name add CONSTRAINT constraint_name UNIQUE (ID,Current);

Upvotes: -1

Related Questions