[Solved] PK on table is increased when TRIGGER is fired

Discuss the database features
Post Reply
dreamquartz
Posts: 881
Joined: Mon May 30, 2011 4:02 am

[Solved] PK on table is increased when TRIGGER is fired

Post by dreamquartz »

Hello All,

Have a simple BEFORE INSERT TRIGGER for PostalCodes.
I want prevent duplication.

Code: Select all

CREATE TRIGGER PUBLIC.POSTALCODE BEFORE INSERT ON PUBLIC."tPostalCode"
	 REFERENCING NEW ROW AS NEWROW FOR EACH ROW
	 BEGIN ATOMIC
		 IF EXISTS
			 (
				 SELECT
					 *
				 FROM
					 PUBLIC."tPostalCode"
				 WHERE
					 "tPostalCode"."PostalCode" = NEWROW."PostalCode"
			 )
			 THEN SIGNAL SQLSTATE'45000'SET MESSAGE_TEXT='This PostalCode already exists. Please verify the Existing entry!';
		 END IF;
	 END
When the TRIGGER is fired, I normally do: "Undo: Data Entry".
This still leads to an increment of the PK: PostalCodeID.

No data is entered, so should the PK not increase?

Dream
Last edited by dreamquartz on Mon May 07, 2018 8:23 pm, edited 1 time in total.
LO 7.x, HSQLDB 2.7.x & Ubuntu 22.04 LTS.
eremmel
Posts: 1080
Joined: Tue Dec 30, 2008 1:15 am

Re: PK on table is increased when TRIGGER is fired

Post by eremmel »

Don't worry about this kind of internals, its just a auto-incremental number. You can not use these numbers when you need strict consecutive numbering, as requested by some governments for financial statements. This behavior is common for all databases I know.
It's Microsoft marketing that tells you computers are qualified for non-technicians
W11 22H2 (build 22621), LO 7.4.2.3(x64)
dreamquartz
Posts: 881
Joined: Mon May 30, 2011 4:02 am

Re: PK on table is increased when TRIGGER is fired

Post by dreamquartz »

You know, I just recently realized it. I do not recall ever reading or hearing about it.

Thanks for the clarification.

Dream
LO 7.x, HSQLDB 2.7.x & Ubuntu 22.04 LTS.
Post Reply