ER Diagram
An ER (Entity-Relationship) diagram is a visual blueprint of a database's structure - it shows the entities (things you want to store data about), their attributes, and how the entities relate to one another, before any table is actually created.
Core Components
- Entity - a real-world object or concept, like Customer or Order, usually drawn as a rectangle
- Attribute - a property of an entity, like name or email, drawn as an oval
- Relationship - how two entities are connected, like "Customer places Order", drawn as a diamond
- Primary Key - the attribute that uniquely identifies each entity instance, usually underlined
Types of Relationships
- One-to-One - one record in Table A relates to exactly one record in Table B
- One-to-Many - one record in Table A relates to many records in Table B (e.g. one Customer, many Orders)
- Many-to-Many - many records in Table A relate to many records in Table B (e.g. Students and Courses)
Why ER Diagrams Matter
Designing an ER diagram before writing any SQL helps catch structural problems early - like missing relationships or duplicated data - and makes it much easier to translate the design into actual CREATE TABLE statements.
A well-designed ER diagram is the difference between a database that scales cleanly and one that becomes tangled with redundant, inconsistent data.