Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:55:54+00:00 2026-05-31T16:55:54+00:00

I am using a trigger function to write data into a new table in

  • 0

I am using a trigger function to write data into a new table in Postgresql 9.1. Everything is now working correctly but I am getting duplicates. Can I use foreign key constraints to prevent duplicates from being produced? I haven`t used foreign key constraints before.

Here is the table structure

DROP TABLE "obx" CASCADE;
   CREATE TABLE "obx" (
    "obxID" serial primary key,
    "Pid" varchar,
    "Sid" varchar,
    "SidOrig" varchar,
    "Parameter" varchar,
    "Result" varchar,
    "ResultOrig" varchar,
    "Units" varchar,
    "RefRange" varchar,
    "Flag" varchar,
    "FlagOrig" varchar,
    "OperatorID" varchar,
    "ObsTime" char(14),
    "MsgTime" char(14),
    "UnixTime" int4,
    "Analyzer" varchar,
    "Segment" varchar
    );

DROP TABLE "testcode" CASCADE;
CREATE TABLE "testcode" (
    "TcodeID" serial primary key,
    "Analyzer" varchar,
    "Parameter" varchar,
    "TestName" varchar,
    "ShortTestName" varchar,
    "TestID" int4
    ) ;
DROP TABLE "finaldata" CASCADE;
CREATE TABLE "finaldata" (
    "FdataID" serial primary key,
    "Pid" varchar,
    "Sid" varchar,
    "SidOrig" varchar,
    "Parameter" varchar,
    "Result" varchar,
    "ResultOrig" varchar,
    "Units" varchar,
    "OperatorID" varchar,
    "ObsTime" varchar,
    "MsgTime" varchar,
    "Analyzer" varchar,
    "TestName" varchar,
    "ShortTestName" varchar,
    "TestID" varchar,
    "XYchar1" varchar,
    "XYchar2" varchar,
    "XYchar3" varchar,
    "XYint1" int4,
    "XYint2" int4,
    "XYint3" int4,
    "XYGuid" uuid
    ) ;

And the trigger function:

BEGIN
      INSERT INTO finaldata ("Pid", "Sid", "SidOrig", "Parameter", "Result", "ResultOrig", "Units"
        , "OperatorID", "ObsTime", "MsgTime", "Analyzer", "TestName", "ShortTestName", "TestID")
      SELECT ob."Pid", ob."Sid", ob."SidOrig", ob."Parameter", ob."Result", ob."ResultOrig", ob."Units"
        , ob."OperatorID", ob."ObsTime", ob."MsgTime", ob."Analyzer"
        , tc."TestName", tc."ShortTestName", tc."TestID"
      FROM obx ob
      JOIN testcode tc ON ob."Parameter" = tc."Parameter"
            WHERE ob."Sid" = NEW."Sid"
            AND ob."ObsTime" = NEW."ObsTime"
            AND ob."Parameter" = NEW."Parameter"
            AND ob."Analyzer" = NEW."Analyzer"
      AND tc."TestID" IS NOT NULL
           ;
      RETURN NEW;
  END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:55:55+00:00Added an answer on May 31, 2026 at 4:55 pm

    Can I use foreign key constraints to prevent duplicates from being produced?

    About duplicates

    No, you use either PRIMARY KEY or NOT NULL UNIQUE to prevent duplicates. This declaration

    CREATE TABLE "finaldata" (
        "FdataID" serial primary key,
    

    guarantees that each row will be unique, but it doesn’t guarantee that the thing the row represents–which is what database designers are interested in–will be unique. Let me give you a smaller example.

    create table person (
      person_id serial primary key,
      full_name varchar(35) not null
    );
    
    insert into person (full_name) values ('Mendez, Cathy');
    insert into person (full_name) values ('Mendez, Cathy');
    insert into person (full_name) values ('Mendez, Cathy');
    insert into person (full_name) values ('Mendez, Cathy');
    insert into person (full_name) values ('Mendez, Cathy');
    
    select * from person;
    --
    1   Mendez, Cathy
    2   Mendez, Cathy
    3   Mendez, Cathy
    4   Mendez, Cathy
    5   Mendez, Cathy
    

    The serial ID number makes each row unique, but it doesn’t do anything for the thing the row represents. If I did this

    delete from person;
    alter table person
    add constraint person_uniq unique (full_name);
    

    then this insert will succeed,

    insert into person (full_name) values ('Mendez, Cathy');
    

    but running it a second time will fail with this error.

    ERROR: duplicate key value violates unique constraint "person_uniq"
    SQL state: 23505
    Detail: Key (full_name)=(Mendez, Cathy) already exists.
    

    So to prevent duplicates in the table finaldata, you need a UNIQUE constraint on some subset of the columns.

    My guess–and it’s just a guess–is that you need at least {“Pid”, “Sid”, “SidOrig”, “Parameter”}.

    About foreign keys

    Foreign keys guarantee that the values you put in one set of columns already exist in a different table.

    For example, the values in the column finaldata.pid probably already exist in another table. If the set of values in the finaldata columns {“Pid”, “Sid”, “SidOrig”, “Parameter”} are supposed to already exist in the table obx, then a foreign key constraint like this will prevent meaningless data from finding its way into those finaldata columns.

    constraint finaldata_fk1
    foreign key ("Pid", "Sid", "SidOrig", "Parameter")
      references obx ("Pid", "Sid", "SidOrig", "Parameter") 
    

    You might also need on delete cascade, on update cascade, or some other referential action.

    A foreign key will only work if there’s a unique constraint on the referenced columns. You don’t have one of those yet. You’d need a unique constraint on the obx columns {“Pid”, “Sid”, “SidOrig”, “Parameter”} for that specific foreign key constraint to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently having my mainwindow write text by using WM_PAINT, but now I realise
How do you trigger a javascript function using actionscript in flash? The goal is
I am using following PHP code for trigger creation but always get error, please
I'm using a BackgroundWorker to trigger a thread, in turn calling a function, which
I am using UpdatePanel to trigger a button click event, which saves some 100+
Using the Northwind sample: I would like to create an insert trigger on the
I am using jQuery to try and trigger a method when an ASP.NET (2.0)
I'm using the following logon trigger on an Oracle 10.2 database: CREATE OR REPLACE
I want to trigger javascript alert using PHP. Is it possible I want to
I am using jquery ui datepicker icon trigger. I want to give the icon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.