“PostgreSQL réinitialiser l'incrément automatique” Réponses codées

PostgreSQL réinitialiser l'incrément automatique

-- if you dont mind losing the data, do the following
TRUNCATE TABLE someTable RESTART IDENTITY;
GutoTrosla

index PostgreSQL RESET AUTO_INCRAMment

-- Change the starting value of the sequence

ALTER SEQUENCE project_id_seq RESTART 3000;

-- Same but dynamic :

SELECT SETVAL('project_id_seq', (SELECT MAX(id) + 1 FROM project));
The Strangest Salamander

Valeur de mise à jour postgresql Auto_increment

ALTER SEQUENCE product_id_seq RESTART WITH 1453
Cybernated Dev

PostgreSQL réinitialiser l'incrément automatique

   Route::get('/sync_database/', function () {
        // Get all the tables from your database
        //for postgres
        $tables = \DB::select('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\' ORDER BY table_name;');
        //For mysql
        // $tables = DB::select('SHOW TABLES');

        // Set the tables in the database you would like to ignore
        $ignores = array('password_resets');

        $altered_tables = array();

        //loop through the tables
        foreach ($tables as $table) {

            // if the table is not to be ignored then:
            if (!in_array($table->table_name, $ignores)) {

                //Get the max id from that table and add 1 to it
                $seq = \DB::table($table->table_name)->max('id') + 1;

                // alter the sequence to now RESTART WITH the new sequence index from above (
                    //for MySQL go \DB::select('ALTER TABLE ' . $table->Tables_in_db . ' AUTO_INCREMENT ' . $seq);
                    // Tables_in_db
                    //for Postgres go \DB::select('ALTER SEQUENCE ' . $table->Tables_in_db . '_id_seq RESTART WITH ' . $seq);)
                    // table_name
                \DB::select('ALTER SEQUENCE ' . $table->table_name . '_id_seq RESTART WITH ' . $seq);
 
                array_push($altered_tables, $table->table_name);
            }
        }

        print_r($altered_tables);
    });
Shadowtampa

Réponses similaires à “PostgreSQL réinitialiser l'incrément automatique”

Questions similaires à “PostgreSQL réinitialiser l'incrément automatique”

Plus de réponses similaires à “PostgreSQL réinitialiser l'incrément automatique” dans Sql

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code