简体   繁体   English

如何定义适用于 DataSet 表中的多个列的 UNIQUE 约束?

[英]How can I define a UNIQUE constraint that applies to multiple columns in a DataSet table?

I have the following table defined in a.sql script file.我在 a.sql 脚本文件中定义了下表。


CREATE TABLE [HwComponent](
    [HwComponentId] [int] NOT NULL,
    [HwComponentTypeId] [int] NOT NULL,
    [ManufactureId] [int] NOT NULL,
    [SerialNumber] [nvarchar](100) NOT NULL,
    [AssignmentType] [nvarchar](1000) NOT NULL,
    [IsFunctional] [nvarchar](10) NOT NULL,
    [Note] [nvarchar](4000) NULL,

    CONSTRAINT [PK_HwComponent] PRIMARY KEY ([HwComponentId]),
    CONSTRAINT (FK1_HwComponent] FOREIGN KEY ([HwComponentTypeId]) REFERENCES [HwComponentType] ([HwComponentTypeId)] ON DELETE NO ACTION ON UPDATE NO ACTION
    CONSTRAINT (FK2_HwComponent] FOREIGN KEY ([HwComponentTypeId], [ManufactureId]) REFERENCES [HwComponentTypeManufacturerInfo] ([HwComponentTypeId], [ManufactureId]) ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT (UI1_HwComponent] UNIQUE ([HwComponentTypeId], [ManufactureId], [SerialNumber]))

GO

I now need to define a new table in the DataSet Designer to match.我现在需要在DataSet Designer中定义一个新表来匹配。

I believe I have everything defined correctly, except the UNIQUE constraint that requires all 3 columns be considered together as the unique constraint.我相信我已经正确定义了所有内容,除了要求将所有 3 列一起视为唯一约束的唯一约束。

How do I define the UNIQUE constraint for this table in the DataSet Designer ?如何在DataSet Designer中为此表定义 UNIQUE 约束?

SQL CE Service 4.0 SP1: VS Prof 2017 - Version 15.8.7 SQL CE 服务 4.0 SP1:VS 教授 2017 - 版本 15.8.7

Does this work for you?这对你有用吗?

CREATE UNIQUE INDEX ixYourConstraint ON HwComponent (col1, col2);

This similar post might be of help to you: MsSql Compact, unique constraint on two and more columns这篇类似的帖子可能对您有所帮助: MsSql Compact, unique constraint on two and more columns

Your code should work if properly formatted.如果格式正确,您的代码应该可以工作。 You have parens where you shouldn't:你有不应该的地方:

CREATE TABLE [HwComponent](
    [HwComponentId] [int] NOT NULL,
    [HwComponentTypeId] [int] NOT NULL,
    [ManufactureId] [int] NOT NULL,
    [SerialNumber] [nvarchar](100) NOT NULL,
    [AssignmentType] [nvarchar](1000) NOT NULL,
    [IsFunctional] [nvarchar](10) NOT NULL,
    [Note] [nvarchar](4000) NULL,

    CONSTRAINT PK_HwComponent PRIMARY KEY ([HwComponentId]),
    CONSTRAINT FK1_HwComponent FOREIGN KEY ([HwComponentTypeId]) REFERENCES [HwComponentType] ([HwComponentTypeId)] ON DELETE NO ACTION ON UPDATE NO ACTION
    CONSTRAINT K2_HwComponent FOREIGN KEY ([HwComponentTypeId], [ManufactureId]) REFERENCES [HwComponentTypeManufacturerInfo] ([HwComponentTypeId], [ManufactureId]) ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT UI1_HwComponent UNIQUE ([HwComponentTypeId], [ManufactureId], [SerialNumber])
);

I would advise you to get rid of all the square braces.我会建议你摆脱所有的方括号。 They just make the code harder to write and to read and to modify.它们只是使代码更难编写、阅读和修改。

Thanks Algef and Gordon for you responses.感谢 Algef 和 Gordon 的回复。

Sorry about the confusion.很抱歉造成混乱。 My bad.我的错。 :-) The SQL sample works fine. :-) SQL 样品工作正常。 I used it as an example only.我仅将其用作示例。 I actually need to represent this table exactly using the DataSet Designer in VS.我实际上需要使用 VS 中的DataSet Designer准确地表示这个表。 The table should look something like the following when done.完成后,表格应如下所示。

HwComponent Table in the DataSet Designer数据集设计器中的 HwComponent 表

My problem is I'm not having success in defining the UNIQUE group properly in the DataSet Designer .我的问题是我没有成功在DataSet Designer中正确定义 UNIQUE 组。 I need to define a UNIQUE GROUP that consists of the HwComponentTypeId, ManufactureId and SerialNumber columns.我需要定义一个由 HwComponentTypeId、ManufactureId 和 SerialNumber 列组成的 UNIQUE GROUP。

Any ideas?有任何想法吗?

Thanks again for any help再次感谢任何帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在postgres的多个表中定义多个列的唯一约束? - How to define unique constraint on multiple columns in multiple tables in postgres? 如何定义由表中所有列组成的唯一键约束 - How to define a Unique Key Constraint that is composed of all columns in the table 表变量中多列的唯一约束 - Unique constraint on multiple columns in a table variable 如何为多个列创建唯一约束? - How do I create unique constraint for multiple columns? 我如何才能在两列上分别制作唯一的约束顺序 - How can I make a unique constraint order independently on two columns 如果所有列都可以为空并且不创建唯一索引但我可以添加约束,我如何在现有的 oracle sql 表中实现唯一性 - how can i make uniqueness in existing oracle sql table if all columns are nullable and also not creating unique index but i can add constraint 多个列的唯一约束是否可以在这些列上分别添加索引 - Can a Unique constraint on multiple Columns add indexes separately on those columns 如何使表唯一,“没有唯一的约束与引用表匹配”? - How can I make table unique, 'there is no unique constraint matching the referenced table'? 具有跨表的多个列的唯一值约束,而不是 Oracle 中的组合 - Unique value Constraint with multiple columns across the table, not the combination in Oracle 跨多列的 UNIQUE 约束 - UNIQUE constraint across multiple columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM