Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should User and Address be in separate tables?

Currently my users table has the below fields

  • Username
  • Password
  • Name
  • Surname
  • City
  • Address
  • Country
  • Region
  • TelNo
  • MobNo
  • Email
  • MembershipExpiry
  • NoOfMembers
  • DOB
  • Gender
  • Blocked
  • UserAttempts
  • BlockTime
  • Disabled
  • I'm not sure if I should put the address fields in another table. I have heard that I will be breaking 3NF if I don't although I can't understand why. Can someone please explain?

    like image 283
    Enzero Avatar asked Jul 04 '11 22:07

    Enzero


    People also ask

    When should I put an attribute in a separate table?

    You should put an attribute in a separate table whenever you expect that one person could have multiple of that attribute. Otherwise, there's not much reason to separate it, and there can be some conceptual overhead in doing so.

    Why is it better to have multiple separate tables?

    In many cases, it may be best to split information into multiple related tables, so that there is less redundant data and fewer places to update.

    Can you only have one table in a database?

    In a simple database, you might have only one table. For most databases you will need more than one. For example, you might have a table that stores information about products, another table that stores information about orders, and another table with information about customers.

    What are the minimum requirements before creating relationship between two tables?

    Generally, a relationship is established by linking these key fields between tables – the primary key in one table and a foreign key in another table. Every table should have a primary key – one or more fields whose contents are unique to each record. This is called entity integrity in the database management.


    1 Answers

    There are several points that are definitely not 3NF; and some questionable ones in addition:

    1. Could there could be multiple addresses per user?
    2. Is an address optional or mandatory?
    3. Does the information in City, Country, Region duplicate that in Address?
    4. Could a user have multiple TelNos?
    5. Is a TelNo optional or mandatory?
    6. Could a user have multiple MobNos?
    7. Is a MobNo optional or mandatory?
    8. Could a user have multiple Emails?
    9. Is an Email optional or mandatory?
    10. Is NoOfMembers calculated from the count of users?
    11. Can there be more than one UserAttempts?
    12. Can there be more than one BlockTime per user?

    If the answer to any of these questions is yes, then it indicates a problem with 3NF in that area. The reason for 3NF is to remove duplication of data; to ensure that updates, insertions and deletions leave the data in consistent form; and to minimise the storage of data - in particular there is no need to store data as "not yet known/unknown/null".

    In addition to the questions asked here, there is also the question of what constitutes the primary key for your table - I would guess it is something to do with user, but name and the other information you give is unlikely to be unique, so will not suffice as a PK. (If you think name plus surname is unique are you suggesting that you will never have more than one John Smith?)

    EDIT: In the light of further information that some fields are optional, I would suggest that you separate out the optional fields into different tables, and establish 1-1 links between the new tables and the user table. This link would be established by creating a foreign key in the new table referring to the primary key of the user table. As you say none of the fields can have multiple values then they are unlikely to give you problems at present. If however any of these change, then not splitting them out will give you problems in upgrading the application and the data to support the application. You still need to address the primary key issue.

    like image 87
    Chris Walton Avatar answered Nov 03 '22 02:11

    Chris Walton