Database schema for managing addresses

不问归期 提交于 2020-01-17 14:47:25

问题


I am developing a MVC project with sql server. in which each user will be given a username after registration. Registration is done using various data like address and email number and phone number. I am using a table to store address and its id is stored in users table. Now as people can have change address so I'm not able to understand it how to manage it. I want to keep the old address as well as new for new users. Can anybody help?


回答1:


You have some options:

In address table, add userid column. In user table, add addressid column.

User table will tell you the current address user has indicated.

select * from user u
inner join address a on u.userid = a.userid and u.addressid = a.addressid
where userid = 45

Address table will tell you ALL the addresses user has used.

select * from address where userid = 45

The advantage is that user table points you to the recent address table. When user adds new address, address table gets an insert and user table is updated. The disadvantage is repetition of userid in address table - if parents and children have the same address (assuming each individual is a user), you'd be adding the same address multiple times for each user. To get current address, you will have to ask both user and address tables.

In address table, add userid column and isPrimary bit column. In user table do NOT add addressid column

User table will not tell you anything about address, but address table will tell you all the addresses the user has used and which one is their primary.

select * from user u
inner join address a on u.userid = a.userid and a.isPrimary = 1
where userid = 45

You can get current address for a given user directly from address table.

select * from address where userid = 45 and isPrimary = 1

Address table, just like in the previous example, will tell you ALL the addresses user has used.

The advantage is just that address table is responsible for telling you who that address belongs to and whether it is primary.

Store user in user table, address in address table. Create junction table to bring address and users together

UserAddress table can be created that has userid, addressid, isPrimary columns. User table will only contain user information. Address table will have addresses and to know which address belongs to which user, you'd ask UserAddress table.

select * from user u
inner join useraddress ua on u.userid = ua.userid and ua.isPrimary = 1
inner join address a on ua.addressid = a.addressid
where u.userid = 45

The advantage of this is associate an address with multiple people. Parents and 2 children residing in the same household can be associated with the same address. If household moves to another location, you have one additional record in address table and 4 records in junction table. The disadvantage is an extra join.

Choose the use case you feel appropriate for your situation.



来源:https://stackoverflow.com/questions/44469411/database-schema-for-managing-addresses

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