Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence as default value for a column

Tags:

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 ) 
like image 831
Endy Tjahjono Avatar asked Jan 19 '13 10:01

Endy Tjahjono


People also ask

How do I set a default column value?

In Object Explorer, right-click the table with columns for which you want to change the scale and select Design. Select the column for which you want to specify a default value. In the Column Properties tab, enter the new default value in the Default Value or Binding property.

What is the default datatype of sequence?

If not specified, the default data type is INT. If specified, the INCREMENT value is a non-zero number which fits in a DataType value. If not specified, the INCREMENT defaults to 1. INCREMENT is the step by which the sequence generator advances.

What is sequence default start with value?

The default starting value is minvalue for ascending sequences and maxvalue for descending ones.

What is the default value of a column?

Default values can be NULL, or they can be a value that matches the data type of the column (number, text, date, for example).


1 Answers

It turned out to be easy enough:

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

or if the table is already created:

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

(thank you Matt Strom for the correction!)

like image 146
Endy Tjahjono Avatar answered Oct 12 '22 16:10

Endy Tjahjono