事实表中的外键参照完整性 - T-SQL
Foreign key referential integrity in the fact tables - T-SQL
我有这个事实table:
CREATE TABLE [dbo].[FACT_TABLE]
(
[A_id] [int] NOT NULL,
[B_id] [int] NOT NULL,
[C_id] [int] NOT NULL,
[D_id] [int] NOT NULL,
[FACT_total] [float] NOT NULL,
[FACT_average] [float] NOT NULL,
CONSTRAINT [PK_FACT_TABLE]
PRIMARY KEY CLUSTERED ([A_id] ASC, [B_id] ASC,
[C_id] ASC, [D_id] ASC)
) ON [PRIMARY]
我有一个 T-SQL 脚本来将数据加载到我的事实中 table:
INSERT INTO [dbo].[FACT_TABLE] ([A_id], [B_id], [C_id], [company_id], [D_id],[FACT_total], [FACT_average])
SELECT
b.A_id, c.B_id, d.C_id, e.D_id,
AVG(FACT_total) AS FACT_total,
AVG(FACT_average) AS FACT_average
FROM
[dbo].[staging_Area] a
INNER JOIN
[dbo].[DIM_A] b ON a.A_id = b.A_id
INNER JOIN
[dbo].[DIM_B] c ON a.[B_id] = c.B_id
INNER JOIN
[dbo].[DIM_C] d ON a.[C_id] = d.C_id
INNER JOIN
[dbo].[DIM_D] e ON a.D_id = e.D_id
GROUP BY
b.A_id, c.B_id, d.C_id, e.D_id
如何保证我不会在我的事实中插入重复值 table 以避免出现引用完整性错误?
我在该查询后尝试使用以下语句:
EXCEPT
SELECT *
FROM [dbo].[FACT_TABLE]
但我认为这不是最好的方法...
有人对此有其他选择吗?抱歉,我不能输入真实的专栏和 table 的名字...
谢谢!
LEFT OUTER JOIN
与 WHERE FT.Column IS NULL
到事实 table 以确保您没有插入已存在的记录:
INSERT INTO [dbo].[FACT_TABLE] ([A_id],[B_id],[C_id],[company_id],[D_id],[FACT_total],[FACT_average])
SELECT b.A_id
,c.B_id
,d.C_id
,e.D_id
,AVG(FACT_total) AS FACT_total
,AVG(FACT_average) AS FACT_average
FROM [dbo].[staging_Area] a
INNER JOIN [dbo].[DIM_A] b ON
a.A_id = b.A_id
INNER JOIN [dbo].[DIM_B] c ON
a.[B_id] = c.B_id
INNER JOIN [dbo].[DIM_C] d ON
a.[C_id] = d.C_id
INNER JOIN [dbo].[DIM_D] e ON
a.D_id = e.D_id
LEFT OUTER JOIN dbo.[FACT_TABLE] FT
ON b.A_ID = FT.A_id
AND c.B_id = FT.B_id
AND D.C_ID = FT.C_id
AND E.D_id = FT.D_id
WHERE FT.A_ID IS NULL
GROUP BY b.A_id
,c.B_id
,d.C_id
,e.D_id
插入 [dbo].[FACT_TABLE]
([A_id], [B_id], [C_id], [D_id],[FACT_total], [FACT_average])
select a.A_id, a.B_id, a.C_id, a.D_id,a.FACT_total,a.FACT_average
来自
(
SELECT
b.A_id, c.B_id, d.C_id, e.D_id,
AVG(FACT_total) AS FACT_total,
AVG(FACT_average) AS FACT_average
来自
[dbo].[staging_Area] a
内连接
[dbo].[DIM_A] b ON a.A_id = b.A_id
内连接
[dbo].[DIM_B] c ON a.[B_id] = c.B_id
内连接
[dbo].[DIM_C] d ON a.[C_id] = d.C_id
内连接
[dbo].[DIM_D] e ON a.D_id = e.D_id
分组依据
b.A_id, c.B_id, d.C_id, e.D_id
)a
其中
不存在
(
SELECT 1 来自 dbao.FACT_TABLE B
哪里
B.A_id = A.A_id AND B.B_id = A.B_id and b.C_id=a.C_id and b.D_id=a.D_id
)
我有这个事实table:
CREATE TABLE [dbo].[FACT_TABLE]
(
[A_id] [int] NOT NULL,
[B_id] [int] NOT NULL,
[C_id] [int] NOT NULL,
[D_id] [int] NOT NULL,
[FACT_total] [float] NOT NULL,
[FACT_average] [float] NOT NULL,
CONSTRAINT [PK_FACT_TABLE]
PRIMARY KEY CLUSTERED ([A_id] ASC, [B_id] ASC,
[C_id] ASC, [D_id] ASC)
) ON [PRIMARY]
我有一个 T-SQL 脚本来将数据加载到我的事实中 table:
INSERT INTO [dbo].[FACT_TABLE] ([A_id], [B_id], [C_id], [company_id], [D_id],[FACT_total], [FACT_average])
SELECT
b.A_id, c.B_id, d.C_id, e.D_id,
AVG(FACT_total) AS FACT_total,
AVG(FACT_average) AS FACT_average
FROM
[dbo].[staging_Area] a
INNER JOIN
[dbo].[DIM_A] b ON a.A_id = b.A_id
INNER JOIN
[dbo].[DIM_B] c ON a.[B_id] = c.B_id
INNER JOIN
[dbo].[DIM_C] d ON a.[C_id] = d.C_id
INNER JOIN
[dbo].[DIM_D] e ON a.D_id = e.D_id
GROUP BY
b.A_id, c.B_id, d.C_id, e.D_id
如何保证我不会在我的事实中插入重复值 table 以避免出现引用完整性错误?
我在该查询后尝试使用以下语句:
EXCEPT
SELECT *
FROM [dbo].[FACT_TABLE]
但我认为这不是最好的方法...
有人对此有其他选择吗?抱歉,我不能输入真实的专栏和 table 的名字...
谢谢!
LEFT OUTER JOIN
与 WHERE FT.Column IS NULL
到事实 table 以确保您没有插入已存在的记录:
INSERT INTO [dbo].[FACT_TABLE] ([A_id],[B_id],[C_id],[company_id],[D_id],[FACT_total],[FACT_average])
SELECT b.A_id
,c.B_id
,d.C_id
,e.D_id
,AVG(FACT_total) AS FACT_total
,AVG(FACT_average) AS FACT_average
FROM [dbo].[staging_Area] a
INNER JOIN [dbo].[DIM_A] b ON
a.A_id = b.A_id
INNER JOIN [dbo].[DIM_B] c ON
a.[B_id] = c.B_id
INNER JOIN [dbo].[DIM_C] d ON
a.[C_id] = d.C_id
INNER JOIN [dbo].[DIM_D] e ON
a.D_id = e.D_id
LEFT OUTER JOIN dbo.[FACT_TABLE] FT
ON b.A_ID = FT.A_id
AND c.B_id = FT.B_id
AND D.C_ID = FT.C_id
AND E.D_id = FT.D_id
WHERE FT.A_ID IS NULL
GROUP BY b.A_id
,c.B_id
,d.C_id
,e.D_id
插入 [dbo].[FACT_TABLE]
([A_id], [B_id], [C_id], [D_id],[FACT_total], [FACT_average])
select a.A_id, a.B_id, a.C_id, a.D_id,a.FACT_total,a.FACT_average
来自
( SELECT
b.A_id, c.B_id, d.C_id, e.D_id,
AVG(FACT_total) AS FACT_total,
AVG(FACT_average) AS FACT_average
来自
[dbo].[staging_Area] a
内连接
[dbo].[DIM_A] b ON a.A_id = b.A_id
内连接
[dbo].[DIM_B] c ON a.[B_id] = c.B_id
内连接 [dbo].[DIM_C] d ON a.[C_id] = d.C_id
内连接
[dbo].[DIM_D] e ON a.D_id = e.D_id
分组依据
b.A_id, c.B_id, d.C_id, e.D_id
)a
其中
不存在
( SELECT 1 来自 dbao.FACT_TABLE B
哪里
B.A_id = A.A_id AND B.B_id = A.B_id and b.C_id=a.C_id and b.D_id=a.D_id
)