yii\db\mysql\QueryBuilder::resetSequence PHP Method

resetSequence() public method

The sequence will be reset such that the primary key of the next new row inserted will have the specified value or 1.
public resetSequence ( string $tableName, mixed $value = null ) : string
$tableName string the name of the table whose primary key sequence will be reset
$value mixed the value for the primary key of the next new row inserted. If this is not set, the next new row's primary key will have a value 1.
return string the SQL statement for resetting sequence
    public function resetSequence($tableName, $value = null)
    {
        $table = $this->db->getTableSchema($tableName);
        if ($table !== null && $table->sequenceName !== null) {
            $tableName = $this->db->quoteTableName($tableName);
            if ($value === null) {
                $key = reset($table->primaryKey);
                $value = $this->db->createCommand("SELECT MAX(`{$key}`) FROM {$tableName}")->queryScalar() + 1;
            } else {
                $value = (int) $value;
            }
            return "ALTER TABLE {$tableName} AUTO_INCREMENT={$value}";
        } elseif ($table === null) {
            throw new InvalidParamException("Table not found: {$tableName}");
        } else {
            throw new InvalidParamException("There is no sequence associated with table '{$tableName}'.");
        }
    }