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

  • Home
  • SEARCH
  • 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 7069897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:30:05+00:00 2026-05-28T05:30:05+00:00

Is it possible in MySQL to insert a row into table2 automatically when a

  • 0

Is it possible in MySQL to insert a row into table2 automatically when a row is inserted into table1?

Say I have the following basic structure:

User Table

id           int(8)       - primary key       - auto increment
username     varchar(20)

UserPrivacy Table – userprivacy.userId is a foreign key of user.id

id           int(8)       - primary key       - auto increment
userId       int(8)       - foreign key

Could I get a row inserted into User Privacy Table with default values but the corresponding user.id when a ran a query such as INSERT INTO user (username) VALUES ('Bob');

Is there a way other than triggers or would it be best to just run two queries whenever a user is inserted into user?

Secondly is it a waste of storage to have a separate userprivacy table if it’s storing a value for every user? could be more efficient to place it in the user table? but I’m trying to make that as light as possible ..

there are more columns in the user table I’m just keeping it simple for this example

  • 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-28T05:30:06+00:00Added an answer on May 28, 2026 at 5:30 am

    You would require triggers in order to “automatically” insert the record into a separate table.

    Inserting the record into UserPrivacy with a separate query would be the most common way of implementing this.

    If it’s a one-to-one relationship, putting it in the same table allows more simple queries (no join required).

    Decisions, Decisions …

    With a one-to-(zero to one) relationship, there are more factors to consider.

    If the UserPrivacy table is large, then it might make sense to put it in a separate table to save space. This would also be more normalized.

    If you were often querying something like “Give me all the Users that don’t have PrivacyData”, then it might make sense to put it in a separate table. Since indexes don’t include NULL values (by default), it would be faster to do a JOIN to a separate table. Of course, the work around is to use a value other than NULL to represent “no privacy settings”, but low cardinality also negatively affects performance. Separate tables would be best for this case.

    Also, if the privacy data was updated frequently, but not the User data, separate tables would prevent row locks on the User table, and updates are faster on smaller tables, which might improve performance.

    If you often needed the UserPrivacy data without the UserData, or vice versa, you might want to separate them.

    Still, this might be premature optimization. You might just want to separate them if they better match your models. Consider the simplicity of keeping it in the same table versus the issues of performance, size, and readability.

    If the relationship was one-to-(zero to many), you would obviously want a separate table, but for one-to-(zero to one), it’s optional.

    Finally…

    Don’t be afraid to separate them, as long as there’s a reason.

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

Sidebar

Related Questions

Is it possible to import csv data into mysql and automatically create the column
I'm running into a problem where it is possible to insert a row into
Using only MySQL , I'm seeing if it's possible run an insert statement ONLY
Is it possible to let MySQL LIMIT have an offset of the total number
Is that possible to connect mysql database between servers. I mean, lets say i
I am using QSqlQuery to insert data into a MySQL database. Currently all I
I have some CSV data files that I want to import into mySQL. I
Is it possible to insert a row, but only if one of the values
I'm trying to determine what situations MySQL updates an index. Say I have the
Is it possible to fire a mysql trigger for both the insert and update

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.