Presentation is loading. Please wait.

Presentation is loading. Please wait.

IT 20303 Relational Database Theory. The Relational Theory –Ways of working with data Types of “Models” –File database model –Hierarchical database model.

Similar presentations


Presentation on theme: "IT 20303 Relational Database Theory. The Relational Theory –Ways of working with data Types of “Models” –File database model –Hierarchical database model."— Presentation transcript:

1 IT 20303 Relational Database Theory

2 The Relational Theory –Ways of working with data Types of “Models” –File database model –Hierarchical database model –Network database model –Relational database model

3 Relational Database Theory The Relational Theory –Meaning of database model The way data is organized & stored The way data is manipulated

4 Relational Database Theory Relational Model of Data –Published in 1970 by Dr. Edgar (Ted) Codd – IBM “A Relational Model of Data for Large Shared Data Banks”

5 Relational Database Theory Relational Model of Data –Purpose Achieve program/data structure independence Treat data in a disciplined way –Apply rigor of mathematics –Uses Set Theory – sets of related data Improve programmer productivity

6 Relational Database Theory The Relational Model –Relational uses familiar concepts The data is perceived as organized in tables –Relational also incorporates the rigor of mathematics Rows of the table are treated as elements in a set Manipulation of rows is based on set operations – (Vinn Diagrams) –User works with a set of rows at a time

7 Relational Database Theory Relational also impacts Data Design –Files were often constructed to support an application –Tables are designed to describe one thing or Entity in the database

8 Relational Database Theory Example of a Relation: –ANIMAL – Entity (Relation) ANAMEAFAMILYWEIGHT CandiceCamel1800 ZonaZebra900 SamSnake5 ElmerElephant5000 LeonardLion1200

9 Relational Database Theory Definition of a Relation –Data is organized & stored in structures called relations –A relation is a table that adheres to certain rules A relation can be called a table

10 Relational Database Theory Definition of a Relation –A relation is a table containing all the data about some entity An entity is a thing or object that is important in this application area Data items in the table are related

11 Relational Database Theory Relational Data Structure ANAMEAFAMILYWEIGHT CandiceCamel1800 ZonaZebra900 SamSnake5 ElmerElephant5000 LeonardLion1200 NameSpeciesWeight Domains Primary Key Relation Attributes Tuples

12 Relational Database Theory Relational Data Structure Definitions –Relation The Table –Tuple A Row –Attribute A Column

13 Relational Database Theory Relational Data Structure Definitions –Primary Key A unique identifier for the table –Domain A pool of legal values from which an attribute value is selected –Related to meaning –Has a Data Type

14 Relational Database Theory Relational Data Structure Definitions –Degree The number of attributes –Cardinality The number of tuples

15 Relational Database Theory Relational Table Rules –A Relation is a table that adheres to the following rules: There are No Duplicate Tuples in the table –The tuples in the table are treated as a mathematical set

16 Relational Database Theory Relational Table Rules –By definition, a set is a collection of unique elements There must be a primary key (unique identifier) for each tuple

17 Relational Database Theory Relational Table Rules There is no order to the tuples (top to bottom) There is no order to the attributes (left to right) –By convention, the primary key attribute is usually the first one on the left side of the table

18 Relational Database Theory Attributes –Each attribute has a datatype Examples: Integer, character, date, user-defined –The data value of an attribute can be null

19 Relational Database Theory Attributes –Each attribute value is atomic There is One & Only One data value in each cell of the table There are no Lists or Arrays One fact per field, one field per fact –Can be called a Field (MS Access)

20 Relational Database Theory Relational Data Structure: Design –Each relation contains data about only one entity Each row corresponds to one unique occurrence of the entity –A relation does not contain arrays, lists or repeating groups No multi-valued attributes

21 Relational Database Theory –Tables are designed according to Rules of Normalization Each data item in the table is determined –By the Primary Key –By the Whole Primary Key –Only by the Primary Key

22 Relational Database Theory –Normalization avoids well-known update problems Optimizes design to minimize redundancy & storage requirements

23 Relational Database Theory Example: Table with repeating group –Animal ANAMEAFAMILYWEIGHTFOOD CandiceCamel1800Hay Buns ZonaZebra900Brush SamSnake5Mice People ElmerElephant5000Leaves LeonardLion1200People Meat

