创建非聚簇PK语法是否不一致?

发布于 2024-10-01 01:19:25 字数 599 浏览 5 评论 0原文

SQL Server 2008 R2

为什么

create table A
(
   id int,
   primary key nonclustered (id)
)

正确并且执行没有错误?

create table A
(
   id int,
   primary key nonclustered id
)

有错误吗?给予

“)”附近的语法不正确。

附带问题:
为什么

create table c(id int primary key clustered)  

执行
但是

create table c(id int primary key nonclustered)

是一个错误吗? 抱歉,两者都有效。

语法不一致是否需要建议更正?

SQL Server 2008 R2

Why

create table A
(
   id int,
   primary key nonclustered (id)
)

is correct and executed without errors?

But

create table A
(
   id int,
   primary key nonclustered id
)

is an error? giving

Incorrect syntax near ')'.

Collateral question:
Why

create table c(id int primary key clustered)  

is executed
but

create table c(id int primary key nonclustered)

is an error? Sorry, both work.

Is it inconsistent syntax to be suggested for correction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是我的挚爱i 2024-10-08 01:19:25

(id) 应位于括号内。类似于 CHECK 条件和 FOREIGN KEY 列。一行:

create table A (id int, primary key nonclustered (id)) --ok
create table A (id int, primary key nonclustered id) --not ok

注意:逗号表示“表级约束”。另一种不带逗号的语法表示“列级约束”。当然,PK 是针对每个表的,您可以拥有无​​法在列级别定义的复合键:

create table A (
   id int,
   something datetime,

   primary key clustered (id,something)
)

注意:我也始终会明确说明名称的约束。否则 SQL Server 会生成一个十六进制的。以及显式定义可空性和索引样式。所以我会这样写:

create table A (
   id int NOT NULL,
   something datetime NOT NULL,

   CONSTRAINT PK_A PRIMARY KEY CLUSTERED (id,something)
)

最后,根据 CREATE TABLE,这没问题,可以在我的 SQL Server 2008 实例上运行:

 create table c(id int primary key nonclustered)

The (id) should be in parenthesis. Like CHECK conditions and FOREIGN KEY columns. On one line:

create table A (id int, primary key nonclustered (id)) --ok
create table A (id int, primary key nonclustered id) --not ok

Note: the comma implies "table level constraint". The other syntax without the comma means "column level constraint". Of course PKs are per table, you can have composite keys which can't be defined at the column level:

create table A (
   id int,
   something datetime,

   primary key clustered (id,something)
)

Note: I'd always be explicit about my constraints too for names. Otherwise SQL Server generate a hex looking one. As well as explicitly defining nullability and index style. So I'd write this:

create table A (
   id int NOT NULL,
   something datetime NOT NULL,

   CONSTRAINT PK_A PRIMARY KEY CLUSTERED (id,something)
)

Finally, according to CREATE TABLE, this is OK and works on my SQL Server 2008 instance:

 create table c(id int primary key nonclustered)
泅渡 2024-10-08 01:19:25

括号是必不可少的,因为否则将无法将 PRIMARY KEY 列列表与其后面的任何内容(可能是列或另一个约束)分隔开。

The parentheses are essential because otherwise there would be no way to delimit the PRIMARY KEY column list from whatever follows it (which could be a column or another constraint).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文