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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:21:39+00:00 2026-05-24T17:21:39+00:00

Why in MySQL, INSERT IGNORE INTO does not change the foreign key constraint errors

  • 0

Why in MySQL, INSERT IGNORE INTO does not change the foreign key constraint errors into warnings?

I’m trying to insert a number of records into a table and I expect MySQL to leave out the ones that result in error, any error, and insert the rest. Does anyone have any suggestions?

And the SET FOREIGN_KEY_CHECKS = 0; is not my answer. Because I expect the rows which defy the constraints not to be inserted at all.

Thanks

  • 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-24T17:21:40+00:00Added an answer on May 24, 2026 at 5:21 pm

    [NEW ANSWER]

    Thanks to @NeverEndingQueue for bringing this up. It seems MySQL has finally fixed this issue. I’m not sure which version this problem was first fixed in, but right now I tested with the following version and the problem is not there anymore:

    mysql> SHOW VARIABLES LIKE "%version%";
    +-------------------------+------------------------------+
    | Variable_name           | Value                        |
    +-------------------------+------------------------------+
    | innodb_version          | 5.7.22                       |
    | protocol_version        | 10                           |
    | slave_type_conversions  |                              |
    | tls_version             | TLSv1,TLSv1.1                |
    | version                 | 5.7.22                       |
    | version_comment         | MySQL Community Server (GPL) |
    | version_compile_machine | x86_64                       |
    | version_compile_os      | Linux                        |
    +-------------------------+------------------------------+
    

    To be clear:

    mysql> INSERT IGNORE INTO child
        -> VALUES
        ->     (NULL, 1)
        ->     , (NULL, 2)
        ->     , (NULL, 3)
        ->     , (NULL, 4)
        ->     , (NULL, 5)
        ->     , (NULL, 6);
    Query OK, 4 rows affected, 2 warnings (0.03 sec)
    Records: 6  Duplicates: 2  Warnings: 2
    

    To better understand the meaning of this last query and why it shows the problem is fixed, please continue with the old answer below.

    [OLD ANSWER]

    My solution is a work around to the problem and the actual solution will always be fixing the problem within the MySQL itself.

    The following steps solved my problem:

    a. Consider having the following tables and data:

    mysql>
    CREATE TABLE parent (id INT AUTO_INCREMENT NOT NULL
                         , PRIMARY KEY (id)
    ) ENGINE=INNODB;
    
    mysql>
    CREATE TABLE child (id INT AUTO_INCREMENT
                        , parent_id INT
                        , INDEX par_ind (parent_id)
                        , PRIMARY KEY (id)
                        , FOREIGN KEY (parent_id) REFERENCES parent(id)
                            ON DELETE CASCADE
                            ON UPDATE CASCADE
    ) ENGINE=INNODB;
    
    mysql>
    INSERT INTO parent
    VALUES (NULL), (NULL), (NULL), (NULL), (NULL), (NULL);
    
    mysql>
    SELECT * FROM parent;
    +----+
    | id |
    +----+
    |  1 |
    |  2 |
    |  3 |
    |  4 |
    |  5 |
    |  6 |
    +----+
    

    b. Now we need to delete some of the rows to demonstrate the problem:

    mysql>
    DELETE FROM parent WHERE id IN (3, 5);
    

    c. PROBLEM: The problem arises when you try to insert the following child rows:

    mysql>
    INSERT IGNORE INTO child
    VALUES
        (NULL, 1)
        , (NULL, 2)
        , (NULL, 3)
        , (NULL, 4)
        , (NULL, 5)
        , (NULL, 6);
    
    ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f
    ails (`test`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERE
    NCES `parent` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
    
    mysql>
    SELECT * FROM child;
    Empty set (0.00 sec)
    

    Even though the IGNORE keyword is used, but MySQL cancels the the requested operation because the generated error is not turned into warning (as it supposed to). Now that the problem is obvious, let’s see how can we execute the last insert into statement without facing any error.

    d. SOLUTION: I’m going to wrap the insert into statement by some other constant statements which are neither dependent on the records inserted, nor on their number.

    mysql>
    SET FOREIGN_KEY_CHECKS = 0;
    
    mysql>
    INSERT INTO child
    VALUES
        (NULL, 1)
        , (NULL, 2)
        , (NULL, 3)
        , (NULL, 4)
        , (NULL, 5)
        , (NULL, 6);
    
    mysql>
    DELETE FROM child WHERE parent_id NOT IN (SELECT id FROM parent);
    
    mysql>
    SET FOREIGN_KEY_CHECKS = 1;
    

    I know that this is not optimum but as long as MySQL has not fixed the problem, this is the best I know. Especially since all the statements can be executed in one request if you use mysqli library in PHP.

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

Sidebar

Related Questions

i have mysql code for input data: $sql = INSERT IGNORE INTO inspection_report ;
I am running the following query in MySQL REPEAT INSERT IGNORE INTO tableB SELECT
I'm trying to insert a file path into an empty MySQL table with PHP
i am running a query in mysql insert ignore into........ using Python after running
im trying to insert this query with mysql_query INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,1,1)
I was wondering how insert now() + 3 days into a mysql insert. kinda
I got on my php script $result1 = mysql_query(insert ignore into...this.... $result2 = mysql_query(insert
$q = $db->prepare('INSERT IGNORE INTO Cities (Name,Stamp) VALUES (?,?)'); $q->execute(array($city,$stamp)); I am running this
I have the following code: INSERT IGNORE INTO unsubscribes (email) VALUES (john@john.com),(kevin@kevin.com),(mike@mike.com),(another@gmail.com) but it
When I am attempting to run "INSERT IGNORE ..." in MYSQL to add only

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.