我正在完成斯坦福大学的关系代数课程,并且很难理解其中一个问题,尽管我已经得到了答案。
这是架构:
/* Delete the tables if they already exist */
drop table if exists Person;
drop table if exists Frequents;
drop table if exists Eats;
drop table if exists Serves;
/* Create the schema for our tables */
create table Person(name text, age int, gender text);
create table Frequents(name text, pizzeria text);
create table Eats(name text, pizza text);
create table Serves(pizzeria text, pizza text, price decimal);
/* Populate the tables with our data */
insert into Person values('Amy', 16, 'female');
insert into Person values('Ben', 21, 'male');
insert into Person values('Cal', 33, 'male');
insert into Person values('Dan', 13, 'male');
insert into Person values('Eli', 45, 'male');
insert into Person values('Fay', 21, 'female');
insert into Person values('Gus', 24, 'male');
insert into Person values('Hil', 30, 'female');
insert into Person values('Ian', 18, 'male');
insert into Frequents values('Amy', 'Pizza Hut');
insert into Frequents values('Ben', 'Pizza Hut');
insert into Frequents values('Ben', 'Chicago Pizza');
insert into Frequents values('Cal', 'Straw Hat');
insert into Frequents values('Cal', 'New York Pizza');
insert into Frequents values('Dan', 'Straw Hat');
insert into Frequents values('Dan', 'New York Pizza');
insert into Frequents values('Eli', 'Straw Hat');
insert into Frequents values('Eli', 'Chicago Pizza');
insert into Frequents values('Fay', 'Dominos');
insert into Frequents values('Fay', 'Little Caesars');
insert into Frequents values('Gus', 'Chicago Pizza');
insert into Frequents values('Gus', 'Pizza Hut');
insert into Frequents values('Hil', 'Dominos');
insert into Frequents values('Hil', 'Straw Hat');
insert into Frequents values('Hil', 'Pizza Hut');
insert into Frequents values('Ian', 'New York Pizza');
insert into Frequents values('Ian', 'Straw Hat');
insert into Frequents values('Ian', 'Dominos');
insert into Eats values('Amy', 'pepperoni');
insert into Eats values('Amy', 'mushroom');
insert into Eats values('Ben', 'pepperoni');
insert into Eats values('Ben', 'cheese');
insert into Eats values('Cal', 'supreme');
insert into Eats values('Dan', 'pepperoni');
insert into Eats values('Dan', 'cheese');
insert into Eats values('Dan', 'sausage');
insert into Eats values('Dan', 'supreme');
insert into Eats values('Dan', 'mushroom');
insert into Eats values('Eli', 'supreme');
insert into Eats values('Eli', 'cheese');
insert into Eats values('Fay', 'mushroom');
insert into Eats values('Gus', 'mushroom');
insert into Eats values('Gus', 'supreme');
insert into Eats values('Gus', 'cheese');
insert into Eats values('Hil', 'supreme');
insert into Eats values('Hil', 'cheese');
insert into Eats values('Ian', 'supreme');
insert into Eats values('Ian', 'pepperoni');
insert into Serves values('Pizza Hut', 'pepperoni', 12);
insert into Serves values('Pizza Hut', 'sausage', 12);
insert into Serves values('Pizza Hut', 'cheese', 9);
insert into Serves values('Pizza Hut', 'supreme', 12);
insert into Serves values('Little Caesars', 'pepperoni', 9.75);
insert into Serves values('Little Caesars', 'sausage', 9.5);
insert into Serves values('Little Caesars', 'cheese', 7);
insert into Serves values('Little Caesars', 'mushroom', 9.25);
insert into Serves values('Dominos', 'cheese', 9.75);
insert into Serves values('Dominos', 'mushroom', 11);
insert into Serves values('Straw Hat', 'pepperoni', 8);
insert into Serves values('Straw Hat', 'cheese', 9.25);
insert into Serves values('Straw Hat', 'sausage', 9.75);
insert into Serves values('New York Pizza', 'pepperoni', 8);
insert into Serves values('New York Pizza', 'cheese', 7);
insert into Serves values('New York Pizza', 'supreme', 8.5);
insert into Serves values('Chicago Pizza', 'cheese', 7.75);
insert into Serves values('Chicago Pizza', 'supreme', 8.5);

我正在努力绕过问题6.问题是:查找仅由24岁以下的人食用的所有比萨饼,或者在他们所服务的地方花费不到10美元的所有比萨饼。
现在,我了解查询。它基本上分为两部分:
我甚至理解答案,即(所有比萨饼和比萨饼之间的差价大于等于10美元的差异)和(人们吃掉的所有比萨饼和大于等于24岁的人吃掉的比萨饼之间的差异)年龄)
(\project_{pizza} (Serves) \diff \project_{pizza} (\select_{price>='10'} Serves))
\union
(\project_{pizza} (Eats) \diff \project_{pizza} (\select_{age>='24'} Person \join Eats))
然而,我无法理解为什么相反的查询没有给出相同的答案,即(比萨饼价格低于10美元)和(比24岁以下的人吃掉的比萨饼)。
(\project_{pizza} (\select_{price<'10'} Serves))
\union
(\project_{pizza} (\select_{age<'24'} Person \join Eats))
为什么第二个查询给出的答案与第一个不同。据我说,他们都应该给出相同的答案。
所有回复都表示赞赏。
答案 0 :(得分:3)
您的第一个查询找到了
的比萨饼不花费超过10美元任何地方, 任何年龄在24岁或以上的人士不会被
当你的上一个查询鳍比萨饼
时售价,价格低于10美元某处,或者 被一些 24岁以下的人吃掉。
这些查询会给比萨饼带来不同的效果,这些比萨饼在某些地方售价低于10美元,其他地方售价超过10美元,或者被24岁以下的人和一些年龄较大的人吃掉。