Lecture 4 Relational Model
Lecture 4 Relational Model
cuiatd.edu.pk
subexpert.com
Abbottabad Campus
Computer Science Department
subexpert.com
DATABASE SYSTEMS
Mukhtiar Zamin,
MS (Computer Science)
Iowa, United States of America
[email protected]
1
ACKNOWLEDFMENT
subexpert.com
Lectures are prepared from the following books:
Text Book:
– Database Systems, A practical approach to Design, Implementation and Management
by Thomas Connolly, 4th edition
Other Books:
– Modern Database Management
by Jeffry A. Hoffer, 10th Edition
– Microsoft SQL Server 2008 Database Development
by Microsoft
In addition there are other examples and code snippets from other sources which are
mentioned on respective slides
2
Lecture 4
• Relational Model
• subexpert.com
Relational Data Terminologies
• Examples: Branch and Staff Relations
• Mathematical Definition of Relation
• Examples - Attribute Domains
• Database Relations
• Properties of Relations
• Relational Keys
• Representing Relational Database Schemas
• Integrity Constraints
• Views
– Updating Views
– Purpose of Views
3
Relational Data Terminologies
subexpert.com
• Relation
– A relation is a table with columns and rows.
– Only applies to logical structure of the database,
not the physical structure.
• Attribute
– Attribute is a named column of a relation.
• Domain
– Domain is the set of allowable values for one or
more attributes.
4
Relational Model
subexpert.com
• Based on the mathematical concept of a relation
• Relational model was first proposed by E. F. Codd in 1970
will following objectives:
– Allow a high degree of data independence
– Provide base for dealing with data semantics, consistency, and
redundancy problems (Normalized relations)
– Expansion of set-oriented data manipulation languages
• First project by IBM’s in 1970 was the prototype relational
DBMS System R and led to
– the development of a structured query language called SQL
– production of various commercial relational DBMS products
5
Relational Data Terminologies
subexpert.com
• Tuple
– Tuple is a row of a relation.
• Degree
– Degree is the number of attributes in a relation.
• Cardinality
– Cardinality is the number of tuples in a relation.
• Relational Database
– Relational Database is a collection of normalized relations with distinct
relation names.
Formal Term Alternative 1 Alternative 2
Relation Table File
Tuple Row Record
Attribute Column Field
6
Examples: Branch and Staff Relations
subexpert.com
7
Mathematical Definition of Relation
subexpert.com
• Consider two sets, D1 & D2, where D1 = {2, 4} and D2 = {1, 3, 5}.
• Cartesian product, D1 ´ D2, is set of all ordered pairs, where first element
is member of D1 and second element is member of D2.
D1 ´ D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}
8
Mathematical Definition of Relation
subexpert.com
• Consider three sets D1, D2, D3 with Cartesian Product D1 ´ D2 ´ D3; e.g.
9
Examples - Attribute Domains
subexpert.com
10
Database Relations
• Relation schemasubexpert.com
– Named relation defined by a set of attribute and domain name pairs.
– Let A1, A2, . . . , An be attributes with domains D1, D2, . . . , Dn. Then the set
{A1:D1, A2:D2,. . . , An:Dn} is a relation schema.
– In our example, the Branch relation has attributes branchNo, street, city, and
postcode, each with its corresponding domain.
relation instance {(branchNo: B005, street: 22 Deer Rd, city: London, postcode: SW1 4EH)}
11
Properties of Relations
• subexpert.com
Relation name is distinct from all other relation names
in relational schema.
• Each cell of relation contains exactly one atomic
(single) value.
• Each attribute has a distinct name.
• Values of an attribute are all from the same domain.
• Each tuple is distinct; there are no duplicate tuples.
• Order of attributes has no significance.
• Each tuple is distinct; there are no duplicate tuples.
• Order of attributes has no significance.
• Order of tuples has no significance, theoretically.
12
Relational Keys
• Uniquely identifies each tuple in a relation
• Superkey subexpert.com
– An attribute, or set of attributes, that uniquely identifies a tuple within a
relation.
• Candidate Key
– Superkey (K) such that no proper subset is a superkey within the relation.
– Has two properties
• In each tuple of R, values of K uniquely identify that tuple (uniqueness).
• No proper subset of K has the uniqueness property (irreducibility).
• Composite key
• When a key consists of more than one attribute
• In our Example city value can determine several branch offices (for example,
London has two branch offices). This attribute cannot be a candidate key.
• On the other hand, since each branch office a unique branch number, then
given a branch number value, branchNo, we can determine at most one tuple,
so that branchNo is a candidate key.
13
Relational Keys
• Primary Key
subexpert.com
– Candidate key selected to identify tuples uniquely within relation.
• Alternate Keys
– Candidate keys that are not selected to be primary key.
• Foreign Key
– Attribute, or set of attributes, within one relation that matches
candidate key of some (possibly same) relation.
– When an attribute appears in more than one relation, its
appearance usually represents a relationship between tuples of the
two relations. For example, branchNo
14
Representing Relational Database Schemas
subexpert.com
• The common convention for representing a relation schema is to
give the name of the relation followed by the attribute names in
parentheses.
• E.g.
– Branch (branchNo, street, city, postcode)
– Staff (staffNo, fName, lName, position, sex, DOB, salary, branchNo)
– PropertyForRent (propertyNo, street, city, postcode, type, rooms, rent,
ownerNo, staffNo,branchNo)
– Client (clientNo, fName, lName, telNo, prefType, maxRent)
– PrivateOwner (ownerNo, fName, lName, address, telNo)
– Viewing (clientNo, propertyNo, viewDate, comment)
– Registration (clientNo, branchNo, staffNo, dateJoined)
15
Integrity Constraints
• subexpert.com
Integrity constraints ensure that the data is accurate.
• Domain constraints
– Form restrictions on the set of values allowed for the attributes of
relations
• Integrity rules
– Constraints or restrictions that apply to all instances of the database
– Two principle rules are
• Entity integrity
• Referential integrity
• Multiplicity
• General constraints
• Null
– Represents value for an attribute that is currently unknown or not applicable for
tuple.
– Deals with incomplete or exceptional data.
– Represents the absence of a value and is not the same as zero or spaces, which
are values.
16
Integrity Constraints
• Base Relation subexpert.com
– Named relation corresponding to an entity in conceptual schema,
whose tuples are physically stored in database.
• Entity Integrity
– In a base relation, no attribute of a primary key can be null.
• Referential Integrity
– If foreign key exists in a relation, either foreign key value must
match a candidate key value of some tuple in its home relation or
foreign key value must be wholly null.
• General Constraints
– Additional rules specified by users or database administrators that
define or constrain some aspect of the enterprise.
17
Views
• subexpert.com
Dynamic result of one or more relational operations
operating on base relations to produce another
relation.
18
Updating Views
subexpert.com
• All updates to a base relation should be
immediately reflected in all views that
reference that base relation.
19
Purpose of Views
subexpert.com
• Provides powerful and flexible security
mechanism by hiding parts of database from
certain users.
20