Skip to content

Instantly share code, notes, and snippets.

@miporto
Last active November 28, 2022 01:04
Show Gist options
  • Save miporto/01d443e83269c555baa435cf48eaaf76 to your computer and use it in GitHub Desktop.
Save miporto/01d443e83269c555baa435cf48eaaf76 to your computer and use it in GitHub Desktop.
Ejercicios de algebra relacional

Database Systems The Complete Book 2nd Edition Exercise 2.4.1 Page 52-55

  1. What PC models have speed of at least 3.00?
π model (σ speed ≥ 3 (PC))
  1. Which manufacturers make laptops with a hard disk of at least 100GB?
S1 = π model (σ hd ≥ 100 Laptop)
π maker (Product ⨝ S1)
  1. Find the model number and price of all products (of any type) made by manufacturer B.
S1 = π model (σ maker = 'B' Product)
PCM = π model, price (S1 ⨝ PC)
LM = π model, price (S1 ⨝ Laptop)
PM = π model, price (S1 ⨝ Printer)
PCM ∪ LM ∪ PM
  1. Find the model number of all color laser printers.
π model (σ type = 'laser' Printer)
  1. Find those manufacturers that sell Laptops, but not PC's.
laptop_man = π maker ((π model Laptop) ⨝ Product)
pc_man = π maker ((π model PC) ⨝ Product)
π maker (laptop_man - pc_man)
  1. Find those hard-disks sizes that occur in two or more PC's.
π PC1.hd (
  σ PC1.hd = PC.hd (
    (ρ PC1 (PC) ⨯ PC)
  )
)

Database Systems The Complete Book 2nd Edition Exercise 2.4.1 Page 55-57

  1. Give the class names and countries of the classes that carried guns of at least 16-inch bore.
π class, country (σ bore ≥ 16 Classes)
  1. Find the ships launched prior to 1921.
σ launched < 1921 Ships
  1. Find the ships sunk in the battle of Denmark Strait.
π ship (σ battle = 'Denmark Strait' ∧ result = 'sunk' Outcomes)
  1. List the ships heavier than 35,000 tons.
V = π class (σ displacement > 35000 Classes)
Ships ⨝ V
  1. List the name, displacement and number of guns of the ships engaged in the battle of Guadalcanal.
S = ρ name←ship (π ship (σ battle = 'Guadalcanal' Outcomes))
S2 = S ⨝ Ships
π name, displacement, numGuns (S2 ⨝ Classes)
  1. List all the capital ships (bc) listed in the database.
C = π class (σ type = 'bc' Classes)
C ⨝ Ships
  1. Find those countries that had both battleships and battlecruisers.
BB = π country (σ type = 'bb' Classes)
BC = π country (σ type = 'bc' Classes)
BB ∩ BC
  1. Find those ships that where damaged in one battle, but later fought in another.
-- No esta del todo bien, algunos edge cases no los contempla (el barco se dañe en dos batallas consecutivas)
D = ρ D (π ship, battle (σ result = 'damaged' Outcomes))
BDATES = ρ BDATES (π date (σ D.battle = Battles.name (D ⨯ Battles)))
PB = π name (σ Battles.date > BDATES.date (BDATES ⨯ Battles))
VALIDBATTLES = σ Outcomes.battle = Battles.name (Outcomes ⨯ PB)
π D.ship (σ D.ship = Outcomes.ship ∧ result ≠ 'damaged' (D ⨯ VALIDBATTLES))

World Cup 2010

  1. Liste los países con los que jugó la Selección Argentina.
S1 = (π away (
  σ NationalTeam.id = Match.home ∧ NationalTeam.name = 'Argentina' (NationalTeam ⨯ Match))
) 
∪ 
(π home (
  σ NationalTeam.id = Match.away ∧ NationalTeam.name = 'Argentina' (NationalTeam ⨯ Match))
)
π name (σ away = id (S1 ⨯ NationalTeam))
  1. Obtenga el listado de los nombres de los jugadores de la Selección Argentina.
π Player.name (
  σ Player.national_team = NationalTeam.id ∧ NationalTeam.name = 'Argentina' (Player ⨯ NationalTeam)
)
  1. Liste el nombre de los continentes que no fueron representados por ningún equipo en los cuartos de final del Mundial.
QF = π home, away, stage (σ stage = Stage.id ∧ name = 'Quarter-finals' (Match ⨯ Stage))
CNT = ρ id←continent (π continent (σ home = id ∨ away = id (NationalTeam ⨯ QF)))
Continent - (CNT ⨝ Continent)
  1. Liste el nombre del equipo ganador de la final del Mundial
FT = π Match.id, home, away (σ Match.stage = Stage.id ∧ Stage.name = 'Final' (Match ⨯ Stage))
HG = ρ team←home (π home (σ match_id = id ∧ team_id = home (FT ⨯ Score)))
AG = ρ team←away (π away (σ match_id = id ∧ team_id = away (FT ⨯ Score)))
HG ∪ AG
  1. Liste el nombre y selección nacional de el/los jugadores más altos del Mundial.
H = π height Player
MH = ρ MH (H - (π H1.height (σ H1.height < H2.height ((ρ H1 H) ⨯ (ρ H2 H)))))
P = ρ P (π name, national_team (Player ⨝ MH))
π P.name, NationalTeam.name (σ P.national_team = NationalTeam.id (P ⨯ NationalTeam))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment