Search code examples
databasedatabase-normalization

Normalization UNF to 1NF


When normalizing a relation with multi-value attributes, we decompose it as a new relation with the primary key field and the values from the multi-valued attribute.

The problem I have is, what is the primary key for that newly created attribute?

If we use the same old primary key as the new one, since we repeat the values there with the values from the multi-valued attribute, how can we have unique values for the primary key field here?

I have a table with customer_names, their hobbies and so on. Some people have more than 1 hobby, so now it's a multi-valued attribute. Now even if I create a new relation only with customer_name & one hobby at a time, how can I add details of the people say who have 3 hobbies?


Solution

  • When you create the lookup table for the new attribute, you don't repeat the value in the lookup table. And usually you add an artificial PK in the form of an IDENTITY column.

    For example: You have a table with a bunch of addresses. There are many addresses with a common city.

    You decide to normalize by creating a city table. You only add a given city to the City table ONCE. Not once for each address.

    EDIT: in the scenario described in your comment, you could create a Hobbies table with CustomerID and HobbyName.

    CustomerID would be a foreign key that references the Primary Key in the Customers table.

    EDIT 2: As Beth and I mentioned in comments, it seems you want a many-to-many relationship.

    Create a Hobbies table, which has only one row per unique Hobby. If two or more customers have the same Hobby, you still only list the hobby in the table once. Use an Identity column to create a HobbyID as the Primary Key.

    Then create a CustomerHobby table. It has CustomerID and HobbyID. Both are foreign keys that reference the primary keys of the Customers table and Hobbies table, respectively.

    That's it.