0

我想制作一个只有两个整数列的表,作为两个不同数据集之间的映射表,我希望对其施加正确的约束。

我最初将两列设置为复合主键,但后来意识到这代表了多对多,只防止发生重复的多对多映射。

如何指定我希望两列在所有行中都是唯一的整数?我正在使用 MS SQL,但我想这是一个一般的数据库设计问题。

4

3 回答 3

2

创建另一个表是为了创建多对多关系,因此您可能不应该创建它(除非我遗漏了什么)。

One to one mandatory is the same as having the two tables as a single table. The only acceptable one to one is optional, and you simply have to relate the two tables, no third table necessary.

于 2008-12-29T21:03:43.447 回答
1

在一个列上创建一个主键,在另一列上创建另一个单独的唯一约束或唯一索引。

CREATE TABLE [dbo].[test](
    [x] [int] NOT NULL,
    [y] [int] NOT NULL,
     CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED 
     ( [x] ASC) 
     WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
         IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, 
         ALLOW_PAGE_LOCKS  = ON) 
     ON [PRIMARY]) 
 ON [PRIMARY]

CREATE UNIQUE NONCLUSTERED INDEX [IX_test] ON [dbo].[test] 
  ([y] ASC) 
   WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
         SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, 
         DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, 
         ALLOW_PAGE_LOCKS  = ON)  
 ON [PRIMARY]
于 2008-12-29T21:02:58.813 回答
0

对每列使用 UNIQUE 列约束。

于 2008-12-29T21:02:05.353 回答