简体   繁体   English

将单个表拆分为多对多关系

[英]Split single table to many-to-many relationship

I have a table that looks like the following: 我有一个表,如下所示:

在此处输入图片说明

I want to split this to 3 different tables with a PK-FK relationship. 我想通过PK-FK关系将其拆分为3个不同的表。 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? 如果可能,使用SQL查询一次完成此操作的最佳方法是什么? Or using tools like phpmyadmin. 或使用phpmyadmin之类的工具。

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 因此,您必须通过代码创建这些表,然后使用sql查询向其中插入值

您可以按以下方式对其进行分区:

Prov Table with Prov_ID as PK.

State Table with State_ID as PK and Prov_ID as FK

City Table with City_ID as PK and State_ID as FK and Prov_ID as FK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM