Reputation: 405
How can one subtract relations that have a different number of attributes and tuples?
For example, if I have a relation A that has 3 attributes and 10 tuples, and relation B that has 2 attributes and 5 tuples, are A-B and B-A possible?
Upvotes: -1
Views: 13214
Reputation: 2806
you don't mention whether your two relations have any attributes in common. As Thanatos says: strictly speaking for relational MINUS, there must be the same set of attributes.
But think about Natural Join: this 'matches' tuples which have the same values on the attributes in common. Then there's an operation (sometimes called 'antijoin' (▷) http://en.wikipedia.org/wiki/Relational_algebra#Antijoin_.28.E2.96.B7.29 ) which returns tuples from the left argument that don't match by values of the attributes in common.
In the case where the relations have the same attributes (union-compatible), antijoin is the same as MINUS.
The result of antijoin has the same attributes as the left argument. So yes you can get both A ▷ B and B ▷ A (that is, putting antijoin in place of minus); the results will have differing headings as well as differing tuples.
Antijoin is roughly equivalent to SQL NOT EXISTS (selecting on the attributes in common being equal). Some dialects of SQL have an operator EXCEPT. This doco compares EXCEPT and NOT EXISTS: http://download.oracle.com/otndocs/products/rdb/pdf/tech_archive/except_intersect_minus_ops.pdf But (as usual and unlike the RA) SQL does not link tables by same-named attributes.
You get linking by column name with EXCEPT CORRESPONDING -- but read that document carefully! SQL does not do what you would expect from RA.
For interest: Tutorial D's name for antijoin is NOT MATCHING -- which just about says it all.
Upvotes: 2
Reputation: 44256
See Wikipedia:
The relational algebra uses set union, set difference, and Cartesian product from set theory, but adds additional constraints to these operators.
For set union and set difference, the two relations involved must be union-compatible—that is, the two relations must have the same set of attributes.
In your case, basically no. This is due to the different attributes: how would you even define "subtraction" across two sets of essentially different things?
If you have some criteria / method for how the subtraction should work, it's likely that relational algebra can express what you want to do; set subtraction may just not be the right tool.
Upvotes: 0