Database - relational algebra operations

topic

An existing product database, the database schema consisting of four relationship, the relationship between these four modes as follows:

\ (Product (Maker, Model, of the type) \)

\ (PC (Model, Speed, RAM, hd,. Price) \)

\ (Laptop (Model, Speed, RAM, the above mentioned id, hd, Screen,. price) \)

\ (Printer (Model, Color, of the type,. price) \)

write the following query relational algebra expression, and sample data for the following cases, given the results of the query. (Your answer should be able to work properly on any data, not just data graph)

A) What kind of PC model with a minimum rate of 3.00?

b) Which manufacturer of notebook (laptop) hard disk capacity of at least 100GB?

c) query vendor B All products manufactured models and prices.

d) query models All color laser printers.

e) query only those vendors sell laptops, do not sell the PC.

f) query capacity hard disk appears in one or both of the PC before.

g) the same query processing speed and memory size of the PC the same right. Each pair is only one list, that list gives \ ((i, j) \ ) but do not give \ ((J, i) \) .

h) query those manufacturers produce two processing speed of at least 2.80 greater than the PC or laptop.

i) the average query processing speed (PC or laptop) the highest of all vendors.

j) the production of at least three different query processing speed of computer manufacturers.

k) query happens to sell three models of PC manufacturers.

Relationship four sample data as shown below:
Four sample data relationships

a) Which model PC with a minimum rate of 3.00?

1.要操作的表:\(PC\)

2.要进行的操作:选择、投影

3.各操作的对象和顺序

  1. 选择关系\(PC\)\(speed\ge3.0\)的元组:\(R_1:=\sigma_{speed\ge3.0}(PC)\)
  2. \(R_1\)投影到属性\(model\)上:\(R_2:=\pi_{model}(R_1)\)

4.关系代数表达式

  • \(\pi_{model}(\sigma_{speed\ge3.0}(PC))\)

5.数据样例查询结果

\(model\)
1005
1006
1013

b)哪个生产厂商的笔记本电脑(笔记本)的硬盘容量至少100GB?

1.要操作的表:\(Laptop\)\(Product\)

2.要进行的操作:选择、自然连接、投影、差

3.各操作的对象和顺序

  1. 选择关系\(Laptop\)\(hd<100\)的元组\(R_1\)\(R_1:=\sigma_{hd<100}(Laptop)\)
  2. 将关系\(Product\)和关系\(R_1\)自然连接\(R_2:=R_1\bowtie Product\)
  3. \(R_2\)投影到属性\(maker\)上:\(R_3:=\pi_{maker}(R_2)\)
  4. 将关系\(Product\)和关系\(Laptop\)自然连接\(R_4:=Product\bowtie Laptop\)
  5. 将关系\(R_4\)投影到属性\(maker\)上:\(R_5:=\pi_{maker}(R_4)\)
  6. 求关系\(R_5\)和关系\(R_3\)\(R_6:=R_5-R_3\)

4.关系代数表达式

  • \(Product\bowtie Laptop-\pi_{maker}(\sigma_{hd<100}(Laptop)\bowtie Product)\)

5.数据样例查询结果

\(maker\)
B
G

c)查询厂商B生产的所有产品的型号和价格。

1.要操作的表:\(Product\)\(PC\)\(Laptop\)\(Printer\)

2.要进行的操作:选择、投影、自然连接、并

