[英]Split non-relational table into multiple relational tables
I have the following table of 60000 rows, in a MySQL 5 database, which is derived from a CSV file: 我在MySQL 5数据库中有60000行的下表,该表是从CSV文件派生的:
CREATE TABLE `smts_import` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`AscCode` varchar(5) NOT NULL,
`AscName` varchar(50) NOT NULL,
`RgnCode` varchar(5) NOT NULL,
`RgnName` varchar(100) NOT NULL,
`SCode` varchar(30) NOT NULL,
`SName` varchar(100) NOT NULL,
`AM` int(11) NOT NULL,
`AF` int(11) NOT NULL,
`LG` decimal(10,4) NOT NULL,
`LT` decimal(10,4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ars_codes` (`AscCode`,`RgnCode`,`SCode`),
KEY `s_code` (`SCode`)
);
and which contains much repeated data. 并且其中包含很多重复的数据。 SCodes are unique, and the relationship between the RgnCode and SCode fields is many-to-one, as is that between the AscCode and RgnCode fields.
SCode是唯一的,并且RgnCode和SCode字段之间的关系与AscCode和RgnCode字段之间的关系是多对一的。 I want to split up (normalize) the data into three separate tables in a relational manner, such that no data are repeated:
我想以一种相关的方式将数据拆分(规范化)为三个单独的表,这样就不会重复任何数据:
CREATE TABLE `ascs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a_code` varchar(6) NOT NULL,
`a_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a_code` (`a_code`)
);
CREATE TABLE `rgns` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a_id` int(11) NOT NULL,
`r_code` varchar(6) NOT NULL,
`r_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a_id`+`r_code` (`a_id`, `r_code`),
KEY `r_code` (`r_code`)
);
CREATE TABLE `sms` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`r_id` int(11) NOT NULL,
`s_code` varchar(16) NOT NULL,
`s_name` varchar(100) NOT NULL,
`a_m` int(11) NOT NULL,
`a_f` int(11) NOT NULL,
`lg` decimal(10,4) NOT NULL,
`lt` decimal(10,4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `r_id+s_code` (`r_id`,`s_code`),
KEY `s_code` (`s_code`)
);
where rgns
. 在哪里
rgns
。 a_id
is a foreign key on ascs
. a_id
是ascs
的外键。 id
, and sms
. id
和sms
。 r_id
is a foreign key on rgns
. r_id
是一个外键rgns
。 id
. id
。
I've created the three tables and successfully populated the first two, ascs
and rgns
, with unique data from the smts_import
table. 我已经创建了三个表,并使用来自
smts_import
表的唯一数据成功地填充了前两个ascs
和rgns
。 My problem comes when I try to populate the third table sms
with just the SCode
, SName
, AM
, AF
, LG
and LT
fields from the smts_import
table, PLUS the appropriate id from the rgns
table. 我的问题是当我尝试填充第三台
sms
只用SCode
, SName
, AM
, AF
, LG
和LT
从田间地头smts_import
表,加上从相应的ID rgns
表。 And here I just get lost, I've tried many variations on the following: 在这里我迷路了,我尝试了以下几种变化:
INSERT INTO sms (r_id, s_code, s_name, a_m, a_f, lg, lt)
SELECT DISTINCT sr.id, SCode, SName, AM, AF, LG, LT
FROM sms_import AS si, rgns AS sr, ascs AS sa
WHERE (sr.r_code = si.RgnCode)
AND (sr.a_id = sa.id)
AND (sa.a_code = si.AscCode)
ORDER BY SCode
but I just end up with too many records. 但我最终得到太多记录。 How do I write this insert statement to get the appropriate fields from all records from the
sms_import
table, plus the correct values in the sms
. 如何编写此插入语句,以从
sms_import
表的所有记录中获取适当的字段,以及sms
的正确值。 r_id
field from the rgns
. r_id
从外地rgns
。 id
field? id
字段?
Thanks for your help 谢谢你的帮助
"the relationship between the RgnCode and SCode fields is many-to-one" So you also get many rgns.id per SCode. “ RgnCode和SCode字段之间的关系是多对一的”,因此每个SCode您还将获得许多rgns.id。 If you select distinct rgns.id, SCode, etc. you will get each SCode more than once depending on how many RgnCode you have for it.
如果选择不同的rgns.id,SCode等,则每个SCode都会获得不止一次,具体取决于您拥有多少RgnCode。 I would suggest adding a id column to the sms table and create a separate table sms2rgns which contains the one-to-many relations sms.id -> rgns.id
我建议将id列添加到sms表并创建一个单独的表sms2rgns,其中包含一对多关系sms.id-> rgns.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.