Database design 1 to 1 relationship

不想你离开。 提交于 2019-12-23 15:58:20

问题


I design my database incorrectly, should I fix this while its in development?

"user" table is suppose to have a 1.1 relationship with "userprofile" table

however the actual design the "user" table has a 1.* relationship with "userprofile" table.

Everything works! but should it be fixed anyways?


回答1:


Do one thing

User Table 
   Userid(p)
   UserName
   othercol..

UserProfile 
   id(p)
   UserId(f) - and unique
   othercol..

hope this way you can easily fix the isse




回答2:


Make the user_id in the user_profile table unique and its fixed.




回答3:


If it's a 1:1 relationship and you often are bringing back records from "user" table and "userprofile" together then you might consider just merging them into one table.




回答4:


Yes, fix this with a unique index on the FK field. The reason why you need to fix it now is that you can't control how badly people are going to insert data over time when the database is not set up correctly with controls that do not allow the behavior you do not want.

The first time you havea a duplicated record inserted into the child table, you might break a lot of code. With no unique index, the chances of a second record getting inserted can be quite high. You can say, you'll control this at the application level but that is usaully a poor choice as there is no guaranteee that other applications, bulk inserts etc aren't gong to happen that circumvent the application. Putting things right as soon as you can in a database design is critical. It becomes really hard to fix a poor design when there are a lot of records in the database.




回答5:


@pranay

User Table 
   Userid(p)
   UserName
   othercol..

UserProfile 
   id(p)
   UserId(f) - and unique
   othercol..

Is that normally how you do it(above)? or do you do this(below)?

User Table 
   Userid(p)
   UserName
   othercol..

UserProfile 
   id(p) <--- userid
   othercol..


来源:https://stackoverflow.com/questions/2786250/database-design-1-to-1-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!