24 Relational Database Theory Example: Table with no repeating group ANAMEFOOD CandiceHay CandiceBuns ZonaBrush SamMice SamPeople ElmerLeaves LeonardPeople LeonardMeat ANAMEAFAMILYWEIGHT CandiceCamel1800 ZonaZebra900 SamSnake5 ElmerElephant5000 LeonardLion1200 Animal Animal-Food

25 Relational Database Theory A Database Models the Real World –A Database represents Reality –The database is a collection of relations A relation represents an entity type Each tuple represents one occurrence of that entity type Each occurrence of an entity is unique

26 Relational Database Theory A Database Models the Real World –A database contains information about Entities Relationships between entities Rules about the entities’ data & the relationships

27 Relational Database Theory Relational Databases Support Relationships –Relational databases support relationships between entities Relationship is established by a Foreign Key Repeat the Primary Key of one table in the related table(s)

28 Relational Database Theory Example: The Zoo has an “Adopt-an-Animal” program –A zoo member can adopt an animal MIDMNAMEMADDR***ANAME 171N. Harrison1400 Blush RdZona 144J. Montagano1108 5 th AveLeonard 194J. Spence1244 Lark LnCandice 303E. Wingate5222 Gains DrCandice 101H. Yarchun177 Beach Rd 270K. Steeg140 Crystal DrZona 291S. Ackerman1172 Park DrSam 301K. Snyder196 279 th Ave ANAMEAFAMILYWEIGHT CandiceCamel1800 ZonaZebra900 SamSnake5 ElmerElephant5000 LeonardLion1200 Animal Foreign Key Zoo-Member

29 Relational Database Theory Example: Another Relationship ANAMEFOOD CandiceHay CandiceBuns ZonaBrush SamMice SamPeople ElmerLeaves LeonardPeople LeonardMeat ANAMEAFAMILYWEIGHT CandiceCamel1800 ZonaZebra900 SamSnake5 ElmerElephant5000 LeonardLion1200 Animal Animal-Food Composite Primary Key Foreign Key

30 Relational Database Theory Relational Integrity Rules –Entity Integrity No part of the Primary Key (PK) may be Null –Referential Integrity The value of a Foreign Key (FK) must either –Be Null or –Be one of the values of the PK in the related table

31 Relational Database Theory Keys, Keys, and More Keys –Characteristic of a Primary Key (PK) Unique Mandatory Unchanging Under the control of IT organization

32 Relational Database Theory Keys, Keys, and More Keys –Names or Types of Keys Candidate Key –A minimal set of attributes that can be used as the unique identifier for a table

33 Relational Database Theory Keys, Keys, and More Keys –Names or Types of Keys Primary Key –One of the candidate keys Alternate Key –A candidate key that is not the primary key

34 Relational Database Theory Keys, Keys, and More Keys –Names or Types of Keys Foreign Key –A primary key of a related table –Indicates relationships

35 Relational Database Theory Keys, Keys, and More Keys –Names or Types of Keys Composite Key –A key composed of more than one attribute Search Key –One or more attributes on which a retrieval is based »Indexes

36 Relational Database Theory Characteristics of Relationships –Referential integrity applies to the relationship between entities Also known as an existence constraint or an enterprise rule For every relationship, referential integrity must be defined

37 Relational Database Theory Relationships have Cardinality –One-To-One –One-To-Many –Many-To-Many Relationships have Optionality –Each entity’s participation is either Mandatory or Optional

38 Relational Database Theory Cardinality reflects Business Rules –One-To-One Relationship One animal is cared for by one zoo worker One zoo worker cares for one animal

39 Relational Database Theory Cardinality reflects Business Rules –One-To-Many Relationship One animal is cared for by many zoo workers One zoo worker cares for only one animal

40 Relational Database Theory Cardinality reflects Business Rules –Many-To-Many Relationship One animal is cared for by many zoo workers One zoo worker cares for many animals

41 Relational Database Theory Mandatory Relationship –The Foreign Key Cannot be Null –Every purchase order must have a supplier –In the example below the FK, SNO, cannot be Null

42 Relational Database Theory Example: ONOSNOODATE*** 7001123403/09/02 7002207903/10/02 7003207903/12/02 *** SUPPLIER SNOSNAMESADDR 1234Farm & Feed 7000 Booth Rd 2079The Grain House 2001 Larkin Dr *** PORDER

