我有一个名为Models的表设置如下:
我想要的是第2,3,4,5列只接受一组唯一的值。我跑了:
ALTER TABLE dbo.Models ADD CONSTRAINT uc_Models UNIQUE (Manufacturer, PriModel, SecModel, FormFactor)
报告的命令已成功完成,我看到已添加索引,但不是约束。我还可以使用Insert和Update命令创建cols 2-5的副本。我不确定问题是外键还是别的东西。
USE [inventory_v2]
GO
/****** Object: Table [dbo].[Models] Script Date: 4/8/2013 2:01:41 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Models](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Manufacturer] [int] NOT NULL,
[PriModel] [int] NOT NULL,
[SecModel] [int] NOT NULL,
[FormFactor] [int] NOT NULL,
[Active] [char](1) NOT NULL,
CONSTRAINT [PK_Models] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [uc_Models] UNIQUE NONCLUSTERED
(
[Manufacturer] ASC,
[PriModel] ASC,
[SecModel] ASC,
[FormFactor] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Models] WITH CHECK ADD CONSTRAINT [FK_Models_Models_FormFactor] FOREIGN KEY([FormFactor])
REFERENCES [dbo].[Models_FormFactor] ([ID])
GO
ALTER TABLE [dbo].[Models] CHECK CONSTRAINT [FK_Models_Models_FormFactor]
GO
ALTER TABLE [dbo].[Models] WITH CHECK ADD CONSTRAINT [FK_Models_Models_OEMs] FOREIGN KEY([Manufacturer])
REFERENCES [dbo].[Models_OEMs] ([ID])
GO
ALTER TABLE [dbo].[Models] CHECK CONSTRAINT [FK_Models_Models_OEMs]
GO
ALTER TABLE [dbo].[Models] WITH CHECK ADD CONSTRAINT [FK_Models_Models_PriModels] FOREIGN KEY([PriModel])
REFERENCES [dbo].[Models_PriModels] ([ID])
GO
ALTER TABLE [dbo].[Models] CHECK CONSTRAINT [FK_Models_Models_PriModels]
GO
ALTER TABLE [dbo].[Models] WITH CHECK ADD CONSTRAINT [FK_Models_Models_SecModels] FOREIGN KEY([SecModel])
REFERENCES [dbo].[Models_SecModels] ([ID])
GO
ALTER TABLE [dbo].[Models] CHECK CONSTRAINT [FK_Models_Models_SecModels]
GO