Skip to main content

Find unmatched referenced column values

When you use LOAD DATA LOCAL INFILE on a table that contains a foreign key constraint you get an error with an explanation that a the table cannot be updated because a foreign key constraint fails.....

1. Turn off foreign key checks with the following command at the MySQL command line client

a. set foreign_key_checks = 0;

3. issue your load data command again

4. once the data is loaded issue the following query

select referencing_table . referencing_column
FROM referencing_table
LEFT JOIN referenced_table ON
(
referencing_table . referencing_column
=
referenced_table.referenced_column
)
WHERE
referenced_table.referenced_column IS NULL;

This will give you a list of all the column values in the offending table that do not match a value in the referenced table.

5. Update all of the values listed in the query results from above

6. turn the foreign_key_checks back on with the following
a. set foreign_key_checks = 1;

Why this works

We are doing a LEFT JOIN which means that everything on the left side of the expression will be returned regardless of whether or not there is a match on the join. We purposefully put our referencing column on the left.

Then we joined it to the table we want to reference with the JOIN....ON statement so there is a relationship between the tables.

We then excluded all instances where there was a match with the WHERE clause by only selecting instances of the join where the referenced column was null (if it wasn't null then the values in the 2 tables did match and therefore, was not breaking the constraint).

Why this is important
Sure you got your data loaded by turning off the foreign key check so you're good right? No you need to take this step and update all the values that don't match because

1. you added the constraint for a reason right (probably consistent, reliable data) so you want to make sure that your data is consistent and reliable.

2. If you issue an alter table statement on a table that is violating a foreign key constraint the server will error and you won't be able to alter your table with out setting the foreign_key_checks = 0 again.

Comments

Popular posts from this blog

Carmel Marathon 2020 Weeks 2 & 3

Training for the Carmel Marathon on April 4th, 2020 in Carmel Indiana Goal: 2:51 Week 2 Runs: 8 Weekly Milage: 72 Cumulative Milage: 139 Workouts / Long Run: 17 with 8 at Marathon Pace Performance Management Chart for the week showing my running fitness progression from the beginning of the cycle through the current week. The main focus this week was just building my milage a little from last week, about 5 more miles, while being ready for my first MP workout. I am pretty focused right now and I struggled some last week with wanting to do more that the plan called for. I really wanted to add in a threshold workout. Marathon training is about discipline and long term thinking though. I could have added in some threshold miles and it probably would have been ok but doing the plan the way the plan is written will get me where I want to go and will keep me feeling on track and help me from wavering in the other direction as well. I'm doing the plan, period. I ran my fi...

Running Goals or Running Goal; What is it That I Want?

The other day I got all caught up in setting running goals for myself for the rest of the year. After the goals were set, I found myself starting to worry about how I was going to reach them. Wondering if there would be time to sufficiently prepare for them all. My goals were to run a 5k in under 20 minutes, to run a half marathon in 1:25 minutes and qualify for and run in the Boston marathon in 2014. At first, I thought that setting and meeting these goals would make me a more serious runner. In fact, I thought that to be the "serious" runner I want to be that I needed these goals. However, while I was running the other morning I realized that first of all, I was thinking about how to shift my training to meet my 5k goal. Then I began thinking about the right approach to a 1:25 half while training to qualify for Boston in a full marathon a month later. That is when I realized that the ancillary goals were starting to consume my focus and quite possibly i...

The Gosh Darn Tuesday Morning 15-Miler

What can you say about a 15-mile Tuesday morning run? I can say a lot. I’ve had a lot of time to think about what I can say about a 15-mile Tuesday morning run on more 15-mile Tuesday Morning runs than I can count. You see, they are a main stay of the marathon training plan that I use. That plan being Pete Pfitzinger’s Advanced Marathoning 18 week 70-85 miles per week plan. The first thing I can say about the Tuesday morning 15-mile run is that it’s not a long run. Pete clearly defines long runs as starting at 16 miles. So, the 15-miler that begins so many Tuesdays of my life is not a long run. I mean, who ever heard of doing a “long run” on a Tuesday morning. That would just be insane. Being that it’s not a long run I can’t do all of the things that I do around a long run. I can’t obsess over the weather for the 15-miler, I can’t eat extra carbs in preparation for the 15-miler, I can’t expect my family to give me deference and make accommodations for me ...