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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:55:48+00:00 2026-05-27T09:55:48+00:00

I was working with UIs where the user will click the add button to

  • 0

I was working with UIs where the user will click the add button to add employees, but when I do it, it gives me an error like this

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`finalpayroll`.`personal_info`, CONSTRAINT `personal_info_ibfk_1` 

How would I fix this?? I know I am using a parent key, and its foreign key is the User, and also take note that the parent key has already a data, but it seems my query won’t work, why is that? I am using a foreign key with delete cascade and on update cascade so that when I delete a data, all of the child table rows will be deleted, vice versa. here’s my key for adding or inserting statements

public void addEmployee(Personal p ,Contact c,Employee e) {
    Connection conn = Jdbc.dbConn();
    Statement statement = null;
    String insert1 = "INSERT INTO personal_info (`First_Name`, `Middle_Initial`, `Last_Name`, `Date_Of_Birth`, `Marital_Status`, `Beneficiaries`) VALUES ('"+p.getFirstName()+"', '"+p.getMiddleInitial()+"'" +
                "       , '"+p.getLastName()+"', '"+p.getDateOfBirth()+"', '"+p.getMaritalStatus()+"', '"+p.getBeneficiaries()+"')";
    try {
        statement = conn.createStatement();
        statement.executeUpdate(insert1);
        statement.close();
        conn.close();
        JOptionPane.showMessageDialog(null, "Employee Added!!");
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

Users table:

CREATE TABLE `users` (
  `idusers` int(11) NOT NULL AUTO_INCREMENT,
  `emp_id` varchar(45) DEFAULT NULL,
  `emp_pass` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`idusers`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

Personal_info table:

CREATE TABLE `personal_info` (
    `idpersonal_info` int(11) NOT NULL AUTO_INCREMENT,
    `First_Name` varchar(45) DEFAULT NULL,
    `Middle_Initial` varchar(45) DEFAULT NULL,
    `Last_Name` varchar(45) DEFAULT NULL,
    `Date_Of_Birth` varchar(45) DEFAULT NULL,
    `Marital_Status` varchar(45) DEFAULT NULL,
    `Beneficiaries` varchar(45) DEFAULT NULL,
    PRIMARY KEY (`idpersonal_info`),
    CONSTRAINT `personal_info_ibfk_1`
    FOREIGN KEY (`idpersonal_info`)
    REFERENCES `users` (`idusers`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
  • 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-27T09:55:48+00:00Added an answer on May 27, 2026 at 9:55 am

    You are trying to insert a record with 6 fields: First_Name, Middle_Initial, Last_Name, Date_Of_Birth, Marital_Status and Beneficiaries. Your schema is currently unknown but none of these fields seem to be a candidate foreign key to id of User table you mentioned. Thus I think there is a default value for that foreign key column and that default value is missing in User table.

    Needless to say, you shouldn’t have a default value for a foreign key of any table..

    I am adding these information regarding your questions in comments and update on your question:

    A foreign key is a link between a child table and parent table, personal_info and users tables in your case respectively. Child table’s foreign key column must reference to a key value in parent table which means that for every value in child table’s FK column, there must be a value in parent table’s linked column.

    Now, in your case when you try to insert a new personal_info record MySQL assigns a idpersonal_info to it, since you defined it as auto increment. But since there is a link to users table, MySQL searchs for the new idpersonal_info to be inserted in users table’s idusers column. And as you are getting this exception, you surely don’t have that value in the users table.

    You can change your table structure as follows:

    CREATE TABLE `personal_info` (
      `idpersonal_info` int(11) NOT NULL AUTO_INCREMENT,
      `user_id` int(11) NOT NULL,
    
      ... OTHER FIELD DEFINITIONS,
    
      PRIMARY KEY (`idpersonal_info`),
      CONSTRAINT `user_id_fk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`idusers`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB
    

    And your query will need to include user_id field as well. So it will be something like this:

    INSERT INTO personal_info
      (`user_id`, `First_Name`, `Middle_Initial`, `Last_Name`, `Date_Of_Birth`, `Marital_Status`, `Beneficiaries`)
    VALUES ( .... SET YOUR VALUES HERE. DON'T FORGET TO SET A VALID USER_ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on BlackBerry user interface. But the BlackBerry UIs are less attractive
Working on this page: http://www.karlsenner.dreamhosters.com/about.php and having trouble with the navigation in IE6. It
Working with TCL and I'd like to implement something like the Strategy Pattern .
I'm working on an EclipsePluginProject. I don't have much(nearly nothing) experience with UIs under
I've been working on a Firefox extension, but it's been slow going (in part
Working on a website http://www.ArenaText.com written in asp.net with Microsoft AJAX control toolkit. iPad
I'm working on a Windows application with an integrated text editor, like an IDE.
I'm working on adding a WCF services layer to my existing .NET application. This
I'm working with the Google Groups on Knockout's forums for help with this too
I'm working on a Django project. But my problem is on JQuery. I used

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.