¿Cómo establecer el valor inicial y el incremento automático en MySQL?


¿Cómo establezco el valor inicial para una columna "id" en una tabla MySQL que comienza desde 1001?

Quiero hacer una inserción "INSERT INTO users (name, email) VALUES ('{$name}', '{$email}')";

Sin especificar el valor inicial para la columna id.

Author: Tuan Dang, 2009-09-28

8 answers

Usa esto:

ALTER TABLE users AUTO_INCREMENT=1001;

O si aún no ha agregado una columna id, también agréguela

ALTER TABLE users ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ADD INDEX (id);
 397
Author: Anatoliy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-17 05:01:10

MySQL-Configurar una clave primaria de auto-incremento que comienza en 1001:

Paso 1, crea tu tabla:

create table penguins(
  my_id       int(16) auto_increment, 
  skipper     varchar(4000),
  PRIMARY KEY (my_id)
)

Paso 2, establezca el número de inicio para la clave primaria de incremento automático:

ALTER TABLE penguins AUTO_INCREMENT=1001;

Paso 3, insertar algunas filas:

insert into penguins (skipper) values("We need more power!");
insert into penguins (skipper) values("Time to fire up");
insert into penguins (skipper) values("kowalski's nuclear reactor.");

Paso 4, interpreta la salida:

select * from penguins

Impresiones:

'1001', 'We need more power!'
'1002', 'Time to fire up'
'1003', 'kowalski\'s nuclear reactor'
 42
Author: Eric Leschinski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-24 17:53:52

MySQL Workbench

Si desea evitar escribir sql, también puede hacerlo en MySQL Workbench haciendo clic derecho en la tabla, elija "Alter Table ..."en el menú.

Cuando se abra la vista de estructura de tabla, vaya a la pestaña "Opciones" (en la parte inferior inferior de la vista) y establezca el campo "Incremento automático" en el valor del siguiente número de incremento automático.

No olvide presionar "Aplicar" cuando haya terminado con todos los cambios.

PhpMyAdmin:

Si usted es usando phpMyAdmin, puede hacer clic en la tabla en el menú de navegación izquierdo, ir a la pestaña "Operaciones" y en Opciones de tabla cambiar el valor AUTO_INCREMENT y haga clic en Aceptar.

 30
Author: Bojan Hrnkas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-30 13:52:17

Primero debe agregar una columna para el incremento automático

alter table users add column id int(5) NOT NULL AUTO_INCREMENT FIRST

Esta consulta para agregar columna al principio. Ahora tienes que restablecer el valor inicial de incremento automático. Así que usa esta consulta

alter table users AUTO_INCREMENT=1001

Ahora su mesa comenzó con 1001

 9
Author: John,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-24 20:09:45

También , en phpMyAdmin , puede seleccionar tabla del lado izquierdo(lista de tablas) y luego hacer esto yendo allí.
Pestaña de Operaciones- > Opciones de tabla- > AUTO_INCREMENT.

Ahora, Establezca sus valores y luego presione Ir debajo de las opciones de tabla Bo x.

 3
Author: Mostafa Fallah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-18 15:42:17

Para esto tienes que establecer AUTO_INCREMENT valor

ALTER TABLE tablename AUTO_INCREMENT = <INITIAL_VALUE>

Ejemplo

ALTER TABLE tablename AUTO_INCREMENT = 101
 2
Author: Arun Kasyakar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-10 08:00:40

ALTER TABLE users AUTO_INCREMENT = 1001;

 0
Author: Jay Momaya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-02 12:11:52

También puede configurarlo en la instrucción create table.

`CREATE TABLE(...) AUTO_INCREMENT=1000`
 0
Author: ospider,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-12 23:24:37