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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:26:21+00:00 2026-06-14T11:26:21+00:00

In my MySQL table I have a simple postal codes table that I have

  • 0

In my MySQL table I have a simple postal codes table that I have converted it from an .xlsx file.

Country   |City    |MiniDistrict |Postal Code
---------------------------------------------
Bulgaria  |Sofia   |Oborishte    | 0844
Bulgaria  |Sofia   |Sredets      | 0988
Bulgaria  |Plovdiv |Montana      | 1299
Bulgaria  |Plovdiv |Dyavol       | 1288
Bulgaria  |Varna   |Andeevo      | 1574
France    |Paris   |Mantois      |38992
France    |Paris   |Valois       |38764
France    |Gard    |Lussan       |55980

As you can see Country and city names are repeating numerious times. also note that there are same city names in different countries. I want to write a PHP algorithm where I’ll generate a country table, a city table, and a minidistrict-postalcode table into MySQL. The country table is easy, SELECT DISTINCT. What algorythm should I write for the other tables? The algorytm idea will be enough for me, no need for the coding part.

  • 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-06-14T11:26:23+00:00Added an answer on June 14, 2026 at 11:26 am

    The “algorithm” is this: you notice that every MiniDistrict has a PostalCode, and every PostalCode has its MiniDistrict. So you put these together in a single table, and associate each pair with an unique ID.

    CREATE TABLE district_postalcode
    (
        id integer not null primary key auto_increment,
        minidistrict varchar(???),
        postalcode char(5)
    );
    

    Then you notice that each city is a single object, and the same for the countries, so you create two tables for them, each with its own ID.

    Now you must store a relationship between minidistricts and cities. But this is a many-to-one relationship: many minidistricts may belong to the same city, but the same minidistrict can’t belong to two cities.

    So you add a reference to cities.id as foreign key city_id to minidistricts.

    The relationship structure is the same between cities and countries. So again you add a foreign key, this time country_id to countries.id into cities.

    At that point, you can also create a VIEW to see your data as before:

    CREATE VIEW old_style AS SELECT
        countries.Country,
        cities.City,
        districts.MiniDistrict,
        districts.PostalCode
    FROM districts
        JOIN cities ON (cities.id = districts.city_id)
        JOIN countries ON (country.id = cities.country_id);
    

    You can also populate your new districts, cities and countries from your existing data, beginning with the table that has no foreign keys:

    INSERT INTO countries ( Country ) SELECT DISTINCT Country FROM old_table;
    
    INSERT INTO cities ( City, country_id )
        SELECT DISTINCT City, Country.id
        FROM old_table
        JOIN countries ON ( old_table.Country = countries.Country );
    

    (This will insert twice a city if it exists with the same name in two countries – one with each country ID – but that’s as it should be: city names are normally not unique).

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

Sidebar

Related Questions

I have a simple Message table, with 2 indexes: mysql> show keys from Message;
Using MySQL, I have a simple table that logs the IP Address that users
I have the following task: there are a simple table from mysql database: <html>
I have a simple MySQL table that is setup for full text search. |
I have a simple mysql table This is the code to connect to database
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
I have a simple table called that contains share prices in MySQL: Table `share_prices`
I have a simple mySQL database table that I am loading into a PHP
I have a simple table in mysql that has different types of records, differentiated
I have a simple MySQL table thats contains a list of categories, level is

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.