relational-algebra

Relational Algebra equivalent of SQL “NOT IN”

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 09:55:32
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 In relational algebra, you can do this using a carthesian product. Something like: R - ρ a1,a2 (π a11,a21 (σ A11 = A22 (ρ a11,a21 (R) x ρ a12, a22 (R)))) rename the

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

时光怂恿深爱的人放手 提交于 2019-12-04 17:13:09
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]." * ( stackoverflow.com retrieved on Feb 12 at 9:01PM). I understand how the equi-join can be seen as a subset of theta-join. They both join and they both use a fixed number of attributes, but the equi-join is restricted to using

Unique constraint over multiple tables

时光怂恿深爱的人放手 提交于 2019-12-04 04:23:07
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 references a Parent having some value of aId ? Stated another way can I created a unique constraint so that the

Natural join if no common attributes

空扰寡人 提交于 2019-12-04 00:57:49
What will natural join return in relational algebra if tables don't have attributes with same names? Will it be null or the same as cross-product (Cartesian operator)? If there are no attributes in common between two relations and you perform a natural join , it will return the cartesian product of the two relations. A cartesian product of two tables will be returned.This is because when we perform any JOIN operation on two tables a cartesian product of those tables is performed and then based on any select condition in WHERE clause the resultant rows are returned.But here as there are no

Clear explanation of the “theta join” in relational algebra?

半城伤御伤魂 提交于 2019-12-03 09:28:59
问题 I'm looking for a clear, basic explanation of the concept of theta join in relational algebra and perhaps an example (using SQL perhaps) to illustrate its usage. If I understand it correctly, the theta join is a natural join with a condition added in. So, whereas the natural join enforces equality between attributes of the same name (and removes the duplicate?), the theta join does the same thing but adds in a condition. Do I have this right? Any clear explanation, in simple terms (for a non

How to find all pizzerias that serve every pizza eaten by people over 30?

a 夏天 提交于 2019-12-02 22:48:40
I'm following the Stanford Database course and there's a question where we have Find all pizzerias that serve every pizza eaten by people over 30 using Relational Algebra only. The problem consist of a small database with four relations: Person(name, age, gender) // name is a key Frequents(name, pizzeria) // [name,pizzeria] is a key Eats(name, pizza) // [name,pizza] is a key Serves(pizzeria, pizza, price) // [pizzeria,pizza] is a key I know how to find which pizza's people over 30 eat and make a cross-product of them, so I could check which pizzeria has both. I can make a list of all the

Find the names of students who are not enrolled in any course - Students, Faculty, Courses, Offerings, Enrolled

拥有回忆 提交于 2019-12-02 22:41:31
问题 Given the database below, project the names of the students who are not enrolled in a course using relational algebra. Students(snum, sname, major, standing, age, gpa) Faculty(fid, fname, deptid) Courses(cnum, cname, course_level, credits) Offerings(onum, cnum, day, starttime, endtime, room, max_occupancy, fid) Enrolled(snum, onum) I can get the snum of all students not enrolled in a course with: π snum Students - π snum Enrolled But how do I project the sname of the student with the snums

Lossless Join and Decomposition From Functional Dependencies

放肆的年华 提交于 2019-12-02 21:16:57
Suppose the relation R( K, L, M, N, P) , and the functional dependencies that hold on R are: - L -> P - MP -> K - KM -> P - LM -> N Suppose we decompose it into 3 relations as follows: - R1(K, L, M) - R2(L, M, N) - R3(K, M, P) How can we tell whether this decomposition is lossless? I used this example R1 ∩ R2 = {L, M}, R2 ∩ R3 = {M}, R1 ∩ R3 = {K,M} we use functional dependencies, and this is not lossless in my opinion, but a little bit confused. It helps if we demystify the concept of lossless decomposition a bit: it really just means that joining R1, R2 and R3 should yield the original R. Do

finding max value among two table without using max function in relational algebra

一个人想着一个人 提交于 2019-12-02 13:14:56
Suppose I have two tables A{int m} and B{int m} and I have to find maximum m among two tables using relational algebra but I cannot use max function.How can I do it?I think using join we can do it but i am not sure if my guess is correct or not. Note: this is an interview question. AntC Hmm, I'm puzzled why the question involves two tables. For the question as asked, I would just UNION the two (as StilesCrisis has done), then solve for a single table. So: how to find the maximum m in a table using only NatJOIN? This is a simplified version of finding the top node on a table that holds a

Find the names of students who are not enrolled in any course - Students, Faculty, Courses, Offerings, Enrolled

梦想的初衷 提交于 2019-12-02 12:54:10
Given the database below, project the names of the students who are not enrolled in a course using relational algebra. Students(snum, sname, major, standing, age, gpa) Faculty(fid, fname, deptid) Courses(cnum, cname, course_level, credits) Offerings(onum, cnum, day, starttime, endtime, room, max_occupancy, fid) Enrolled(snum, onum) I can get the snum of all students not enrolled in a course with: π snum Students - π snum Enrolled But how do I project the sname of the student with the snums that I find? Every base table holds the rows that make a true proposition (statement) from some