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 8886137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:29:06+00:00 2026-06-14T21:29:06+00:00

I have 5 columns a, b, c, d and e. I want to insert

  • 0

I have 5 columns a, b, c, d and e.
I want to insert values into first 4 columns and have kept default value as 1 in last column.
I have kept the first 4 columns as primary keys.
If duplicate values are inserted in the first 4 columns, then I want to insert the same values again but with increment in the 5th column.
Eg: if 1,2,3,4 are values for the first 4 columns. then when 1,2,3,4 comes again they need to be inserted as 1,2,3,4 but with fifth column as 2.

  • 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-06-14T21:29:07+00:00Added an answer on June 14, 2026 at 9:29 pm

    First remove the primary key and add a unique key instead.

    ALTER TABLE test DROP PRIMARY KEY, ADD UNIQUE KEY 'test_uIdx_a_b_c_d_e' ( a,b,c,d,e );
    

    Assuming your table now looks like this

    CREATE TABLE test (
        a int NOT NULL DEFAULT '0',
        b int NOT NULL DEFAULT '0',
        c int NOT NULL DEFAULT '0',
        d int NOT NULL DEFAULT '0',
        e int NOT NULL DEFAULT '0',
        UNIQUE KEY 'test_uIdx_a_b_c_d_e' ( a,b,c,d,e )
    )
    

    Then you can create a trigger on before insert

    DELIMITER ;;
    CREATE TRIGGER before_test_insert 
    BEFORE INSERT ON test
    FOR EACH ROW
    BEGIN
        SET NEW.e := ( SELECT COUNT(*)+1 FROM test WHERE a = NEW.a AND b = NEW.b AND c = NEW.c AND d = NEW.d;
    END;;
    
    DELIMITER ;
    

    This will set field e to the number of occurrences of a,b,c,d

    One of the comments above offered a simpler alternative

    http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

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

Sidebar

Related Questions

I have a non-nullable database column which has a default value set. When inserting
I have a table in which the first column is auto_increment. I want to
If I have an insert statement with a bunch of values where the first
I have a listview with 3 coloumns. The first two columns has values and
I have been attempting to insert a VLookup formula into a column using VBA.
I have a file containing lines like insert into table_name (params1,params2,params3) values (43044,'x',23547003); insert
I want to have two columns on the bottom of this webpage I'm making,
I have a gridview with couple of columns,I want to achieve the following: If
I have 3 columns in Oracle database having table mytable and i want records
I want to show datagrid content (rows,columns) in XPS document.I have 20 columns. When

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.