Postgres sequence does not reset when deleted and recreated - postgresql

I have a Postgres sequence defined with the following code -
CREATE SEQUENCE mydb.mytable
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 25
CACHE 1;
I am running this as part of a SQL script on a Postgres 9.5 database.
When I ran the script the first time, the sequence generates integers starting with 1 correctly.
While testing, I dropped the sequence and the table associated with it and re ran the SQL script which re created the sequence. When re created, the sequence is incorrectly generating integers from the last number previously used. Why does it not reset to the minimum value 1 on re create?

Related

Does hbase have similar feature as nextval('sequence_name') provided by postgres db

Is there any feature in hbase to create and use sequence similar to postgres like below
CREATE SEQUENCE:
CREATE SEQUENCE schemaname."SEQUENCE_123456"
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 20
NO CYCLE;
FETCH NextVal of SEQUENCE:
SELECT nextval('SEQUENCE_123456');
For Example if we call SELECT nextval('SEQUENCE_123456') it should return value as 1, if it call for next time it should give 2 and so on...
If not HBase is there any other nosql database which supports this feature.
Tried verifying hbase documentation.

What is the equivalent for NO ORDER of oracle sequence in postgresql?

I have a query
CREATE SEQUENCE "SEQ_ID"
MINVALUE 1
MAXVALUE 99999
INCREMENT BY 1 START WITH 121
CACHE 20
NOORDER
NOCYCLE;
This one is of oracle commands.
Now, I want to convert into a PostgreSQL command.
How do I replace NO ORDER here.
How do I replace NO ORDER here.
Just remove it.
There is no equivalent, and I you probably didn't need (or specify) it in Oracle to begin with.
In Oracle ORDER is only needed in a RAC environment and NOORDER is the default.
So the equivalent would be:
CREATE SEQUENCE seq_id
MINVALUE 1
MAXVALUE 99999
INCREMENT BY 1 START WITH 121
CACHE 20
NO CYCLE;
I removed the double quotes because you should avoid those dreaded quoted identifiers.

Can't use OrientDB sequence in 2.2.16

I've download community 2.2.16 and I'm trying to use sequence in the following way:
DROP SEQUENCE idseq
CREATE SEQUENCE idseq TYPE CACHED START 1 INCREMENT 1 CACHE 20
SELECT SEQUENCE('idseq').next() from V LIMIT 20
But I can't get any values (it's working on 2.2.15)
In addition, I know the sequence created since when I'm trying to recreate the index it throws me an error:
Sequence 'IDSEQ' already exists
My problem was that because my DB was empty the way I tried to select values from sequence didn't work and retunes an empty result set.
Since there isn't DUAL table like in Oracle,
I changed the query to be:
SELECT SEQUENCE('idseq').next() from OIdentity LIMIT 20

Getting SQL command not properly ended when creating a db2 sequence

I'm trying to run some DB2 SQL from a tutorial and I keep getting "SQL command not properly ended" for this SQL:
CREATE SEQUENCE mdm_robins80.V_ELATTR_ID_SEQ AS INT START WITH 1000000 INCREMENT BY 1 MINVALUE 1000000 MAXVALUE 1009999 CACHE 10;
I can't see anything wrong with this statement.

ORACLE 10g auto increment trigger/sequence doesn't increment as programmed

I've develop the following sequence on a ORACLE 10 DB wich autoincrement an id column of a table named t_client by 1 (A trigger is developed also and execute when new data on this table is registered)
The issue is that sometimes the sequecce doesn't increment by 1, if not in 21. I doesn't really find the issue by it's increment by 21. Here is the code sample
Secuence ClientConsecutive
CREATE SEQUENCE user_owner.ClientConsecutive
MINVALUE 0
MAXVALUE 999999999999999999999999999
INCREMENT BY 1
START WITH 1
CACHE 20
NOORDER NOCYCLE ;
Trigger_Client_ID
create or replace
TRIGGER user_owner.TRIGGER_CLIENT_ID
BEFORE INSERT ON T_CLIENT
REFERENCING NEW AS NEW FOR EACH ROW
DECLARE valueSequence NUMBER := 0;
BEGIN
SELECT ClientConsecutive.NEXTVAL INTO valorSecuencia FROM DUAL;
:NEW.ID_CLIENTE := valueSequence;
END;
When the application associated to this objects executes it, on the SQLdeveloper looking for the sequence details sometimes it visualizes
LAST_NUMBER 2
which is correct when I register new data on the empty table but sometimes says
LAST_NUMBER 21
Any ideas?
Oracle sequences are not guaranteed to be gap-free. In fact, you can all but guarantee that there will occasionally be gaps.
In your case, you're using the default cache size of 20. When the database is shut down or the sequence cache is aged out of memory, whatever values were part of the sequence cache will be lost. So if the cache holds the numbers 1-20 initially, you do a nextval that returns a value 1, and the cache ages out of SGA before you call nextval again, you'd expect to get a value of 21 the next time you call nextval.
You can reduce the frequency of gaps by reducing the CACHE size on the sequence to 1. That will not eliminate gaps-- there will still, for example, be cases where a transaction gets rolled back, for example. And it will decrease performance. In general, you shouldn't be concerned with gaps since sequence-generated primary keys are, by definition, merely supposed to be unique identifiers.