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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:05:51+00:00 2026-05-28T00:05:51+00:00

I am having trouble with mysql triggers assigning primary key values. Consider my tables:

  • 0

I am having trouble with mysql triggers assigning primary key values. Consider my tables:

v_object : v_object_id,create_date, .. some generic attributes
users : user_id,name, .. attributes of a user

I have a trigger on users table as follows:

create trigger new_vobject_for_user before insert on users
for each row begin
    insert into v_object(v_type,...) values(...);
    set NEW.id = // How do i get the new v_object's id here?
end;

When I insert an user, I need the vobject created and use its new auto incremented value for user_id.

I also would like to know if there is a way to insert the users without specifying the id. For example:

insert into users(n1,n2,n3,...) values(m1,m2,m3,...);

It gives me an error saying user_id is not specified. And that’s true.. what I am trying to do is create vobject & generate id in the trigger.

  • 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-28T00:05:52+00:00Added an answer on May 28, 2026 at 12:05 am

    use LAST_INSERT_ID()

    create trigger new_vobject_for_user before insert on users
    for each row begin
        insert into v_object(v_type,...) values(...);
        set NEW.id = LAST_INSERT_ID();
    end;
    

    alternatively have a look at INFORMATION_SCHEMA tables and get last auto increment for table v_object

    create trigger new_vobject_for_user before insert on users
    for each row begin
        insert into v_object(v_type,...) values(...);
        set NEW.id = (SELECT AUTO_INCREMENT FROM information_schema.TABLES
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'v_object')
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble updating a row in a MySQL database. Here is the
I'm having trouble with some MYSQL. The code below has been working fine until
I'm having some trouble with a MySQL Select Where statement that uses aliases and
I'm trying to port some old MySQL queries to PostgreSQL, but I'm having trouble
I am having trouble binding some data from my MySQL database to a DataGridView
I am having some trouble ordering using MySQL as when using the standard ORDER
I having trouble cause I am fetching some text from the db(mysql), that text
I'm having trouble to get unicode values out of mysql queries. Here is how
I'm having some trouble with MySql right now. I have an query that works
I'm having trouble with MySQL or Python and can't seem to isolate the problem.

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.