3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
這稱為觀察模式。
在此處輸入圖片說(shuō)明
以三個(gè)對(duì)象為例
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
編輯:
杰弗里提出了一個(gè)正確的觀點(diǎn)(見(jiàn)評(píng)論),所以我將擴(kuò)大答案。
該模型允許動(dòng)態(tài)(動(dòng)態(tài))創(chuàng)建具有任何類型的屬性的任意數(shù)量的實(shí)體,而無(wú)需更改架構(gòu)。但是,這種靈活性要付出代價(jià)-與通常的桌子設(shè)計(jì)相比,存儲(chǔ)和搜索更慢,更復(fù)雜。
是一個(gè)例子了,但是首先,為了使事情變得容易,我將模型展平為一個(gè)視圖。
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
;
從評(píng)論中使用杰弗里的例子
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 ;
編寫(xiě)起來(lái)似乎很復(fù)雜,但是仔細(xì)檢查后,您可能會(huì)注意到CTE中的模式。
現(xiàn)在假設(shè)我們有一個(gè)標(biāo)準(zhǔn)的固定模式設(shè)計(jì),其中每個(gè)對(duì)象屬性都有自己的列。查詢?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貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊
我本來(lái)不打算回答,但是現(xiàn)在被接受的答案是一個(gè)非常糟糕的主意。關(guān)系數(shù)據(jù)庫(kù)絕對(duì)不能用于存儲(chǔ)簡(jiǎn)單的屬性-值對(duì)。這將在以后引起很多問(wèn)題。
解決此問(wèn)題的最佳方法是為每種類型創(chuàng)建一個(gè)單獨(dú)的表。
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.
每個(gè)表的每一行都應(yīng)代表一個(gè)關(guān)于真實(shí)世界的命題,并且表的結(jié)構(gòu)及其約束應(yīng)反映所代表的現(xiàn)實(shí)。您越接近這個(gè)理想,數(shù)據(jù)將越干凈,并且以其他方式進(jìn)行報(bào)告和擴(kuò)展系統(tǒng)也就越容易。它還將更有效地運(yùn)行。

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