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 )
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.
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.
The default starting value is minvalue for ascending sequences and maxvalue for descending ones.
Default values can be NULL, or they can be a value that matches the data type of the column (number, text, date, for example).
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!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With