CampusFlow

πŸ—ΊοΈ ER Diagram Builder

Design and visualize entity-relationship models for relational databases. Learn how entities, attributes, and relationships map to normalized database schemas.

ER Modeling Theory

πŸ“¦ Entities

Real-world objects or concepts (e.g., Student, Course). Each entity becomes a table in the database.

🏷️ Attributes

Properties describing an entity (e.g., name, email). Become columns in the table. Keys uniquely identify rows.

πŸ”— Relationships

Associations between entities (e.g., Student enrolls in Course). Defined by cardinality and participation.

πŸ“ Cardinalities

Define how many instances relate: 1:1, 1:M, or M:N. Determines foreign key placement in tables.

University Management System ERD

πŸ‘€

Student

πŸ”‘student_idPK
β€’name
β€’email
β€’phone
enrolls inM:N
πŸ“‹

Enrollment

πŸ”‘enrollment_idPK
β€’grade
β€’semester
hasM:N
πŸ“–

Course

πŸ”‘course_idPK
β€’title
β€’credits
β€’dept
teaches1:M
πŸ‘¨β€πŸ«

Instructor

πŸ”‘instructor_idPK
β€’name
β€’dept

Relationship Mapping

β€’
Student—enrolls in→Enrollment(M:N)
β€’
Enrollment—has→Course(M:N)— Many students enroll in many courses
β€’
Instructor—teaches→Course(1:M)— One instructor teaches many courses

Relational Schema from ERD

Converting an ER diagram to a relational schema follows these rules:

Strong Entities

Become tables directly. Primary key of the entity becomes the table's PK.

1:M Relationships

Add PK of the "1" side as a foreign key in the "M" side table.

M:N Relationships

Create a bridge table with composite PKs referencing both entities.

-- Entities (strong)
Student(student_id PK, name, email, phone)
Course(course_id PK, title, credits, dept, instructor_id FK→Instructor)
Instructor(instructor_id PK, name, dept)

-- Bridge table (M:N)
Enrollment(enrollment_id PK, student_id FK→Student, course_id FK→Course, grade, semester)

Relationship Types Reference

1️⃣

One-to-One (1:1)

One entity instance relates to exactly one instance of another entity

Example:

One student has exactly one student profile

SQL Rule:

Add UNIQUE constraint to foreign key

1οΈβƒ£βž‘οΈπŸ“¦

One-to-Many (1:M)

One entity instance relates to many instances of another entity

Example:

One department has many employees

SQL Rule:

Place foreign key on the 'many' side table

πŸ“¦πŸ“¦

Many-to-Many (M:N)

Many instances of one entity relate to many instances of another entity

Example:

Many students enroll in many courses

SQL Rule:

Create a junction/bridge table with composite foreign keys

Interview Questions

1What is an Entity-Relationship Diagram (ERD)?

An ERD is a visual representation of entities (objects/concepts) within a system and the relationships between them. It is used in database design to model the logical structure of a database before implementation.

2Explain the difference between strong and weak entities.

A strong entity can exist independently and has its own primary key. A weak entity depends on a strong entity for its existence and does not have a primary key of its own β€” it uses a foreign key combined with a partial discriminator to form a primary key.

3What are cardinality constraints in ER modeling?

Cardinality constraints define the maximum number of instances of one entity that can relate to instances of another entity. The three main types are: One-to-One (1:1), One-to-Many (1:M), and Many-to-Many (M:N). They define the structural constraints of relationships.

4How do you convert an M:N relationship into a relational schema?

An M:N relationship cannot be directly represented. A bridge/junction table is created containing the primary keys of both related entities as composite foreign keys. For example, Enrollment bridges Student and Course with (student_id, course_id) as a composite primary key.

5What is the difference between an entity and an attribute?

An entity is a real-world object or concept (e.g., Student, Course) that is stored as a table in the database. An attribute describes a property of an entity (e.g., student's name, email) and is stored as a column. While entities have an independent existence, attributes are always tied to an entity.