我有一堆来自不同供应商的平面文件,我每月导入这些文件,我想跟踪哪些记录来自哪个文件。
目前:
CREATE TABLE Fact.Order
(
Id INT NOT NULL IDENTITY PRIMARY KEY
,OrderId INT NOT NULL
,VendorId INT NOT NULL REFERENCES Vendor (Id)
,OrderTypeId INT NOT NULL REFERENCES OrderType (Id)
,SaleDate DATETIME2 NOT NULL
,Amount MONEY NOT NULL
);
文件跟踪表:
CREATE TABLE [File]
(
Id INT IDENTITY NOT NULL PRIMARY KEY,
VendorId INT NOT NULL REFERENCES Vendor (Id)
FileHash BINARY(20) NOT NULL,
FileSize BIGINT NOT NULL,
ModifiedDate DATETIME2 NOT NULL,
FileRecordCount INT NOT NULL,
CreatedDate DATE NOT NULL DEFAULT GETUTCDATE(),
UpdatedDate DATE NOT NULL DEFAULT GETUTCDATE(),
IsDelete BIT NOT NULL DEFAULT 0,
CONSTRAINT UQ_Files_FileHash_IsDelete UNIQUE NONCLUSTERED
(
FileHash,
IsDelete
) WITH (IGNORE_DUP_KEY = ON)
);
CREATE TABLE FileImport
(
Id INT IDENTITY NOT NULL PRIMARY KEY,
FileId INT NOT NULL REFERENCES [File] (Id),
OrderTypeId INT NOT NULL REFERENCES OrderType (Id),
ImportRecordCount INT NOT NULL,
CreatedDate DATE NOT NULL DEFAULT GETUTCDATE(),
UpdatedDate DATE NOT NULL DEFAULT GETUTCDATE(),
IsDelete BIT NOT NULL DEFAULT 0
);
最好将FileImport
密钥保留在Fact.Order
表中还是应该将数据放入自己的表中?
编辑:
到目前为止,我决定VendorId
从File
表中删除,所以现在它只存储有关文件的信息。
然后,我为每个包含唯一标识符的事实表创建了元数据表,以与业务键相关联。
CREATE TABLE File.Order
(
OrderId INT NOT NULL,
VendorId INT NOT NULL,
FileId INT NOT NULL REFERENCES File (Id),
CreatedDate DATE NOT NULL DEFAULT GETUTCDATE(),
UpdatedDate DATE NOT NULL DEFAULT GETUTCDATE(),
IsDelete BIT NOT NULL DEFAULT 0,
CONSTRAINT UQ_File_Order_OrderId_VendorId PRIMARY KEY CLUSTERED
(
OrderId,
VendorId,
FileId
)
)