Sub topic 2
Attribute Types in an ER Diagram
This chapter assumes you know what an entity and an entity set are, and what a table, row, and column are. It also assumes basic CREATE TABLE syntax. Here you learn how to classify attributes and how each type is drawn and mapped to tables. GATE questions on ER diagrams often depend on these classifications.
What an attribute is
An attribute is a property that describes an entity. Each attribute takes its values from a domain, which is the set of allowed values. For example, the domain of salary might be positive decimals with two places.
Attributes are classified along three independent axes:
- Structure: simple or composite.
- Number of values: single-valued or multivalued.
- Origin: stored or derived.
A key attribute is a separate role that an attribute can play. One attribute can combine properties from several axes. For example, address can be composite, single-valued, and stored.
Simple and composite attributes
A simple (atomic) attribute cannot be divided into smaller parts that the model cares about. emp_id and salary are simple.
A composite attribute is built from several component attributes. name can be made of first_name and last_name. address can be made of street, city, and pin_code. Composites can nest, so street could itself split into house_no and street_name.
Make an attribute composite only when queries need its parts. If you only ever print the full address, treat it as simple.
Single-valued and multivalued attributes
A single-valued attribute holds at most one value per entity. birth_date is single-valued.
A multivalued attribute can hold several values for one entity. phone is multivalued if an employee can have many numbers. It is drawn as a double ellipse. Some textbooks also write the allowed count as {min, max}, for example {1, 3}.
Composite and multivalued attributes can be combined. A multivalued composite attribute, such as several addresses, each with street and city, is possible.
Stored and derived attributes
A derived attribute can be computed from other attributes or from related entities. age is derived from birth_date and the current date. num_employees of a department is derived by counting related employee entities. It is drawn as a dashed ellipse.
The attribute that the value is computed from is the stored attribute. Storing age would be a mistake, because the stored value becomes wrong every year. Storing birth_date is correct.
Key attributes
A key attribute has a value that is unique for each entity in an entity set. It is drawn with its name underlined. A key can be composite. For example, (course_id, section_no) can together identify a class section.
An entity set with no key of its own is a weak entity set. That topic has its own chapter.
Notation summary
| Attribute type | Drawing | Example |
|---|---|---|
| Simple, single-valued | Single ellipse | `salary` |
| Composite | Ellipse with child ellipses attached | `name` with `first_name`, `last_name` |
| Multivalued | Double ellipse | `phone` |
| Derived | Dashed ellipse | `age` |
| Key | Underlined name | `emp_id` |
| Null-permitted value | No special symbol | `middle_name` |
How each type becomes columns
The mapping rules come from the ER-to-relational step, but they follow directly from the attribute type.
- Simple attribute: becomes one column.
- Composite attribute: only the leaf components become columns. The composite name itself gets no column.
- Multivalued attribute: becomes a separate table. Its primary key is the owner's key plus the attribute. A column cannot hold a set without breaking first normal form.
- Derived attribute: gets no column. It is computed in queries or in a view.
- Key attribute: becomes the primary key, or a
UNIQUEcolumn if there are several candidate keys.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
birth_date DATE
);
CREATE TABLE employee_phone (
emp_id INT,
phone VARCHAR(15),
PRIMARY KEY (emp_id, phone),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE
);The derived attribute age is computed when needed. This query uses PostgreSQL syntax:
SELECT emp_id, EXTRACT(YEAR FROM age(birth_date)) AS age
FROM employee;Counting columns and tables
GATE asks you to count the tables or columns that a diagram produces. Work through one entity:
Student(roll_no, name(first, last), phone{}, dob, age)
Here phone is multivalued, age is derived, and roll_no is the key.
studenttable:roll_no,first,last,dob. That is 4 columns.student_phonetable:roll_no,phone. That is 2 columns.ageadds nothing.
The result is 2 tables.
Common errors
- Mapping the composite itself. Creating a
namecolumn as well asfirstandlastduplicates data. - Storing derived values. The stored copy goes out of date.
- Putting a multivalued attribute in one column as a comma-separated string. This breaks first normal form and makes searches awkward.
- Confusing composite with multivalued. Composite means one value with parts. Multivalued means many values.
- Treating a key as a single column. A key can be composite, as with
(course_id, section_no). - Forgetting that a multivalued composite needs its own table with all leaf components as columns.
Exercise
An entity Customer has attributes cust_id (key), name (composite of first and last), email (multivalued), dob, and age (derived). How many tables and how many columns in total result from mapping it to the relational model?
Show answer
There are 2 tables. The customer table has cust_id, first, last, and dob, which is 4 columns. The customer_email table has cust_id and email, which is 2 columns. The derived age gets no column and the composite name gets none either. The total is 6 columns.
Exercise
Why can a multivalued attribute not simply be stored as one column of the owner's table?
Show answer
A column holds one atomic value per row. A set of values in one column breaks first normal form, and it makes searching and updating individual values unreliable. A separate table with the owner's key plus the value keeps every cell atomic.