COMP9311 Database Systems Lab8

本次lab的目的是練習(xí)Relational Design Theory宝踪。

1. Functional dependencies

1.1 a

What functional dependencies are implied if we know that a set of attributes X is a candidate key for a relation R?
因?yàn)閄是candidate key毁枯,所以X可以推出所有其他attribute底靠。即X --> R - X

1.2 b

What functional dependencies can we infer do not hold by inspection of the following relation?

A   B   C
a   1   x
b   2   y
c   1   z
d   2   x
a   1   y
b   2   z

當(dāng)A = a, a時(shí)雁社,B = 1, 1, C = x, y久脯,所以A推出B保留,而A是不能推出C的帘撰,B也不能推出C,AB不能推出C摧找;
當(dāng)A = b, b時(shí)核行,B = 2, 2牢硅, C = y, z芝雪,和上面結(jié)論一致,沒(méi)有新的結(jié)論绵脯;
當(dāng)A = c, d時(shí)蛆挫,B = 1, 2妙黍, C = z, x悴侵,確認(rèn)A-->B可免。
同理做粤,判斷B-->A?因?yàn)锽= 1, 1, 1時(shí)妇垢,A = a, c, a肉康,所以B不能推出A。
判斷C-->A涨薪?因?yàn)镃 = x, x時(shí)炫乓,A = a, d,所以C不能推出A
判斷C-->B末捣?因?yàn)镃 = x, x時(shí)光督,B = 1, 2塔粒,所以C不能推出B

1.3 c

Suppose that we have a relation schema R(A,B,C) representing a relationship between two entity sets E and F with keys A and B respectively, and suppose that R has (at least) the functional dependencies A → B and B → A. Explain what this tells us about the relationship between E and F.
A-->B說(shuō)明每個(gè)A有一個(gè)B對(duì)應(yīng),反之船老,也就是說(shuō)A和B是一一對(duì)應(yīng)的,用離散數(shù)學(xué)的術(shù)語(yǔ)說(shuō)是雙射bijection柳畔。

2. Relation and FD exercise1

Consider the relation R(A,B,C,D,E,F,G) and the set of functional dependencies F = { A → B, BC → F, BD → EG, AD → C, D → F, BEG → FA } compute the following:

2.1 A+

根據(jù)上述F薪韩,可以推出的結(jié)論在括號(hào)中顯示:A-->B(AB),A/B/AB無(wú)法推出任何結(jié)論罗捎,所以A+ = {A, B}

2.2 ACEG+

根據(jù)上述F拉盾,可以推出的結(jié)論在括號(hào)中顯示:A-->B(ABCEG), BC-->F(ABCEFG), BEG-->FA(ABCEFG),所以ACEG+ = {A, B, C, E, F, G}

2.3 BD+

根據(jù)上述F倒得,可以推出的結(jié)論在括號(hào)中顯示:BD-->EG(BDEG), D-->F(BDEFG), BEG-->FA(ABDEFG), AD-->C(ABCDEFG)夭禽,所以ACEG+ = {A, B, C, D, E, F, G}

3. Relation and FD exercise2

Consider the relation R(A,B,C,D,E) and the set set of functional dependencies F = { A → B, BC → E, ED → A }

3.1 List all of the candidate keys for R.

根據(jù)F讹躯,如果選定A作為candidate key,則B可以被A推導(dǎo)蜀撑,C不可以被推導(dǎo)酷麦,所以candidate key更新為AC,D不可以被推導(dǎo)沃饶,所以candidate key更新為ACD糊肤,E可以被BC推導(dǎo);
如果選定B作為candidate key业舍,C不可以被推導(dǎo),所以candidate key更新為BC舷暮,D不可以被推導(dǎo)下面,所以candidate key更新為BCD,E可以被BC推導(dǎo)耗啦,A可以被ED推導(dǎo)机杜;
CD在上述兩中情況下都是candidate key,繼續(xù)思考E:
如果選定E作為candidate key,C不可以被推導(dǎo)会喝,所以candidate key更新為EC肢执,D不可以被推導(dǎo),所以candidate key更新為ECD兴溜,A可以被ED推導(dǎo)耻陕,B可以被A推導(dǎo)。
綜上膘怕,所有candidate keys是ACD, BCD, CDE

