Auto-increment primary key in SQL tables

前端 未结 6 1187
执念已碎
执念已碎 2020-12-02 16:54

Using Sql Express Management Studio 2008 GUI (not with coding), how can I make a primary key auto-incremented?

Let me explain: there is a table which has a column na

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 17:25

    Although the following is not way to do it in GUI but you can get autoincrementing simply using the IDENTITY datatype(start, increment):

    CREATE TABLE "dbo"."TableName"
    (
       id int IDENTITY(1,1) PRIMARY KEY NOT NULL,
       name varchar(20),
    );
    

    the insert statement should list all columns except the id column (it will be filled with autoincremented value):

    INSERT INTO "dbo"."TableName" (name) VALUES ('alpha');
    INSERT INTO "dbo"."TableName" (name) VALUES ('beta');
    

    and the result of

    SELECT id, name FROM "dbo"."TableName";
    

    will be

    id    name
    --------------------------
    1     alpha
    2     beta
    

提交回复
热议问题