chapter 3 the relational model transparencies last updated: pebruari 2010 by m. arief

Post on 05-Jan-2016

235 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 3Chapter 3

The Relational ModelThe Relational Model

TransparenciesTransparenciesLast Updated: Pebruari 2010

By M. Arief

http://arief.ismy.web.id

2

Chapter 3 - ObjectivesChapter 3 - Objectives

► TerminologyTerminology of relational model. of relational model.►How How tablestables are used to represent are used to represent

data.data.► Connection between mathematical Connection between mathematical

relations and relations in the relations and relations in the relational model.relational model.

► PropertiesProperties of database relations. of database relations.►How to identify How to identify candidatecandidate, , primaryprimary, ,

and and foreign keysforeign keys..►Meaning of Meaning of entity integrityentity integrity and and

referential integrityreferential integrity..► Purpose and advantages of Purpose and advantages of viewsviews..

http://arief.ismy.web.id

3

Database ApproachDatabase Approach► Arose because:Arose because:

SeparationSeparation and and isolationisolation of data of data DuplicationDuplication of data of data Definition of dataDefinition of data was was embedded in embedded in

application programsapplication programs, rather than being stored , rather than being stored separately and independently.separately and independently.

No controlNo control over access and manipulation of over access and manipulation of data beyond that imposed by application data beyond that imposed by application programs.programs.

► Result: Result: the database and Database Management the database and Database Management

System (DBMS).System (DBMS).

http://arief.ismy.web.id

4

DatabaseDatabase

►Shared collection of Shared collection of logically related logically related datadata and and a description of this dataa description of this data, , designed to meet the designed to meet the information information needs of an organizationneeds of an organization..

►System catalog (metadata)System catalog (metadata) provides provides description of data to enable description of data to enable program–data independence.program–data independence.

http://arief.ismy.web.id

5

Database Management System Database Management System (DBMS)(DBMS)

►A software system that enables A software system that enables users to users to definedefine, , createcreate, and , and maintainmaintain the database and that provides the database and that provides controlled access to this database.controlled access to this database.

http://arief.ismy.web.id

6

Database Management System Database Management System (DBMS)(DBMS)

http://arief.ismy.web.id

7

Relational Model HistoryRelational Model History► A Relational model for large shared A Relational model for large shared

banks’, EF Codd – 1970.banks’, EF Codd – 1970.

►Objectives:Objectives: To allow a high degree of To allow a high degree of data data

independenceindependence. Between application . Between application programs and internal data representations.programs and internal data representations.

To provide substantial ground for dealing To provide substantial ground for dealing with with consistencyconsistency and and redundancyredundancy problems problems the concept of the concept of normalizednormalized relations. relations.

To enable the expansion of To enable the expansion of set-oriented set-oriented data manipulation languagesdata manipulation languages..

http://arief.ismy.web.id

8

Relational Model TerminologyRelational Model Terminology► RDBMSRDBMS: 2: 2ndnd gen. Of DBMS, based on the relational data gen. Of DBMS, based on the relational data

model, EF Codd – 1970.model, EF Codd – 1970.

► Data is logically structured within Data is logically structured within relationsrelations..

► A relation is a A relation is a tabletable with with columnscolumns and and rowsrows.. Only applies to logical structure of the database, not Only applies to logical structure of the database, not

the physical structure.the physical structure.

► AttributeAttribute is a is a named columnnamed column of a relation. of a relation.

► DomainDomain is the set of allowable values for one or more is the set of allowable values for one or more attributes. attributes. (attribute, domain name, meaning, domain definition)(attribute, domain name, meaning, domain definition)

► IntensionIntension is the structure of a relation with its domain. is the structure of a relation with its domain. Usually fixed.Usually fixed.

http://arief.ismy.web.id

9

Relational Model TerminologyRelational Model Terminology► TupleTuple is a row of a relation and contains one value per attribute. is a row of a relation and contains one value per attribute.

► ExtensionExtension or state of a relation or state of a relation tuple. Changes over time tuple. Changes over time

► DegreeDegree is the number of attributes in a relation. (unary, binary, ternary, n-ary) is the number of attributes in a relation. (unary, binary, ternary, n-ary)

► CardinalityCardinality is the number of tuples in a relation. is the number of tuples in a relation.

► Relational DatabaseRelational Database is a collection of normalized relations with distinct is a collection of normalized relations with distinct relation names.relation names.