3.各操作的对象和顺序

  1. 选择关系\(Product\)\(maker='B'\)的元组:\(R_1:=\sigma_{maker='B'}(Product)\)
  2. 将关系\(R_1\)投影到属性\(model\)上:\(R_2:=\pi_{model}(R_1)\)
  3. 将关系\(R_2\)和关系\(PC\)自然连接\(R_3:=R_2\bowtie PC\)
  4. 将关系\(R_2\)和关系\(Laptop\)自然连接\(R_4:=R_2\bowtie Laptop\)
  5. 将关系\(R_2\)和关系\(Printer\)自然连接\(R_5:=R_2\bowtie Printer\)
  6. 将关系\(R_3\)投影到属性组\(\{ model,price \}\)上:\(R_6:=\pi_{model,price}(R_3)\)
  7. 将关系\(R_4\)投影到属性组\(\{ model,price \}\)上:\(R_7:=\pi_{model,price}(R_4)\)
  8. 将关系\(R_5\)投影到属性组\(\{ model,price \}\)上:\(R_8:=\pi_{model,price}(R_5)\)
  9. 将关系\(R_6\)\(R_7\)\(R_8\)起来:\(R_9:=R_6\cup R_7\cup R_8\)

4.关系代数表达式

  • \(R_9\),该表达式展开写会太长,因此省略其展开式。

5.数据样例查询结果

\(model\) \(price\)
1004 649
1005 630
1006 1049
2007 1429

d)查询所有彩色激光打印机的型号。

1.要操作的表:\(Printer\)

2.要进行的操作:选择、投影

3.各操作的对象和顺序

  1. 选择关系\(Printer\)\(color=true\)并且\(type='laser'\)的元组:\(R_1:=\sigma_{color=true\wedge type='laser'}(Printer)\)
  2. 将关系\(R_1\)投影到属性\(model\)上:\(R_2:=\pi_{model}(R_1)\)

4.关系代数表达式

  • \(\pi_{model}(\sigma_{color=true\wedge type='laser'}(Printer))\)

5.数据样例查询结果

\(model\)
3003
3007

e)查询那些只出售笔记本电脑,不出售PC的厂商。

1.要操作的表:\(Product\)\(PC\)\(Laptop\)

2.要进行的操作:自然连接、投影、差

3.各操作的对象和顺序

  1. 将关系\(Product\)与关系\(Laptop\)自然连接\(R_1:=Product\bowtie Laptop\)
  2. 将关系\(Product\)与关系\(PC\)自然连接\(R_2:=Product\bowtie PC\)
  3. 将关系\(R_1\)投影到属性\(maker\)上:\(R_3:=\pi_{maker}(R_1)\)
  4. 将关系\(R_2\)投影到属性\(maker\)上:\(R_4:=\pi_{maker}(R_2)\)
  5. 求关系\(R_3\)\(R_4\)\(R_5:=R_3-R_4\)

4.关系代数表达式

  • \(\pi_{maker}(Product\bowtie Laptop)-\pi_{maker}(Product\bowtie PC)\)

5.数据样例查询结果

\(model\)
F
G

f)查询在一种或者两种PC机中出现过的硬盘的容量。

1.要操作的表:\(PC\)

2.要进行的操作:重命名、\(\theta\)连接、投影、差

3.各操作的对象和顺序

  1. 将关系\(PC\)通过重命名复制:\(R_1:=\rho_{PC1(model_1,speed,ram,hd,price)}(PC)\)
  2. 将关系\(PC\)通过重命名复制:\(R_2:=\rho_{PC2(model_2,speed,ram,hd,price)}(PC)\)
  3. 将关系\(R_1\)\(R_2\)进行\(\theta\)连接\(R_3:=R_1\bowtie_{R_1.hd=R_2.hd\wedge R_1.model_1\neq R_2.model_2}R_2\)
  4. 将关系\(R_3\)\(PC\)进行\(\theta\)连接\(R_4:=PC\bowtie_{PC.hd=R_3.hd\wedge PC.model\neq R_3.model_1\wedge PC.model\neq R_3.model_2}R_3\),由此得到至少出现三次的\(hd\)
  5. 将关系\(R_4\)投影到属性\(hd\)上:\(R_5:=\pi_{hd}(R_4)\)
  6. 将关系\(PC\)投影到属性\(hd\)上:\(R_6:=\pi_{hd}(PC)\)
  7. 求关系\(R_6\)\(R_5\)\(R_7:=R_6-R_5\)

