Column level vs table level constraints in sql server?

前端 未结 4 880
逝去的感伤
逝去的感伤 2021-02-07 19:43

a. Column Level

GO

CREATE TABLE Products
(
ProductID INT CONSTRAINT pk_products_pid PRIMARY KEY,
ProductName VARCHAR(25)
);

GO

b.

4条回答
  •  星月不相逢
    2021-02-07 20:11

    No. It's just a matter of personal taste how you apply the constraint.

    The primary key constraint is just a primary key constraint - it always applies to the table (after all: it could contain multiple columns - it cannot be "at the column level").

    It's not "at the column level" once or at the "table level" in the other case - it's the same always.

    Just for fun - you can also create the primary key constraint a third way:

    (CREATE TABLE statement)
    GO
    
    ALTER TABLE dbo.Products
    ADD CONSTRAINT PK_Products_pid PRIMARY KEY(ProductID)
    

    and that again would be identical to the two other options you already have.

提交回复
热议问题