Does postgres support to set start value at serial definition? - sql

Here is a question about changing serial value with alter key word. Can this be done in create table defintion? Like that:
CREATE TABLE tablename (
colname SERIAL stars 1000
);

You can try this query.Its worked for me.
CREATE SEQUENCE tablename_colname_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1 ---> here you can mention startup nummber as you need
CACHE 1;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);

Identity Columns
There is no currently no way to do this in a CREATE TABLE command in PostgreSQL 9.x using the PostgreSQL-specific serial-type syntax. However, it's coming in PostgreSQL 10 using the standardized Identity Columns syntax, see my answer on it here
Here is how it will look
CREATE TABLE foo (
id int GENERATED { ALWAYS | BY DEFAULT }
AS IDENTITY [ ( sequence_options ) ]
);
Where (sequence_options) is START WITH 1000, so something like this.
CREATE TABLE foo (
foo_id int GENERATED ALWAYS AS IDENTITY (START WITH 1000)
);

Related

generate sequence in sql server 2014

I am trying to generate a sequence varchar text type, but I do not want to have to create another column to get the id to format it and insert it I want to generate it in the same column, help
create table tbl (
Id int identity not null,
CusId as 'CUS' + format(Id, '00000'),
-- ...
)
You can use sequence object that appeared in SQL Server 2012 + default value like this:
create sequence dbo.ids as int
minvalue 1;
create table dbo.tbl (
CusId varchar(100) default 'CUS' + format(NEXT VALUE FOR dbo.ids, '00000'));
insert into dbo.tbl (CusId) default values;
insert into dbo.tbl (CusId) default values;
insert into dbo.tbl (CusId) default values;
select *
from dbo.tbl;
-----
--CusId
--CUS00001
--CUS00002
--CUS00003
Believe the only viable solution is using 2 columns as you mentioned, and discussed here:
Autoincrement of primary key column with varchar datatype in it
Have not seen it achieved in a single column on its own.

SQL Primary Key Increment

How can I have a primary key that auto increments?
for example: when I insert something at the field name, the field id should be assign a value that is +1 that the last one.
id, name
1, test1
2, test2
so I insert the name something like testX and the id should be auto assigned as 3
how can i do that?
In addition to answer of ain, you can use generator like :
Create a generator :
CREATE GENERATOR <name>;
SET GENERATOR <name> TO <value>;
Getting the current value of generator:
SELECT GEN_ID( <GeneratorName>, 0 ) FROM RDB$DATABASE;
Generating the next value
SELECT GEN_ID( <GeneratorName>, 1 ) FROM RDB$DATABASE;
Using generator in trigger
CREATE OR ALTER trigger <trigger_name>_bi for <table_name>
active before insert position 0
as
begin
if (new.id is null) then
new.id = gen_id(my_generator,1);
end
If you want numeration without holes you have to be very careful.
Generators/Sequences are non-transactional.
Once you get a value if your operation fails, you will have a hole.
If you're using Firebird 3 then you can use the identity column feature:
create table T (
id integer generated by default as identity primary key,
...
);
If you're using some older version of Firebird, then using an sequence with trigger can be used to achieve the autoincrement:
create table T (
id integer primary key,
...
);
CREATE SEQUENCE t_seq;
create trigger t_gen_id
active before insert
on T
as
begin
if(new.id is null)then new.id = next value for t_seq;
end;
Since Firebird 2 the returning clause is supported which is very handy to get the id value generated on the server side.

H2 equivalent of Postgres `SERIAL` or `BIGSERIAL` column?

