views:

40

answers:

3

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

+1  A: 
  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...
Balint Pato
+1  A: 

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)
jimmy_keen
+2  A: 

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)
Daniel Vassallo