Lecture Slides available: PDF PowerPoint Relational AlgebraContents
In order to implement a DBMS, there must exist a set of rules which state how the database system will behave. For instance, somewhere in the DBMS must be a set of statements which indicate than when someone inserts data into a row of a relation, it has the effect which the user expects. One way to specify this is to use words to write an `essay' as to how the DBMS will operate, but words tend to be imprecise and open to interpretation. Instead, relational databases are more usually defined using Relational Algebra. Relational Algebra is :
Operators in relational algebra are not necessarily the same as SQL operators, even if they have the same name. For example, the SELECT statement exists in SQL, and also exists in relational algebra. These two uses of SELECT are not the same. The DBMS must take whatever SQL statements the user types in and translate them into relational algebra operations before applying them to the database. Terminology
Operators - Write
Operators - RetrievalThere are two groups of operations:
Relational SELECTSELECT is used to obtain a subset of the tuples of a relation that satisfy a select condition. For example, find all employees born after 1st Jan 1950: SELECTdob '01/JAN/1950'(employee) Relational PROJECTThe PROJECT operation is used to select a subset of the attributes of a relation by specifying the names of the required attributes. For example, to get a list of all employees surnames and employee numbers: PROJECTsurname,empno(employee) SELECT and PROJECTSELECT and PROJECT can be combined together. For example, to get a list of employee numbers for employees in department number 1:
Set Operations - semanticsConsider two relations R and S.
SET Operations - requirementsFor set operations to function correctly the relations R and S must be union compatible. Two relations are union compatible if
UNION Example
INTERSECTION Example
DIFFERENCE Example
CARTESIAN PRODUCTThe Cartesian Product is also an operator which works on two sets. It is sometimes called the CROSS PRODUCT or CROSS JOIN. It combines the tuples of one relation with all the tuples of the other relation. CARTESIAN PRODUCT example
JOIN OperatorJOIN is used to combine related tuples from two relations:
The notation used is R JOINjoin condition S JOIN Example
Natural JoinInvariably the JOIN involves an equality test, and thus is often described as an equi-join. Such joins result in two attributes in the resulting relation having exactly the same value. A `natural join' will remove the duplicate attribute(s).
OUTER JOINsNotice that much of the data is lost when applying a join to two relations. In some cases this lost data might hold useful information. An outer join retains the information that would have been lost from the tables, replacing missing data with nulls. There are three forms of the outer join, depending on which data is to be kept.
OUTER JOIN example 1
OUTER JOIN example 2
|
|