WeCodeAcademy

Designing with Entities and Relationships

Good databases are designed before they are built. The usual steps:

  1. List the entities: the things you keep data about (Customer, Product, Order).
  2. List the attributes of each (name, price, date).
  3. Choose a primary key for each entity.
  4. Decide the relationships between them and how many on each side.
  5. 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
Open in editor »
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.