Search code examples
mysqldatabase-designphpmyadminmany-to-manydatabase-normalization

Split single table to many-to-many relationship


I have a table that looks like the following:

enter image description here

I want to split this to 3 different tables with a PK-FK relationship. Essentially a province can have many state, a state can have many cities. What is the best way to do this in one shot using a SQL query if it's even possible? Or using tools like phpmyadmin.


Solution

  • Province table:

    Province_ID (PK)
    Province_Name
    

    State table:

    State_ID (PK)
    State_Name
    Province_ID (FK)
    AreaCode
    

    Citi table:

    Citi_ID (PK)
    Citi_Name
    State_ID (FK)
    Citi_Code
    

    I don't think there's any tools that can help you normalize this table automatically. So you have to create these tables by code then using sql query to insert values into it