创建非聚簇PK语法是否不一致?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(id) 应位于括号内。类似于 CHECK 条件和 FOREIGN KEY 列。一行:
注意:逗号表示“表级约束”。另一种不带逗号的语法表示“列级约束”。当然,PK 是针对每个表的,您可以拥有无法在列级别定义的复合键:
注意:我也始终会明确说明名称的约束。否则 SQL Server 会生成一个十六进制的。以及显式定义可空性和索引样式。所以我会这样写:
最后,根据 CREATE TABLE,这没问题,可以在我的 SQL Server 2008 实例上运行:
The (id) should be in parenthesis. Like CHECK conditions and FOREIGN KEY columns. On one line:
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:
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:
Finally, according to CREATE TABLE, this is OK and works on my SQL Server 2008 instance:
括号是必不可少的,因为否则将无法将 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).