【问题标题】:Transform Many to Many relation to One to Many relation将多对多关系转换为一对多关系
【发布时间】:2013-03-17 00:00:32
【问题描述】:

我有以下表格(简体):

create table dbo.Users
(
  User_Id int identity not null 
    constraint PK_Users_Id primary key clustered (Id),  
); 

create table dbo.UsersSeals
(
  UserId int not null, 
  SealId int not null,
    constraint PK_UsersSeals_UserId_SealId primary key clustered (UserId, SealId)
);

create table dbo.Seals
(
  Seal_Id int identity not null 
    constraint PK_Seals_Id primary key clustered (Id),  
);

alter table dbo.UsersSeals
add constraint FK_UsersSeals_UserId foreign key (UserId) references Users(Id) on delete cascade on update cascade,
    constraint FK_UsersSeals_SealId foreign key (SealId) references Seals(Id) on delete cascade on update cascade;

所以我在用户和封印之间有一个多对多的关系。一个用户可以有多个印章,一个印章可以有多个用户。我需要一个一对多,一个用户可以有多个印章,但一个印章只有一个用户。

是的,我可以删除 UsersSeals 表并将 UserId 添加到 Seals 表中。但是,我在其他表中使用同样的方式密封。

我希望只有一个 Seals 表与用户表和其他表具有一对多关系。

这个可以吗?

【问题讨论】:

  • 我看不到UsersSeals 之间的关系。
  • 我刚刚更新了我的代码以包含约束

标签: sql database-design


【解决方案1】:

SealID 列的UsersSeals 表上添加单独的unique constraint

然后您保证此表在SealID 上是唯一的,这意味着一个印章只能与一个用户关联,但一个用户可以有多个印章。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多