3.2 Is R in third normal form (3NF)?

3NF的要求是:對(duì)所有的FDs X --> Y
(1)要么Y是X的子集
(2)要么X是超鍵(candidate key/ super key)
(3)要么Y是key中的一個(gè)attribute
因?yàn)?->右側(cè)的BEA都是candidate key的attribute召庞,所以符合3NF

3.3 Is R in Boyce-Codd normal form (BCNF)?

通常3NF都符合BCNF篮灼,但個(gè)別的不符合。BCNF的要求是:對(duì)所有的FDs X --> Y
(1)要么Y是X的子集
(2)要么X是超鍵(candidate key/ super key)
二者的區(qū)別是BCNF不能接受Y是key的一個(gè)attribute诅诱。
左邊沒(méi)有super key,而任何一個(gè)FD都不是子集關(guān)系旦袋,所以不是BCNF疤孕。

4. Relation and FD exercise3

Consider a relation R(A,B,C,D). For each of the following sets of functional dependencies, assuming that those are the only dependencies that hold for R, do the following:
a. List all of the candidate keys for R.
b. Show whether R is in Boyce-Codd normal form (BCNF)?
c. Show whether R is in third normal form (3NF)?

4.1 C → D, C → A, B → C

DAC可以被推出,C推出的最多鹉戚,所以先假設(shè)B是candidate key专控,B-->C(BC), C-->D(BCD), C-->A(ABCD)。candidate key是B赢底。
C不是key卻出現(xiàn)在-->左邊柏蘑,所以不是BCNF。
C-->D洽损,C, D都是非key革半,所以不是3NF又官。

4.2 B → C, D → A

CA可以被推出,優(yōu)先思考BD访娶,假設(shè)B是candidate key觉阅,B-->C(BC),D不可以被推出劫哼,所以更新candidate key為BD割笙,D-->A(ABCD)眯亦。candidate key是BD妻率。
因?yàn)樽筮叾疾皇莝uperkey板祝,而左右又不是子集關(guān)系,所以不是BCNF孤里;而右邊又不是single key attribute橘洞,所以也不是3NF。

4.3 ABC → D, D → A

DA可以被推出虏等,優(yōu)先思考BC适肠,假設(shè)BC是candidate key迂猴,不能推出任何內(nèi)容背伴,更新candidate key為ABC,ABC-->D(ABCD)息尺;更新candidte key為BCD疾掰,D-->A(ABCD)。所以candidate key是ABC或BCD炭懊。
D-->A中的A是key的一部分侮腹,所以被允許稻励,符合3NF愈涩。
D-->A的D不是key履婉,二者也不是子集關(guān)系斟览,所以不屬于BCNF。

4.4 A → B, BC → D, A → C

BDC可以被推出狸棍,假設(shè)candidate key是A味悄,A-->B(AB), A-->C(ABC), BC-->D(ABCD)。所以candidate key是A唐片。
BC-->D左右都是non-key费韭,不符合3NF庭瑰。
BC不是key,不符合BCNF督暂。

4.5 AB → C, AB → D, C → A, D → B

ABCD都可以被推出穷吮,假設(shè)candidate key是A捡鱼,A無(wú)法推出任何,更新candidate key為AB缠诅,AB-->C(ABC), AD-->D(ABCD)乍迄;假設(shè)candidate key是B,B無(wú)法推出任何汉匙,更新candidate key為BC,C-->A(ABC), AB-->D(ABCD)戏自;假設(shè)candidate key是C伤锚,C-->A(AC)屯援,更新candidate key為CD,D-->B(ABCD)弯淘;假設(shè)candidate key是D吉懊,D-->B(BD),更新candidate key為AD态鳖,AB-->C(ABCD)恶导。所以candidate key可能是AB, BC, CD, AD惨寿。
因?yàn)?->右側(cè)的CDAB都是candidate key中的attribute,所以符合3NF虎韵。
如果candidate key是AB缸废,那么C-->A既不是子集關(guān)系企量,C也不是key亡电,所以不符合BCNF。