► Tuple adalah baris hubungan dan memuat satu nilai per atribut.Tuple adalah baris hubungan dan memuat satu nilai per atribut.

Extension Extension atau keadaan relasi tupel. Perubahan dari waktu ke waktuatau keadaan relasi tupel. Perubahan dari waktu ke waktu

Degree adalah jumlah atribut dalam relasi. (unari, biner, terner, n-ary)Degree adalah jumlah atribut dalam relasi. (unari, biner, terner, n-ary)

Kardinalitas adalah jumlah tuple dalam relasi.Kardinalitas adalah jumlah tuple dalam relasi.

Relational Database adalah kumpulan dari hubungan normalisasi hubungan dengan Relational Database adalah kumpulan dari hubungan normalisasi hubungan dengan nama yang berbeda.nama yang berbeda.

http://arief.ismy.web.id

10

Branch and Staff (part) RelationsBranch and Staff (part) Relations

http://arief.ismy.web.id

11

Examples of Attribute DomainsExamples of Attribute Domains

http://arief.ismy.web.id

12

Alternative Terminology Alternative Terminology for Relational Modelfor Relational Model

http://arief.ismy.web.id

13

Mathematical Definition of RelationMathematical Definition of Relation

► Consider two sets, Consider two sets, DD11 & & DD22, where , where DD11 = {2, 4} and = {2, 4} and DD22 = =

{1, 3, 5}. {1, 3, 5}.

► Cartesian productCartesian product, , DD11 DD22, is set of all ordered pairs, , is set of all ordered pairs,

where first element is member of where first element is member of DD11 and second and second

element is member of element is member of DD22. .

DD11 DD22 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)} = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}

► Alternative way is to find all combinations of elements Alternative way is to find all combinations of elements with first from with first from DD11 and second from and second from DD22. .

http://arief.ismy.web.id

14

Mathematical Definition of RelationMathematical Definition of Relation

►AnyAny subset subset of Cartesian product is a of Cartesian product is a relationrelation; e.g.; e.g.

RR = {(2, 1), (4, 1)} = {(2, 1), (4, 1)}

►May specify which pairs are in relation May specify which pairs are in relation using some condition for selection; e.g.using some condition for selection; e.g. second element is 1:second element is 1:

RR = {( = {(xx, , yy) | ) | x x DD11, , y y DD22, and , and yy = 1} = 1}

first element is always twice the second:first element is always twice the second:

SS = {( = {(xx, , yy) | ) | x x DD11, , y y DD22, and , and xx = 2 = 2yy}}

http://arief.ismy.web.id

15

Mathematical Definition of RelationMathematical Definition of Relation

►Consider three sets Consider three sets DD11, , DD22, , DD33 with with Cartesian Product Cartesian Product DD11 DD22 DD33; e.g.; e.g.

DD11 = {1, 3} = {1, 3} DD22 = {2, 4} = {2, 4} DD33 = {5, 6} = {5, 6}

DD11 DD22 DD33 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6), = {(1,2,5), (1,2,6), (1,4,5), (1,4,6), (3,2,5), (3,2,6), (3,4,5), (3,4,6)} (3,2,5), (3,2,6), (3,4,5), (3,4,6)}

►Any subset of these ordered triples is a Any subset of these ordered triples is a relation.relation.

http://arief.ismy.web.id

16

Mathematical Definition of RelationMathematical Definition of Relation

►The Cartesian product of The Cartesian product of nn sets ( sets (DD11, , DD22, . . ., , . . ., DDnn) is:) is:

DD11 DD22 . . .. . .DDnn = {( = {(dd11, , dd22, . . . , , . . . , ddnn) | ) | dd1 1 DD11, , dd2 2 DD22, . . . , , . . . , ddnnDDnn} }

usually written asusually written as: : nnXXDDiiii == 11

►Any set of Any set of nn-tuples from this Cartesian -tuples from this Cartesian product is a relation on the product is a relation on the nn sets. sets.

http://arief.ismy.web.id

17

Database RelationsDatabase Relations►Relation schemaRelation schema

Named relation defined by a set of Named relation defined by a set of attributeattribute and and domain domain namename pairs. pairs.

AttributesAttributes: : AA11 & & AA22, , DomainsDomains: : DD11 & & DD22

