在db2中如何AUTO_INCREMENT? [英] How to AUTO_INCREMENT in db2?

查看:780
本文介绍了在db2中如何AUTO_INCREMENT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为这很简单,但我似乎不能在我的db2数据库中使用AUTO_INCREMENT。我做了一些搜索,人们似乎使用默认生成,但这对我来说不起作用。

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 this doesn't work for me.

如果它有帮助,这里是我要创建的表,其中sid是自动递增的。

If it helps, here's the table I want to create with the sid being auto incremented.

  create table student(
      sid integer NOT NULL <auto increment?>
      sname varchar(30),
      PRIMARY KEY (sid)
      );

任何指针都不胜感激。

推荐答案

您正在寻找被称为IDENTITY列:

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>为您的特殊情况。阅读更多信息比较序列到标识列

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.

这篇关于在db2中如何AUTO_INCREMENT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