Subject Re: [firebird-support] Transactions and triggers
Author Svein Erling Tysvaer
I doubt it. I'd simply add another table and let Firebird handle duplicates:

CREATE TABLE1 (
PK BIGINT PRIMARY KEY,
F1 INTEGER UNIQUE);

CREATE TABLE2 (
PK BIGINT PRIMARY KEY,
F2 INTEGER UNIQUE);

CREATE TABLE3 (
F1F2 INTEGER PRIMARY KEY);

CREATE TRIGGER TRIGGER1I FOR TABLE1 AFTER INSERT POSITION 0
AS
BEGIN
INSERT INTO TABLE3(F1F2) VALUES(NEW.F1);
END;

CREATE TRIGGER TRIGGER1U FOR TABLE1 AFTER UPDATE POSITION 0
AS
BEGIN
UPDATE TABLE3 SET F1F2 = NEW.F1 WHERE F1F2 = OLD.F1;
END;

CREATE TRIGGER TRIGGER2I FOR TABLE2 AFTER INSERT POSITION 0
AS
BEGIN
INSERT INTO TABLE3(F1F2) VALUES(NEW.F2);
END;

CREATE TRIGGER TRIGGER2U FOR TABLE2 AFTER UPDATE POSITION 0
AS
BEGIN
UPDATE TABLE3 SET F1F2 = NEW.F2 WHERE F1F2 = OLD.F2;
END;

HTH,
Set

lobolo2000 wrote:
> The SQL below guarantees uniqueness of F1 union F2 within a single
> transaction. Can this concept be extended to cover 2 simultaneous
> transactions that add records to these tables then commit one after
> the other?
>
> CREATE TABLE1 (
> PK BIGINT PRIMARY KEY,
> F1 INTEGER UNIQUE):
>
> CREATE TABLE2 (
> PK BIGINT PRIMARY KEY,
> F2 INTEGER UNIQUE):
>
> CREATE TRIGGER TRIGGER1 FOR TABLE1 BEFORE INSERT OR UPDATE POSITION 0
> AS BEGIN
> IF (EXISTS(SELECT F2 FROM TABLE2 WHERE F2=NEW.F1)) THEN
> EXCEPTION DUPLICATE_FIELD;
> END
>
> CREATE TRIGGER TRIGGER2 FOR TABLE2 BEFORE INSERT OR UPDATE POSITION 0
> AS BEGIN
> IF (EXISTS(SELECT F1 FROM TABLE1 WHERE F1=NEW.F2)) THEN
> EXCEPTION DUPLICATE_FIELD;
> END