Database Er Model

#ER model

The ER data model employs three basic concepts: entity set, relationship set, attributes.

#Entity sets:

An entity is an object that exists and is distinguishable from other objects. Example: specific person, company, event, plant

An entity set is a set of entities of the same type that share the same properties. Example: set of all persons, companies, trees, holidays.

An entity is represented by a set of attributes: i.e. descriptive properties possessed by all members of an entity set. Examples: instructor = (ID, name, street, city, salary) course = (course_id, title, credits)

A subset of attributes form a primary key of the entity set. i.e., uniquely identifying each member of the set.

#Relationship sets

A relationship is an association among several entities.

Example:

student entity relationship instructor entity

A relationship set is a mathematical relation among $n \geq 2$ entities, each taken from entity sets

$$\{ (e_1, e_2, \cdots , e_n) | e_1 \in E_1, e_2 \in E_2, \cdots, e_n \in E_n \}$$

where $(e_1, e_2, e_3, \cdots, e_n)$ is a relationship.

Example:

$(44553, 231232) \in advisor$

An attribute can also be property of a relationship set. For instance, the advisor relationship set between entity sets instructor and student may have the attribute date which tracks when the student started being associated with the advisor

#Degree of a relationship set

binary relationship:

1. involve two entity sets(or degree two)
2. most relationship sets in a database system are binary.

relationships between more than two entity sets are rare, most relationships are binary.

Example: student work on research projects under the guidance of an instructor. relationship proj_guide is a ternary relationship between instructor, student and project.

#Mapping cardinality constraints

Express the number of entities to which another entity can be associated via a relationship set.

Most useful in describing binary relationship sets.

For a binary relationship set the mapping cardinality must be one of the following types:

1. one to one
2. one to many
3. many to one
4. many to many
#Identifying relationship

An identifying relationship is when the existence of a row in a child table depends on a row in a parent table. This may be confusing because it's common practice these days to create a pseudokey for a child table, but not make the foreign key to the parent part of the child's primary key. Formally, the "right" way to do this is to make the foreign key part of the child's primary key. But the logical relationship is that the child cannot exist without the parent.

Example: A Person has one or more phone numbers. If they had just one phone number, we could simply store it in a column of Person. Since we want to support multiple phone numbers, we make a second table PhoneNumbers, whose primary key includes the person_id referencing the Person table.

