Preventing duplicate column value using Trigger [message #446370] |
Mon, 08 March 2010 00:31 |
malhi
Messages: 96 Registered: December 2009 Location: Karachi
|
Member |
|
|
I want to avoid duplication on old_nic_no and new_nic_no number in a Table, for this i am using a following trigger
create or replace TRIGGER chk_ip_duplications
BEFORE UPDATE
ON cb_insured_person_bak
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
nic VARCHAR2 (13);
cnic VARCHAR2 (15);
BEGIN
IF UPDATING AND :NEW.old_nic_no IS NOT NULL
THEN
SELECT a.old_nic_no
INTO nic
FROM cb_insured_person_bak a
WHERE a.old_nic_no = :NEW.old_nic_no;
IF SQL%FOUND
THEN
raise_application_error (-20009,
'This NIC '
|| :NEW.old_nic_no
|| ' Already Exist'
);
END IF;
ELSIF UPDATING AND :NEW.new_nic_no IS NOT NULL
THEN
SELECT a.new_nic_no
INTO cnic
FROM cb_insured_person_bak a
WHERE a.new_nic_no = :NEW.new_nic_no;
IF SQL%FOUND
THEN
raise_application_error (-20009,
'This CNIC '
|| :NEW.new_nic_no
|| ' Already Exist'
);
END IF;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
END;
when i use following command
update cb_insured_person_bak set new_nic_no = '34601-0774284-9' where eobi_no = '4300G043746'
34601-0774284-9 already exist with another eobi_no , i expect error described in rise_application_error clause but i give following error
ORA-04091: Table cb_insured_person_bak is mutating..........
How i can resolve this problem.....
|
|
|
|
|
|
Re: Preventing duplicate column value using Trigger [message #446387 is a reply to message #446379] |
Mon, 08 March 2010 01:30 |
|
Michel Cadot
Messages: 68718 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
It is possible:
SQL> create table t (val integer);
Table created.
SQL> insert into t values(0);
1 row created.
SQL> /
1 row created.
SQL> commit;
Commit complete.
SQL> create index ti on t(val);
Index created.
SQL> alter table t add constraint t_unq unique (val) using index ti enable novalidate;
Table altered.
SQL> insert into t values(0);
insert into t values(0)
*
ERROR at line 1:
ORA-00001: unique constraint (MICHEL.T_UNQ) violated
SQL> insert into t values (1);
1 row created.
SQL> /
insert into t values (1)
*
ERROR at line 1:
ORA-00001: unique constraint (MICHEL.T_UNQ) violated
Regards
Michel
[Updated on: Mon, 08 March 2010 01:30] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Preventing duplicate column value using Trigger [message #446518 is a reply to message #446504] |
Tue, 09 March 2010 00:39 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
malhi wrote on Tue, 09 March 2010 06:15I shall do like this....
alter table t add constraint nic_cnic_chk check (old_nic_no is not null or new_nic_no is not null) enable novalidate;
But this will allow people to enter a value for both columns. Is that what you want?
|
|
|