Lecture Notes 3 - Part 2
Lecture Notes 3 - Part 2
ADVANCED DATABASES
Data Modelling Processes
(Part 2)
Keys in a Database Management System
What are Keys in a Database Management System (DBMS)?
Key is also helpful for finding unique record or row from the
table.
Here are some reasons for using SQL key in the DBMS system.
1. Super Key
2. Primary Key
3. Candidate Key
4. Alternate Key
5. Foreign Key
6. Compound Key
7. Composite Key
8. Surrogate Key
Here are brief explanations of each of the keys commonly used
in a database management system:
1. Super Key
A super key is a set of one or more attributes that, taken
together, uniquely identify each record within a table.
It may contain more attributes than necessary to uniquely
identify records, ie a Super key may have additional
attributes that are not needed for unique identification
A super key has relaxed rules and includes candidate keys,
primary keys, alternate keys and other attributes.
A super key may contain null values and repeated attributes.
2. Primary Key
A primary key is a specific type of super key that uniquely
identifies each record in a table.
It must be unique for each record and cannot contain null
values.
Primary keys enforce entity integrity and are used to
establish relationships between tables in a relational
database.
3. Candidate Key
A candidate key is a minimal super key, meaning it is a super
key with the fewest possible attributes necessary to uniquely
identify records.
In a table, there may be multiple candidate keys from which
one is chosen to be the primary key.
4. Alternate Key
An alternate key is any candidate key that is not
selected as the primary key.
It serves as an alternative means of uniquely identifying
records in a table.
5. Foreign Key
A foreign key is a field in one table that refers to the
primary key in another table.
It establishes a relationship between the two tables by
enforcing referential integrity, ensuring that values in the
foreign key column match values in the primary key
column of the related table.
6. Compound Key
A compound key is a primary key composed of two or more
attributes.
It is possible that each column may not be unique by itself
within the database.
It is used when no single attribute uniquely identifies records,
but the combination of multiple attributes does.
It may also be referred as a composite key. The two terms
are often used interchangeably.
7. Composite Key
Composite key is another term for a compound key, a
primary key composed of multiple attributes.
The main difference between the two is that attributes that
make a primary key in composite key are usually NOT used
for any other purpose.
The individual components of a compound key may however
be used for other roles such as foreign keys.
8. Surrogate Key
A surrogate key is an artificially generated unique identifier
used as the primary key in a table.
It has no inherent meaning and is typically implemented as
an auto-incrementing integer or a globally unique identifier
(GUID).
Surrogate keys are normally used when there is no suitable
natural key available; ie when there is no column with
properties of a primary key.
OR when the natural key is subject to change; such as an
email address.
OR in the table when the primary key is too big or
complicated.
Primary Key + Alternate Key = Candidate Key
Primary Key Foreign Key
You can ONLY have the single You can have multiple foreign
Primary key in a table. keys in a table.
Entity Representation
One common method to represent an entity is to use entity-
relationship diagrams, where each entity is represented by a
box with two compartments, the first for entity name and the
second for attributes.
You may also come across diagrams that employ ellipses to
represent the attributes belonging to each entity.
The 'crows foot' link shows that there may be more than one
member of the orchestra for each type of musical instrument.
22 Phiri Tasila
55 Mulenga Mary 22
76 Moonga Mutinta 22
and
Optional Relationships
We can extend the entity-relationship model by declaring that
some relationships are mandatory, whereas others are optional.
and
Performers are paid a fee for each booking, and agents earn
commission on the fee paid to each performer.
and
participation conditions
1. Both ends mandatory
It might be the case that each performer has only one agent,
and that all bookings for any one performer must be made by
one agent, and that agent may only make bookings for that one
performer.
The solid circle at each end of the relationship shows that the
relationship is mandatory in both directions; each performer
must have an agent, and each agent must deal with one
performer.
2. One end mandatory, other end optional:
It might be possible for agents to make bookings that do not
involve performers; for example, a venue might be booked for
an art exhibition. Each performer, however, must have an agent,
although an agent does not have to make a booking on behalf
of a performer.
The solid circle at the agent end of the relationship shows each agent
must be associated with a performer. The hollow circle at the
performer end of the relationship indicates that a performer could be
represented by an agent, but that this is not compulsory. Each agent
must deal with only one performer, but each performer does not have
to have an agent.
4. Both ends optional:
Another possibility is that agents may make bookings that do
not involve performers; for example, a venue might be booked
for an art exhibition.
and
participation conditions
It might be the case that a performer has only one agent, and
that all bookings for any one performer must be made by one
agent, although any agent may make bookings for more than
one performer.
and
participation conditions
We could say that there is a many-to-many relationship
between performers and agents, with each agent making
bookings for many performers, and each performer having
bookings made by many agents.
Agents are not allowed to make bookings for events that do not
involve performers (such as conferences or exhibitions).
and
Strong Entities
A weak entity set is one whose existence is dependent on the
existence of a strong entity set; also called the identifying entity
set.
We may also say that an entity set that does not have a primary
key of its own is referred to as a weak entity set.
with
and
The above model looks okay at first glance, but it has a pitfall.
0...* 1 1 1...*
Staff Campus Department
FAN TRAPS
Campus
ID
Campus Name
C1
- London
1 Campus
C2 Lagos
C3 Cairo
1...* 1 1...* 1
Staff Department Campus
Campus Campus Name
ID
C1FAN London
TRAPS
Campus
C2 -2
Lagos
C3 Cairo
We can clearly note that, not all departments have staff and not
all staff belong to a department.
2 Redbridge
House’