4.6 A → BCD

可以直接看出candidate key是A恕汇。
既符合3NF瘾英,也符合BCNF。

5. Non-trivial FDs exercise1

Specify the non-trivial functional dependencies for each of the relations in the following Teams-Players-Fans schema and then show whether the overall schema is in BCNF.

Team(name(key), captain)
Player(name(key), teamPlayedFor)
Fan(name(key), address)
TeamColours(teamName(key), colour(key))
FavouriteColours(fanName(key), colour(key))
FavouritePlayers(fanName(key), playerName(key))
FavouriteTeams(fanName(key), teamName(key))

Functional dependencies:
Team: name → captain
Player: name → teamPlayedFor
Fan: name → address
TeamColours: no non-trivial fds
FavouriteColours: no non-trivial fds
FavouritePlayers: no non-trivial fds
FavouriteTeams: no non-trivial fds
non-trivial FDs就是找非子集的dependency但惶,如果只有兩個(gè)variable,都是key阳啥,那么都是trivial的察迟。以上都屬于superkey推出non-key,所以都是BCNF喊废。

6. Non-trivial FDs exercise2

Specify the non-trivial functional dependencies for each of the relations in the following Trucks-Shipments-Stores schema and then show whether the overall schema is in BCNF.

Warehouse(warehouse#(key), address)
Source(trip(key), warehouse(key))
Trip(trip#(key), date, truck)
Truck(truck#(key), maxvol, maxwt)
Shipment(shipment#(key), volume, weight, trip, store)
Store(store#(key), storename, address)

Functional dependencies:
Warehouse ... warehouse# → address
Source ... no non-trivial fds
Trip ... trip# → date,truck
Truck ... truck# → maxvol,maxwt
Shipment ... shipment# → volume,weight,trip,store
Store ... store# → storename,address
和上面練習(xí)思路一致污筷,這里也都是BCNF乍赫。

7. BCNF & 3NF decomposition

For each of the sets of dependencies in question 4:
(1) if R is not already in 3NF, decompose it into a set of 3NF relations;
(2) if R is not already in BCNF, decompose it into a set of BCNF relations

7.1 C → D, C → A, B → C

candidate key是B雷厂,不是BCNF,不是3NF
(1)decompose to BCNF
C-->D違背了BCNF诈皿,拆解出來(lái)CD像棘。
剩余ABC,F(xiàn)D變?yōu)镃-->A, B-->C截歉。C-->A違背了BCNF瘪松,拆解出來(lái)CA。
剩余BC性宏,F(xiàn)D變?yōu)锽-->C状飞,符合BCNF。
所以最終BCNF為(R1(CD), R2(CA), R3(BC))
(2)decompose to 3NF
minimal cover: C-->D, C-->A, B-->C
key: B
F' = C-->DA, B-->C
CDA(KEY = C), BC(KEY = B)

7.2 B → C, D → A

candidate key是BD酵使,不是BCNF口渔,不是3NF
(1)decompose to BCNF
B-->C違背BCNF穿撮,拆解出來(lái)BC悦穿。
剩余ABD,F(xiàn)D變?yōu)镈-->A礁扮,違背BCNF瞬沦,拆解出來(lái)DA。
剩余BD逛钻,F(xiàn)D為空。
所以最終BCNF為(R1(BC), R2(DA), R3(BD))
(2)decompose to 3NF
minimal cover無(wú)法合并曙痘,所以3NF是BC(KEY = B), DA(KEY = A), BD(KEY = BD)

7.3 ABC → D, D → A

candidate key是ABC/BCD屡江,不是BCNF赛不,是3NF
(1)decompose to BCNF
D-->A違背BCNF踢故,拆解出來(lái)DA惹苗。
剩余BCD桩蓉,F(xiàn)D為空劳闹。
所以最終BCNF為(R1(DA), R2(BCD))

7.4 A → B, BC → D, A → C

candidate key是A本涕,不是BCNF,不是3NF
(1)decompose to BCNF
BC-->D違背BCNF样漆,拆解出BCD晦闰。
剩余ABC呻右,F(xiàn)D變?yōu)锳-->B, A-->C,都符合BCNF骗奖。
所以最終BCNF為(R1(BCD), R2(AB), R3(AC))
(2)decompose to 3NF
minimal cover和FDs一致醒串,所以3NF是AB(KEY = A), BCD(KEY = BC), AC(KEY = A)

7.5 AB → C, AB → D, C → A, D → B

candidate key是AB, BC, CD, AD芜赌,不是BCNF,是3NF
(1)decompose to BCNF
C-->A違背BCNF膘壶,拆解出來(lái)CA
剩余BCD颓芭,F(xiàn)D變?yōu)镈-->B柬赐,不符合BCNF,拆解出來(lái)DB州藕。
剩余CD床玻,F(xiàn)D為空。
所以BCNF為(R1(CA), R2(DB), R3(CD))贫堰,但彼此之間沒(méi)有聯(lián)系严嗜,所以還要加入聯(lián)系洲敢,變成(R1(CA), R2(DB), R3(CD), R4(ABC), R5(ABD))

