Entities and attributes: from requirements to tables
Overview
A training center needs to record learners, instructors, courses and enrollments. An entity is a distinguishable thing or concept about which the system stores facts; an entity type describes a class of such things. An attribute is a property of that type. A learner with id 1 is an instance, while Learner is the type. The key identifies an instance, even when two learners have the same name. Load er-training.sql from this batch into a fresh SQLite database with foreign keys enabled before running these examples.
First example
SELECT id, name FROM er_learner ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1, "Mina" ], [ 2, "Mina" ], [ 3, "Omid" ]]How it works
The first query shows two distinct learners named Mina; names are not identifiers. The second calculates how many enrollments each learner has, a derived attribute rather than a stored counter. An attribute can be simple (name), composite in the conceptual model (address with city and street), multivalued (phone numbers), or derived (enrollment count). Represent multivalued attributes in a related table if each value must be queried independently. A dependent enrollment is an associative entity with its own status. A weak entity in the classical ER sense needs an owner key plus a partial key for identification, such as a course lesson numbered within its course; not every table with a foreign key is weak.
Practical example
SELECT l.id, COUNT(e.course_id) AS enrollmentsFROM er_learner l LEFT JOIN er_enrollment e ON e.learner_id=l.idGROUP BY l.id ORDER BY l.id;Observed SQLite result (rows in JSON):
[ [ 1, 2 ], [ 2, 1 ], [ 3, 0 ]]Practice and mistakes
Practice: list learner IDs and names alongside counts of active enrollments, retaining learners without enrollments. Hint: put the status filter in the LEFT JOIN ON clause. Expected counts for IDs 1–3 are 1, 1, 0. Do not split entities merely because two attributes have different types. First ask which real-world facts must be recorded and which identify the same thing.