douzhou7037 2012-03-15 21:06
浏览 121
已采纳

MySQL数据库表结构化

I am working on a user based social network. I am building the site in PHP and I want to use a MySQL database to store user data. I can create databases/tables no problem (I use phpMyAdmin) I am not sure how many tables are necessary and what would be more practical for my web application. Would it be smart to have many tables? For example, a USERS table. With column names USER_ID, EMAIL, PASSWORD, LAST_LOGIN and then a table named USER_SETTINGS that would hold the account settings for each user, and another table named POSTS with the names and values attributed to a "status update". Or is smart to have everything in one table? What is the best practice?

  • 写回答

2条回答 默认 最新

  • dro44817 2012-03-15 21:11
    关注

    Definately do NOT keep "everything in one table". You'll likely end up with "many tables", but that sounds bad - basically, you should segment your data based on logical usage.

    For instance, if you DID keep posts in the users table - how would that work? What happens when they make a new post - would you add another field? (bad) - or add another item TO a field and separate by a character (bad)...etc. The only real way to do it is to have another table. You should definitely NOT keep posts in the same table as users.

    As far as 'profile data' (or whatever you want to call it), I like to keep it separate - some people like to keep it in the users table - matter of preference there.

    In your case, I'd suggest something like this:

    //users table
    id,
    email,
    password,
    last_login,
    //...
    
    //profiles table
    id,
    user_id,
    profile,
    age,
    gender,
    //...
    
    //posts table
    id,
    user_id,
    data,
    created (datetime),
    modified (datetime)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?