4.关系代数表达式

  • \(R_7\),该关系代数表达式展开长度太长,略

5.数据样例查询结果

\(hd\)
80
320
200
300
160

g)查询有同样处理速度和同样内存大小的PC对。

1.要操作的表:\(PC\)

2.要进行的操作:重命名、\(\theta\)连接、投影

3.各操作的对象和顺序

  1. 将关系\(PC\)通过重命名复制:\(R_1:=\rho_{PC1(model_1,speed,ram,hd,price)}(PC)\)
  2. 将关系\(PC\)通过重命名复制:\(R_2:=\rho_{PC2(model_2,speed,ram,hd,price)}(PC)\)
  3. 将关系\(R_1\)\(R_2\)进行\(\theta\)连接\(R_3:=R_1\bowtie_{R_1.speed=R_2.speed\wedge R_1.ram=R_2.ram\wedge R_1.model_1\neq R_2.model_2}R_2\)
  4. 将关系\(R_3\)投影到属性组\(\{ model_1,model_2 \}\)上:\(R_4:=\pi_{model_1,model_2}(R_3)\)

4.关系代数表达式

  • \(R_4\),该关系代数表达式展开长度太长,略

5.数据样例查询结果

\(model_1\) \(model_2\)
1004 1012

h)查询那些至少生产两种处理速度大于2.80的PC或者笔记本电脑的厂商。

1.要操作的表:\(PC\)

2.要进行的操作:选择、并、投影、自然连接、重命名、\(\theta\)连接

3.各操作的对象和顺序

  1. 选择关系\(PC\)中属性\(speed>2.80\)的元组:\(R_1:=\sigma_{speed>2.80}(PC)\)
  2. 选择关系\(Laptop\)中属性\(speed>2.80\)的元组:\(R_2:=\sigma_{speed>2.80}(Laptop)\)
  3. 将关系\(R_1\)\(R_2\)起来,并求其在属性\(model\)上的投影\(R_3=\pi_{model}(R_1\cup R_2)\)
  4. 将关系\(R_3\)\(Product\)自然连接,并求其在属性组\(\{ maker,model \}\)上的投影\(R_4:=\pi_{maker,model}(R_3\bowtie Product)\)
  5. 将关系\(R_4\)通过重命名复制:\(R_5:=\rho_{R_5(maker,model_5)}(R_4)\)
  6. 将关系\(R_4\)通过重命名复制:\(R_6:=\rho_{R_6(maker,model_6)}(R_4)\)
  7. 将关系\(R_5\)\(R_6\)进行\(\theta\)连接\(R_7:=R_5\bowtie_{R_5.maker=R_6.maker\wedge R_5.model_5\neq R_6.model_6}R_6\)
  8. 将关系\(R_7\)投影到属性\(maker\)上:\(R_8:=\pi_{maker}(R_7)\)

4.关系代数表达式

  • \(R_8\),该关系代数表达式展开长度太长,略

5.数据样例查询结果

\(maker\)
B

i)查询平均处理速度(PC或者是笔记本电脑)最高的所有厂商。

1.要操作的表:\(PC\)\(Laptop\)\(Product\)

2.要进行的操作:投影、重命名、\(\theta\)连接、差、自然连接

3.各操作的对象和顺序

  1. 将关系\(PC\)\(Laptop\)投影到属性组\(\{ model,speed \}\)上并将两者起来:\(R_1:=\pi_{model,speed}(PC)\cup\pi_{model,speed}(Laptop)\)
  2. 通过重命名复制关系\(R_1\)\(R_2:=\rho_{R_2}(R_1)\)
  3. 关系\(R_1\)\(R_2\)进行\(\theta\)连接\(R_3:=R_1\bowtie_{R_1.speed<R_2.speed} R_2\),求出\(speed\)小于最大值的元组
  4. 求关系\(R_1\)\(R_3\)\(R_4:=R_1-R_3\)
  5. 关系\(R_4\)\(Product\)自然连接\(R_5:=R_4\bowtie Product\)

