假设我有一个对象,其中包含我要跟踪历史记录的某些字段,以及某些我不想跟踪历史记录的字段。从规范化的角度来看,以下架构是否正确:
CREATE TABLE MyObject AS (
MyObjectId INT IDENTITY NOT NULL PRIMARY KEY,
MyObjectField1 VARCHAR(100) NOT NULL,
MyObjectField2 VARCHAR(100) NOT NULL,
MyObjectField3 VARCHAR(100) NOT NULL,
MyObjectTrackedField1 VARCHAR(100) NOT NULL,
MyObjectTrackedField2 VARCHAR(100) NOT NULL,
MyObjectTrackedField3 VARCHAR(100) NOT NULL,
)
CREATE TABLE MyObjectHistory AS (
MyObjectHistoryId INT IDENTITY NOT NULL PRIMARY KEY,
MyObjectId INT NOT NULL FOREIGN KEY REFERENCES MyObject(MyObjectId),
MyObjectTrackedField1 VARCHAR(100) NOT NULL,
MyObjectTrackedField2 VARCHAR(100) NOT NULL,
MyObjectTrackedField3 VARCHAR(100) NOT NULL,
)
其中MyObjectHistory包含除最新修订版以外的所有跟踪字段。或者,应将所有跟踪的字段都放在一个表中,而所有修订(包括最新版本)都应在该表中,如下所示:
CREATE TABLE MyObject AS (
MyObjectId INT IDENTITY NOT NULL PRIMARY KEY,
MyObjectField1 VARCHAR(100) NOT NULL,
MyObjectField2 VARCHAR(100) NOT NULL,
MyObjectField3 VARCHAR(100) NOT NULL,
)
CREATE TABLE MyObjectHistory AS (
MyObjectHistoryId INT IDENTITY NOT NULL PRIMARY KEY,
MyObjectId INT NOT NULL FOREIGN KEY REFERENCES MyObject(MyObjectId),
MyObjectTrackedField1 VARCHAR(100) NOT NULL,
MyObjectTrackedField2 VARCHAR(100) NOT NULL,
MyObjectTrackedField3 VARCHAR(100) NOT NULL,
)