+-------------+----------------------------+-------------------+-----------+-----------+-------------------+
| postal_code | city_district | city | district | region | state |
+-------------+----------------------------+-------------------+-----------+-----------+-------------------+
| 60313 | Innenstadt | Frankfurt am Main | NULL | Darmstadt | Hessen |
| 10719 | Charlottenburg-Wilmersdorf | Berlin | NULL | NULL | Berlin |
| 73773 | NULL | Aichwald | Esslingen | Stuttgart | Baden-Württemberg |
+-------------+----------------------------+-------------------+-----------+-----------+-------------------+
Sometimes city_district, district, and region can be NULL. Small cities have no city districts. A city (Frankfurt am Main) can include/be a district. A city (Berlin) can include/be a district and a region.
The tables are empty and will be populated from validated user input. I created for each columns its own table and joined them, like this:
postalcode: city_id
city_district: city_id
city: district_id
district: region_id
region: state_id
state: name
SELECT
*
FROM
postalcode
LEFT JOIN
city ON postalcode.city_id = city.id
LEFT JOIN
district ON city.district_id = district.id
LEFT JOIN
region ON district.region_id = region.id
LEFT JOIN
state ON region.state_id = state.id;
Is that so good? Over normalization? How can I join the tables with NULL values? I want to get the above table with Joins. If I want to join "citydistrict" after "postalcode" I get an error message "Not unique table/alias". How can I alias the tables correctly?
Postal code: https://en.wikipedia.org/wiki/Postal_codes_in_Germany
City district: https://en.wikipedia.org/wiki/Stadtbezirk
District: https://en.wikipedia.org/wiki/Districts_of_Germany
Region: https://en.wikipedia.org/wiki/Regierungsbezirk
State: https://en.wikipedia.org/wiki/States_of_Germany