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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:13:22+00:00 2026-05-15T22:13:22+00:00

I have a MySQL database which contains a table of users. The primary key

  • 0

I have a MySQL database which contains a table of users. The primary key of the table is ‘userid’, which is set to be an auto increment field.

What I’d like to do is when I insert a new user into the table is to use the same value that the auto increment is creating in the ‘userid’ field in a different field, ‘default_assignment’.

e.g.

I’d like a statement like this:

INSERT INTO users ('username','default_assignment') VALUES ('barry', value_of_auto_increment_field())

so I create user ‘Barry’, the ‘userid’ is generated as being 16 (for example), but I also want the ‘default_assignment’ to have the same value of 16.

Is there any way to achieve this please?

Thanks!

Update:

Thanks for the replies. The default_assignment field isn’t redundant. The default_assigment can reference any user within the users table. When creating a user I already have a form that allows a selection of another user as the default_assignment, however there are cases where it needs to be set to the same user, hence my question.

Update:

Ok, I’ve tried out the update triggers suggestion but still can’t get this to work. Here’s the trigger I’ve created:

CREATE TRIGGER default_assignment_self BEFORE INSERT ON `users`  
FOR EACH ROW BEGIN
SET NEW.default_assignment = NEW.userid;
END;

When inserting a new user however the default_assignment is always set to 0.

If I manually set the userid then the default_assignment does get set to the userid.

Therefore the auto assignment generation process clearly happens after the trigger takes effect.

  • 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-15T22:13:22+00:00Added an answer on May 15, 2026 at 10:13 pm

    there’s no need to create another table, and max() will have problems acording to the auto_increment value of the table, do this:

    CREATE TRIGGER trigger_name BEFORE INSERT ON tbl FOR EACH ROW
    BEGIN
       DECLARE next_id;
       SET next_id = (SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='tbl');
       SET NEW.field = next_id;
    END
    

    I declare the next_id variable because usually it will be used in some other way(*), but you could do straight new.field=(select …)

    CREATE TRIGGER trigger_name BEFORE INSERT ON tbl FOR EACH ROW
    BEGIN
       SET NEW.field=(SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='tbl');
    END
    

    Also in cases of (SELECT string field) you can use CAST value;

    CREATE TRIGGER trigger_name BEFORE INSERT ON tbl FOR EACH ROW
    BEGIN
       SET NEW.field=CAST((SELECT aStringField FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='tbl') AS UNSIGNED);
    END
    

    (*) To auto-name an image:

    SET NEW.field = CONCAT('image_', next_id, '.gif');
    

    (*) To create a hash:

    SET NEW.field = CONCAT( MD5( next_id ) , MD5( FLOOR( RAND( ) *10000000 ) ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql database which as one of the fields contains a html
I have a mysql database which has grown to over 200 tables in it.
I have a MySQL database behind a firewall which can only be accessed via
I have a PHP web application which uses a MySQL database for object tagging,
I have a MySQL database table with a couple thousand rows. The table is
I have a mysql database set as utf-8, and csv data set as utf-8,
I have a database that has two tables, one of which contains a foreign
I have a MySQL database of keywords that are presently mixed-case. However, I want
I have a MySQL database that I want to archive . What is the
I have a mysql database filled up and running on a Windows computer, is

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.