应用于用户和商店的地址表

时间:2017-04-21 14:56:38

标签: sql sql-server database-design

在数据库中,我有以下表格:

create table dbo.Stores ( 
  Id int not null
  Name nvarchar (120) not null
)

create table dbo.Users ( 
  Id int not null
  Name nvarchar (120) not null
)

每个用户或商店都可以: 1 - 一个物理地址; 2 - 一个网站或社交媒体地址。

我是否应该拥有地址和社交媒体表格。例如:

create table dbo.Addresses ( 
  Id int not null
  Street nvarchar (120) not null,
  PostalCode nvarchar (12) not null
  City nvarchar (40) not null,
  Latitude float null,
  Longitude float null
)

create table dbo.UserAddresses ( 
  UserId int not null,
  AddressId int not null
)

社交媒体,电话号码等也一样......

我应该只将列添加到Users表吗?

更新1

对于网站地址和社交媒体地址,我正在考虑以下内容:

create table dbo.UserWebAddresses ( 
  UserId int not null,
  WebAddressTypeId int not null,
  Value nvarchar(200) not null
)

create table dbo.WebAddressTypes ( 
  Id int not null,
  Name nvarchar(20) not null
)

这有意义吗?

1 个答案:

答案 0 :(得分:0)

如果没有重复的行,则将这些列直接添加到dbo.Stores和dbo.Users。如果它们可能重复,则创建一个新表并使用FK。

let
    Source = Xml.Tables(File.Contents("C:\Users\User\Desktop\test.xml")),
    ExpandTable = Table.ExpandTableColumn(Source, "channel", {"title", "link", "description", "language", "category", "image", "currency", "item"}, {"channel.title", "channel.link", "channel.description", "channel.language", "channel.category", "channel.image", "channel.currency", "channel.item"})
in
    ExpandTable

如果我理解了一切正确的话,我们也可以使用以下解决方案:

CREATE TABLE dbo.Stores 
( 
  Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
  Name NVARCHAR(120) NOT NULL,
  Address_Id INT NULL
);

CREATE TABLE dbo.Users 
( 
  Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
  Name NVARCHAR(120) NOT NULL,
  Address_Id INT NULL
);

CREATE TABLE dbo.Addresses 
( 
  Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
  Street NVARCHAR(120) NOT NULL,
  PostalCode NVARCHAR(12) NOT NULL,
  City NVARCHAR(40) NOT NULL,
  Latitude FLOAT NULL,
  Longitude FLOAT NULL
);

ALTER TABLE dbo.Stores
  ADD CONSTRAINT Stores_Address_Id_FK FOREIGN KEY (Address_Id) REFERENCES dbo.Addresses(Id);

ALTER TABLE dbo.Users
  ADD CONSTRAINT Users_Address_Id_FK FOREIGN KEY (Address_Id) REFERENCES dbo.Addresses(Id);