Sql Server中的一对一关系 [英] One To One Relationship In Sql Server

查看:94
本文介绍了Sql Server中的一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究bscs学位的最后一年项目。我正在努力解决数据库设计问题,因为我反对一对一关系的问题。我希望你能理解我的问题,并建议我一个最好的解决方案。

有三张桌子

1.DomesticUsers(国内用户,用户名,位置)

2.CommercialUsers(commercialuserid,用户名,位置)

3.ElectricMeter(meterno,meterphase)

只有一米被分配给一个用户,无论是国内用户还是商业用户



现在我应该把外键放在哪里.. ??

解决方案

DomesticUsers和CommercialUsers表必须各有一个外键

包含ElectricMeter的主键



SQL关系在这里得到了很好的解释:

http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561 [ ^ ]


如果我理解正确,我怀疑桌面设计不好!



用户应该只有一张桌子: MyUsers(uid,uname,tid,eid)

其中 tid 是外键和引用 tid in TypeOfUser(tid,tdescription)



其中 eid 是外键并且在电表(eid,meterphase)中引用 eid / code>。



注意: TypeOfUser ElectricMeter
表格可以添加用户。



请参阅: SQLFiddle [ ^ ]


如果它不起作用,请更改设计:

meter(meterno,meterphase)

user (userid,usertypeid,username,location,meterno)

usertype(usertypeid,usertypedesc)





其中usertypedesc可以拥有国内,商业等的vaues。


I am working on my final year project of bscs degree. em struggling with the database design as i counter a problem of one to one relationship .I hope you will understand my problem and suggest me a finest solution .
There are three tables
1.DomesticUsers(domesticuserid,username,location)
2.CommercialUsers(commercialuserid,username,location)
3.ElectricMeter(meterno,meterphase)
only one meter is alocated to one user either domesticuser or commerical user

Now where should i place the foreign keys ..??

解决方案

DomesticUsers and CommercialUsers tables must each have a foreign key
containing the primary key of the ElectricMeter

SQL Relationships are well explained here:
http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561[^]


If i understand you correctly, i suspect bad table design!

It should be only one table for users: MyUsers(uid, uname, tid, eid),
where tid is Foreign Key and reference to tid in TypeOfUser(tid, tdescription)
and
where eid is Foreign Key and reference to eid in ElectricMeter(eid, meterphase).

Note: there must exists data in TypeOfUser and ElectricMeter tables to be able to add user.

See: SQLFiddle[^]


If it is not working, change the design:

meter (meterno, meterphase)

user (userid, usertypeid, username, location, meterno)

usertype (usertypeid, usertypedesc)



where usertypedesc can have the vaues of domestic, commercial, etc.


这篇关于Sql Server中的一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