relational-algebra

Equivalent of GroupBy and Having clause in Relational Algebra

╄→尐↘猪︶ㄣ 提交于 2019-12-10 21:00:03
问题 I was doing an assignment where I had to convert SQL queries into Relational Algebra queries. I got stuck in converting the group by clause. Could anyone please tell me how the group by clause can be written in relational algebra? e.g.: SELECT job, sal FROM emp GROUP BY job ; Thanks! 回答1: First of all your query is wrong you cannot select something that you did not group unless you use aggregation. I assume you want to get sum of the sal. job F sum(sal), job(emp). 回答2: Noting you want to get

SQL -> Relational Algebra

旧巷老猫 提交于 2019-12-10 17:50:50
问题 Suppose I have the following relations: Branch (branchNo(PK), street, city, postcode) Staff (staffNo(PK), fName, lName, sex, branchNo(FK)) Not that it matters for this question, but PK = primary key & FK = foreign key How would I write the relational algebra for the following query: List the names of all female staff that work in Glasgow. My attempt: σ Staff.sex=F & Branch.city = Glasgow (π fName, lName, sex, branchNo (Staff) x π city, branchNo (Branch)) I know that my selection (σ) statement

Unique constraint over multiple tables

孤者浪人 提交于 2019-12-09 16:24:11
问题 Let's say we have these tables: CREATE TABLE A ( id SERIAL NOT NULL PRIMARY KEY ); CREATE TABLE B ( id SERIAL NOT NULL PRIMARY KEY ); CREATE TABLE Parent ( id SERIAL NOT NULL PRIMARY KEY, aId INTEGER NOT NULL REFERENCES A (id), bId INTEGER NOT NULL REFERENCES B (id), UNIQUE(aId, bId) ); CREATE TABLE Child ( parentId INTEGER NOT NULL REFERENCES Parent (id), createdOn TIMESTAMP NOT NULL ); Is it possible to create a unique constraint on Child such that for all rows in Child at most one

Relational Algebra - Cartesian Product vs Natural Join?

↘锁芯ラ 提交于 2019-12-09 11:24:49
问题 I am studying for exams and am failing to find a solid criteria by which I can determine if the Cartesian Product x is to be used or if Natural Join |X| is to be used. I had come up with a rough guide that: "If you need to project an attribute that has the same name as an attribute in the table to be joined you must use x and state the table names to be projected: tableA.colname1 = tableB.colname1 " This however doesn't follow some of the solutions in my notes and my lecturer seems to use x

Writing the following query using relational algebra

心已入冬 提交于 2019-12-08 14:12:09
问题 I am trying to write the following query using the relational algebra: "Find the names of sailors who reserved a red or a green boat" Here is my solution: But the book gives the following solution: And another equivalent solution given by book: End here is the related tables: My question is, can't we use the "or" symbol (V) in selection operator? Is my solution wrong? Why do we need renaming operator? Thanks 回答1: Yet more proof that there is no such thing as "the" relational algebra. As for

Relational Algebra Cross Product and Natural Join

我的未来我决定 提交于 2019-12-08 13:07:35
问题 I'm a little confused on when to use both of these operators. I feel like I understand them but I have a hard time figuring out when I would need them in a relational algebraic statement. Can someone give me some insight/advice on this? 回答1: Some versions of the relational algebra have relation headings that are sets of (unordered, uniquely named) attributes. Then (relational (Cartesian)) PRODUCT, aka CROSS JOIN, aka CROSS PRODUCT, is defined only when the input relations share no attribute

Relational Algebra rule for column transformation

爷,独闯天下 提交于 2019-12-08 10:54:12
问题 What is the rule for the transformation of a column in Relational Algebra ? For example, I want to divide all values of a column with the average of that column. I can get average using aggregate rule. But cannot find the rule for column manipulation. P.S: I am interested in the rule (like \Pi is used for projection). 回答1: There's no standard approach to this. Also there's no single relational algebra, so you should give a reference to yours. Suppose you supply the division operator on values

Relational Algebra equivalent of SQL “NOT IN”

删除回忆录丶 提交于 2019-12-07 05:22:01
问题 Is there a relational algebra equivalent of the SQL expression NOT IN ? For example if I have the relation: A1 | A2 ---------- x | y a | b y | x I want to remove all tuples in the relation for which A1 is in A2. In SQL I might query: SELECT * FROM R WHERE R.A1 NOT IN ( SELECT A2 FROM R ) / What is really stumping me is how to subquery inside the relational algebra selection operator, is this possible?: σ some subquery here R 回答1: In relational algebra, you can do this using a carthesian

How to compute a natural join?

試著忘記壹切 提交于 2019-12-06 14:29:43
Could someone explain to me what is going on here and how to solve this problem? Suppose relation R(A,B) has the tuples: A B 1 2 3 4 5 6 and the relation S(B,C,D) has tuples: B C D 2 4 6 4 6 8 4 7 9 Compute the natural join of R and S. Then, identify which of the following tuples is in the natural join R |><| S. You may assume each tuple has schema (A,B,C,D). I don't know what a natural join truly means. Can you explain it to me? A natural join is joining ("sticking together") elements from two relations where there is a match. In this example (1, 2 ) matches ( 2 , 4, 6) so you get (1, 2, 4, 6

can the natural-join be seen as a subset of the equi-join and theta-join?

空扰寡人 提交于 2019-12-06 11:39:23
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 6 years ago . I have a question regarding relational algebra and how the theta-join, equi-join, and natural-join can be classified in relation to each other. In a comment on stackoverflow.com sqlvogel quotes E.F. Codd, the computer scientist who invented the relational model for database management, "[the] Natural[-join] is a subset of Equi[-join] which is a subset of Theta[-join]." *