I have one table with all of the pertinent information I need for my database with the columns: CompanyName, StoreLocation, StoreNumber, ItemName, and ItemModel
I want to move the column CompanyName
to its own Company
Table that will contain a CompanyID
and the CompanyName
in its own table, as well as moving the StoreLocation
and StoreNumber
columns to their own Store
Table. I need to assign unique (auto-incrementing is fine) IDs for each company and for each store while maintaining foreign-primary key integrity for the IDs in each table. After making these changes the database ERD should look something like this:
Here is the expected output for all of the data in the one table:
And lastly, here is the expected output when all of the tables are normalized:
What is the best way to go about doing this? I can use Python's Pandas if necessary, but was wondering the best way to add this normalization in to avoid the duplication of many values if it were to stay in one table.
Let me know if I can provide any additional information.
This can be done with 3 SQL queries:
INSERT INTO company (companyname)
SELECT DISTINCT companyname
FROM originalTable;
INSERT INTO store (storelocation, storenumber, companyid)
SELECT DISTINCT o.storelocation, o.storenumber, c.companyid
FROM originalTable o
JOIN company c ON o.companyname = c.companyname;
INSERT INTO item (itemname, itemmodel, storeid)
SELECT o.itemname, o.itemmodel, s.storeid
FROM originalTable o
JOIN store s ON s.storelocation = o.storelocation AND s.storenumber = o.storenumber
JOIN company c ON s.companyid = s.companyid AND o.companyname = c.companyname;
The join with company
in the third query is necessary if the original table can have duplicate location+storenumber for stores from different companies.