[컴퓨터공학]/[데이터베이스]

[데이터베이스] Relational Algebra 예제

딥러닝 도전기 2022. 3. 30. 23:15

데이터베이스 관계 대수 예제입니다.

다음의 교재를 참고했음을 미리 밝힙니다.

 

 


p.49 Exercise 4.1

 

Exercise 4.1 : This exercise builds upon the products schema of Exercise 3.1. Recall that the database schema consists of four relations, whose schemas are:

     Product(maker, model, type)
     PC(model, speed, ram, hd, price)
     Laptop(model, speed, ram, hd, screen, price)
     Printer(model, color, type, price)

Some sample data for the relation Product is shown in Fig. 20. Sample data for the other three relations is shown in Fig. 21. Manufacturers and model numbers have been “sanitized,” but the data is typical of products on sale at the beginning of 2007.

Write expressions of relational algebra to answer the following queries. You may use the linear notation of Section 4.13 if you wish. For the data of Figs. 20 and 21, show the result of your query. However, your answer should work for arbitrary data, not just the data of these figures.

 

 

 

a) What PC models have a speed of at least 3.00?

ANSWER

 

"PC모델 중 속도가 3.00을 넘는 모델" 과 동치인 Relational algebra를 구하는 문제이다.

Selection 연산을 사용하면 간단하게 해결 될 것으로 보인다.

 

$\sigma_{speed \geq 3.00}(PC)$

 


b) Which manufacturers make laptops with a hard disk of at least 100GB?

ANSWER

 

 Laptop의 hd가 100GB를 넘는 제품만 생산하는 "manufacturers"를 구하는 문제이다.

위의 그림에는 manufacturer 이 maker로 표현되어 있는 것으로 보인다.

 

1. Laptop에서 selection 연산을 통하여 100GB가 넘는 튜플만 선택한다.

2. Laptop에는 maker가 없으니 Product와 natural join연산을 한 후, maker을 추출하기 위해 projection을 한다.

 

$\pi_{maker}(Product\bowtie(\sigma_{hd>100GB}(Laptop)))$


c) Find the model number and price of all products (of any type) made by manufacturer B.

ANSWER

 

model number는 product table만 활용해서 구할 수 있지만, price는 PC, Laptop, Printer table을 확인해야 하는 것으로 보인다.

 

1. maker가 B인 튜플을 selection연산을 통해 선택한 후 TEMP로 치환한다.

2. PC, Laptop, Printer와 TEMP를 각각 natural joint연산을 한 후 union연산으로 합친다.

3. 구하고자 하는 model, price로 projection을 한다.

 

$\rho_{TEMP}(\sigma_{maker = B}(product))$

 

많이 쓰일 것 같은 식이기 때문에 $\rho$를 사용하여 TEMP로 치환한다.

 

$\pi_{model, price}((TEMP\bowtie PC) \cup (TEMP\bowtie Laptop) \cup (TEMP\bowtie Printer))$

 (괄호는 쓰는 것이 맞는지, 안 쓰는 것이 맞는지 정확히 잘 모르겠습니다.)

 


d) Find the model numbers of all color laser printers.

ANSWER

 

컬러 레이저 프린터의 model number를 찾는 문제입니다.

 

1. 프린터에서 selection을 이용하여 type = laser, color = true인 튜플을 찾는다.

2. projection을 이용하여 model name을 찾는다.

 

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

 


e) Find those manufacturers that sell Laptops, but not PC’s.

ANSWER

 

Laptop만 판매하고 PC는 판매하지 않는 maker을 찾는 문제입니다.

 

1. Laptop의 모델명과 PC의 모델명을 projection을 통해 추출한 후 각각 LM, PM으로 치환한다. (Laptop_Model, PC_Model)

2. LM-PM을 한 후,  Product와 natural joint 연산을 한다.

3. projection 연산을 통해 maker를 추출한다.

 

$\rho_{LM}(\pi_{model}(Laptop))$

$\rho_{PM}(\pi_{model}(PC))$

$\pi_{maker}(Product \bowtie (LM-PM))$

 


f)  Find those hard-disk sizes that occur in two or more PC’s.

ANSWER

두 번 이상 등장하는 hd를 구하는 문제입니다.

 

1. PC를 PC1, PC2로 치환합니다.

2. theta-join을 통해 hd가 같고, 모델명이 다른 hd값을 추출합니다.

 

$\rho_{PC1}(PC)$

$\rho_{PC2}(PC)$

$\pi_{hd}(PC1 \bowtie_{PC1.hard = PC2.hard \wedge PC1.model \neq PC2.model}(PC1\times PC2))$

 

g) Find those pairs of PC models that have both the same speed and RAM. A pair should be listed only once; e.g., list (i,j) but not (j,i).

ANSWER

해당 문제는 speed와 ram이 같은 PC의 쌍을 구하는 문제입니다. 이때, 같은 두 PC의 쌍을 구하거나 (i, j)를 구했으면 (j, i)는 구하지 않는 것을 규칙으로 합니다.

 

1. PC를 PC1, PC2로 치환합니다.

2. PC1과 PC2의 theta-join을 통해 speed와 ram이 같은 PC의 쌍을 구합니다.

3. 이때, 문제의 조건(중복)을 만족하게 하기 위해서 key값으로 쓰일 수 있는 model을 사용하여 연산을 추가해줍니다.

 

$$\rho_{PC1}(PC) \quad \rho_{PC2}(PC)$$

$$ \pi_{PC1.model, PC2.model}(PC1 \Join_{PC1.speed = PC2.speed \wedge PC1.ram = PC2.ram \wedge PC1.model > PC2.model}(PC2))$$

여기에서 $PC1.model > PC2.model$ 을 추가한 이유는 다음과 같습니다.

 

$PC1 \Join_\theta PC2  = \sigma_\theta(PC1 \times PC2)$ 

theta-join은 위와 같이 정의되기 때문에, $PC1.model > PC2.model$ 이 조건을 넣어주면 중복이 사라지게 됩니다.

h) Find those manufacturers of at least two different computers (PC’s or laptops) with speeds of at least 2.80.

ANSWER

위의 문제는 speed가 2.80이 넘는 두 가지 종류의 컴퓨터를 생산하는 maker를 구하는 문제입니다. 

 

1. 속도가 2.8이 넘는 모든 PC의 모델과 Laptop의 모델을 각각 추출한다.

2. 합집합으로 maker를 구한다.

 

$$\pi_{model}(\sigma_{speed > 2.8}(PC)) \cup \pi_{model}(\sigma_{speed > 2.8}(Laptop))$$

 

 

 

i) Find the manufacturer(s) of the computer (PC or laptop) with the highest available speed.

ANSWER

 

 

j) Find the manufacturers of PC’s with at least three different speeds.

ANSWER

 

 

k) Find the manufacturers who sell exactly three different models of PC.

ANSWER

 


 

P.59 exercise 5.1

Exercise 5.1: Express the following constraints about the relations of Exer- cise 3.1, reproduced here:

     Product(maker, model, type)
     PC(model, speed, ram, hd, price)
     Laptop(model, speed, ram, hd, screen, price)
     Printer(model, color, type, price)

You may write your constraints either as containments or by equating an ex- pression to the empty set. For the data of Exercise 4.1, indicate any violations to your constraints.

 

a) A PC with a processor speed less than 2.00 must not sell for more than $500.

b) A laptop with a screen size less than 15.4 inches must have at least a 100 gigabyte hard disk or sell for less than $1000.

 

c) No manufacturer of PC’s may also make laptops.

 

 

 

반응형