1

我有一个名为 Models 的表,设置如下:

  1. ID - PK,身份
  2. 制造商 FK - Int32
  3. PriModel FK - Int32
  4. SecModel FK - Int32
  5. 外形尺寸 FK - Int32
  6. 活跃字符(1)

我想要的是 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
4

0 回答 0