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?
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: