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

查看:42
本文介绍了如何在 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 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天全站免登陆