MySQL Forums
Forum List  »  Portuguese

Re: Duvida Simples
Posted by: Airton Lastori
Date: April 25, 2013 09:19AM

Você pode usar eventos do MySQL.

Crie um database de exemplo:
CREATE DATABASE teste_evento;
USE teste_evento;

Crie uma tabela t1 que será populada automaticamente:
CREATE TABLE t1 (
id INT AUTO_INCREMENT,
PRIMARY KEY (id)
) ENGINE=InnoDB;

Crie uma tabela com suas configurações e insira uma linha para configurar qual o valor máximo que a tabela t1 vai receber:
CREATE TABLE config (
parametro CHAR(50),
valor VARCHAR(255),
PRIMARY KEY (parametro)
) ENGINE=InnoDB;

INSERT INTO config VALUES('id-maximo', '100');


Crie seu evento:
DELIMITER $$
CREATE
EVENT insere_id_ate_maximo ON SCHEDULE EVERY 1 SECOND STARTS NOW()
DO BEGIN
-- verifica qual o id-maximo
SELECT valor FROM teste_evento.config WHERE parametro = 'id-maximo' LIMIT 1 INTO @id_max;
-- verifica qual o valor maximo valor ja inserido
SELECT MAX(id) FROM teste_evento.t1 LIMIT 1 INTO @max_t1;
-- insere registro caso nao seja id-maximo
IF @max_t1 IS NULL OR (@id_max IS NOT NULL AND @max_t1 < @id_max) THEN
INSERT INTO t1 VALUES();
END IF;
END $$
DELIMITER ;


Veja se há valores na tabela t1 e se há um evento rodando:
SELECT * FROM t1;
SHOW PROCESSLIST;

Para iniciar os eventos:
SET GLOBAL event_scheduler = ON;
SHOW PROCESSLIST;

Agora você deve ver sua tabela t1 sendo incrementada automaticamente:
SELECT * FROM t1;
SELECT COUNT(*) FROM t1;

Para reiniciar o processo:
ALTER TABLE t1 AUTO_INCREMENT = 1;
DELETE FROM t1 where id < 101;

Espero que ajude.

Airton Lastori

Options: ReplyQuote


Subject
Views
Written By
Posted
3340
April 02, 2013 02:02PM
Re: Duvida Simples
2376
April 25, 2013 09:19AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.