SQL and Relational Algebra queries


This section asks you to write some queries in Relational Algebra and SQL.Your SQL queries will be graded by running them in MySQL on test cases; queries that return the wrong answer will receive 0 points, i.e. there is no partial credit. In particular, please make sure your query returns the right column(s).

Consider the schema given by the following two SQLCREATEstatements.

   
  CREATE TABLE SuppInfo (suppid INTEGER NOT NULL,
  
prodid INTEGER NOT NULL,
PRIMARY KEY (suppid, prodid));

CREATE TABLE Purchases (purchaseid INTEGER PRIMARY KEY,
custid INTEGER,
prodid INTEGER,
purchasemethod INTEGER);

This schema comes from a simple retail setting. The rst table keeps track of which suppliers supply which products, and each (sno, pno) pair states that supplier number sno suppliers product number pno. The second table keeps track of customer purchases { a purchase has an ID, a customer ID and a ag to specify whether the purchase was in person, online or over the phone. Write the following queries over the above schema.For the SQL portion, each of your answers must be a single SQL query.

(a) Find all the pairs of suppids for suppliers that supply the exact same products. Each pair of suppids should appear only once in the answer. Give queries in relational algebra (4 points) and SQL (4 points).
(b) Find custids of all customers who made purchases usingexactly two di erent methods. E.g. if a customer made three purchases { one over the phone and two in person, their record is included in the answer. But if they made two purchases, both online, their record is not included. Give queries in relational algebra (4 points), SQL using GROUP BY(4 points) and SQL without using GROUP BY(4 points).

国外一所普通大学计算机大二的数据库家庭作业,大牛们不要小看,试试你的解题能力吧!

sql mysql

下雨没雨伞 11 years, 5 months ago

relational algebra这么高深的玩意就不做了.

mysql:

   
  题一:
  
select prodid, group_concat(suppid) from SuppInfo group by prodid;

题二:
select custid from Purchases group by custid having count(distinct purchasemethod)>1;
select custid from Purchases P1 where exists (select 1 from Purchases P2 where P1.purchasemethod <> P2.purchasemethod );

阿拉玛港分 answered 11 years, 5 months ago

Your Answer