序列作为列的默认值 [英] Sequence as default value for a column

查看:75
本文介绍了序列作为列的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个序列:

I have already created a sequence:

create sequence mainseq as bigint start with 1 increment by 1

如何使用此序列作为列的默认值?

How do I use this sequence as the default value of a column?

create table mytable(
    id      bigint not null default mainseq     -- how?
    code    varchar(20) not null
)

推荐答案

结果证明很简单:

create table mytable (
    id      bigint not null constraint DF_mytblid default next value for mainseq,
    code    varchar(20) not null
)

或者如果表已经创建:

alter table mytable
add constraint DF_mytblid
default next value for mainseq
for id

(感谢 Matt Strom 的更正!)

(thank you Matt Strom for the correction!)

这篇关于序列作为列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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