We may think of the phone number(s) as belonging to a person, even though they are modeled as attributes of a separate table. This is a strong clue that this is an identifying relationship (even if we don't literally include person_id in the primary key of PhoneNumbers).

A non-identifying relationship is when the primary key attributes of the parent must not become primary key attributes of the child. A good example of this is a lookup table, such as a foreign key on Person.state referencing the primary key of States.state. Person is a child table with respect to States. But a row in Person is not identified by its state attribute. I.e. state is not part of the primary key of Person.

A non-identifying relationship can be optional or mandatory, which means the foreign key column allows NULL or disallows NULL, respectively.

stack overflow: identifying relationships

#Weak entity sets

An entity set that does not have a primary key is referred to as a weak entity set.

The existence of a weak entity set depends on the existence of a identifying entity set.

1. It must relate to the identifying entity set via a total, one-to-many relationship set from the identifying to the weak entity set.
2. Identifying relationship depicted using a double diamond

The discriminator (primary key) of a weak entity set is the set of attributes that distinguished among all the entities of a weak entity set.

The primary key of a weak entity set is formed by the primary key of the strong entity set on which the weak entity set is existence dependent, plus the weak entity set's discriminator.

We underlining the discriminator of a weak entity set with a dashed line. We put the identifying relationship of a weak entity in a double diamond. Primary key for section - (course_id, sec_id, semester, year)

Note: the primary key of the strong entity set is not explicitly stored with the weak entity set, since it is implicit in the identifying relationship.

If the course_id were explicitly stored, section could be made a strong entity, but then the relationship between section and course would be duplicated by an implicit relationship defined by the attribute course_id common to course and section.

#Weak entity:

In a relational database, a weak entity is an entity that cannot be uniquely identified by its attributes alone; therefore, it must be use a foreign key in conjunction with its attributes to create a primary key.

In entity relationship diagrams, a weak entity set is indicated by a bold (or double-lined) rectangle(the entity) connected by a bold(or double-lined) type arrow to a bold(or double-lined) diamond(the relationship). This type of relationship is called an identifying relationship and in IDEF1X notation it it represented by an oval entity rather than a square entity for base tables.

An identifying relationship is one where the primary key is populated to the child weak entity as a primary key in that entity.

#Attributes

An entity is represented by a set of attributes, that is descriptive properties possessed by all members of an entity set.

Examples: instructor = (ID, name, street, city, salary) course = (course_id, title, credits)

Domain: the set of permitted values of each attribute.

Attribute types:

Simple and composite attributes.

Single-valued and multivalued attribute. Example: multivalued attribute: phone_numbers

Derived attributes: Can be computed from other values Example: age, given data_of_birth

Composite attributes: fist_name, middle_name, last_name -> name

street, city, state, postal_code -> address street_number, street_name, apartment_number -> street

#Keys

A super key of an entity set is a set of one or more attributes whose values uniquely determine each entity.

A candidate key of an entity set is a minimal super key, examples: ID is candidate key of instructor, course_id is candidate key of course

Although several candidate keys may exist, one of the candidate keys is selected to be the primary key.

#Keys for relationship sets

The combination of primary keys of the participating entity sets forms a super key of a relationship set. (s_id, i_id) is the super key of advisor Note, this meas a pair of entity sets can have at most one relationship in a particular relationship set. Example: if we wish to track multiple meeting dates between a student and her advisor, we cannot assume a relationship for each meeting. We can use a multivalued attribute though.

#ER Diagrams

Rectangles represent entity sets

Diamonds represent relationship sets

Attributes listed inside entity rectangle

Underline indicates primary key attributes

Entity relationship diagrams(ER diagrams)

1. Entities and relationships
2. Entity set (boxed) and relationship set (diamonds) to connect the boxes.
3. Additional info via underlining(keys), arrows (type of relationship), etc
4. Relationship models how entity related to each other, not via shared attributes (foreign keys) as in relational model.
#Cardinality constraints

We express cardinality constraints by drawing either a directed line ($\rightarrow$), signifying "one" or an undirected line ($-$), signifying "many", between the relationship set and the entity set.

one-to-one relationship between an instructor and a student

1. an instructor is associated with at most one student via advisor
2. and a student is associated with at most one instructor via advisor

one-to-many relationship between an instructor and a student

1. an instructor is associated with several (including 0) students via advisor
2. a student is associated with at most one instructor via advisor

many-to-one relationship between an instructor and a student

1. an instructor is associated with at most one student via advisor
2. and a student is associated with several (including 0) instructors via advisor

many-to-many relationship between an instructor and a student

1. an instructor is associated with several (possibly 0) students via advisor
2. a student is associated with several (possibly 0) instructors via advisor
#Participation of an entity set in a relationship set

Total participation (indicated by double line): every entity in the entity set participates in at least one relationship in the relationship set.

Partial participation: some entities may not participate in any relationship in the relationship set.

#Cardinality constraints on ternary relationship

We allow at most one arrow out of a ternary(or greater degree) relationship to indicate a cardinality constraint

If there is more than one arrow, there are two ways of defining the meaning:

a ternary relationship R between A, B and C, with arrows to B and C could mean

1. each A entity is associated with a unique entity from B and C or
2. each pair of entities from (A, B) is associated with a unique C entity, and each pair (A, C) is associated with q unique B

To avoid confusion, we outlaw more than one arrow.

#Reduction to relation schemas

Entity sets and relationship sets can be expressed uniformly as relation schemas that represent the contents of the database.

A database which conforms to an E-R diagram can be represented y a collection of schemas.

For each entity set and relationship set there is a unique schema that is assigned the name of the corresponding entity set or relationship set.

Each schema has a number of columns (generally corresponding to attributes), which have unique names

A weak entity set becomes a table that includes a column for primary key of the identifying strong entity set. section(course_id, sec_id, semester, year)

#representing relationship set

A many-to-many relationship set is represented as a schema with attributes for the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set.

#Composite and multivalued attributes

Composite attributes are flattened out by creating a seperate attribute for each component attribute.

Ignoring multivalued attributes, extended instructor schema is instructor(ID, first_name, middle_name, last_name, street_number, apt_number, city, state, zip_code, date_of_birth)

A multivalued attribute M of an entity E is represented by a separate schema EM

Schema EM has attributes corresponding to the primary key of E and an attribute corresponding to multivalued attribute M. Example: Multivalued attribute phone_number of instructor is represented by a schema: inst_phone = (ID, phone_number)

Each value of the multivalued attribute maps to a separate tuple of the relation on schema EM

Special case: entity time_slot has only one attribute other than the primary-key attribute, and that attribute is multivalued. Don't create the relation corresponding to the entity, just create the one corresponding to the multivalued attribute.

#E-R design Decisions

The use of an attribute or entity set to represent an object

Whether a real-world concept is best expressed by an entity set or a relationship set.

The use of a ternary relationship versus a pair of binary relationships.

The use of a strong or weak entity set.

The use of specialization/generalization - contributes to modularity in the design.

The use of aggregation - can treat the aggregate entity set as a single unit without concern for the details of its internal structure.