I have a special case with a PHP project where I am working with a database without a 3rd normal form structure. The database consists of only 1 table with loads of columns. Some data that is supposed to be in a separate table, is clamped in 1 column, separated by a certain sign (in this case, semicolon ";").
There is also other columns where clamped data should be moved to the same separated table as mentioned. This must be confusing, so let me elaborate:
**HugeTable**
id | Column1 | Column2 | Column3
123 | Data1;Data2 Data3;Data4 Data5;Data6
I need to put the data above in a separate table that looks like this:
**NewTable**
id | idHugeTable | Column1 | Column2 | Column3
1 | 123 | Data1 | Data3 | Data5
2 | 123 | Data2 | Data4 | Data6
So for each clamped data in the huge table, I need to make a new row in the new table. This process would help me to normalize the database so that it is at least workable. Right now it's a nightmare. This needs to be done either through PHP or MySQL, preferably PHP since looping is easier for one-shot queries per loop through the scripting language.
Edit: Example code of what I have tried in PHP:
$delimiter = ";";
$query = "SELECT * FROM HugeTable";
$result = mysqli_query($connection_var, $query);
while ($row = mysqli_fetch_assoc()){
$column1_data = explode($delimiter, $row['Column1']);
$column2_data = explode($delimiter, $row['Column2']);
$column3_data = explode($delimiter, $row['Column3']);
foreach ($column1_data as $key => $value){
//skip if empty value
if ($value == ""){
continue;
}
else{
$query_ins = "INSERT INTO NewTable (idHugeTable, Column1, Column2, Column3) VALUES (".$row['id'].", ".$column1_data[$key].", ".$column2_data[$key].", ".$column3_data[$key].");";
mysqli_query($connection_var, $query_ins);
}//end if
}//end foreach
}//end while
mysqli_close($connection_var);