Jump to content

[SOLVED] Automatic normalization


Jaguar

Recommended Posts

I'm importing an Access file into MySQL and there's a lot of redundant data. I have only 100 authors over 80,000 rows. I know I should make a seperate authors table and replace the authors with numbers. Is there any quick way to do this?

 

How I'm doing it is first in MySQL.

SELECT author FROM books GROUP BY author;

 

Then copying the results into Notepad.

author1

author2

...

author100

 

Then turning every line into SQL.

 

UPDATE books SET author = 1 WHERE author = 'name1';

UPDATE books SET author = 2 WHERE author = 'name2';

...

UPDATE books SET author = 100 WHERE author = 'name100';

 

I can write a macro to do it fairly quick but there must be a better way?

Link to comment
https://forums.phpfreaks.com/topic/68735-solved-automatic-normalization/
Share on other sites

<?php
$sql = "CREATE TABLE author (
    authorID int not null auto_increment primary key,
    name varchar(50)
    )";
mysql_query($sql);

$sql = "INSERT INTO author (name)
        SELECT DISTINCT author FROM books";
mysql_query($sql);

/**
* add a new column "authorID" to books table with phpmyadmin or similar, then
*/

$sql = "UPDATE books b INNER JOIN author a ON b.author = a.name SET b.authorID = a.authorID";
mysql_query($sql);

/**
* now drop column "author" from books table
*/

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.