In Postgres, defining a column with SERIAL/BIGSERIAL has a triple effect as discussed here:
Define a int/bigint column.
Create a sequence object to generate auto-incrementing numbers.
Set the default of the column to call nextval() on the sequence.
Is there a similar shortcut command in H2 to get this related set of behavior?
If not, what would the long version of the SQL be?
Where does the sequence live? How can you adjust its value or reset it?
If you create a column as auto_increment (or identity) H2 creates a sequence in the background. The name of that sequence can be obtained by looking at information_schema.columns:
create table foo
(
id integer auto_increment,
other_column varchar(20)
);
If you then run:
select column_name, column_default
from information_schema.columns
where table_name = 'FOO'
and table_schema = 'PUBLIC';
You'll get something like this:
COLUMN_NAME | COLUMN_DEFAULT
-------------+-----------------------------------------------------------------------------
ID | (NEXT VALUE FOR PUBLIC.SYSTEM_SEQUENCE_C1C36118_ED1C_44D6_B573_6C00C5923EAC)
OTHER_COLUMN |
You can alter that sequence without problems:
alter sequence SYSTEM_SEQUENCE_C1C36118_ED1C_44D6_B573_6C00C5923EAC
restart with 42;
This is essentially identical to Postgres' serial data type
If not, what would the long version of the SQL be?
create sequence foo_id_seq;
create table foo
(
id integer default foo_id_seq.nextval,
other_column varchar(20)
);
The big difference between this and a Postgres serial is that H2 does not know that the sequence "belongs" to the column. You need to drop it manually when the table is dropped.
foo_id_seq.nextval will actually be converted to (NEXT VALUE FOR PUBLIC.FOO_ID_SEQ) when the table is created (and it will be stored like that in information_schema.columns.

HSQLDB - link several sequences for several columns of the table

I would like to link several sequences for generating default values for several columns of a table.
For example:
CREATE SEQUENCE seq1 START WITH 1;
CREATE SEQUENCE seq2 START WITH 1;
CREATE TABLE mytable (rid int GENERATED BY DEFAULT AS SEQUENCE seq1 PRIMARY KEY, p63 int GENERATED BY DEFAULT AS SEQUENCE seq2)
Unfortunately, an error is occured: "identity definition not allowed"
In Postgresql is working.
Any idea ?
Thanks
The first use of the sequence is allowed. You can write a BEFORE INSERT TRIGGER to insert sequence values into the second column.
Having the same issue, I came upon this post. I used another workaround :
CREATE SEQUENCE seq1 START WITH 1;
CREATE SEQUENCE seq2 START WITH 1;
CREATE TABLE mytable (
rid int DEFAULT nextval('seq1') NOT NULL,
p63 int NOT NULL
);
ALTER TABLE mytable ALTER p63 SET DEFAULT NEXTVAL('seq2');
Altering the p63 column after table creation made possible to use the seq2 sequence while it was not accepted when creating the table.

SQL Server Database unique number generation on any record insertion

I have like 11 columns in my database table and i am inserting data in 10 of them. i want to have a unique number like "1101 and so on" in the 11th column.
Any idea what should i do?? Thanks in advance.
SQL Server 2012 and above you can generate Sequence
Create SEQUENCE RandomSeq
start with 1001
increment by 1
Go
Insert into YourTable(Id,col1...)
Select NEXT VALUE FOR RandomSeq,col1....
or else you can use Identity
Identity(seed,increment)
You can start the seed from 1101 and increment the sequence by 1
Create table YourTable
(
id INT IDENTITY(1101,1),
Col varchar(10)
)
If you want to have that unique number in a different field then you can manipulate that field with primary key and insert that value.
If you want in primary key value, then open the table in design mode, go to 'Identity specification', set 'identity increment' and 'identity seed' as you want.
Alternatively you can use table script like,
CREATE TABLE Persons
(
ID int IDENTITY(12,1) PRIMARY KEY,
FName varchar(255) NOT NULL,
)
here the primary key will start seeding from 12 and seed value will be 1.
If you have your table definition already in place you can alter the column and add Computed column marked as persisted as:
ALTER TABLE tablename drop column column11;
ALTER TABLE tablename add column11 as '11'
+right('000000'+cast(ID as varchar(10)), 2) PERSISTED ;
--You can change the right operator value from 2 to any as per the requirements.
--Also replace ID with the identity column in your table.
create table inc
(
id int identity(1100,1),
somec char
)