2

I have three columns as a composite primary key in a table of PostgreSql database. Data for two columns will be provided in insert query. But another column has to be incremented every time insertion happens. Here's an example:

CREATE TABLE "food_index_directory" ( "primary_key_one" VARCHAR(6) NOT NULL, "primary_key_two" NUMERIC(4) NOT NULL, "primary_key_three" NUMERIC(5) NOT NULL, "varchar_column_one" VARCHAR(30) NOT NULL, "numeric_column_one" INTEGER NOT NULL, "numeric_column_two" NUMERIC(12,9) DEFAULT .001 NOT NULL, "numeric_column_three" NUMERIC(5,2), "last_upd_date" DATE NOT NULL, CONSTRAINT "PK_dummy_table" PRIMARY KEY ("primary_key_one","primary_key_two","primary_key_three") ); 

As I said above, data for two primary key columns and another(primary_key_three) has to be incremented based on number of occurrences of first and second key columns. Second key column can only take values from 1 to 12, this third column is a count column for second column.
ex:

primary_key_one primary_key_two primary_key_three ...... 91 1 1 ...... 91 1 2 ...... 91 2 1 ...... 91 2 2 ...... 91 1 3 ...... 91 2 3 ...... 91 3 1 ...... 91 2 4 ...... 34 3 1 ...... 91 1 4 ...... 91 4 1 ...... 91 5 1 ...... 34 4 1 ...... 

In the above example, (I have made spaces for better understanding, when data in first two columns are repeated), third column counts number of times first two columns have repeated in this table.

During Insert, I will be providing data for first two columns and the third column has to increment automatically as shown and explained above? How do I do this?

3
  • you can use trigger for your requirement. Commented May 20, 2020 at 16:56
  • I don't know how to write a trigger. Will you please be able to write it and answer my question @AkhileshMishra? Commented May 20, 2020 at 17:04
  • posted the trigger Commented May 20, 2020 at 17:15

1 Answer 1

1

Find the procedure

CREATE OR REPLACE FUNCTION "fn_trig_pk"() RETURNS "pg_catalog"."trigger" AS $BODY$ begin new.primary_key_three = (select count(*)+1 from food_index_directory where primary_key_one=new.primary_key_one and primary_key_two=new.primary_key_two ); return NEW; end; $BODY$ LANGUAGE plpgsql VOLATILE COST 100 

and Trigger for your Table

CREATE TRIGGER trig_pk BEFORE insert ON food_index_directory FOR EACH ROW EXECUTE PROCEDURE fn_trig_pk(); 
Sign up to request clarification or add additional context in comments.

5 Comments

I now have one doubt. After this trigger, can i not specify value for primary_key_three in INSERT statement and will it be executed without issues?
if you specify the value for the column primary_key_three, trigger will overwrite it as per its logic. if you will not provide the value for column it will add the value for primary_key_three
I mean to ask, after this not specify the primary_key_three column name and also not prove any value for it, like fire example: INSERT INTO food_index_directory (primary_key_one, primary_key_two, varchar_column_one, numeric_column_one, numeric_column_two, numeric_column_three VALUES (value1, value2,,,,,,,);
Thanks for the fast response.
No, "fn_trig_pk"() can be replaced with "schema_name"."trigger_name". RETURNS "pg_catalog"."trigger" you have to keep as it is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.