doufu9947 2014-07-19 11:16
浏览 29
已采纳

使用两个表,如一个表

I have two different type users table. One of them is ordinary user table for registered users. Other one is for unregistered users. I need to use these two tables with other tables. For example;

registered_users: userid, username, password, name, sex, email, ip

unregistered_users: userid, name, sex, ip

some_table: id, title, content, userid

How can I join some_table with user table?

I have some oppinion but none of them does not feel right.

  1. use all table regUser column and unRegUser column with null values and join two table etc. This way non suitable because all tables have lots of null cell.
  2. if all table like some_table add a column userType and each query has "if". But this way is long and spends resources.
  3. if userid column type will be string (r13, u32 - r:registered, u:unregistered) and use "substring" and "if" but this is more difficult
  4. create a connection table like "id, type, userid" and use this id instead of userid. but also need "if"
  5. create a connection table like "id, regUser, unRegUser" with null values.

Or create a different user table to be combine of two user tables. Of course we have null columns again.

You think which way is the best? Or any one has different opinion.

  • 写回答

1条回答 默认 最新

  • duangai1916 2014-07-19 11:28
    关注

    When contents are same why do you try to duplicate it by creating two tables instead you can create a single table called users

    create table users
    (
    user_id int not null auto_increment primary key
    username varchar(500),
    password varchar(500),
    email  varchar(400),
    ip     varchar(800),
    reg_status enum(0,1),
    );
    

    then you can use this user_id and reference it your secondary table.

    You can build up your query on reg_status

    0 - Unregisterd
    1 - Registered
    

    If you want more normalized table

    then you can use two tables

    create table users
    (
    user_id int not null auto_increment primary key
    email  varchar(400),
    ip     varchar(800),
    reg_status enum(0,1),
    );
    
    
    create table registered_users
    (
    login_id int not null auto_increment primary key
    username varchar(500),
    password varchar(500),
    user_id - F.k (Foreign Key)
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?