Designing with Entities and Relationships
Good databases are designed before they are built. The usual steps:
- List the entities: the things you keep data about (Customer, Product, Order).
- List the attributes of each (name, price, date).
- Choose a primary key for each entity.
- Decide the relationships between them and how many on each side.
- Draw it as an entity-relationship diagram (boxes for entities, lines for relationships), then turn each entity into a table and each relationship into a foreign key or junction table.
A good test question: "could this fact be stored in exactly one place?" If the answer is no, the design is repeating data.
Examplepython
Quick check
In an order system, where does the quantity ordered belong?
Your turn: write the code
Create two tables, authors (id, name) and books (id, title, author_id). Then print the names of the tables, sorted, as a list.
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.