多列的唯一约束
CREATE TABLE [dbo].[user](
[userID] [int] IDENTITY(1,1) NOT NULL,
[fcode] [int] NULL,
[scode] [int] NULL,
[dcode] [int] NULL,
[name] [nvarchar](50) NULL,
[address] [nvarchar](50) NULL,
CONSTRAINT [PK_user_1] PRIMARY KEY CLUSTERED
(
[userID] ASC
)
) ON [PRIMARY]
GO
如何使用 t-sql
和/或 management studio
为列 fcode、scode、dcode
添加唯一约束? fcode、scode、dcode
一起必须是唯一的。
CREATE TABLE [dbo].[user](
[userID] [int] IDENTITY(1,1) NOT NULL,
[fcode] [int] NULL,
[scode] [int] NULL,
[dcode] [int] NULL,
[name] [nvarchar](50) NULL,
[address] [nvarchar](50) NULL,
CONSTRAINT [PK_user_1] PRIMARY KEY CLUSTERED
(
[userID] ASC
)
) ON [PRIMARY]
GO
How do I add a unique constraint for columns fcode, scode, dcode
with t-sql
and/or management studio
? fcode, scode, dcode
must be unique together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果该表已在数据库中创建,那么您可以稍后使用以下 SQL 查询添加唯一约束:
If the table is already created in the database, then you can add a unique constraint later on by using this SQL query:
通过在创建表时使用约束定义,您可以指定一个或多个跨越多个列的约束。语法简化自 technet 文档,其形式为:
因此,结果表定义将是:
By using the constraint definition on table creation, you can specify one or multiple constraints that span multiple columns. The syntax, simplified from technet's documentation, is in the form of:
Therefore, the resuting table definition would be:
这也可以在 GUI 中完成。下面是向现有表添加多列唯一约束的示例。
单击“确定” 在每个窗口中,您就完成了。
This can also be done in the GUI. Here's an example adding a multi-column unique constraint to an existing table.
Click OK in each window and you're done.