4.关系代数表达式

  • \(R_5\),该关系代数表达式展开长度太长,略

5.数据样例查询结果

\(maker\)
B

j)查询至少生产三种不同处理速度电脑的厂商。

1.要操作的表:\(PC\)\(Product\)

2.要进行的操作:自然连接、投影、重命名、\(\theta\)连接

3.各操作的对象和顺序

  1. 关系\(Product\)\(PC\)自然连接投影到属性组\(\{ maker,speed \}\)上:\(R_1:=\pi_{maker,speed}(Procuct\bowtie PC)\)
  2. 复制并重命名关系\(R_1\)\(R_2:=\rho_{R_2(maker,speed_2)}(R_1)\)
  3. 复制并重命名关系\(R_1\)\(R_3:=\rho_{R_3(marker,speed_3)}(R_1)\)
  4. 关系\(R_1\)\(R_2\)进行\(\theta\)连接\(R_4:=R_1\bowtie_{R_1.maker=R_2.maker\wedge R_1.speed\neq R_2.speed_2}R_2\)
  5. 关系\(R_3\)\(R_4\)进行\(\theta\)连接\(R_5:=R_3\bowtie_{R_3.maker=R_4.maker\wedge R_4.speed\neq R_3.speed_3 /wedge R_4.speed_2\neq R_3.speed_3}R_4\)
  6. 将关系\(R_5\)投影到属性\(maker\)上:\(R_6:=\pi_{maker}(R_5)\)

4.关系代数表达式

  • \(R_5\),该关系代数表达式展开长度太长,略

5.数据样例查询结果

\(maker\)
A
D
E

k)查询恰好出售三种型号的PC厂商。

1.要操作的表:\(PC\)\(Product\)

2.要进行的操作:自然连接、投影、重命名、\(\theta\)连接、差

3.各操作的对象和顺序

  1. 关系\(Product\)\(PC\)自然连接投影到属性组\(\{ maker,model \}\)上:\(R_1:=\pi_{maker,model}(Procuct\bowtie PC)\)
  2. 复制并重命名关系\(R_1\)\(R_2:=\rho_{R_2(maker,model_2)}(R_1)\)
  3. 复制并重命名关系\(R_1\)\(R_3:=\rho_{R_3(marker,model_3)}(R_1)\)
  4. 复制并重命名关系\(R_1\)\(R_4:=\rho_{R_4(marker,model_4)}(R_1)\)
  5. 关系\(R_1\)\(R_2\)进行\(\theta\)连接\(R_5:=R_1\bowtie_{R_1.maker=R_2.maker\wedge R_1.model\neq R_2.model_2}R_2\)
  6. 关系\(R_3\)\(R_5\)进行\(\theta\)连接\(R_6:=R_3\bowtie_{R_3.maker=R_5.maker\wedge R_5.model\neq R_3.model_3 /wedge R_5.model_2\neq R_3.model_3}R_5\)
  7. 关系\(R_4\)\(R_6\)进行\(\theta\)连接\(R_7:=R_4\bowtie_{R_4.maker=R_6.maker\wedge R_6.model\ne R_4.model_4 \wedge R_6.model_2\ne R_4.model_4 \wedge R_6.model_3\ne R_4.model_4}R_6\)
  8. 求关系\(R_6\)\(R_7\)\(R_8:=R_6-R_7\)
  9. 将关系\(R_8\)投影到属性\(maker\)上:\(R_8:=\pi_{maker}(R_8)\)

4.关系代数表达式

  • \(R_8\),该关系代数表达式展开长度太长,略

5.数据样例查询结果

\(maker\)
A
B
D
E

Author: @ smelly salted fish

Please indicate the source: https://www.cnblogs.com/chouxianyu/

Welcome to discuss and share!


Guess you like

Origin www.cnblogs.com/chouxianyu/p/11664142.html