# RELATIONAL DATABASES. Relational data Structure RELATION: Table with columns and rows ATTRIBUTE: Column of a relation DOMAIN: Set of allowable values.

## Presentation on theme: "RELATIONAL DATABASES. Relational data Structure RELATION: Table with columns and rows ATTRIBUTE: Column of a relation DOMAIN: Set of allowable values."— Presentation transcript:

RELATIONAL DATABASES

Relational data Structure RELATION: Table with columns and rows ATTRIBUTE: Column of a relation DOMAIN: Set of allowable values for one or more attributes TUPLE: Row of a relation DEGREE of a relation: Number of attributes CARDINALITY of a relation: –Number of tuples it contains Relational Database: A collection of normalized relations Fig 3.1 pg 75 Fig 3.2 pg 76

Alternate terminology for relational model terms Formal Terms Alternative 1Alternative 2 RelationTableFile TupleRowRecord AttributeColumnField

Mathematical Relations D1={2,4} and D2={1,3,5} D1 x D2= {(2,1),(2,3),(2,5),(4,1),(4,3),(4,5)} R={(2,1),(4,1)} D1={1,3} D2={2,4} D3={5,6} D1 x D2 x D3 = –{(1,2,5),(1,2,6),(1,4,5),(3,2,5),(3,2,6),(3,4,5),(3,4,6)}

DATABASE RELATIONS Relation Schema: A relation name followed by a set of attribute and domain name pairs. A1,A2,…..An be attributes with domains D1,D2,….Dn {A1:D1,A2:D2,…….An:Dn} is a relation schema Relation R is a set of n-tuples: (A1:d1,A2:d2,…..An:dn) such that d1  D1, d2  D2 …….dn  Dn {(Bno: B5, Street: 22 Deer Rd, Area: Sidcup, City: London, Pcode:SW1 4EH, Tel_No: 0171-886-3838, Fax_No: 0171-886- 3272}

Properties of Relations Relation has a name that is distinct Each cell of the relation contains exactly (single) value Each attribute has a distinct name The values of the attribute form the same domain The order of the attributes have no significance Each tuple is distinct; no duplicate tuples The order of the tuples has no significance Relations do not contain repeating groups –two telephone nos for a single branch office in a single cell. First Normal Form

Relational Keys Superkey: An attribute or set of attributes that uniquely identifies a tuple within a relation Candidate Key: A superkey such that no proper subset is a superkey within the relation –several candidate keys for a relation. –Composite Key: when a key consists of more than one attribute. Primary Key: The candidate key that is selected to identify tuples uniquely within the relation Alternate key: The candidate keys that are not selected to be the primary key Foreign Key: An attribute or set of attributes within on relation that matches the candidate key of some (possibly the same) relation

Representing Relational Database Schemas A relational database consists of a number of relations. Relational schemas Branch: (Bno, Street, Area, City, Pcode, tel_No, fax_No) Staff:(Sno, Fname, Lname, Address, tel_No, Position, Sex, DOB, Salary, NIN, Bno) Property_for_RentPno, Street, Area, City, Pcode, Type, Rooms, Rent, Ono, Sno, Bno) Renter(Rno, Fname, Lname, Address, Tel_No, Pref_Type, Max_Rent, Bno) Owner(Ono, Fname, Lname, Address, Tel_No) Viewing(Rno, Pno, Date, Comment) Fig 3.3 -- pg83

Relational Integrity Null –Value currently unknown or not applicable Entity Integrity –No attribute of a primary key can be null Referential Integrity –Foreign key value must match a candidate key value Enterprise constraints –Additional rule specified

Selection Operation List all staff with a salary greater than \$10,000  salary > 10000 (Staff) logical operators  (AND),  (OR) and ~ (NOT)

Projection Operation List of salaries for all staff, showing only Sno, Fname, Lname and Salary details  sno, fname, lname, salary (Staff) Sno Fname Lname Salary SL21 JohnWhite 30000 SA25 ReneHowe 30000 SF31 AlLee 30000 S55 MaryAnn 30000

Cartesian Product Operation (  rno, fname, lname (Renter)) x (  rno, no,comment (Viewing)) abab 123123 aaabbbaaabbb 123123123123 R SR x S

Tuples where Renter.Rno = Viewing.Rno  renter.rno = viewing.rno ((  rno, fname, lname (Renter)) x (  rno, no,comment (Viewing))) Renter.RnoFnameLnameViewing.RnoPnoComment CR76JohnKayCR76PG4too remote CR79RickBayCR79PG4 CR88MollyHollyCR88PG4too small CR96BobRayCR96PG4no dining room

Union Operation Construct a list of all areas where there is either a branch or a property  area (Branch)   area (Property_for_rent) Area Sidcup Dyce Patrick Leigh dee Kilbern Hynland

Set Difference Operation Construct a list of all cities where there is a branch office but no properties  city (Branch) -  city (Property_for_rent) City Bristol

Natural Join Operation List the names and comments of all renters who have viewed a property  rn,fname,lname (Renter)  rno,pno,comment (Viewing) Renter.RnoFnameLnameViewing.RnoPnoComment CR76JohnKayCR76PG4too remote CR79RickBayCR79PG4 CR88MollyHollyCR88PG4too small CR96BobRayCR96PG4no dining room A B C a 1 x a 1 y T U

Left Outer Join Operation Produce a status report on property listings  rpno,street,city (Property_for_Rent) Viewing PnoStreetCityRnoDateComment PA1416HolheadAberdeenCR5624-May-98too small PA1416HolheadAberdeenCR6214-May-98no dining PL946ArgyllLondonnull null null PG4Lawrence St.GlasgowCR7620-Apr-98too remote PG4Lawrence St.GlasgowCR5627-May-98 PG362Manon Rd GlasgowCR5628-Apr-98

Views View: A dynamic result of one or more relational operation to produce another relation A view is a relation, but does not exist in storage Views are dynamic

Purpose of Views Hiding parts of the database from some users –Staff records without Salary attribute Same data can be seen by different users in different ways at the same time A view may be a join of two relations Order of columns may be changed

Similar presentations