*Set Operations on Relations*

The three most common operations on sets are union, intersection, and difference. We assume the reader is familiar with these operations, which are described as follows on arbitrary sets R and S:

● R U S, the union of R and S, is the set of elements that are in R or S or both. An element appears only once in the union even if it is present in both R and S.

● R S, the intersection of R and S. is the set of elements that are in both R and S.

● R - S, the difference of R and S, is the set of elements that are in R but not in S. Note that R - S is different from S - R; the latter is the set of elements that are in S but not in R.

When we apply these operations to relations, we need to put some conditions on R and S.

1. R and S must have schemas with identical sets of attributes, and the types (domains) for each attribute must be the same in R and S.

2. Before we compute the set-theoretic union, intersection, or difference of sets of tuples, the columns of R and S must be ordered so that the order of attributes is the same for both relations.

Occasionally we would like to take the union, intersection, or difference of relations that have the same number of attributes, with corresponding domains, but that use different names for their attributes. If so, we may use the renaming operator to be discussed in "Renaming" to change the schema of one or both relations and give them the same set of attributes.

**Example (a):**Assume we have the two relations R and S, instances of the relation MovieStar of "Relational Algebra". Current instances of R and S are shown in Figure (a). Then the union R U S is

Note that the two tuples for Carrie Fisher from the two relations appear only once in the result.

The intersection R S is

Now, only the Carrie Fisher tuple appears, because only it is in both relations.

The difference R - S is

That is, the Fisher and Hamill tuples appear in R and thus are candidates for R - S. On the other hand, the Fisher tuple also appears in S and so is not in R - S.

### Projection

The projection operator is used to create from a relation R a new relation that has only some of R's columns. The value of expression is a relation that has only the columns for attributes **A**of R. The schema for the resulting value is the set of attributes

_{1}, A_{2},. . . , A_{n}**{A**, which we conventionally show in the order listed.

_{1}, A_{2},. . . , A_{n}}**Example (b):**Take a look at the relation Movie with the relation schema explained in "Relational Algebra". An instance of this relation is shown in Figure (b). We can project this relation onto the first three attributes with the expression

The resulting relation is

As another instance, we can project onto the attribute inColor with the expression

The result is the single-column relation

Note that there is only one tuple in the resulting relation, since all three tuples of Figure (b) have the same value in their component for attribute inColor, and in the relational algebra of sets, duplicate tuples are always removed.

### Tags

- attributes
- tuples
- schema
- relational algebra
- Scrolling Cursors
- Protecting Against Concurrent Updates
- Modifications by Cursor
- Cursors
- Instead-Of Triggers
- Triggers in SQL
- Schema-Level Constraints and Triggers
- Tuple-Based CHECK Constraints
- Constraints on Attributes and Tuples
- Declaring Foreign-Key Constraints
- Keys Declared With UNIQUE
- Constraints and Triggers
- Interpreting Queries Involving Views
- Modifying Views
- View Definitions
- Introduction to Selection of Indexes
- Default Values / Indexes
- Simple Table Declarations
- Defining a Relation Schema in SQL
- Deletion / Updates
- Database Modifications
- Grouping / HAVING Clauses
- Full-Relation Operations
- Natural Joins / Outerjoins
- Subqueries in FROM Clauses
- Conditions Involving Tuples
- Subqueries
- Union, Intersection, and Difference of Queries
- Interpreting Multirelation Queries
- Tuple Variables
- Disambiguating Attributes
- Queries Involving More Than One Relation
- Null Values and Comparisons Involving NULL
- Selection in SQL
- Projection in SQL
- The Database Language SQL
- Additional Constraint Examples
- Referential Integrity Constraints
- Constraints on Relations
- Extending the Projection Operator
- Grouping
- Extended Operators of Relational Algebra
- Selection on Bags / Product of Bags / Joins of Bags
- Union, Intersection, and Difference of Bags
- Relational Operations on Bags
- A Linear Notation for Algebraic Expressions
- Dependent and Independent Operations
- Renaming
- Combining Operations to Form Queries
- Natural Joins / Theta-Joins
- Selection / Cartesian Product
- An Algebra of Relational Operations
- Relational Algebra
- Attribute Lists
- Document Type Definitions
- XML and Its Data Model
- Information Integration Via Semistructured Data
- Semistructured Data Representation
- Object-Oriented Versus Object-Relational
- References
- Nested Relations
- The Object-Relational Model
- What If There Is No Key
- Representing ODL Relationships
- Representing Other Type Constructors
- Representing Set-Valued Attributes
- Nonatomic Attributes in Classes
- Declaring Keys in ODL
- Subclasses in ODL / Multiple Inheritance in ODL
- Types in ODL
- Methods in ODL
- Multiplicity of Relationships
- Relationships in ODL / Inverse Relationships
- Attributes in ODL
- Introduction to ODL
- The Type System
- Relationships Among Normal Forms
- Decomposition into Fourth Normal Form
- Reasoning About Multivalued Dependencies
- Definition of Multivalued Dependencies
- Multivalued Dependencies
- Third Normal Form
- Boyce-Codd Normal Form
- Decomposing Relations
- Projecting Functional Dependencies
- Closing Sets of Functional Dependencies
- The Transitive Rule
- Why the Closure Algorithm Works
- Computing the Closure of Attributes
- Trivial Functional Dependencies
- The Splitting/Combining Rule
- Rules About Functional Dependencies
- Keys of Relations
- Using Null Values to Combine Relations - Comparison of Approaches
- An Object-Oriented Approach
- Converting Subclass Structures to Relations
- Handling Weak Entity Sets
- Combining Relations
- From E/R Relationships to Relations
- From Entity Sets to Relations
- From E/R Diagrams to Relational Designs
- Relation Instances
- Equivalent Representations of a Relation
- Tuples / Domains
- Attributes / Schemas
- The Relational Data Model
- Summary of The Entity-Relationship Data Model
- Weak Entity Set Notation
- Requirements for Weak Entity Sets
- Representing Keys in the E/R Model
- Keys in the E/R Model
- The Modeling of Constraints
- Picking the Right Kind of Element
- Design Principles
- Subclasses in the E/R Model
- Converting Multiway Relationships to Binary
- Attributes on Relationships
- Multiway Relationships
- Elements of the E/R Model
- The Entity-Relationship Data Model
- Database System Implementation
- Database Programming
- Database Design
- The Query Processor
- Multimedia Data
- Relational Database Systems