自动表规范化

发布于 2024-09-06 05:50:08 字数 369 浏览 6 评论 0原文

我有一个具有这种结构的表(简化):

artID: 1
artName: TNT
ArtBrand: ACME
...

我想对其进行规范化,为品牌制作一个单独的表(它将包含有关每个品牌的附加数据) 所以我想最终得到这个

文章表:

artID: 1
artName: TNT
brandID: 1
...

品牌表

brandID: 1
brandName: ACME
brandInfo: xyz
....

该表有太多品牌,无法手动执行此操作。 有什么简单的方法可以自动执行此操作吗? 我正在使用 MySQL

I have a table with this structure (simplified):

artID: 1
artName: TNT
ArtBrand: ACME
...

And I want to normalize it making a separate table for the brand (it will have additional data about every brand)
So I want to end up with this

article table:

artID: 1
artName: TNT
brandID: 1
...

brand table

brandID: 1
brandName: ACME
brandInfo: xyz
....

This table have way too many brands to do this manually.
Any easy way to automate this?
I'm using MySQL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日记撕了你也走了 2024-09-13 05:50:08

正如其他答案所建议的,您可以使用 INSERT ... SELECT 语法执行如下操作:

INSERT INTO brands (brandName)
SELECT   artBrand
FROM     original
GROUP BY artBrand;

INSERT INTO articles (artName, brandID)
SELECT   o.artName, b.brandID
FROM     original o
JOIN     brands b ON (b.brandName = o.artBrand);

测试用例:

CREATE TABLE original (artID int, artName varchar(10), artBrand varchar(10));
CREATE TABLE articles (artID int auto_increment primary key, artName varchar(10), brandID int);
CREATE TABLE brands (brandID int auto_increment primary key, brandName varchar(10));

INSERT INTO original VALUES (1, 'TNT1', 'ACME1');
INSERT INTO original VALUES (2, 'TNT2', 'ACME1');
INSERT INTO original VALUES (3, 'TNT3', 'ACME1');
INSERT INTO original VALUES (4, 'TNT4', 'ACME2');
INSERT INTO original VALUES (5, 'TNT5', 'ACME2');
INSERT INTO original VALUES (6, 'TNT6', 'ACME3');
INSERT INTO original VALUES (7, 'TNT7', 'ACME3');
INSERT INTO original VALUES (8, 'TNT8', 'ACME3');
INSERT INTO original VALUES (9, 'TNT9', 'ACME4');

结果:

SELECT * FROM brands;
+---------+-----------+
| brandID | brandName |
+---------+-----------+
|       1 | ACME1     |
|       2 | ACME2     |
|       3 | ACME3     |
|       4 | ACME4     |
+---------+-----------+
4 rows in set (0.00 sec)


ELECT * FROM articles;
+-------+---------+---------+
| artID | artName | brandID |
+-------+---------+---------+
|     1 | TNT1    |       1 |
|     2 | TNT2    |       1 |
|     3 | TNT3    |       1 |
|     4 | TNT4    |       2 |
|     5 | TNT5    |       2 |
|     6 | TNT6    |       3 |
|     7 | TNT7    |       3 |
|     8 | TNT8    |       3 |
|     9 | TNT9    |       4 |
+-------+---------+---------+
9 rows in set (0.00 sec)

As the other answers suggested, you can use the INSERT ... SELECT syntax to do something like this:

INSERT INTO brands (brandName)
SELECT   artBrand
FROM     original
GROUP BY artBrand;

INSERT INTO articles (artName, brandID)
SELECT   o.artName, b.brandID
FROM     original o
JOIN     brands b ON (b.brandName = o.artBrand);

Test case:

CREATE TABLE original (artID int, artName varchar(10), artBrand varchar(10));
CREATE TABLE articles (artID int auto_increment primary key, artName varchar(10), brandID int);
CREATE TABLE brands (brandID int auto_increment primary key, brandName varchar(10));

INSERT INTO original VALUES (1, 'TNT1', 'ACME1');
INSERT INTO original VALUES (2, 'TNT2', 'ACME1');
INSERT INTO original VALUES (3, 'TNT3', 'ACME1');
INSERT INTO original VALUES (4, 'TNT4', 'ACME2');
INSERT INTO original VALUES (5, 'TNT5', 'ACME2');
INSERT INTO original VALUES (6, 'TNT6', 'ACME3');
INSERT INTO original VALUES (7, 'TNT7', 'ACME3');
INSERT INTO original VALUES (8, 'TNT8', 'ACME3');
INSERT INTO original VALUES (9, 'TNT9', 'ACME4');

Result:

SELECT * FROM brands;
+---------+-----------+
| brandID | brandName |
+---------+-----------+
|       1 | ACME1     |
|       2 | ACME2     |
|       3 | ACME3     |
|       4 | ACME4     |
+---------+-----------+
4 rows in set (0.00 sec)


ELECT * FROM articles;
+-------+---------+---------+
| artID | artName | brandID |
+-------+---------+---------+
|     1 | TNT1    |       1 |
|     2 | TNT2    |       1 |
|     3 | TNT3    |       1 |
|     4 | TNT4    |       2 |
|     5 | TNT5    |       2 |
|     6 | TNT6    |       3 |
|     7 | TNT7    |       3 |
|     8 | TNT8    |       3 |
|     9 | TNT9    |       4 |
+-------+---------+---------+
9 rows in set (0.00 sec)
三五鸿雁 2024-09-13 05:50:08
  1. 我会使用 create table 作为选择
    ...
    创建品牌的语法
    具有生成的 id-s 的表
  2. 创建 Brand_id 列,并使用文章表中现有的品牌列,用 Brands 表中生成的 id-s 填充它。
  3. 从文章表中删除品牌列,当然,brand_id
  4. 创建外键除外...
  1. I would use create table as select
    ...
    syntax to create the brands
    table with generated id-s
  2. create the brand_id column, and fill it up with the generated id-s from the brands table, using the existing brand columns in article table.
  3. remove the brand columns from article table except of course brand_id
  4. create the foreign key...
萝莉病 2024-09-13 05:50:08

生成品牌表应该相当简单:

CREATE TABLE brands ( 
  id INT PRIMARY KEY AUTO_INCREMENT, 
  brand_name VARCHAR(50), 
  brand_info VARCHAR(200)
);

INSERT INTO brands VALUES (brand_name)
SELECT ArtBrand FROM Table
GROUP BY ArtBrand;

在原始表和新品牌表之间创建关系的情况类似,只是插入中的 select 语句如下所示:

SELECT t.artId, b.id 
FROM table t JOIN brands b ON (t.ArtBrand = b.brand_name)

Generating brands table should be fairly simple:

CREATE TABLE brands ( 
  id INT PRIMARY KEY AUTO_INCREMENT, 
  brand_name VARCHAR(50), 
  brand_info VARCHAR(200)
);

INSERT INTO brands VALUES (brand_name)
SELECT ArtBrand FROM Table
GROUP BY ArtBrand;

Similar story with creating relations between your original table and new brands table, just that select statement in your insert will look like this:

SELECT t.artId, b.id 
FROM table t JOIN brands b ON (t.ArtBrand = b.brand_name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文