3 回答

TA貢獻1818條經(jīng)驗 獲得超3個贊
這稱為觀察模式。
在此處輸入圖片說明
以三個對象為例
Book
Title = 'Gone with the Wind'
Author = 'Margaret Mitchell'
ISBN = '978-1416548898'
Cat
Name = 'Phoebe'
Color = 'Gray'
TailLength = 9 'inch'
Beer Bottle
Volume = 500 'ml'
Color = 'Green'
這是表格的樣子:
Entity
EntityID Name Description
1 'Book' 'To read'
2 'Cat' 'Fury cat'
3 'Beer Bottle' 'To ship beer in'
。
PropertyType
PropertyTypeID Name IsTrait Description
1 'Height' 'NO' 'For anything that has height'
2 'Width' 'NO' 'For anything that has width'
3 'Volume' 'NO' 'For things that can have volume'
4 'Title' 'YES' 'Some stuff has title'
5 'Author' 'YES' 'Things can be authored'
6 'Color' 'YES' 'Color of things'
7 'ISBN' 'YES' 'Books would need this'
8 'TailLength' 'NO' 'For stuff that has long tails'
9 'Name' 'YES' 'Name of things'
。
Property
PropertyID EntityID PropertyTypeID
1 1 4 -- book, title
2 1 5 -- book, author
3 1 7 -- book, isbn
4 2 9 -- cat, name
5 2 6 -- cat, color
6 2 8 -- cat, tail length
7 3 3 -- beer bottle, volume
8 3 6 -- beer bottle, color
。
Measurement
PropertyID Unit Value
6 'inch' 9 -- cat, tail length
7 'ml' 500 -- beer bottle, volume
。
Trait
PropertyID Value
1 'Gone with the Wind' -- book, title
2 'Margaret Mitchell' -- book, author
3 '978-1416548898' -- book, isbn
4 'Phoebe' -- cat, name
5 'Gray' -- cat, color
8 'Green' -- beer bottle, color
編輯:
杰弗里提出了一個正確的觀點(見評論),所以我將擴大答案。
該模型允許動態(tài)(動態(tài))創(chuàng)建具有任何類型的屬性的任意數(shù)量的實體,而無需更改架構(gòu)。但是,這種靈活性要付出代價-與通常的桌子設(shè)計相比,存儲和搜索更慢,更復(fù)雜。
是一個例子了,但是首先,為了使事情變得容易,我將模型展平為一個視圖。
create view vModel as
select
e.EntityId
, x.Name as PropertyName
, m.Value as MeasurementValue
, m.Unit
, t.Value as TraitValue
from Entity as e
join Property as p on p.EntityID = p.EntityID
join PropertyType as x on x.PropertyTypeId = p.PropertyTypeId
left join Measurement as m on m.PropertyId = p.PropertyId
left join Trait as t on t.PropertyId = p.PropertyId
;
從評論中使用杰弗里的例子
with
q_00 as ( -- all books
select EntityID
from vModel
where PropertyName = 'object type'
and TraitValue = 'book'
),
q_01 as ( -- all US books
select EntityID
from vModel as a
join q_00 as b on b.EntityID = a.EntityID
where PropertyName = 'publisher country'
and TraitValue = 'US'
),
q_02 as ( -- all US books published in 2008
select EntityID
from vModel as a
join q_01 as b on b.EntityID = a.EntityID
where PropertyName = 'year published'
and MeasurementValue = 2008
),
q_03 as ( -- all US books published in 2008 not discontinued
select EntityID
from vModel as a
join q_02 as b on b.EntityID = a.EntityID
where PropertyName = 'is discontinued'
and TraitValue = 'no'
),
q_04 as ( -- all US books published in 2008 not discontinued that cost less than $50
select EntityID
from vModel as a
join q_03 as b on b.EntityID = a.EntityID
where PropertyName = 'price'
and MeasurementValue < 50
and MeasurementUnit = 'USD'
)
select
EntityID
, max(case PropertyName when 'title' than TraitValue else null end) as Title
, max(case PropertyName when 'ISBN' than TraitValue else null end) as ISBN
from vModel as a
join q_04 as b on b.EntityID = a.EntityID
group by EntityID ;
編寫起來似乎很復(fù)雜,但是仔細檢查后,您可能會注意到CTE中的模式。
現(xiàn)在假設(shè)我們有一個標準的固定模式設(shè)計,其中每個對象屬性都有自己的列。查詢?nèi)缦滤荆?/p>
select EntityID, Title, ISBN
from vModel
WHERE ObjectType = 'book'
and PublisherCountry = 'US'
and YearPublished = 2008
and IsDiscontinued = 'no'
and Price < 50
and Currency = 'USD'
;

TA貢獻1793條經(jīng)驗 獲得超6個贊
我本來不打算回答,但是現(xiàn)在被接受的答案是一個非常糟糕的主意。關(guān)系數(shù)據(jù)庫絕對不能用于存儲簡單的屬性-值對。這將在以后引起很多問題。
解決此問題的最佳方法是為每種類型創(chuàng)建一個單獨的表。
Product
-------
ProductId
Description
Price
(other attributes common to all products)
Book
----
ProductId (foreign key to Product.ProductId)
ISBN
Author
(other attributes related to books)
Electronics
-----------
ProductId (foreign key to Product.ProductId)
BatteriesRequired
etc.
每個表的每一行都應(yīng)代表一個關(guān)于真實世界的命題,并且表的結(jié)構(gòu)及其約束應(yīng)反映所代表的現(xiàn)實。您越接近這個理想,數(shù)據(jù)將越干凈,并且以其他方式進行報告和擴展系統(tǒng)也就越容易。它還將更有效地運行。

TA貢獻1786條經(jīng)驗 獲得超13個贊
您可以采用無模式方法:
將元數(shù)據(jù)作為JSON對象(或其他序列化形式)保存在TEXT列中,但由于稍后說明的原因,JSON更好。
該技術(shù)的優(yōu)點:
更少的查詢:您只需一次查詢即可獲取所有信息,而無需“定向”查詢(獲取元元數(shù)據(jù))和聯(lián)接。
您可以隨時添加/刪除所需的任何屬性,而無需更改表(這在某些數(shù)據(jù)庫中是有問題的,例如,Mysql鎖定了表,而使用大型表則需要很長時間)
由于它是JSON,因此您不需要在后端進行額外的處理。您的網(wǎng)頁(我假設(shè)它是一個Web應(yīng)用程序)僅從Web服務(wù)中讀取JSON,僅此而已,您可以根據(jù)需要使用JSON對象和javascript。
問題:
潛在的浪費空間是,如果您有100本書與同一位作者在一起,那么一個作者表(其中所有書籍都只有author_id)是更經(jīng)濟的空間選擇。
需要實現(xiàn)索引。由于您的元數(shù)據(jù)是JSON對象,因此您不會立即擁有索引。但是,為所需的特定元數(shù)據(jù)實現(xiàn)特定索引非常容易。例如,您想按作者進行索引,因此您可以使用author_id和item_id創(chuàng)建一個author_idx表,當有人搜索作者時,您可以查找此表和項目本身。
根據(jù)規(guī)模,這可能是一個過大的殺傷力。在較小規(guī)模的連接上可以正常工作。
添加回答
舉報