[英]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.