您如何根据以下数据设计数据库关系?

时间:2019-05-14 19:03:40

标签: mysql django database sqlite database-design

您将如何构建关系数据库中的数据?当我更新数据库时,我想在两个表中添加一个相应的批号(batch_no),以便可以链接数据。我该怎么办?

第一个表(可能需要拆分????)是数据上传的分类结果。

+--------------+-----------+--------+----------+---------+----------+
|              | precision | recall | f1-score | support | batch_id |
+--------------+-----------+--------+----------+---------+----------+
|              |           |        |          |         |          |
| positive     | 0.56      | 0.7    | 0.62     |    1034 |        1 |
| negative     | 0.96      | 0.94   | 0.95     |    8966 |        1 |
|              |           |        |          |         |          |
| micro avg    | 0.91      | 0.91   | 0.91     |   10000 |        1 |
| macro avg    | 0.76      | 0.82   | 0.79     |   10000 |        1 |
| weighted avg | 0.92      | 0.91   | 0.92     |   10000 |        1 |
|              |           |        |          |         |          |
| positive     | 0.56      | 0.7    | 0.62     |    1034 |        2 |
| negative     | 0.96      | 0.94   | 0.95     |    8966 |        2 |
|              |           |        |          |         |          |
| micro avg    | 0.91      | 0.91   | 0.91     |   10000 |        2 |
| macro avg    | 0.76      | 0.82   | 0.79     |   10000 |        2 |
| weighted avg | 0.92      | 0.91   | 0.92     |   10000 |        2 |
+--------------+-----------+--------+----------+---------+----------+

第二张表是很多评论。

+----+------------+-----------+--------+----------+
| id | reviewtext | predicted | actual | batch_id |
+----+------------+-----------+--------+----------+
|  1 | blah blah  | pos       | neg    |        1 |
|  2 | blah blah  | pos       | pos    |        1 |
|  3 | blah blah  | neg       | neg    |        1 |
|  4 | blah blah  | pos       | neg    |        2 |
|  5 | blah blah  | pos       | pos    |        2 |
|  6 | blah blah  | neg       | neg    |        2 |
+----+------------+-----------+--------+----------+

我已经尝试过使用复合键,并将第一个数据库分为正数和负数,然后有一个主键来更新评论表,但是我似乎无法弄清它。

1 个答案:

答案 0 :(得分:0)

您的“分类”表应将“ id”列作为主键:

---+--------------+-----------+--------+----------+---------+
 id|              | precision | recall | f1-score | support | 
---+--------------+-----------+--------+----------+---------+
  1|              |           |        |          |         | 
  2| positive     | 0.56      | 0.7    | 0.62     |    1034 |
  3| negative     | 0.96      | 0.94   | 0.95     |    8966 |
  4|              |           |        |          |         |
  5| micro avg    | 0.91      | 0.91   | 0.91     |   10000 |
  6| macro avg    | 0.76      | 0.82   | 0.79     |   10000 |
  7| weighted avg | 0.92      | 0.91   | 0.92     |   10000 |
---+--------------+-----------+--------+----------+---------+

然后在“评论”表上,为“分类”表创建外键:

---+------------+-----------+--------+------------------+
 id| reviewtext | predicted | actual | classification_id|
---+------------+-----------+--------+------------------+
  1| blah blah  | pos       | neg    |        1         |
  2| blah blah  | pos       | pos    |        2         |
  3| blah blah  | neg       | neg    |        3         |
---+------------+-----------+--------+------------------+

现在,如果要在一个查询中访问两个表中的数据,则可以JOIN个表:

SELECT 
*.t1,
*.t2
FROM classification t1
LEFT JOIN reviews t2
ON (t1.id = t2.classification_id)

如果您需要有关Django ORM的帮助,请使用您的模型更新您的帖子,并告诉我。