Relation SchemaRelation Schema SS: {: {AA11::DD1 1 , A, A22::DD22, … A, … Ann::DDnn}}RelationRelation RR is a set of n-tuples: is a set of n-tuples:((AA11::dd1 1 ,A,A22::dd22, … A, … Ann::ddnn) such that ) such that dd1 1 DD11, , dd2 2 DD22, . . . , , . . . , ddnnDDnn} }

In tableIn table: attribute name as column heading and tuples as : attribute name as column heading and tuples as rows (rows (dd1 1 ,d,d22, … d, … dnn) )

A relation in the relational modelA relation in the relational model is any subset of the is any subset of the Cartesian product of the domains of the attributes.Cartesian product of the domains of the attributes.

Ex. Ex. Branch relation: any subset of the Cartesian product of the Branch relation: any subset of the Cartesian product of the

domainsdomains{(B005, 22 Deer Rd., London, SW1 4EH)} or{(B005, 22 Deer Rd., London, SW1 4EH)} or{(branchNo: B005, … , postcode: SW1 4EH)}{(branchNo: B005, … , postcode: SW1 4EH)}

http://arief.ismy.web.id

18

Database RelationsDatabase Relations

►Relational database schemaRelational database schema Set of relation schemas, each with a Set of relation schemas, each with a

distinct name.distinct name.

SS1 1 , S, S22, … S, … Sn n are a set of relation schemas.are a set of relation schemas.

Relational (database) schema:Relational (database) schema: S = (SS = (S1 1 , S, S22, … S, … Snn))

http://arief.ismy.web.id

19

Properties of RelationsProperties of Relations

►Relation name is distinct from all other Relation name is distinct from all other relation names in relational schema.relation names in relational schema.

► Each cell of relation contains exactly one Each cell of relation contains exactly one atomic (single) value.atomic (single) value.

► Each attribute has a distinct name.Each attribute has a distinct name.

►Values of an attribute are all from the Values of an attribute are all from the same domain.same domain.

http://arief.ismy.web.id

20

Properties of RelationsProperties of Relations► Each tuple is distinct; there are no Each tuple is distinct; there are no

duplicate tuplesduplicate tuples. . Relational keys, Relational keys, next slide.next slide.

►Order of attributesOrder of attributes has has nono significance.significance.

►Order of tuplesOrder of tuples has has nono significance, significance, theoretically.theoretically.

http://arief.ismy.web.id

21

RelationalRelational Keys Keys► SuperkeySuperkey An attribute, or a set of attributes, that An attribute, or a set of attributes, that uniquely uniquely

identifies a tupleidentifies a tuple within a relation. May contain within a relation. May contain additional attribute that are additional attribute that are not necessarynot necessary for unique for unique identification.identification.

► Candidate KeyCandidate Key Superkey (K) such that no proper subset is a superkey Superkey (K) such that no proper subset is a superkey

within the relation. (contain minimum number of within the relation. (contain minimum number of attribute).attribute).

Properties:Properties: In each tuple of R, values of K uniquely identify that In each tuple of R, values of K uniquely identify that

tuple (tuple (uniquenessuniqueness).). No proper subset of K has the uniqueness property No proper subset of K has the uniqueness property

((irreducibilityirreducibility).). Composite keyComposite key: key that consists of more than one : key that consists of more than one

attribute.attribute. Ex. Branch rel.: branchNo or postcode; Viewing rel.: Ex. Branch rel.: branchNo or postcode; Viewing rel.:

clientNo+propertyNoclientNo+propertyNo

http://arief.ismy.web.id

22

Relational KeysRelational Keys►Primary KeyPrimary Key

Candidate key selected to identify tuples Candidate key selected to identify tuples uniquely within relation.uniquely within relation.

►Alternate KeysAlternate Keys Candidate keys that are not selected to be Candidate keys that are not selected to be

primary key. primary key.

►Foreign KeyForeign Key Attribute, or set of attributes, within one Attribute, or set of attributes, within one

relation that matches candidate key of relation that matches candidate key of some (possibly same) relation.some (possibly same) relation.

Ex. Staff rel.: branchNoEx. Staff rel.: branchNo

http://arief.ismy.web.id

23

Relational IntegrityRelational Integrity►To ensure that the To ensure that the data is accuratedata is accurate

►NullNull Represents value for an attribute that is Represents value for an attribute that is

currently currently unknownunknown or or not applicablenot applicable for for tuple.tuple.