43 Relational Database Theory Example: FK can be Null ANIDANAMEAFAMILYWEIGHT 0001CandiceCamel1800 0002ZonaZebra900 0003SamSnake5 0004ElmerElephant5000 0005LeonardLion1200 ANIMAL MIDMNAMEMADDR***ANID 171N. Harrison1400 Blush Rd0002 144J. Montagano1108 5 th Ave0005 194J. Spence1244 Lark Ln0001 303E. Wingate5222 Gains Dr0001 101H. Yarchun177 Beach Rd 270K. Steeg140 Crystal Dr0002 291S. Ackerman1172 Park Dr0003 301K. Snyder196 279 th Ave Foreign Key ZOO-MEMBER

44 Relational Database Theory What happens when a Tuple is deleted? –For every relationship, there are three possible delete options Cascades –Delete the target tuple and –Delete the related tuples

45 Relational Database Theory Restricted –Delete restricted to cases for which there are no related tuples Nullifies –Delete the target tuple and –Set the FK to null in the related tuples

46 Relational Database Theory Relational Algebra Operations –Select –Project –Join –Union –Intersect –Difference

47 Relational Database Theory Our Zoo Database Tables ANIDANAMEAFAMILYWEIGHT 0001CandiceCamel1800 0002ZonaZebra900 0003SamSnake5 0004ElmerElephant5000 0005LeonardLion1200 ANIMAL MIDMNAMEMADDR***ANID 171N. Harrison1400 Blush Rd0002 144J. Montagano1108 5 th Ave0005 194J. Spence1244 Lark Ln0001 303E. Wingate5222 Gains Dr0001 101H. Yarchun177 Beach Rd 270K. Steeg140 Crystal Dr0002 291S. Ackerman1172 Park Dr0003 301K. Snyder196 279 th Ave ZOO-MEMBER ANIMAL-FOOD ANIDFOOD 0001Hay 0001Buns 0002Brush 0003Mice 0003People 0004Leaves 0005People 0005Meat

48 Relational Database Theory Relational Algebra: SELECT –Extracts specified tuples from a relation (or get rows from a table)

49 Relational Database Theory Example: SELECT out from the ANIMAL-FOOD table (display) the rows where FOOD=PEOPLE ANIMAL-FOOD ANIDFOOD 0001Hay 0001Buns 0002Brush 0003Mice 0003People 0004Leaves 0005People 0005Meat ANIDFOOD 0003People 0005People RESULTS

50 Relational Database Theory Relational Algebra: PROJECT –Extracts specified attributes(columns) from a relation (or get columns from a table)

51 Relational Database Theory Example: PROJECT from the ZOO-MEMBER table columns (MID, NAME) MIDMNAMEMADDR***ANID 171N. Harrison1400 Blush Rd0002 144J. Montagano1108 5 th Ave0005 194J. Spence1244 Lark Ln0001 303E. Wingate5222 Gains Dr0001 101H. Yarchun177 Beach Rd 270K. Steeg140 Crystal Dr0002 291S. Ackerman1172 Park Dr0003 301K. Snyder196 279 th Ave ZOO-MEMBER MIDMNAME 171N. Harrison 144J. Montagano 194J. Spence 303E. Wingate 101H. Yarchun 270K. Steeg 291S. Ackerman 301K. Snyder RESULTS

52 Relational Database Theory Relational Algebra: JOIN –Join the data in two tables Concatenate one row from Table 1 with one row from Table 2 –Usually based on a common column called the join condition

53 Relational Database Theory Example: JOIN T1 and T2 based on the AFAMILY column ANIDAFAMILY 0001Camel 0002Zebra T1 AFAMILYAREA Camel01 Zebra03 T2 ANIDAFAMILY AREA 0001Camel 01 0002Zebra 03 RESULT

54 Relational Database Theory Different types of Joins –Equijoin – means a row in T1 is joined with a row in T2 where the values in the common column(s) are equal –This is the most common type of join ANIDAFAMILY 0001Camel 0002Zebra T1 AFAMILYAREA Camel01 Zebra03 T2 ANIDAFAMILY AREA 0001Camel 01 0002Zebra 03 RESULT Join T1 and T2 where T1.AFAMILY=T2.AFAMILY

55 Relational Database Theory Natural Join –The rows of T1 are joined with the rows of T2 where the PK value in one table equals the FK value in the other table Where column name are the same Don’t use this in a Production Database – renaming causes problems ANIDAFAMILY 0001Camel 0002Zebra T1 AFAMILYAREA Camel01 Zebra03 T2 ANIDAFAMILY AREA 0001Camel 01 0002Zebra 03 RESULT T1 NATURAL JOIN T2

