你也許可以有這樣的事情。我沒有測試代碼,所以請原諒任何錯誤。代碼是MS-SQL
CREATE TABLE tblUserDefinedEntity
(
UserDefinedEntityID INT NOT NULL PRIMARY KEY,
Name VARCHAR(255)
)
INSERT INTO tblUserDefinedEntity
VALUES
(1,'Person'),
(2,'Business')
-- properties each entity can have.
CREATE TABLE tblUserDefinedEntityProperty
(
UserDefinedEntityPropertyID INT NOT NULL PRIMARY KEY,
Name VARCHAR(255)
)
INSERT INTO tblUserDefinedEntityProperty
VALUES
(1,'FirstName'),
(2,'Surname'),
(3,'Address')
--maps properties to entities EG a person has a firstname
CREATE TABLE tblUserDefinedEntityPropertyMapping
(
ID INT NOT NULL PRIMARY KEY,
UserDefinedEntityID INT FOREIGN KEY REFERENCES tblUserDefinedEntityID(UserDefinedEntityID),
UserDefinedEntityPropertyID INT FOREIGN KEY REFERENCES tblUserDefinedEntityPropertyID(UserDefinedEntityPropertyID),
)
-- a person has a firstname, surname and address, a business has a firstname and address
INSERT INTO tblUserDefinedEntityPropertyMapping
VALUES
(1,1,1),
(2,1,2),
(3,1,3),
(4,2,1),
(5,2,3)
--all the available values for each attribute, eg all the firstnames, surnames and addresses
CREATE TABLE tblValue
(
ValueID INT NOT NULL PRIMARY KEY,
Value VARCHAR(255),
UserDefinedEntityPropertyID INT FOREIGN KEY REFERENCES tblUserDefinedEntityProperty (UserDefinedEntityPropertyID),
)
INSERT INTO tblValue
VALUES
(1,'John',1),
(2,'James,1),
(3,'Jill',1),
(4,'Smith',2),
(5,'Jones',2),
(6,'123 Fake Street',3)
(7,'124 Fake Street',3)
--creating instances of your entities and assigning them values
CREATE TABLE tblDataEnty
(
DataItemID INT,
UserDefinedEntityPropertyMappingID INT FOREIGN KEY REFERENCES tblUserDefinedEntityPropertyMapping(UserDefinedEntityPropertyMappingID),
ValueID INT FOREIGN KEY REFERENCES tblValue(ValueID),
PRIMARY KEY (DataItemID, UserDefinedEntityPropertyMappingID)
)
-- we have added
-- john jones (no address)
-- james smith (124 fake street)
INSERT INTO tblDataEnty
VALUES
(1,1,1),
(1,2,5),
(2,1,2),
(2,2,4),
(2,3,7)
這實際上是一個好主意:將表格視爲域。但是它有一個限制:你想要一個帶有名字的表,並讓* fact table引用它?或出生日期? – wildplasser
由於您目前正在學習數據庫設計,請讓我與您分享此鏈接:https://www.slideshare.net/billkarwin/models-for-hierarchical-data 當您需要存儲某種類型的數據時,這將派上用場分層數據。 –