Branch and Staff (part) Relations Relational Model Relational Model - 1 Examples of Attribute Domains Relational Model - 2 Relational Model Terminology o A relation is a table with columns and rows. Only applies to logical structure of the database, not the physical structure. o Attribute is a named column of a relation. o Domain is a set of allowable values for one or more attributes. Relational Model - 3 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 4 Relational Model Terminology Alternative Terminology (Relational Model) o Tuple is a row of a relation. o Degree is a number of attributes in a relation. o Cardinality is a number of tuples in a relation. o Relational Database is a collection of normalized relations. Relational Model - 5 Mathematical Relations Mathematical Relation Mathematical definition of relation – Consider two sets, D1 and D2, where D1 = {2, 4} and D2 = {1, 3, 5}. – Cartesian product is D1 × D2, the set of all ordered pairs, first element is member of D1 and second element is member of D2. – Alternative, find all combinations of elements with first from D1 and second from D2. D1 ×=D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)} Relational Model - 7 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 6 Any subset of Cartesian product is a relation. Eg., – R = {(2, 1), (4, 1)} – Using same sets, form another relation, S, where first element is always twice the second. S = {(x, y) | x ∈D1, y ∈D2, and x = 2y} – Only one ordered pair in the Cartesian Product satisfies this condition. S = {(2, 1)} Relational Model - 8 Mathematical Relation Mathematical Relation o Consider three sets D1, D2, and D3 with Cartesian Product D1 × D2 × D3. o To define a general relation on n domains, let D1, D2, . . ., Dn be n sets with Cartesian product defined as D1 × D2 ×=. . . ×=Dn = {(d1, d2, . . . , dn) | d1 ∈D1, d2 ∈D2, . . . , dn∈Dn} D1 = {1, 3} D2 = {2, 4} D3 = {5, 6} D1 × D2 × D3 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6), (3,2,5), (3,2,6), (3,4,5), (3,4,6)} usually written as n XD i o Any subset of these ordered triples is a relation. i=1 o In defining relations we specify the sets, or domains, from which we chose values. Relational Model - 9 Relational Model - 10 Properties of Relations Relational Keys o Relation name is distinct from all other relations. o Superkey (Key) – An attribute or a set of attributes that uniquely identifies a tuple within a relation. o Each cell of relation contains exactly one atomic (single) value. o Candidate Key o Each attribute has a distinct name. o Values of an attribute are all from the same domain. – A superkey (K) such that no proper subset is a superkey within the relation. o Order of attributes has no significance. – In each tuple of R, the values of K uniquely identify that tuple (uniqueness). o Each tuple is distinct; there are no duplicate tuples. – No proper subset of K has the uniqueness property (irreducicility). o Order of tuples has no significance, theoretically. Relational Model - 11 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 12 Relational Keys Relational Integrity o Primary Key – Candidate key selected to identify tuples uniquely within relation. o Null o Alternate Keys – Candidate keys that are not selected to be the primary key. o Foreign Key – An attribute or set of attributes within one relation that matches candidate key of some (possibly same) relation. – Represents a value for an attribute that is currently unknown or is not applicable for this tuple. – Deals with incomplete or exceptional data. – Null represents the absence of a value and is not the same as zero or spaces, which are values. Relational Model - 13 Relational Model - 14 Relational Integrity Relational Integrity 2 principal rules for the relational model: Enterprise Constraints o Entity Integrity – In a base relation, no attribute of a primary key can be null. o Referential Integrity – Additional rules specified by users or database administrators. – Eg., the maximum number of staff in a branch. – If foreign key exists in a relation, either the foreign key value must match a candidate key value of some tuple in its home relation or foreign key value must be wholly null. Relational Model - 15 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 16 Relational Algebra Relational Algebra o Relational algebra operations work on one or more relations to define another relation without changing the original relations. o 5 basic operations in relational algebra: Selection, Projection, Cartesian Product, Union, and Set Difference. o Thus, both operands and results are relations, so output from one operation can become input to another operation. o Performs most of the data retrieval operations needed. o This allows expressions to be nested, just as in arithmetic. This property is called closure. o Also have Join, Intersection, and Division operations that can be expressed in terms of 5 basic operations. Relational Model - 17 Relational Algebra Operations Relational Algebra Operations Relational Model - 19 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 18 Relational Model - 20 Selection Example - Selection Selection Operation o List all staff with a salary greater than £10,000: σsalary > 10000 (Staff) σpredicate (R) works on a single relation R and defines a relation that contains only those tuples (rows) of R that satisfy the specified condition (predicate). Relational Model - 21 Projection Relational Model - 22 Example - Projection o Produce a list of salaries for all staff, showing only the Sno, FName, LName, and Salary details: Projection Operation Π sno, fname, lname, salary(Staff) Πcol1, . . . , coln(R) works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates. Relational Model - 23 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 24 Cartesian Product Example - Cartesian Product o List the names and comments of all renters who have viewed a property. Π rno, pno,comment (Viewing)) (Π Π rno, fname, lname(Renter)) X (Π Cartesian Product RXS defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S. Relational Model - 25 Relational Model - 26 Eg. Cartesian Product and Selection Union o Use selection operation to extract those tuples where Renter.Rno = Viewing.Rno. ∏rno,fname,lname(Renter)) Χ σrenter.rno = viewing.rno((∏ (∏ ∏rno,pno,comment(Viewing))) Union Operation o Cartesian product and Selection can be reduced to a single operation called a join. Relational Model - 27 CS3462 Introduction to Database Systems Helena Wong, 2001 R∪S Union of two relations R and S with I and J tuples, respectively, is obtained by concatenating them into one relation with a maximum of (I + J) tuples, duplicate tuples being eliminated. R and S must be unioncompatible. Relational Model - 28 Example - Union Set Difference Set Difference Operation R–S o Construct a list of all areas where there is either a branch or a property. defines a relation consisting of the tuples that are in relation R, but not in S. R and S must be union-compatible. Π area(Branch) ∪ Πarea (Property_for_Rent) Relational Model - 29 Relational Model - 30 Example - Set Difference Join Operations o Construct a list of all cities where there is a branch office but no properties. o Join is a derivative of Cartesian product. Π city (Branch) – Πcity (Property_for_Rent) o Equivalent to performing a selection, using the join predicate as the selection formula, over the Cartesian product of the two operand relations. o One of the most difficult operations to implement efficiently in a relational DBMS and one of the reasons why relational systems have intrinsic performance problems. Relational Model - 31 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 32 Join Operations Theta-join (θ-join) o There are various forms of join operation oR – Theta-join – Equi-join (a particular type of theta-join) – Natural join – Outer join – Semi-join – – FS Defines a relation that contains tuples satisfying the predicate F from the Cartesian product of R and S. The predicate F is of the form R.ai θ S.bi where θ may be one of the comparison operators (<, < =, >, > =, =, ~ =). Relational Model - 33 Relational Model - 34 Theta-join (θ-join) Example - Equi-join o We can rewrite the theta-join in terms of the basic Selection and Cartesian product operations. List the names and comments of all renters who have viewed a property. (Π Πrno,fname,lname (Renter)) renter.rno = viewing.rno (Π Πrno,pno,comment(Viewing)) R FS = σF(R Χ S) o Degree of a theta-join is sum of the degrees of the operand relations R and S. If predicate F contains only equality (=), the term equi-join is used. Relational Model - 35 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 36 Natural Join Example - Natural Join oR o List the names and comments of all renters who have viewed a property. – S Natural join is an equi-join of the two relations R and S over all common attributes x. One occurrence of each common attribute is eliminated from the result. (Πrno,fname,lname (Renter)) Relational Model - 37 Relational Model - 38 Outer Join Semi-join o Often in joining two relations, there is no matching value in the join columns. To display rows in the result that do not have matching values in the join column, we use the outer join. oR oR – – FS The semi-join operation defines a relation that contains the tuples of R that participate in the join of R with S. S The (left) outer join is a join in which tuples from R that do not have matching values in the common columns of S are also included in the result relation. Relational Model - 39 CS3462 Introduction to Database Systems Helena Wong, 2001 (Πrno,pno,comment(Viewing)) Relational Model - 40 Example - Semi-join Intersection o List complete details of all staff who work at the branch in Partick. Staff staff.bno = branch.bno and branch.area = ‘Partick’ o R∩S – The intersection operation consists of the set of all tuples that are in both R and S. – R and S must be union-compatible. Branch o Expressed using basic operations R ∩ S = R – (R – S) Relational Model - 41 Relational Model - 42 Division Example - Division o R÷S – The division operation consists of the set of tuples from R defined over the attributes C that match the combination of every tuple in S. o Identify all renters who have viewed all properties with three rooms. (Π Π rno,pno (Viewing)) ÷ (Π Π pno (σ σrooms = 3 (Property_for_Rent))) o Expressed using basic operations T1 = ΠC(R) T2 = ΠC((S X T1) – R) T = T1 – T 2 Relational Model - 43 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 44 Views Views o Base Relation A named relation, corresponding to an entity in conceptual schema, whose tuples are physically stored in database. o View Dynamic result of one or more relational operations operating on the base relations to produce another relation. Relational Model - 45 Relational Model - 46 Views Purpose of Views o A view is a virtual relation that does not actually exist in the database but is produced upon request, at time of request. o Provides a powerful and flexible security mechanism by hiding parts of database from certain users. o Contents of a view are defined as a query on one or more base relations. o Permits users to access data in a customized way, so that same data can be seen by different users in different ways, at same time. o Views are dynamic, meaning that changes made to base relations that affect view attributes are immediately reflected in the view. Relational Model - 47 CS3462 Introduction to Database Systems Helena Wong, 2001 o It can simplify complex operations on base relations. Relational Model - 48 Updating Views Updating Views o All updates to a base relation should be immediately reflected in all views that reference that base relation. o However, there are restrictions on types of modifications that can be made through views: o If view is updated, underlying base relation should reflect change. – Updates are allowed if query involves a single base relation and contains a candidate key of base relation. – Updates are not allowed involving multiple base relations. – Updates are not allowed involving aggregation or grouping operations. Relational Model - 49 Views - Advantages / Disadvantages Advantages Disadvantages Data independence Update restriction Currency Structure restriction Improved security Performance Reduced complexity Convenience Customization Data integrity Relational Model - 51 CS3462 Introduction to Database Systems Helena Wong, 2001 Relational Model - 50