Deals with incomplete or exceptional data.Deals with incomplete or exceptional data. Represents the Represents the absence of a valueabsence of a value and is and is

not the same as not the same as zerozero or or spacesspaces, which are , which are values.values.

http://arief.ismy.web.id

24

Relational IntegrityRelational IntegrityTYPE:TYPE:►Domain ConstraintsDomain Constraints

Restrict the values allowed for the Restrict the values allowed for the attributes of relations (see: domain attributes of relations (see: domain definition).definition).

► Integrity RuleIntegrity Rule Entity integrityEntity integrity Referential integrityReferential integrity

► Enterprise ConstraintsEnterprise Constraints

http://arief.ismy.web.id

25

Relational IntegrityRelational Integrity► Entity IntegrityEntity Integrity

In a base relation, In a base relation, no attributeno attribute of a primary of a primary key can be key can be nullnull..►Be careful: Viewings rel.: Be careful: Viewings rel.: CompositeComposite Primary Key Primary Key

►Referential IntegrityReferential Integrity If foreign key exists in a relation, either If foreign key exists in a relation, either

foreign key value must foreign key value must match a candidate match a candidate keykey value of some tuple in its home relation value of some tuple in its home relation or foreign key value must be or foreign key value must be wholly nullwholly null..►Be careful: Staff rel.: Null for branchNo attr. of Be careful: Staff rel.: Null for branchNo attr. of

new staff that has not yet been assign to a new staff that has not yet been assign to a particular branchparticular branch

http://arief.ismy.web.id

26

Relational IntegrityRelational Integrity

►Enterprise ConstraintsEnterprise Constraints Additional rules specified by users Additional rules specified by users

or database administrators.or database administrators.►Ex. Max. staff number for a branch is Ex. Max. staff number for a branch is

20.20.

http://arief.ismy.web.id

27

ViewsViews

►Base RelationBase Relation Named relation corresponding to an Named relation corresponding to an

entityentity in conceptual schema, whose in conceptual schema, whose tuples are physically stored in tuples are physically stored in database.database.

►ViewView Dynamic result of one or more Dynamic result of one or more

relational operationsrelational operations operating on base operating on base relations to produce another relation. relations to produce another relation.

http://arief.ismy.web.id

28

ViewsViews►A A virtual relationvirtual relation that does not necessarily that does not necessarily

actually exist in the database but is actually exist in the database but is produced upon request, at time of request.produced upon request, at time of request.

► Contents of a view are defined as a Contents of a view are defined as a queryquery on one or more base relations. on one or more base relations.

►Views are Views are dynamicdynamic, meaning that changes , meaning that changes made to base relations that affect view made to base relations that affect view attributes are immediately reflected in the attributes are immediately reflected in the view. view.

http://arief.ismy.web.id

29

Purpose of ViewsPurpose of Views

► Provides powerful and flexible Provides powerful and flexible securitysecurity mechanismmechanism by hiding parts of database by hiding parts of database from certain users. from certain users.

► Permits users to Permits users to accessaccess data in a data in a customizedcustomized way, so that same data can way, so that same data can be seen by different users in different be seen by different users in different ways, at same time.ways, at same time.

► Can Can simplify complex operationssimplify complex operations on base on base relations. relations.

http://arief.ismy.web.id

30

Updating ViewsUpdating Views

►All updates to a base relation All updates to a base relation should be should be immediately reflectedimmediately reflected in all views that reference that in all views that reference that base relation. base relation.

►If If view is updatedview is updated, underlying , underlying base relation should reflect base relation should reflect change.change.

http://arief.ismy.web.id

31

Updating ViewsUpdating Views► There are restrictions on types of There are restrictions on types of

modifications that can be made modifications that can be made through views:through views:

- Updates are allowed if query involves - Updates are allowed if query involves a a single base relationsingle base relation and contains a and contains a candidate keycandidate key of base relation. of base relation.

- Updates are not allowed involving - Updates are not allowed involving multiple base relationsmultiple base relations..

- Updates are not allowed involving - Updates are not allowed involving aggregationaggregation or or groupinggrouping operations. operations.

http://arief.ismy.web.id

32

Updating ViewsUpdating Views

►Classes of views are defined as:Classes of views are defined as: theoretically theoretically not updateablenot updateable;; theoretically theoretically updateableupdateable;; partially updateablepartially updateable. .

http://arief.ismy.web.id

top related