Professional Documents
Culture Documents
Entity Relationship Model and Diagram
Entity Relationship Model and Diagram
COMPONENTS OF ER DIAGRAM
Entity
Attributes
Relationship
Weak entity
Strong entity
Simple attribute
Key attribute
Composite attribute
Derived attribute
Multivalued attribute
ENTITY
It may be an object, person, place or event that stores data in a database. In a
relationship diagram an entity is represented in rectangle form.
ENTITY SET
It is a collection of entities of the same type which share similar properties.
For example, a group of students in a college and students are an entity set
Entity is characterized into two types as follows −
ATTRIBUTES
It is the name, thing etc. These are the data characteristics of entities or data
elements and data fields.
TYPES OF ATTRIBUTES
The types of attributes in the Entity Relationship (ER) model are as follows –
1. Primary Key
2. Natural Key
Advantages of a Natural Key
When designing your database, you’ll need to choose a primary key for each
of your tables. One option you have is to choose a natural key.
There are several advantages of using a natural key as a primary key:
It already exists, meaning you don’t need to create a new column in
your table.
It simplifies the quality of your data as it ensures only one row can
exist for this value
3. Candidate Key
The candidate keys refer to those attributes that identify rows uniquely in a
table. In a table, we select the primary key from a candidate key. Thus, a
candidate key has similar properties as that of the primary keys that we have
explained above. In a table, there can be multiple candidate keys.
Using our earlier examples, these would be candidate keys for a “customer”
table:
These keys are all candidate keys, as they are an option or candidate to be the
primary key. They don’t contain any redundant attributes – we don’t specify
“Social Security Number and First Name” as a candidate key, because Social
Security Number is a key by itself. Unlike the previous keys we looked at
(Natural, Surrogate, Composite), a candidate key is a name for existing types
of keys, and not a new type of key.
4. Alternate Key
As we have stated above, any table can consist of multiple choices for the
primary key. But, it can only choose one. Thus, all those keys that did not
become a primary key are known as alternate keys.
They are an option or alternate to the primary key. This can also be called
a unique key.
For example, using the candidate keys:
o Social Security Number
o Customer ID
o First Name, Last Name, Date of Birth
If the Social Security Number was chosen as the primary key, then
Customer ID and “First Name, Last Name, Date of Birth” would be
alternate keys.
Unlike the previous keys we looked at (Natural, Surrogate, Composite), an
alternate key is a name for existing types of keys, and not a new type of
key.
5. Foreign Key
We use a foreign key to establish relationships between two available tables.
The foreign key would require every value present in a column/set of columns
to match the referential table’s primary key. A foreign key helps us to
maintain data as well as referential integrity.
They are used to specify that this record in this table is related to that
record in that table.
Using the earlier customer and bank account example, you are a
customer at a bank. You may have several accounts with that bank.
These account details would be records in an account table.
6. Composite Key
The composite key refers to a set of multiple attributes that help us uniquely
identify every tuple present in a table. The attributes present in a set may not
be unique whenever we consider them separately. Thus, when we take them
all together, it will ensure total uniqueness.
The attributes already exist, so you don’t need to create any new
attributes or columns on your table like you do with the surrogate
key. This simplifies the table and reduces storage.
The combination of the attributes ensures the record is unique,
like the natural key
It allows you to use business values to identify a record if a single
value does not uniquely identify a record. If a natural key doesn’t
work, you can use a composite key.
Risk of change to the business rules. Like a natural key, if any of the
business rules change (type of field, format of data) then this will need
to be updated. Also, the data may no longer uniquely identify a row so
you may need to expand your composite key.
All fields are needed in related tables. When you want to refer to this
record from another table, you’ll need to store all of the fields in the
composite key in another table.
7. Unique Key
A unique key refers to a column/a set of columns that identify every record
uniquely in a table. All the values in this key would have to be unique.
Remember that a unique key is different from a primary key. It is because it is
only capable of having one null value. A primary key, on the other hand,
cannot have a null value.
8. Surrogate Key
The word surrogate means substitute. A surrogate key is an attribute that is
invented or made up for the sole purpose of being used as the primary key. It
has no value to the business or the real world.
An example of a surrogate key is an “address ID” for a table of
addresses. Outside of the system, an address ID has no value to
anyone. But for the database, it’s used to uniquely identify the record.
Surrogate keys are often used when there is no other way to identify a
record – when there is no natural key. They are often an integer value,
starting at 1 and incrementing for each new record.
It has no business or real world value, which means you have control
over the format and usage of the value. No matter what happens to
the other data in the record (e.g. change in formats or types), this key
won’t change.
It’s easy to define. There’s no need to work out which of the existing
attributes can make a key, as you can use a new column for the
identifier.
It’s another extra column in your table, which increases storage space.
However, storage is cheap so this may not be an issue.
You can’t look at the key and determine anything about the record,
unlike a Social Security Number or a Country Name.