7.6 A → BCD

candidate key是A,是BCNF睦优,是3NF

8. Case1

Consider (yet another) banking application that contains information about accounts, branches and customers. Each account is held at a specific branch, but a customer may hold more than one account and an account may have more than one associated customer.
Consider an unnormalised relation containing all of the attributes that are relevant to this application:

acct# - unique account indentifier
branch# - unique branch identifier
tfn - unique customer identifier (tax file number)
kind - type of account (savings, cheque, ...)
balance - amount of money in account
city - city where branch is located
name - customer's name
i.e. consider the relation R(acct#, branch#, tfn, kind, balance, city, name)

Based on the above description:

8.1 Devise a suitable set of functional dependencies among these attributes.

根據(jù)定義acct# --> kind, balance汗盘;branch#-->city隐孽;tfn-->name

8.2 Using these functional dependencies, decompose R into a set of BCNF relations.

Account(acct#(key), kind, balance, branch)
Branch(branch#(key), city)
Customer(tfn(key), name)
CustAcc(customer(key), account(key))

8.3 State whether the new relations are also in 3NF.

They fit in 3NF.

9. Case2

Consider a schema representing projects within a company, containing the following information:
pNum - project's unique identifying number
pName - name of project
eNum - employee's unique identifying number
eName - name of employee
jobClass - type of job that employee has on this project
payRate - hourly rate, dependent on the kind of job being done
hours - total hours worked in this job by this employee
This schema started out life as a large spreadsheet and now the company wants to put it into a database system.

As a spreadsheet, its schema is: R(pNum, pName, eNum, eName, jobClass, payRate, hours)

Based on the above description:

9.1 Devise a suitable set of functional dependencies among these attributes.

primary key是unique菱阵,所以根據(jù)定義推出
pNum → pName
eNum → eName
jobClass → payRate
pNum,eNum → jobClass,payRate,hours

9.2 Using these functional dependencies, decompose R into a set of BCNF relations.

pNum → pName is a dependency on part of the key
to fix: decompose to R1(pNum,eNum,eName,jobClass,payRate,hours) and R2(pNum,pName)
eNum → eName is a dependency on part of the key
to fix: decompose to R1(pNum,eNum,jobClass,payRate,hours) and R2(pNum,pName) and R3(eNum,eName)
jobClass → payRate is a dependency on a non-key attribute
to fix: decompose to R1(pNum,eNum,jobClass,hours) and R2(pNum,pName) and R3(eNum,eName) and R4(jobClass,payRate)

pNum → pName
eNum → eName
jobClass → payRate
pNum,eNum → jobClass,hours

Project(pNum, pName)
Employee(eNum, eName)
AwardRates(jobClass, payRate)
Assignment(pNum, eNum, jobClass, hours)

9.3 State whether the new relations are also in 3NF.

The new schema is not in 3NF because we have lost the dependency: pNum,eNum → payRate

后續(xù)還有若干練習(xí),和前面的題目大同小異嫡锌,不再贅述。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市啦桌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌躲庄,老刑警劉巖钾虐,帶你破解...
    沈念sama閱讀 212,294評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件效扫,死亡現(xiàn)場(chǎng)離奇詭異菌仁,居然都是意外死亡静暂,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,493評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén)摹迷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人郊供,你說(shuō)我怎么就攤上這事峡碉。” “怎么了驮审?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,790評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵鲫寄,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我疯淫,道長(zhǎng)地来,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,595評(píng)論 1 284
  • 正文 為了忘掉前任熙掺,我火速辦了婚禮,結(jié)果婚禮上适掰,老公的妹妹穿的比我還像新娘颂碧。我一直安慰自己,他們只是感情好类浪,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,718評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布载城。 她就那樣靜靜地躺著,像睡著了一般费就。 火紅的嫁衣襯著肌膚如雪诉瓦。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,906評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音睬澡,去河邊找鬼固额。 笑死,一個(gè)胖子當(dāng)著我的面吹牛煞聪,可吹牛的內(nèi)容都是我干的斗躏。 我是一名探鬼主播,決...
    沈念sama閱讀 39,053評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼昔脯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼啄糙!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起云稚,我...
    開(kāi)封第一講書(shū)人閱讀 37,797評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤隧饼,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后静陈,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體燕雁,經(jīng)...
    沈念sama閱讀 44,250評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,570評(píng)論 2 327
  • 正文 我和宋清朗相戀三年鲸拥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拐格。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,711評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡崩泡,死狀恐怖禁荒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情角撞,我是刑警寧澤呛伴,帶...
    沈念sama閱讀 34,388評(píng)論 4 332
  • 正文 年R本政府宣布,位于F島的核電站谒所,受9級(jí)特大地震影響热康,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜劣领,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,018評(píng)論 3 316
  • 文/蒙蒙 一姐军、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧尖淘,春花似錦奕锌、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,796評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至趁桃,卻和暖如春辽话,著一層夾襖步出監(jiān)牢的瞬間肄鸽,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,023評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工油啤, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留典徘,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,461評(píng)論 2 360
  • 正文 我出身青樓益咬,卻偏偏與公主長(zhǎng)得像逮诲,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子础废,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,595評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,437評(píng)論 0 23
  • 40多了汛骂,今年四月才開(kāi)始想著要畫(huà)畫(huà)罕模。 參加了一個(gè)21天打卡群后评腺,現(xiàn)在一直自己在折騰,先是鉛筆臨摹網(wǎng)上的工筆畫(huà)圖片淑掌,...
    月光灑落閱讀 722評(píng)論 6 18
  • 設(shè)置國(guó)內(nèi)registry蒿讥,加快下載速度 臨時(shí)設(shè)置訪問(wèn)源,命令行輸入: 命令行中直接加registry參數(shù)抛腕。 若想永...
    mr_franklin閱讀 2,139評(píng)論 0 3
  • 剛剛在整理東西的時(shí)候担敌,無(wú)意翻出了以前的照片摔敛,居然還有和前任的照片,頓時(shí)覺(jué)得心情DOWN了下來(lái)全封,其實(shí)已經(jīng)走出來(lái)了马昙,可...
    若愚123閱讀 185評(píng)論 0 0
  • 2017年6月6日星期二 小雨 晚上臨睡前給女兒講了個(gè)大禹治水的故事,告訴她耍學(xué)習(xí)大禹豎持不懈刹悴、不屈不撓的精神...
    廈小薛智一爸爸閱讀 100評(píng)論 0 3