gpt4 book ai didi

sql - 为什么我不能在同一个表上同时指定主键和唯一约束?

转载 作者:行者123 更新时间:2023-12-02 17:31:30 25 4
gpt4 key购买 nike

我正在尝试创建一个表:

CREATE TABLE [MyTable]
(
[Id] [int] IDENTITY,
[Column1] [int] NOT NULL,
[Column2] [int] NOT NULL

CONSTRAINT [PK_MyTable_Id] PRIMARY KEY ([Id])
CONSTRAINT [UQ_MyTable_Column1_Column2] UNIQUE ([Column1], [Column2])
)

此脚本失败并出现以下错误:

Both a PRIMARY KEY and UNIQUE constraint have been defined for column'Column2', table 'MyTable'. Only one is allowed.

为何实现此限制?如何创建具有这些属性的表?

最佳答案

主键约束后漏掉了一个逗号。

CREATE TABLE [MyTable]
(
[Id] [int] IDENTITY,
[Column1] [int] NOT NULL,
[Column2] [int] NOT NULL

CONSTRAINT [PK_MyTable_Id] PRIMARY KEY ([Id]),
CONSTRAINT [UQ_MyTable_Column1_Column2] UNIQUE ([Column1], [Column2])
)

关于sql - 为什么我不能在同一个表上同时指定主键和唯一约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23012479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com