Before normalization, a flat file could be used to describe the city and state data:
id city code country
1 Amsterdam NED Netherlands
2 Anvers BEL Belgium
3 Athènes GRE Greece
4 Atlanta USA United States
5 Barcelone ESP Spain
For this data, no more normalization is necessary. In fact, it would be counter-productive.
But what if you wanted to store information about the country that you don't want repeated in every city's record? i.e., you wanted to keep population size, crime rate, name of currency... whatever. You wouldn't want to store that information with every record that references the country; that would be a lot of duplicated information, and if you wanted to update the country's data, you'd have to change it in every record that has a city in that country.
This is where the concept of foreign keys comes in to play. You would split this into two different tables: city and country. Information that is specific about a city (city name etc) goes into the city table, and information that is specific to a country (population of country, currency name, etc) goes in to the country table.
But now, how do we populate the tables?
In a web-based environment, typically you would have a data entry page that would have an input for the city name, and a drop down box (or AJAX lookup) for the country with the value being the id for the country name. This way, when you submit the city and country pair, you have the country id to insert with the name of the city.
But in this case, you already have the city and country tables populated. In order to update the city table with the country id, you have to be able to join the tables. Fortunately, you have such a field in the country code. This is a happy accident, because this is actually duplicate information that shouldn't be in the city table... not to mention it's really not a good idea to rely on a varchar field to join on (hard to guarantee that values are identical)
But since it's there, let's use it!
First, how do you connect the tables? You join them on a field that they share.
Given:
City:
id name code country_id
1 Amsterdam NED
2 Anvers BEL
3 Athènes GRE
4 Atlanta USA
5 Barcelone ESP
Country
id name code
1 Afghanistan AFG
2 Albanie ALB
3 Algérie ALG
4 Andorre AND
5 Angola ANG
...
20 Belgium BEL
...
30 Netherlands NED
SELECT CITY.*, COUNTRY.*
FROM CITY
INNER JOIN COUNTRY ON CITY.CODE = COUNTRY.CODE
result (only two match with the result set I'm showing):
1 Amsterdam NED null 30 Netherlands NED
2 Anvers BEL null 20 Belgium BEL
Now that you can join the two tables, you can update the city table based on that data. MySql has its own way to do that:
UPDATE CITY
INNER JOIN COUNTRY ON CITY.CODE=COUNTRY.CODE
SET CITY.COUNTRY_ID = COUNTRY.ID
(See MySQL - UPDATE query based on SELECT Query)