patternsqlCritical
PostgreSQL - Truncate a table on cascade and reset all hierarchic sequences with 1
Viewed 0 times
postgresqlallwithhierarchicresettruncatecascadeandsequencestable
Problem
Is there any way to reset all the sequences of tables, when truncate a table on cascade.
I already read this post How to reset sequence in postgres and fill id column with new data?
It work only for one sequence, but my problem is to restart all the sequence of the truncated tables.
Consider when I use
I already read this post How to reset sequence in postgres and fill id column with new data?
ALTER SEQUENCE seq RESTART WITH 1;
UPDATE t SET idcolumn=nextval('seq');It work only for one sequence, but my problem is to restart all the sequence of the truncated tables.
Consider when I use
TRUNCATE sch.mytable CASCADE; It affect 3 related tables, which mean three sequences, Is there any solution to restart this sequence in one shot.Solution
The
If
TRUNCATE statement has an additional option RESTART IDENTITY which resets the sequences associated with the table columns. TRUNCATE sch.mytable RESTART IDENTITY CASCADE;If
CASCADE is defined, then the sequences of all affected tables are reset.Code Snippets
TRUNCATE sch.mytable RESTART IDENTITY CASCADE;Context
StackExchange Database Administrators Q#193495, answer score: 80
Revisions (0)
No revisions yet.