473,948 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a trigger to enforce unique if not null DB2 v8.1.9 linux

I have successfully designed a trigger to make sure a set of fields are
unique if not null. However it fails in one case: when the duplication
occurs among a set of rows being updated simultaneously. The trigger is
below. Can you suggest a way to improve it to eliminate the failure? I
don't know enough about triggers to know if changing BEFORE UPDATE to
AFTER UPDATE, for instance, might do what I want. This is only my 5th
trigger.

BTW: Is the column list in the OF phrase an 'or' or an 'and' between the
columns?

CREATE TRIGGER IS3.AN_apr_UNQ_ U
NO CASCADE BEFORE UPDATE OF assoc,prefix,re gnum ON is3.animals
REFERENCING NEW AS N OLD AS O
FOR EACH ROW MODE DB2SQL
WHEN (n.regnum<>''
AND EXISTS(SELECT * FROM is3.animals
WHERE assoc=N.assoc
AND prefix=n.prefix
AND regnum=n.regnum
AND ((n.assoc<>o.as soc OR
n.prefix<>o.pre fix OR
n.regnum<>o.reg num)
OR
o.regnum='')))
SIGNAL SQLSTATE '75103'
SET MESSAGE_TEXT='D uplicate assoc/prefix/regnum'
GO
May 26 '06 #1
2 2773
Bob Stearns wrote:
I have successfully designed a trigger to make sure a set of fields are
unique if not null. However it fails in one case: when the duplication
occurs among a set of rows being updated simultaneously. The trigger is
below. Can you suggest a way to improve it to eliminate the failure? I
don't know enough about triggers to know if changing BEFORE UPDATE to
AFTER UPDATE, for instance, might do what I want. This is only my 5th
trigger.

BTW: Is the column list in the OF phrase an 'or' or an 'and' between the
columns?

CREATE TRIGGER IS3.AN_apr_UNQ_ U
NO CASCADE BEFORE UPDATE OF assoc,prefix,re gnum ON is3.animals
REFERENCING NEW AS N OLD AS O
FOR EACH ROW MODE DB2SQL
WHEN (n.regnum<>''
AND EXISTS(SELECT * FROM is3.animals
WHERE assoc=N.assoc
AND prefix=n.prefix
AND regnum=n.regnum
AND ((n.assoc<>o.as soc OR
n.prefix<>o.pre fix OR
n.regnum<>o.reg num)
OR
o.regnum='')))
SIGNAL SQLSTATE '75103'
SET MESSAGE_TEXT='D uplicate assoc/prefix/regnum'
GO


I don't know enought abou TRIGGERs either.

I would think, however, that this does not need to be done in a
TRIGGER. Rather, a GENERATED COLUMN could do.

I had a similar problem. That, i had two COLUMNs, where the first was
NOT NULL, the second could be NULL. But, the second could be NULL only
once per value of the first. So, i added a third COLUMN, GENERATED
ALWAYS AS (COALESCE(Col2, (Col1 || '-'))), and put a UNIQUE INDEX on
that.

In your case, the NULL is causing problems. However, if NULL was a
unique value it would not be. To generate unique values a SEQUENCE
could be used, and allow it when the value IS NULL.

regnum_unique GENERATED ALWAYS AS (COALESCE(Regnu m, Sequence.NextVa l))

Then throw the CONSTRAINT on regnum_unique instead of just regnum.

B.

May 26 '06 #2
Please, provide the script to demonstrate your issue. Your design isn't
clear.

May 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1774
by: rkrueger | last post by:
Given the following 3 Tables: CREATE TABLE ( NOT NULL , NOT NULL CONSTRAINT DEFAULT (getdate()), NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
0
1737
by: Rajesh Jain | last post by:
I Have 2 separate schemas. --------------Schema 1 is defined as below----------- <xs:schema targetNamespace="http://Schemas/1" xmlns="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="Loan"> <xs:complexType> <xs:sequence> <xs:element name="Borrower" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="BorrID" use="required">
5
11572
by: Dave Sisk | last post by:
Hey folks: I'm trying to do this: CREATE TRIGGER datawhse.emp_ti AFTER INSERT ON emp REFERENCING NEW AS n FOR EACH ROW MODE DB2ROW BEGIN DECLARE v_rrn DECIMAL(15,0);
5
4547
by: William of Ockham | last post by:
Hi, I was asked to recreate a new clean database for our developers because the current one they use is not entirely up to date. So I created a new database and I run into the followin strange problem. First some facts: System: DB2 V8.1 Fixpack5 Redhat Linux 8.0 dual processor Database A is the current database and all DDL I currently have executes fine. Database B is the new one and is created on the same instance as A.
1
4599
by: Robert Fitzpatrick | last post by:
I am running PostgreSQL 7.4.5 and have a trigger on a table called tblriskassessors which inserts, updates or delete a corresponding record in tblinspectors by lookup of a contact id and license number match. The INSERT and DELETE work fine. The UPDATE works good unless I update the license number. The error, at the bottom of this message, suggests the primary key violation. But my UPDATE in no way alters the primary key, which is...
3
3741
by: teddysnips | last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!) but I can't get it to work because it references ntext fields. Is there any alternative? I could write it in laborious code in the application, but I'd rather not! DDL for table and trigger below. TIA
7
7017
by: Serge Rielau | last post by:
Hi all, Following Ian's passionate postings on problems with ALTOBJ and the alter table wizard in the control center I'll try to explain how to use ALTOBJ with this thread. I'm not going to get into the GUI because it is hard to describe in text. First of all what is the purpose of ALTOBJ()? This procedure was created mostly for ISVs who need to do produce change scripts to upgrade application from release to release, but it can also
13
6598
by: dennis | last post by:
Hello, I'm having trouble solving the following problem with DB2 UDB 8.2. I need to create a trigger that performs certain extra constraint validations (temporal uniqueness). One of the tables has no primary key. I'm having trouble expressing an update trigger that checks for existing rows, contemporary to the updated one, but exclusing the
3
5979
by: Wojto | last post by:
Hi there! I need to write a trigger that will check referential integrity of my data. I have few FOREIGN KEY constraints but, as You probably konow, the cannot be deferred (in the meaning of SQL 92 standard). So I decided to add NOCHECK CONSTRAINT ALL to the modified table and then run a trigger (after secon altertion of my table). But I cannot write a trigger for ALTER. I found something on msdn, byt their example doesn't work. To show...
0
11183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8261
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7434
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6122
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4545
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3550
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.