How to AUTO_INCREMENT in db2?

前端 未结 4 547
轻奢々
轻奢々 2020-12-05 17:17

I thought this would be simple, but I can\'t seem to use AUTO_INCREMENT in my db2 database. I did some searching and people seem to be using \"Generated by Default\", but t

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 17:59

    You're looking for is called an IDENTITY column:

    create table student (
       sid integer not null GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1)
      ,sname varchar(30)
      ,PRIMARY KEY (sid)
    );
    

    A sequence is another option for doing this, but you need to determine which one is proper for your particular situation. Read this for more information comparing sequences to identity columns.

提交回复
热议问题