-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Tempo de geração: 11/08/2026 às 19:45
-- Versão do servidor: 10.4.32-MariaDB
-- Versão do PHP: 8.2.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Banco de dados: `projetoum`
--

DELIMITER $$
--
-- Procedimentos
--
CREATE DEFINER=`root`@`localhost` PROCEDURE `validamacmiguel` (IN `$idmac` VARCHAR(17), OUT `$ativo` BOOLEAN, OUT `$contador` INT, OUT `$limite` INT)   BEGIN
SELECT ativo into $ativo from macmiguel
where idmacmiguel=$idmac;

SELECT contador into $contador from macmiguel
where idmacmiguel=$idmac;

SELECT limite into $limite from macmiguel
where idmacmiguel=$idmac;

END$$

DELIMITER ;

-- --------------------------------------------------------

--
-- Estrutura para tabela `leituramiguel`
--

CREATE TABLE `leituramiguel` (
  `idleituramiguel` int(10) UNSIGNED NOT NULL,
  `macmiguel_idmacmiguel` varchar(17) NOT NULL,
  `datahora` datetime DEFAULT NULL,
  `valor` int(10) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Acionadores `leituramiguel`
--
DELIMITER $$
CREATE TRIGGER `tgdeleteleituramiguel` AFTER DELETE ON `leituramiguel` FOR EACH ROW begin
update macmiguel
set contador=contador-1
where idmacmiguel=old.macmiguel_idmacmiguel;
end
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `tginsertleituramiguel` AFTER INSERT ON `leituramiguel` FOR EACH ROW BEGIN
UPDATE macmiguel
set contador=contador+1
WHERE idmacmiguel=new.macmiguel_idmacmiguel;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `tgvalidamacmiguel` BEFORE INSERT ON `leituramiguel` FOR EACH ROW begin
CALL validamacmiguel(new.macmiguel_idmacmiguel,@ativo,@contador,@limite);

if @ativo=0 then SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'MAC INATIVO';
end if ; 
if @contador>=@limite then SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'LIMITE DE LEITURAS ATINGIDO';
end if ; 

end
$$
DELIMITER ;

-- --------------------------------------------------------

--
-- Estrutura para tabela `macmiguel`
--

CREATE TABLE `macmiguel` (
  `idmacmiguel` varchar(17) NOT NULL,
  `nome` varchar(25) DEFAULT NULL,
  `contador` int(10) UNSIGNED DEFAULT 0,
  `limite` int(10) UNSIGNED DEFAULT 0,
  `ativo` tinyint(1) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Despejando dados para a tabela `macmiguel`
--

INSERT INTO `macmiguel` (`idmacmiguel`, `nome`, `contador`, `limite`, `ativo`) VALUES
('01:02:03', 'Placa um', 0, 2, 1);

--
-- Índices para tabelas despejadas
--

--
-- Índices de tabela `leituramiguel`
--
ALTER TABLE `leituramiguel`
  ADD PRIMARY KEY (`idleituramiguel`),
  ADD KEY `leituramiguel_FKIndex1` (`macmiguel_idmacmiguel`);

--
-- Índices de tabela `macmiguel`
--
ALTER TABLE `macmiguel`
  ADD PRIMARY KEY (`idmacmiguel`);

--
-- AUTO_INCREMENT para tabelas despejadas
--

--
-- AUTO_INCREMENT de tabela `leituramiguel`
--
ALTER TABLE `leituramiguel`
  MODIFY `idleituramiguel` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

--
-- Restrições para tabelas despejadas
--

--
-- Restrições para tabelas `leituramiguel`
--
ALTER TABLE `leituramiguel`
  ADD CONSTRAINT `leituramiguel_ibfk_1` FOREIGN KEY (`macmiguel_idmacmiguel`) REFERENCES `macmiguel` (`idmacmiguel`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
