简体   繁体   English

将表从一对多转换为多对多MYSQL

[英]convert table from one to many into many to many MYSQL

i had a table that a used to have one to one relation using parent_id column, our logic changed and we needed to enable many to many so we -unforunatly- didn't have luxury to change database model, so we used to insert multiple rows one for each relation and group them after wards in code.我有一个表,该表过去使用 parent_id 列具有一对一的关系,我们的逻辑发生了变化,我们需要启用多对多,所以我们 - 不幸的是 - 没有奢侈地更改数据库模型,所以我们曾经插入多行每个关系一个,并在代码中将它们分组。

now i need to restructure mysql table to reflect many to many relation.现在我需要重组 mysql 表以反映多对多关系。

example例子

Table Blog
ID, Body, target_id, grouping
1   etc     1          1
2   etc     2          1
3   etc2    1          3
4   etc2    2          3

Currently, when ever we are creating a new blog post.目前,每当我们创建新的博客文章时。 we insert the first row.我们插入第一行。 grap its id, and replicate insert for each target.抓住它的 id,并为每个目标复制插入。

so now as db grew much larger.所以现在随着 db 变得更大。 we need to stop this and create an intermediate table that hold relations.我们需要停止这种情况并创建一个保存关系的中间表。 so above table will become所以上表会变成

Table Blog
    ID, Body
    1   etc 
    3   etc2

Table Blog_target
    blog_id,target_id
    1     , 1
    1     , 2
    3     , 1
    3     , 2

so how can i split data from old table into new without losing any data in mysql?那么如何在不丢失mysql中任何数据的情况下将旧表中的数据拆分为新表?

something like this maybe?像这样的东西? new_blog table because your blog table already exists, after you're all done and happy with the data in new_blog table and blog_target table, you can just drop your blog table and RENAME TABLE new_blog TO blog; new_blog 表,因为您的 blog 表已经存在,在您完成并对 new_blog 表和 blog_target 表中的数据感到满意后,您可以删除您的 blog 表并RENAME TABLE new_blog TO blog;

INSERT INTO new_blog(ID, Body)
SELECT DISTINCT grouping, body FROM blog;

INSERT INTO blog_target(blog_id,target_id)
SELECT grouping,target_id
FROM blog;

sqlfiddle sqlfiddle

I don't know if this will help your case, but I had to change a oneToMany table relation into a manyToMany table, and below is how I achieved that.我不知道这是否会对您的情况有所帮助,但我必须将oneToMany表关系更改为manyToMany表,下面是我如何实现这一点。

One easy way to do that is simply just drop the unique constraint of that column with the following command:一种简单的方法是使用以下命令删除该列的唯一约束:

ALTER TABLE relation_table DROP INDEX UK_constraint_id;

If you have MySql Workbench installed it is even easier, because you don't need to discover the constraint id.如果您安装了 MySql Workbench,那就更容易了,因为您不需要发现约束 id。 Just select your table -> alter table and unmark the UQ checkbox of the column and then click on "Apply".只需选择您的表 -> 更改表并取消选中该列的 UQ 复选框,然后单击“应用”。

在此处输入图像描述

If you do not have MySql Workbench installed you can do the following query to find out the constraint id:如果您没有安装 MySql Workbench,您可以执行以下查询以找出约束 id:

USE INFORMATION_SCHEMA;
SELECT TABLE_NAME,
       COLUMN_NAME,
       CONSTRAINT_NAME,
       REFERENCED_TABLE_NAME,
       REFERENCED_COLUMN_NAME
FROM KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = "<schema_name>" 
      AND TABLE_NAME = "<table_name>";

and just use the ALTER TABLE mentioned above.并且只需使用上面提到的 ALTER TABLE。

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

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