第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

設(shè)計數(shù)據(jù)庫以保存不同的元數(shù)據(jù)信息

設(shè)計數(shù)據(jù)庫以保存不同的元數(shù)據(jù)信息

喵喵時光機 2019-09-21 15:24:31
因此,我正在嘗試設(shè)計一個數(shù)據(jù)庫,以使我可以將一個產(chǎn)品與多個類別聯(lián)系起來。我已經(jīng)知道了這一部分。但是我無法解決的問題是持有不同類型的產(chǎn)品詳細信息。例如,產(chǎn)品可以是一本書(在這種情況下,我將需要引用該書的元數(shù)據(jù),例如isbn,author等),也可以是業(yè)務(wù)清單(具有不同的元數(shù)據(jù))。我該如何解決?
查看完整描述

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'

;


查看完整回答
反對 回復(fù) 2019-09-21
?
搖曳的薔薇

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)也就越容易。它還將更有效地運行。


查看完整回答
反對 回復(fù) 2019-09-21
?
開滿天機

TA貢獻1786條經(jīng)驗 獲得超13個贊

您可以采用無模式方法:

將元數(shù)據(jù)作為JSON對象(或其他序列化形式)保存在TEXT列中,但由于稍后說明的原因,JSON更好。

該技術(shù)的優(yōu)點:

  1. 更少的查詢:您只需一次查詢即可獲取所有信息,而無需“定向”查詢(獲取元元數(shù)據(jù))和聯(lián)接。

  2. 您可以隨時添加/刪除所需的任何屬性,而無需更改表(這在某些數(shù)據(jù)庫中是有問題的,例如,Mysql鎖定了表,而使用大型表則需要很長時間)

  3. 由于它是JSON,因此您不需要在后端進行額外的處理。您的網(wǎng)頁(我假設(shè)它是一個Web應(yīng)用程序)僅從Web服務(wù)中讀取JSON,僅此而已,您可以根據(jù)需要使用JSON對象和javascript。

問題:

  1. 潛在的浪費空間是,如果您有100本書與同一位作者在一起,那么一個作者表(其中所有書籍都只有author_id)是更經(jīng)濟的空間選擇。

  2. 需要實現(xiàn)索引。由于您的元數(shù)據(jù)是JSON對象,因此您不會立即擁有索引。但是,為所需的特定元數(shù)據(jù)實現(xiàn)特定索引非常容易。例如,您想按作者進行索引,因此您可以使用author_id和item_id創(chuàng)建一個author_idx表,當有人搜索作者時,您可以查找此表和項目本身。

根據(jù)規(guī)模,這可能是一個過大的殺傷力。在較小規(guī)模的連接上可以正常工作。


查看完整回答
反對 回復(fù) 2019-09-21
  • 3 回答
  • 0 關(guān)注
  • 559 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號