04-Relational Model and Keys
04-Relational Model and Keys
Relational Model
& Keys
Dr. Maqbool Khan
1 www.pktutorials.com
Relational Model:
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.
2 www.pktutorials.com
Relational Model:
▪ First project by IBM in 1970 was the prototype relational DBMS System R,
that led to:
▪ The development of a structured query language called SQL.
▪ The production of various commercial RDBMS products during the late
1970s and the 1980s, For example:
DB2 and SQL/DS from IBM and Oracle from Oracle Corporation.
3 www.pktutorials.com
Relational Database Terminology:
Relation is a table with columns and rows. Only applies to logical structure of
the database, not the physical structure.
Attribute is a named column of a relation.
Domain is the set of allowable values for one or more attributes.
Tuple is a row of a relation.
4 www.pktutorials.com
Relational Database Terminology:
5 www.pktutorials.com
Alternative Terminology for Relational Model:
6 www.pktutorials.com
Examples: Branch and Staff Relations:
7 www.pktutorials.com
Examples of Attribute Domains:
8 www.pktutorials.com
Mathematical Definition of Relation:
▪ 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)}
▪ Alternative way is to find all combinations of elements with first from D1
and second from D2.
9 www.pktutorials.com
Mathematical Definition of Relation:
Any subset of Cartesian product is a relation; e.g.
R = {(2, 1), (4, 3)}
May specify which pairs are in relation using some condition for selection e.g.
second element is 1:
R = {(x, y) | x ∈D1, y ∈D2, and y = 1}
R = {(2, 1), (4, 1)}
S = {(2,1)}
10 www.pktutorials.com
Mathematical Definition of Relation:
Consider three sets D1, D2, D3 with Cartesian Product D1 × D2 × D3; e.g.
D1 = {1, 3}
D2 = {2, 4}
D3 = {5, 6}
11 www.pktutorials.com
Mathematical Definition of Relation:
Cartesian product of n sets (D1, D2, . . ., Dn) is:
D1 × D2 × . . . × Dn = {(d1, d2, . . . , dn) | d1 ∈D1, d2 ∈D2, . . . , dn∈Dn}
usually written as:
Any set of n-tuples from this Cartesian product is a relation on the n sets.
12 www.pktutorials.com
Database Relations:
Relation schema
▪ 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)}
13 www.pktutorials.com
Database Relations:
▪ If R1, R2, . . . , Rn are a set of relation schemas, then we can write the relational
database schema, or simply relational schema, R, as:
14 www.pktutorials.com
Properties of Relations:
▪ Relation name is distinct from all other relation names in relational schema.
▪ Each cell of relation contains exactly one atomic (single) value. (e.g., a phone
field may not contain multiple phone numbers)
▪ 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.
▪ Order of tuples has no significance, theoretically.
15 www.pktutorials.com
Relational Keys:
▪ Candidate Key
▪ Primary Key
▪ Super Key
▪ Alternate Key
▪ Foreign Key
▪ Composite Key
16 www.pktutorials.com
Candidate Key:
▪ The minimal set of attributes that can uniquely identify a tuple is known as a
candidate key. For Example, STUD_NO in STUDENT relation.
17 www.pktutorials.com
Candidate Key:
The candidate key can be simple (having only one attribute) or composite as well.
18 www.pktutorials.com
Primary Key:
▪ There can be more than one candidate key in relation out of which one can be
chosen as the primary key.
▪ It is a unique key.
▪ It can identify only one tuple (a record) at a time.
▪ It has no duplicate values, it has unique values.
▪ It cannot be NULL.
▪ Primary keys are not necessarily to be a single column; more than one column
can also be a primary key for a table.
19 www.pktutorials.com
Primary Key:
20 www.pktutorials.com
Super Key:
▪ The set of attributes that can uniquely identify a tuple is known as Super Key.
▪ Adding zero or more attributes to the candidate key generates the super key.
▪ A candidate key is a super key but vice versa is not true.
▪ Super Key values may also be NULL.
21 www.pktutorials.com
Super Key:
22 www.pktutorials.com
Alternate Key:
▪ The candidate key other than the primary key is called an alternate key.
▪ All the keys which are not primary keys are called alternate keys.
▪ It is a secondary key.
▪ It contains two or more fields to identify two or more records.
▪ These values are repeated.
▪ Eg:- SNAME, and ADDRESS is Alternate keys
23 www.pktutorials.com
Alternate Key:
24 www.pktutorials.com
Foreign Key:
▪ It is a key it acts as a primary key in one table, and it acts as secondary key in
another table.
▪ It combines two or more relations (tables) at a time.
▪ They act as a cross-reference between the tables.
▪ For example, DNO is a primary key in the DEPT table and a non-key in EMP
25 www.pktutorials.com
Foreign Key:
26 www.pktutorials.com
Composite Key:
▪ It acts as a primary key if there is no primary key in a table
▪ Two or more attributes are used together to make a composite key.
▪ Different combinations of attributes may give different accuracy in terms of
identifying the rows uniquely.
27 www.pktutorials.com
Relational Keys:
28 www.pktutorials.com
Relational Keys:
29 www.pktutorials.com
Relational Keys:
30 www.pktutorials.com
Representing Relational Database Schemas
31 www.pktutorials.com
THANKS
32 3/12/2024 www.pktutorials.com