1. Home
  2. Explore
  3. Computer Science
  4. Database Design and Normalization

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Database Design and Normalization

Speaker 1Speaker 2

Database Design and Normalization

13:23
0:00-13:23
  1. Michael

    Welcome to our episode on database design. The big goal is to move from a conceptual data model—where we identify entities and relationships—to tables and structures we can actually implement in a database.

  2. Anna

    So it’s not enough to draw an entity and give it attributes. We also have to make decisions about keys, data types, constraints, relationships, and whether the design should be normalized or deliberately denormalized.

  3. Michael

    Exactly. We might begin with a relation written in parentheses, such as an item relation with itemId and several other attributes. Underlining itemId indicates that it is the primary key. We can turn that relation into an entity in an entity-relationship model, but the entity still needs more detail before it becomes a real database table.

  4. Anna

    For each entity, we generally create a table, add its attributes as columns, and specify a primary key. Then we define properties for each column: its data type, whether it can contain nulls, and whether it should have a default value when no value is supplied.

  5. Michael

    And we may add constraints to protect data integrity. A range constraint can require values to fall between a minimum and maximum, for example, while another constraint can limit a column to a predefined set of acceptable values. After those decisions, we can review the table’s structure in light of normalization and performance needs.

  6. Anna

    Let’s start with normalization. The reason it matters is that poorly structured tables can develop modification anomalies: problems that arise when we insert, update, or delete data.

  7. Michael

    A deletion anomaly occurs when removing one piece of information accidentally removes another piece we wanted to keep. Imagine a table that combines customers and their cars. If Leonard is the only customer recorded with a particular car, deleting Leonard’s row could also erase the table’s only record that the car exists.

  8. Anna

    An insertion anomaly can happen when the table’s structure makes it awkward to add one fact without also supplying unrelated information. If Sheldon becomes a customer but doesn’t drive, putting him into a combined customer-and-car table may leave missing car values. Later, if we add a car Sheldon buys, a data-entry mistake could also produce inconsistent information about a car already listed.

  9. Michael

    Then there’s an update anomaly. Suppose the same kind of car appears in rows for both Penny and Sheldon, and we change its car ID in Penny’s row but forget to change Sheldon’s. Now the table contains conflicting information about what should be the same car type.

  10. Anna

    Normalization addresses those problems by separating different business concepts into appropriately structured tables. Instead of keeping customers and cars together in one list, we can represent them in separate tables and link them as needed. That reduces the chance that an insertion, update, or deletion will unintentionally affect another fact.

  11. Michael

    A few definitions help explain how we judge a design. A functional dependency exists when the value of one attribute determines the value of another attribute or set of attributes. The attribute doing the determining is called a determinant.

  12. Anna

    And a candidate key is an attribute, or a combination of attributes, that can determine the other non-key attributes in a row. A simple candidate key uses one attribute; a composite candidate key uses two or more attributes together.

  13. Michael

    Normal forms describe stages in structuring relations. A relation is, by definition, in First Normal Form. Removing partial dependencies takes us toward Second Normal Form; removing transitive dependencies takes us toward Third Normal Form.

  14. Anna

    Boyce-Codd Normal Form goes further: every determinant in the table must also be a candidate key. It’s a somewhat more stringent version of Third Normal Form, which is why it’s sometimes called 3.5 Normal Form. There are still higher normal forms, but for this lesson, a design normalized to Third Normal Form or above is generally considered sufficient for business data and avoids the modification anomalies we discussed.

  15. Michael

    Here’s a customer example. Suppose a customer table stores customer ID, name, address, city, state, zip code, accountant ID, accountant name, and accountant phone number. Customer ID is the primary key, but not every other attribute depends only on that key.

  16. Anna

    Right. A zip code determines its city and state, so those location attributes depend on zip code. And accountant ID determines the accountant’s name and phone number. Those dependencies mean the original table combines information about customers, postal codes, and accountants.

  17. Michael

    We can normalize it into three tables. The Customer table keeps customer ID as its primary key, along with customer details and the zip code and accountant IDs as foreign keys. The Zip Code table uses zip code as its primary key and stores city and state. The Accountant table uses accountant ID as its primary key and stores the accountant’s name and phone.

  18. Anna

    Together, those tables preserve the information while placing each fact with the thing it describes. The foreign keys connect the tables, and the functional dependencies are represented in the appropriate places.

  19. Michael

    Now let’s turn to denormalization, which deliberately combines tables that normalization had separated. A designer might do that for a defensible business reason, commonly to make the design simpler or to improve query speed.

  20. Anna

    For example, we could combine the Zip Code and Customer tables by copying city and state into the Customer table. Then a query for a customer’s address could read everything from one table instead of joining Customer to Zip Code.

  21. Michael

    That can improve the performance of that kind of query, because joining tables takes processing time. It can also make the design easier to understand and make some SQL commands simpler. But the trade-off is that we bring back the possibility of modification anomalies.

  22. Anna

    And we create repetition. If many customers live in Los Angeles, California, that city-and-state information appears in many rows. Repetition also raises the risk of inconsistent data entry: someone might write California, someone else might enter CA, and another person might use different capitalization.

  23. Michael

    So denormalization should be a conscious response to a real business need, not a default. Constraints or validity checks in software may help control inconsistencies, but they also add work. The extra safeguards may reduce the simplicity that motivated denormalization in the first place.

  24. Anna

    Let’s move from table structure to relationships. In a relational database, we typically connect tables by matching a primary key in one table with a foreign key in another. Where that foreign key belongs depends on the relationship’s cardinality.

  25. Michael

    For a one-to-one relationship, placement depends on optionality. If both sides are optional, either table can hold the foreign key; the designer can choose the arrangement that makes the most conceptual sense.

  26. Anna

    Think of employees and lockers, where an employee may have a locker and a locker may have an employee, but either might be unassigned. We could put employee ID in Locker, or locker ID in Employee. The choice is between two ways of expressing the same relationship.

  27. Michael

    If one side is optional and the other side is required, the usual strategy is to put the foreign key in the table on the optional side. For example, every hospital patient is assigned exactly one bed, but a bed may be empty. We put bed ID as a foreign key in Patient.

  28. Anna

    That lets each patient record point to a bed, while an empty bed doesn’t need to point to a patient. Once we know where the foreign key is, a one-to-one query joins the tables by matching that foreign key to the corresponding primary key.

  29. Michael

    A one-to-many relationship has a simpler placement rule: put the foreign key on the many side. The one side is often called the parent table, and the many side the child table.

  30. Anna

    For example, a team can have zero or many players, while each player belongs to one team. Team is the parent; Player is the child. Team ID is the primary key in Team and also appears as a foreign key in Player.

  31. Michael

    To query related data, we join the two tables by matching those team IDs. The join can be expressed in the FROM clause with a JOIN, or in the WHERE clause, as long as the key values are matched correctly.

  32. Anna

    Many-to-many relationships need an additional table. A student can take many classes, and a class can include many students, so we can’t represent that relationship directly with just a foreign key in Student or Class.

  33. Michael

    Instead, we create an intersection table, such as Student_Class, between the two parent tables. A common design gives it a composite primary key made from student ID and class number. Each of those attributes also serves individually as a foreign key back to its parent table.

  34. Anna

    To list students and the classes they’ve taken, we join all three tables: Student to Student_Class using student ID, and Student_Class to Class using class number. The intersection table provides the links needed to assemble the result.

  35. Michael

    An intersection table can also include attributes that describe the relationship itself. If Student_Class includes Grade, for example, the grade describes a particular student’s performance in a particular class. This kind of intersection table is called an associative entity, and the design is an association relationship.

  36. Anna

    There’s an important choice in an associative entity: use the composite parent-key pair as the primary key, or use a separate surrogate key. With the composite key, a given pair—one student and one class, for example—can appear at most once.

  37. Michael

    The same logic applies to a customer rating a product. If Customer ID and Product ID together form the primary key in Rating, each customer can rate each product only once. That design enforces one row for each customer-product pair.

  38. Anna

    If Rating instead has a surrogate primary key, such as Rating ID, then Customer ID and Product ID are only foreign keys. Their combination is no longer the primary key, so the same customer-product pair can appear multiple times—allowing a customer to rate the same product more than once.

  39. Michael

    So a surrogate key is not just a different label for a row; in this example, it changes what combinations the table can contain. The key choice should reflect the business rule about whether repeated relationships are allowed.

  40. Anna

    Our final topic is recursive relationships, where a table is related to itself. For a one-to-one or one-to-many recursive relationship, we can use a foreign key in the same table. A many-to-many recursive relationship requires an intersection table.

  41. Michael

    Consider one-to-one sponsorship: a person may be sponsored by zero or one other person. We can store a foreign key in Person that identifies the person each person sponsored, or instead store the sponsor for each person. Either approach represents the relationship, depending on which direction is most useful to express.

  42. Anna

    To query a one-to-one recursive relationship, SQL uses the same table twice, giving each instance a different alias. The aliases let us treat the two roles—such as sponsor and sponsored person—as separate references, then join them using the foreign key.

  43. Michael

    A one-to-many example is customer referrals. One customer may refer many other customers, while each customer is referred by at most one other customer. We store a referred-by foreign key in the Customer table, pointing back to another row in that same table.

  44. Anna

    If we want the customers referred by customer 100, we again query Customer twice with different aliases. One alias represents the referring customer, and the other represents the referred customer; joining them on the referral foreign key identifies the matches.

  45. Michael

    For a many-to-many recursive relationship, consider doctors who can treat many other doctors, while also being treated by many other doctors. A single foreign key in Doctor can’t represent all those pairings, so we add an intersection table that records which doctor treats which other doctor.

  46. Anna

    The SQL can look unusual because we select from Doctor twice, using different aliases, and also include the intersection table. But the logic is the same as before: the aliases represent the two doctor roles, and the intersection table connects the appropriate rows.

  47. Michael

    That gives us a useful design checklist: model entities as tables, define keys and column properties, normalize to prevent anomalies, and denormalize only for a justified reason. Then place foreign keys according to cardinality, use intersection tables for many-to-many relationships, and remember that recursive relationships may require a table to join to itself.

  48. Anna

    And when we write SQL for a recursive relationship, different aliases make it possible to refer to the same table in its different roles. With those ideas, we can move from conceptual diagrams toward database structures that are practical to implement and query.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent