将 csv 文件导入到 MySQL 中具有外键和主键的 3 个表
Import csv file to 3 tables with foreign and primary keys in MySQL
我需要一些帮助来将 Excel table (.csv
) 导入带有外键的 MySQL 数据库。
MySQLtable是:
测试 table
test_ID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
patient_ID INT DEFAULT NULL FOREIGN KEY (patient.id)
test_type_ID INT DEFAULT NULL FOREIGN KEY (tests_types.id)
result DOUBLE DEFAULT NULL
comment VARCHAR(255) DEFAULT NULL
tests_types table
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
rf_nmu_ID INT DEFAULT NULL,
loinc_od INT DEFAULT NULL,
name VARCHAR(100) DEFAULT NULL,
test_sourcename VARCHAR(255) DEFAULT NULL,
患者 table
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
来源 .csv
包含:
p_id
列用于 patient_ID
字段(第一列有 1
、2
、3
、4
等)
- 每个测试类型的
p_id
个测试值列(t1
、t2
、t3
、t4
等)
- (
t1
,t2
,t3
,t4
, etc) 是 tests
中外键字段 test_type_ID
的值 table,链接到 tests_types
table 中的 ID
主键字段
tests
中 result
字段的数值 table
来源.csv
:
+--------+--------------------+
| p_id | t1 | t2 | t3 | t4 |
+--------+-----+-----+-----+----+
| 1 | 123 | 4,4 | 0,83 | 37 |
+--------+-----+-----+-----+----+
| 2 | 124 | 4,3 | 0,86 | 36 |
+--------+-----+-----+-----+----+
| 3 | 146 | 4,4 | 0,99 | 44 |
+--------+-----+-----+-----+----+
| 4 | 96 | 3,5 | 0,82 | 30 |
+--------+-----+-----+-----+----+
| 5 | 116 | 4 | 0,87 | 38 |
+--------------------------------+
发布这个问题后,我了解到我可以 unpivot columns in Excel Power Questions making table 1D from 2D as you can see at这个 link:
- Unpivot columns (Power Query) 在 Microsoft Office 支持网站上
将源 .csv
table 从 2D 转换为 1D 后,我得到:
+--------+--------+--------+
| pat_id | test_id |值 |
+--------+--------+--------+
| 1 | 1 | 123 |
+--------+--------+--------+
| 1 | 2 | 4,4 |
+--------+--------+--------+
| 1 | 3 | 0,83 |
+--------+--------+--------+
| 1 | 4 | 37 |
+--------+--------+--------+
| 1 | 5 | 7 |
+--------+--------+--------+
和这个 table 我已经导入到我的 tests
table 现在一切正常!
我需要一些帮助来将 Excel table (.csv
) 导入带有外键的 MySQL 数据库。
MySQLtable是:
测试 table
test_ID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
patient_ID INT DEFAULT NULL FOREIGN KEY (patient.id)
test_type_ID INT DEFAULT NULL FOREIGN KEY (tests_types.id)
result DOUBLE DEFAULT NULL
comment VARCHAR(255) DEFAULT NULL
tests_types table
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
rf_nmu_ID INT DEFAULT NULL,
loinc_od INT DEFAULT NULL,
name VARCHAR(100) DEFAULT NULL,
test_sourcename VARCHAR(255) DEFAULT NULL,
患者 table
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
来源 .csv
包含:
p_id
列用于patient_ID
字段(第一列有1
、2
、3
、4
等)- 每个测试类型的
p_id
个测试值列(t1
、t2
、t3
、t4
等) - (
t1
,t2
,t3
,t4
, etc) 是tests
中外键字段test_type_ID
的值 table,链接到tests_types
table 中的 tests
中result
字段的数值 table
ID
主键字段
来源.csv
:
+--------+--------------------+
| p_id | t1 | t2 | t3 | t4 |
+--------+-----+-----+-----+----+
| 1 | 123 | 4,4 | 0,83 | 37 |
+--------+-----+-----+-----+----+
| 2 | 124 | 4,3 | 0,86 | 36 |
+--------+-----+-----+-----+----+
| 3 | 146 | 4,4 | 0,99 | 44 |
+--------+-----+-----+-----+----+
| 4 | 96 | 3,5 | 0,82 | 30 |
+--------+-----+-----+-----+----+
| 5 | 116 | 4 | 0,87 | 38 |
+--------------------------------+
发布这个问题后,我了解到我可以 unpivot columns in Excel Power Questions making table 1D from 2D as you can see at这个 link:
- Unpivot columns (Power Query) 在 Microsoft Office 支持网站上
将源 .csv
table 从 2D 转换为 1D 后,我得到:
+--------+--------+--------+
| pat_id | test_id |值 |
+--------+--------+--------+
| 1 | 1 | 123 |
+--------+--------+--------+
| 1 | 2 | 4,4 |
+--------+--------+--------+
| 1 | 3 | 0,83 |
+--------+--------+--------+
| 1 | 4 | 37 |
+--------+--------+--------+
| 1 | 5 | 7 |
+--------+--------+--------+
和这个 table 我已经导入到我的 tests
table 现在一切正常!