Last active
May 26, 2022 08:23
-
-
Save f3l1x/a68e2bfe3a5be13db95ddc0d3d6bcc6b to your computer and use it in GitHub Desktop.
SQL utils (Postgres / MySQL MariaDB)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select 'SELECT SETVAL(' || seq [ 1] || ', COALESCE(MAX('||column_name||')+1, 1) ) FROM '||table_name||';' | |
from ( | |
SELECT table_name, column_name, column_default, regexp_match(column_default, '''.*''') as seq | |
from information_schema.columns | |
where column_default ilike 'nextval%' | |
) as sequense_query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Reset sequence | |
DO $$ | |
DECLARE | |
i TEXT; | |
BEGIN | |
FOR i IN (SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public') LOOP | |
EXECUTE 'Select setval('''||i||'_id_seq'', (SELECT max(id) as a FROM "'||i||'")+1);'; | |
END LOOP; | |
END$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment