Database Entities, Attributes and ER Diagrams
What you will learn
- What an entity and an attribute are
- How to read and draw a simple ER diagram
- How an ER diagram turns into tables
- How to check the table structure in SQLite
1. Entities and attributes
An entity is a kind of thing you want to store facts about: a teacher, a class, a customer, a product. An attribute is one fact about it: a teacher's name, a class's room.
Think of it in grammar: entities are the nouns, attributes are their details, and relationships are the verbs ("a teacher teaches classes").
| Entity | Attributes |
|---|---|
| Teacher | name, email |
| Class | title, room |
2. The ER diagram
An entity-relationship (ER) diagram is a picture of the entities and how they relate, drawn before any tables exist. Many notations exist; here is a plain text one. Each box is an entity with its attributes, and the line between boxes shows the relationship.
+------------------+ +------------------+
| TEACHER | | CLASS |
|------------------| teaches |------------------|
| id (key) | 1 -------< N | id (key) |
| name | | title |
| email | | room |
+------------------+ +------------------+
Read it: "One teacher teaches many classes." The 1 and N (or the crow's foot <) mark how many on each side; lesson 3.7 gives the exact meaning.
3. From boxes to tables
The conversion is mechanical:
- Each entity becomes a table.
- Each attribute becomes a column (with a data type).
- Each entity gets a primary key.
- A one-to-many line becomes a foreign key on the "many" side.
- A many-to-many line becomes a junction table (lesson 3.4).
Try it on the sample database you chose in lesson 4.1 (use the Example data picker at the top); the examples add two small tables of their own to your browser's copy, and Reset data starts again:
CREATE TABLE brands (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT);
CREATE TABLE promotions (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
room TEXT,
brands_id INTEGER NOT NULL REFERENCES brands (id)
);
(done)
CREATE TABLE advisors (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT);
CREATE TABLE meetings (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
room TEXT,
advisors_id INTEGER NOT NULL REFERENCES advisors (id)
);
(done)
CREATE TABLE mentors (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT);
CREATE TABLE workshops (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
room TEXT,
mentors_id INTEGER NOT NULL REFERENCES mentors (id)
);
(done)
Notice that the foreign key column (such as brands_id) is not an attribute in the diagram. It appears only when the line is turned into tables.
4. Checking the structure
SQLite can describe what you built:
SELECT name, type, "notnull", pk FROM pragma_table_info('brands');
SELECT name, type, "notnull", pk FROM pragma_table_info('promotions');
SELECT "from" AS column_name, "table" AS points_to, "to" AS in_column FROM pragma_foreign_key_list('promotions');
name type notnull pk
----- ------- ------- --
id INTEGER 0 1
name TEXT 1 0
email TEXT 0 0
(3 rows)
name type notnull pk
--------- ------- ------- --
id INTEGER 0 1
title TEXT 1 0
room TEXT 0 0
brands_id INTEGER 1 0
(4 rows)
column_name points_to in_column
----------- --------- ---------
brands_id brands id
(1 row)
SELECT name, type, "notnull", pk FROM pragma_table_info('advisors');
SELECT name, type, "notnull", pk FROM pragma_table_info('meetings');
SELECT "from" AS column_name, "table" AS points_to, "to" AS in_column FROM pragma_foreign_key_list('meetings');
name type notnull pk
----- ------- ------- --
id INTEGER 0 1
name TEXT 1 0
email TEXT 0 0
(3 rows)
name type notnull pk
----------- ------- ------- --
id INTEGER 0 1
title TEXT 1 0
room TEXT 0 0
advisors_id INTEGER 1 0
(4 rows)
column_name points_to in_column
----------- --------- ---------
advisors_id advisors id
(1 row)
SELECT name, type, "notnull", pk FROM pragma_table_info('mentors');
SELECT name, type, "notnull", pk FROM pragma_table_info('workshops');
SELECT "from" AS column_name, "table" AS points_to, "to" AS in_column FROM pragma_foreign_key_list('workshops');
name type notnull pk
----- ------- ------- --
id INTEGER 0 1
name TEXT 1 0
email TEXT 0 0
(3 rows)
name type notnull pk
---------- ------- ------- --
id INTEGER 0 1
title TEXT 1 0
room TEXT 0 0
mentors_id INTEGER 1 0
(4 rows)
column_name points_to in_column
----------- --------- ---------
mentors_id mentors id
(1 row)
The last result says which column of the child table points to which column of the parent. The NOT NULL flag of the foreign key column is the "every child has a parent" rule from lesson 3.7.
Worked example: draw a diagram for a small shop
Facts: a shop sells products; each product is in one category; a category has many products.
+------------+ +------------+
| CATEGORY | contains | PRODUCT |
|------------| 1 ------< N |------------|
| id (key) | | id (key) |
| name | | name |
+------------+ | price |
+------------+
Tables: categories(id, name) and products(id, name, price, category_id REFERENCES categories(id)). Names are made up.
Common mistakes
- Putting foreign keys in the diagram as attributes. Show the relationship line; add the key when you make tables.
- Using a plural and a singular mixed up, such as an entity called "Classes". Name an entity for one item ("Class"); name its table in the plural if you like.
- Making an attribute that is really its own entity (a teacher's "department" with its own facts).
- Drawing the diagram after the tables, which hides design mistakes.
Key takeaways
- Entities are the things; attributes are facts about them.
- An ER diagram shows entities and relationships before any tables exist.
- Entity becomes table, attribute becomes column, line becomes foreign key (or junction table).
PRAGMA table_infoandPRAGMA foreign_key_listshow what SQLite built.
Practice
1. Is "price" an entity or an attribute of a product?
Show answer
An attribute.
2. Turn the diagram "one category contains many products" into tables.
Show answer
categories(id, name) and products(id, name, price, category_id), where category_id references categories(id).
3. Which side of a one-to-many line gets the foreign key?
Show answer
The "many" side.
4. In grammar terms, what are entities, attributes and relationships?
Show answer
Nouns, details of the nouns, and verbs.
5. How do you see which columns a table has in SQLite?
Show answer
PRAGMA table_info(table_name).
6. Draw an ER box for a Customer with a name and a phone number.
Show answer
+------------------+
| CUSTOMER |
|------------------|
| id (key) |
| name |
| phone |
+------------------+