sql >> Base de Datos >  >> RDS >> Mysql

PHP + MYSQL en la CLAVE duplicada aún aumenta la CLAVE DE ÍNDICE

ok sí, ahora recuerdo este problema. Hubo una vez un chico que quería hacer inserciones, pero cada inserción tenía que ser en incrementos de 100 si pudieras imaginar, comenzando @1000. Y tuvimos que envolver todo en un proceso almacenado para tener un lugar de vulnerabilidad. Su problema salió a la luz y desvió su numeración por 1 más o menos.

Al envolverlo, podríamos tener un punto de hacerlo, con un candado, y mantener el valor de auto_inc con ALTER TABLE

El otro enfoque que le dije fue tener una tabla incrementadora, bloquear la fila 1, obtener el valor en esa fila, usarla, actualizar esa tabla incremental en 100. desbloquear.

Todo el tiempo nos reíamos de los problemas del TOC. Creo que solo le gustaban los múltiplos de 10, no sé

Editar:

Esquema:

-- drop table ocd_nextnums;
create table ocd_nextnums
(   -- id table for nextnum, for the OCD impaired
    tableName varchar(100) not null,
    nextnum int not null
    -- won't bother with indexes, go for it if you want
)engine=INNODB; -- note engine type

insert ocd_nextnums(tableName,nextnum) values('thing',1);
insert ocd_nextnums(tableName,nextnum) values('some_other_table',1);

-- drop table thing;
create table thing
(   id int primary key, -- NOT an auto_increment, but is a PK
    email varchar(100) not null,
    version varchar(20) not null,
    lastupdate datetime not null,
    UNIQUE KEY (email)
)engine=MyIsam;

Proceso almacenado:

-- drop procedure putInThing;
delimiter $$
create procedure putInThing
(
    email_In varchar(100), version_In varchar(20)
)
BEGIN
    declare toUse int;
    declare theCount int;

    select count(*) into theCount from thing where email=email_In;
    select id into toUse from thing where email=email_In;   -- useful for result set @end
    IF theCount=1 THEN
        -- was there, do UPDATE
        update thing set version=version_In,lastupdate=now() where email=email_In;
    ELSE
        -- new row, do INSERT (please note the FOR UPDATE clause)
        select nextnum into toUse from ocd_nextnums where tableName='thing' FOR UPDATE;
        update ocd_nextnums set nextnum=nextnum+1 where tableName='thing';

        insert thing(id,email,version,lastupdate) values (toUse,email_In,version_In,now());
    end if;
    select toUse;   -- <------- that was your id
END
$$

Prueba:

call putInThing('[email protected]','111');
call putInThing('[email protected]','121');
call putInThing('[email protected]','107');
select * from thing;
+----+----------+---------+---------------------+
| id | email    | version | lastupdate          |
+----+----------+---------+---------------------+
|  1 | [email protected] | 111     | 2015-08-14 17:08:10 |
|  2 | [email protected] | 121     | 2015-08-14 17:08:54 |
|  3 | [email protected] | 107     | 2015-08-14 17:08:56 |
+----+----------+---------+---------------------+

call putInThing('[email protected]','101111007'); -- is an update
call putInThing('[email protected]','42'); -- is an update
call putInThing('[email protected]','10007'); -- is an update
call putInThing('[email protected]','1'); -- is an insert

select * from thing;
+----+----------------------+---------+---------------------+
| id | email                | version | lastupdate          |
+----+----------------------+---------+---------------------+
|  1 | [email protected]             | 111     | 2015-08-14 17:08:10 |
|  2 | [email protected]             | 121     | 2015-08-14 17:08:54 |
|  3 | [email protected]             | 10007   | 2015-08-14 17:22:09 |
|  4 | [email protected] | 1       | 2015-08-14 17:22:47 |
+----+----------------------+---------+---------------------+

De la parte de Mysql INNODB del Manual :

¿Me verás usando esto? Probablemente no. Solo mostrándolo. Estoy bien con lagunas y durmiendo por la noche. Es por eso que nombré la primera tabla como lo hice :>