问题描述
我想知道是否有人对如何规范化数据库有任何建议.现在,我不是指设计结构,我指的是如何将数据库数据从旧结构实际移动到新的规范化结构.我知道我可以编写类似 PHP 脚本的东西,但我想知道是否有办法在 SQL 中完成它.特别是 MySQL.
I was wondering if anyone had any suggestions on how to normalize a database. Now, I don't mean designing the structure, I mean how to actually move the database data from an old structure to the new, normalized structure. I know I could write something like a PHP script, but I was wondering if there was a way to do it in SQL. Specifically MySQL.
**有没有人尝试过类似 SwisSQL 的东西?这是一个迁移工具,但我不确定它是否能满足我的要求.
** Has anyone tried something like SwisSQL? It's a migration tool, but i'm not sure if it will do what I'm asking.
推荐答案
这是一个在脚本中规范化表的示例.我建议你做这样的事情
Here is an Example of Normalizing Tables in a Script. I advise you do something like this
e.g Table: tbl_tmpData
Date, ProductName, ProductCode, ProductType, MarketDescription, Units, Value
2010-01-01, 'Arnotts Biscuits', '01', 'Biscuit', 'Store 1', 20, 20.00
2010-01-02, 'Arnotts Biscuits', '01', 'Biscuit', 'Store 2', 40, 40.00
2010-01-03, 'Arnotts Biscuits', '01', 'Biscuit', 'Store 3', 40, 40.00
2010-01-01, 'Cola', '02', 'Drink', 'Store 1', 40, 80.00
2010-01-02, 'Cola', '02', 'Drink', 'Store 2', 20, 40.00
2010-01-03, 'Cola', '02', 'Drink', 'Store 2', 60, 120.00
2010-01-01, 'Simiri Gum', '03', 'Gum', 'Store 1', 40, 80.00
2010-01-02, 'Simiri Gum', '03', 'Gum', 'Store 2', 20, 40.00
2010-01-03, 'Simiri Gum', '03', 'Gum', 'Store 3', 60, 120.00
您将首先创建您的日期表:
You would Create Your Date Table first:
CREATE TABLE tbl_Date
(
DateID int PRIMARY KEY IDENTITY(1,1)
,DateValue datetime
)
INSERT INTO tbl_Date (DateValue)
SELECT DISTINCT Date
FROM tbl_Data
WHERE Date NOT IN (SELECT DISTINCT DateValue FROM tbl_Date)
然后您将创建您的市场表
you would then Create your Market Table
CREATE TABLE tbl_Market
(
MarketID int PRIMARY KEY IDENTITY(1,1)
,MarketName varchar(200)
)
INSERT INTO tbl_Market (MarketName)
SELECT DISTINCT MarketDescription
FROM tbl_tmpData
WHERE MarketName NOT IN (SELECT DISTINCT MarketDescription FROM tbl_Market)
然后您将创建您的 ProductType 表
you would then Create your ProductType Table
CREATE TABLE tbl_ProductType
(
ProductTypeID int PRIMARY KEY IDENTITY(1,1)
,ProductType varchar(200)
)
INSERT INTO tbl_ProductType (ProductType)
SELECT DISTINCT ProductType
FROM tbl_tmpData
WHERE ProductType NOT IN (SELECT DISTINCT ProductType FROM tbl_ProductType)
然后您将创建您的产品表
you would then Create your Product Table
CREATE TABLE tbl_Product
(
ProductID int PRIMARY KEY IDENTITY(1,1)
, ProductCode varchar(100)
, ProductDescription varchar(300)
,ProductType int
)
INSERT INTO tbl_Product (ProductCode, ProductDescription, ProductType)
SELECT DISTINCT tmp.ProductCode,tmp.ProductName, pt.ProductType
FROM tbl_tmpData tmp
INNER JOIN tbl_ProductType pt ON tmp.ProductType = pt.ProductType
WHERE ProductCode NOT IN (SELECT DISTINCT ProductCode FROM tbl_Product)
然后您将创建您的数据表
you would then Create your Data Table
CREATE TABLE tbl_Data
(
DataID int PRIMARY KEY IDENTITY(1,1)
, DateID varchar(100)
, ProductID varchar(100)
, MarketID varchar(300)
,Units decimal(10,5)
, value decimal(10,5)
)
INSERT INTO tbl_Data (ProductID, MarketID, Units, Value)
SELECT t.DateID
, p.ProductID
, m.MarketID
, SUM(tmp.Units)
, SUM(tmp.VALUE)
FROM tbl_tmpData tmp
INNER JOIN tbl_Date t ON tmp.Date = t.DateValue
INNER JOIN tbl_Product p ON tmp.ProductCode = p.ProductCode
INNER JOIN tbl_Market m ON tmp.MarketDescription = m.MarketName
GROUP BY t.DateID, p.ProductID, m.MarketID
ORDER BY t.DateID, p.ProductID, m.MarketID
这篇关于如何规范化 SQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!