56 Relational Database Theory Inner Join –The rows of T1 are joined with the rows of T2 based on the join condition specified Only rows from T1 with a matching row in T2 are in the result Often an Inner Join is both a Natural & a Equijoin

57 Relational Database Theory Example: Inner Join –T1 INNER JOIN T2 on T1.AFAMILY=T2.AFAMILY ANIDAFAMILY 0001Camel 0002Zebra T1 AFAMILYAREA Camel01 Zebra03 T2 ANIDAFAMILY AREA 0001Camel 01 0002Zebra 03 RESULT

58 Relational Database Theory Outer Join –The rows of T1 are joined with the rows of T2 All rows from one of the tables are included in the result even if there is no matching row in the other table

59 Relational Database Theory Example: Outer Join –T1 RIGHT OUTER JOIN T2 on T1.AFAMILY=T2.AFAMILY ANIDAFAMILY 0001Camel 0002Zebra T1 AFAMILYAREA Camel01 Zebra03 Snake05 T2 ANIDAFAMILY AREA 0001Camel 01 0002Zebra 03 Snake05 RESULT

60 Relational Database Theory Cross Join –Every row in T1 is joined with every row in T2 All possible combinations of rows in the two tables Also called a Cartesian Product

61 Relational Database Theory Example: Cross Join –T1 CROSS JOIN T2 ANIDAFAMILY 0001Camel 0002Zebra T1 AFAMILYAREA Camel01 Zebra03 T2 ANIDAFAMILY AREA 0001Camel 01 0001CamelZebra03 0002ZebraCamel01 0002Zebra 03 RESULT

62 Relational Database Theory An RDBMS manipulates Data using Relational Algebra Operations –There are (usually) several sequences of operations to answer a query One sequence may be more efficient than another –A relational DBMS internally has routines that do the relational algebra

63 Relational Database Theory –A relational DBMS generates a sequence or plan of relational algebra operations to accomplish the request –A relational DBMS has a query optimizer to develop an efficient query plan A least-cost optimizer generates several execution plans and chooses the least- cost one; i.e.. Least amount of I/O

64 Relational Database Theory Union, Intersection, and Minus Union – union together (append) the result tables from two queries Intersect – take only the rows that are identical in the result tables from two queries Difference – take only the rows in the first result table that have no identical rows in the second result table

65 Relational Database Theory Relational Algebra: UNION –Union together the results of two queries Result contains every element in either one or both sets –Query 1 Select the rows from ANIMAL where WEIGHT > 2000 into T1 Project from T1(ANID) into result 1

66 Relational Database Theory –Query 2 Select the rows from ANIMAL-FOOD where FOOD=PEOPLE into T2 Project from T2(ANID) into Result 2 –Query 1 UNION Query 2

67 Relational Database Theory ANID 0003 0004 0005 ANID 0003 0005 ANID 0004 RESULT 1RESULT 2RESULT UNION

68 Relational Database Theory Relational Algebra: INTERSECTION –Take only the rows (tuples) that are identical in the result tables of two queries Query 1 –Select out the rows from ANIMAL where WEIGHT > 1000 into T1 –Project from T1(ANID) into Result 1

69 Relational Database Theory Query 2 –Project from ZOO-MEMBER(ANID) into Result 2 Query 1 INTERSECT Query 2 ANID 0001 0005 ANID 0002 0005 0001 0003 ANID 0001 0004 0005 RESULT 1RESULT 2RESULT INTERSECT

70 Relational Database Theory Relational Algebra: Minus/Difference/Except –Subtract from the results of one query from the results of a second query Query 1 –Project from ANIMAL(ANID) into Result 1 Query 2 –Project from ZOO-MEMBER(ANID) into Result 2

71 Relational Database Theory Query 1 EXCEPT Query 2 ANID 0004 ANID 0002 0005 0001 0003 ANID 0001 0002 0003 0004 0005 RESULT 1RESULT 2RESULT EXCEPT

72 Relational Database Theory Strengths of the Relational Approach –Simple People are familiar with tables Few rules Few operations –Easy to learn Relational algebra is straightforward Multiple high-level, non-procedural languages are available -SQL

73 Relational Database Theory –Well founded Basis is mathematics, set theory


Download ppt "IT 20303 Relational Database Theory. The Relational Theory –Ways of working with data Types of “Models” –File database model –Hierarchical database model."

Similar presentations


Ads by Google