To solve such a problem it will be necessary to use a composite primary key, and control the auto increment through a Trigger, so it will no longer be automatic in the field itself.
Based on the script below, you will be able to extend the rules for changing periods as you need and so adjust the id to 1 for each new period, but note that the period count is manual, to make it automatic it will be necessary to extend the Rigger code so that it is based on a third parameterization table.
-- --------------------------------------------------------
--
-- Table structure for table `tab_confere_evento`
--
CREATE TABLE IF NOT EXISTS `tab_confere_evento` (
`id` bigint(11) NOT NULL,
`evento_id` bigint(11) unsigned DEFAULT NULL,
`evento_periodo` int(10) unsigned NOT NULL,
`usuario` varchar(100) DEFAULT NULL,
`status` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `tab_evento`
--
CREATE TABLE IF NOT EXISTS `tab_evento` (
`id` bigint(20) unsigned NOT NULL,
`periodo` int(10) unsigned NOT NULL,
`data` timestamp NULL DEFAULT NULL,
`evento` varchar(120) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Tabela para guardar os logs de modulos abertos pelo usuário' ;
--
-- Triggers `tab_evento`
--
DELIMITER //
CREATE TRIGGER `prx_periodo_trigger` BEFORE INSERT ON `tab_evento`
FOR EACH ROW begin
set new.id=(select ifnull((select max(prx_id)+1 from tab_event where periodo=new.periodo),1));
end
//
DELIMITER ;
--
-- Indexes for table `tab_confere_evento`
--
ALTER TABLE `tab_confere_evento`
ADD PRIMARY KEY (`id`,`evento_periodo`), ADD KEY `idx_evento_id` (`evento_id`,`evento_periodo`);
--
-- Indexes for table `tab_evento`
--
ALTER TABLE `tab_evento`
ADD PRIMARY KEY (`id`,`periodo`), ADD KEY `id` (`id`), ADD KEY `periodo` (`periodo`);
For optimization reasons, wouldn’t it be interesting to use a composite ID? by what it seems this table works with events, you would have other fields that could compose this primary key and make it composed? something like date?
– Delfino
@Delfino even using the date as part of the primary key the problem continues, of the error, is that the id field does not increment more of the same error
– SneepS NinjA
actually you will have to change the bank and go to use a precedent to auto-increment the composite key, zeroing the sequential counter whenever it pops, and have a second field that will count this burst either by period or simply by overflow, I think this can help you; http://stackoverflow.com/a/18120156/2766598
– Delfino
@Delfino this case of creating process to auto-increment does not solve because the incremented number I have, it just can not insert it because it is larger than the field supports, if I reset the counter I get problems in the relationship between another table
– SneepS NinjA
seriously that you have 18446744073709551616 records in your table??? I was impressed. what is this about?
– Italo Rodrigo