CREATE DATABASE  IF NOT EXISTS `flat_db` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `flat_db`;
-- MySQL dump 10.13  Distrib 5.7.9, for Win64 (x86_64)
--
-- Host: 127.0.0.1    Database: flat_db
-- ------------------------------------------------------
-- Server version	5.5.23

/*!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 utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `counterdata`
--

DROP TABLE IF EXISTS `counterdata`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `counterdata` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `counters_id` int(11) NOT NULL,
  `value` decimal(10,2) DEFAULT NULL,
  `measureDateTime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_counterData_counters1_idx` (`counters_id`),
  CONSTRAINT `fk_counterData_counters1` FOREIGN KEY (`counters_id`) REFERENCES `counters` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `counterdata`
--

LOCK TABLES `counterdata` WRITE;
/*!40000 ALTER TABLE `counterdata` DISABLE KEYS */;
INSERT INTO `counterdata` VALUES (1,1,1.00,'2018-10-14 09:04:40'),(2,1,2.00,'2018-10-14 09:04:50'),(3,1,3.00,'2018-10-14 09:04:56'),(4,2,1.00,'2018-10-14 09:08:37'),(5,5,1.01,'2018-10-14 14:06:21'),(6,5,1.02,'2018-10-14 14:10:25'),(7,5,1.03,'2018-10-14 14:16:32');
/*!40000 ALTER TABLE `counterdata` ENABLE KEYS */;
UNLOCK TABLES;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `flat_db`.`counterData_BEFORE_INSERT` BEFORE INSERT ON `counterData` FOR EACH ROW
BEGIN
SET NEW.MeasureDateTime = Now();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;

--
-- Table structure for table `counters`
--

DROP TABLE IF EXISTS `counters`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `counters` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `creationDateTime` datetime DEFAULT NULL,
  `serialNumber` varchar(45) DEFAULT NULL,
  `lastCheckDate` datetime DEFAULT NULL,
  `nextCheckDate` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `counters`
--

LOCK TABLES `counters` WRITE;
/*!40000 ALTER TABLE `counters` DISABLE KEYS */;
INSERT INTO `counters` VALUES (1,NULL,'1',NULL,NULL),(2,NULL,'2',NULL,NULL),(3,NULL,'3',NULL,NULL),(4,'2018-10-14 11:36:25','eSerialNumber1231','2018-10-14 11:10:42','2018-10-14 11:10:42'),(5,'2018-10-14 13:11:12','eSerialNumber1','2018-10-14 11:10:42','2018-10-14 11:10:42'),(6,'2018-10-14 15:56:46','eSerialNumber6','2018-10-14 11:10:42','2018-10-14 11:10:42'),(7,'2018-10-14 15:58:33','eSerialNumber7','2018-10-14 11:10:42','2018-10-14 11:10:42'),(8,'2018-10-14 19:20:20','eSerialNumber6','2018-10-14 11:10:42','2018-10-14 11:10:42'),(9,'2018-10-14 19:21:56','eSerialNumber7','2018-10-14 11:10:42','2018-10-14 11:10:42'),(10,'2018-10-14 19:24:19','eSerialNumber8','2018-10-14 11:10:42','2018-10-14 11:10:42'),(11,'2018-10-14 19:25:56','eSerialNumber9','2018-10-14 11:10:42','2018-10-14 11:10:42'),(12,'2018-10-14 19:27:53','eSerialNumber10','2018-10-14 11:10:42','2018-10-14 11:10:42'),(13,'2018-10-14 20:49:25','eSerialNumber11','2018-10-14 11:10:42','2018-10-14 11:10:42'),(14,'2018-10-14 22:40:17','eSerialNumber12','2018-10-14 11:10:42','2018-10-14 11:10:42'),(15,'2018-10-15 09:52:24','eSerialNumber13','2018-10-14 11:10:42','2018-10-14 11:10:42'),(16,'2018-10-15 10:04:03','eSerialNumber','2018-10-14 11:10:42','2018-10-14 11:10:42'),(17,'2018-10-15 11:30:24','eSerialNumber14','2018-10-14 11:10:42','2018-10-14 11:10:42'),(18,'2018-10-15 11:40:00','eSerialNumber15','2018-10-14 11:10:42','2018-10-14 11:10:42'),(19,'2018-10-15 12:04:48','eSerialNumber16','2018-10-14 11:10:42','2018-10-14 11:10:42'),(20,'2018-10-15 12:28:33','eSerialNumber17','2018-10-15 12:28:24','2018-10-15 12:28:24'),(21,'2018-10-15 12:28:44','eSerialNumber18','2018-10-15 12:28:35','2018-10-15 12:28:35'),(22,'2018-10-15 12:31:31','eSerialNumber19','2018-10-15 12:31:19','2018-10-15 12:31:19'),(23,'2018-10-15 12:33:14','eSerialNumber20','2018-10-15 12:33:09','2018-10-15 12:33:09'),(24,'2018-10-15 12:34:40','eSerialNumber21','2018-10-15 12:34:33','2018-10-15 12:34:33'),(25,'2018-10-15 12:37:27','eSerialNumber22','2018-10-15 12:37:22','2018-10-15 12:37:22');
/*!40000 ALTER TABLE `counters` ENABLE KEYS */;
UNLOCK TABLES;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `flat_db`.`counters_BEFORE_INSERT` BEFORE INSERT ON `counters` FOR EACH ROW
BEGIN
SET NEW.creationDateTime=Now();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;

--
-- Table structure for table `countersupdatehistory`
--

DROP TABLE IF EXISTS `countersupdatehistory`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `countersupdatehistory` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `counterOldValue` decimal(10,2) DEFAULT NULL,
  `flats_id` int(11) NOT NULL,
  `counters_id` int(11) NOT NULL,
  `countersOld_id` int(11) DEFAULT NULL,
  `datetime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_countersUpdateHistory_flats1_idx` (`flats_id`),
  KEY `fk_countersUpdateHistory_counters1_idx` (`counters_id`),
  KEY `fk_countersUpdateHistory_counters2_idx` (`countersOld_id`),
  CONSTRAINT `fk_countersUpdateHistory_counters2` FOREIGN KEY (`countersOld_id`) REFERENCES `counters` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_countersUpdateHistory_counters1` FOREIGN KEY (`counters_id`) REFERENCES `counters` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_countersUpdateHistory_flats1` FOREIGN KEY (`flats_id`) REFERENCES `flats` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `countersupdatehistory`
--

LOCK TABLES `countersupdatehistory` WRITE;
/*!40000 ALTER TABLE `countersupdatehistory` DISABLE KEYS */;
INSERT INTO `countersupdatehistory` VALUES (1,1.00,1,1,1,'2018-10-14 17:12:37'),(2,3.00,2,7,2,'2018-10-14 19:01:18'),(3,NULL,10,12,NULL,'2018-10-14 19:27:53'),(4,3.00,9,12,11,'2018-10-14 19:28:25'),(5,3.00,9,12,11,'2018-10-14 19:30:54'),(6,3.00,9,12,11,'2018-10-14 19:36:07'),(7,3.00,9,12,11,'2018-10-14 22:25:03'),(8,3.00,10,11,12,'2018-10-14 22:25:03'),(12,3.00,10,13,11,'2018-10-14 22:38:17'),(13,3.00,9,13,12,'2018-10-14 22:39:55'),(14,3.00,10,12,13,'2018-10-14 22:39:55'),(18,NULL,11,14,NULL,'2018-10-14 22:51:36'),(21,3.00,18,18,18,'2018-10-15 12:03:50'),(22,0.00,18,18,18,'2018-10-15 12:03:50'),(23,NULL,19,19,NULL,'2018-10-15 12:05:56'),(24,3.00,19,18,19,'2018-10-15 12:08:26'),(25,0.00,18,19,18,'2018-10-15 12:08:26'),(26,3.00,18,18,19,'2018-10-15 12:10:36'),(27,0.00,19,19,18,'2018-10-15 12:10:36'),(28,3.00,19,18,19,'2018-10-15 12:15:25'),(29,0.00,18,19,18,'2018-10-15 12:15:25'),(31,0.00,1,24,7,'2018-10-15 12:34:40'),(32,0.00,1,25,24,'2018-10-15 12:37:27'),(33,3.00,1,25,25,'2018-10-15 12:47:44'),(34,0.00,1,25,25,'2018-10-15 12:47:44');
/*!40000 ALTER TABLE `countersupdatehistory` ENABLE KEYS */;
UNLOCK TABLES;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `flat_db`.`countersUpdateHistory_BEFORE_INSERT` BEFORE INSERT ON `countersUpdateHistory` FOR EACH ROW
BEGIN
SET NEW.datetime=Now();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;

--
-- Table structure for table `flats`
--

DROP TABLE IF EXISTS `flats`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `flats` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `flatNumber` varchar(45) DEFAULT NULL,
  `houseNumbers_id` int(11) NOT NULL,
  `counters_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_flats_houseNumbers_idx` (`houseNumbers_id`),
  KEY `fk_flats_counters1_idx` (`counters_id`),
  CONSTRAINT `fk_flats_houseNumbers` FOREIGN KEY (`houseNumbers_id`) REFERENCES `housenumbers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_flats_counters1` FOREIGN KEY (`counters_id`) REFERENCES `counters` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `flats`
--

LOCK TABLES `flats` WRITE;
/*!40000 ALTER TABLE `flats` DISABLE KEYS */;
INSERT INTO `flats` VALUES (1,'1',1,NULL),(2,'2',1,2),(3,'3',1,3),(4,'4',1,4),(5,'5',1,5),(6,'6',1,8),(7,'7',1,9),(8,'8',1,NULL),(9,'9',1,12),(10,'10',1,17),(11,'11',1,14),(12,'1',2,NULL),(13,'2',2,NULL),(14,'3',2,NULL),(15,'1',8,15),(16,'2',8,16),(17,'3',8,NULL),(18,'12',1,19),(19,'13',1,18);
/*!40000 ALTER TABLE `flats` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `housenumbers`
--

DROP TABLE IF EXISTS `housenumbers`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `housenumbers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `streets_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_houseNumbers_streets1_idx` (`streets_id`),
  CONSTRAINT `fk_houseNumbers_streets1` FOREIGN KEY (`streets_id`) REFERENCES `streets` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `housenumbers`
--

LOCK TABLES `housenumbers` WRITE;
/*!40000 ALTER TABLE `housenumbers` DISABLE KEYS */;
INSERT INTO `housenumbers` VALUES (1,'1A',1),(2,'1B',1),(3,'1F',2),(4,'2N',2),(8,'1A',3),(9,'2F',3),(10,'3B',3);
/*!40000 ALTER TABLE `housenumbers` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `streets`
--

DROP TABLE IF EXISTS `streets`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `streets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `streets`
--

LOCK TABLES `streets` WRITE;
/*!40000 ALTER TABLE `streets` DISABLE KEYS */;
INSERT INTO `streets` VALUES (1,'Первая улица'),(2,'Вторая улица'),(3,'Третья улица');
/*!40000 ALTER TABLE `streets` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Dumping events for database 'flat_db'
--

--
-- Dumping routines for database 'flat_db'
--
/*!50003 DROP FUNCTION IF EXISTS `getLastCounterDataRecord` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `getLastCounterDataRecord`(aCounterId integer) RETURNS int(11)
BEGIN

RETURN (SELECT max(id) FROM flat_db.counterdata where counters_id=aCounterId);
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!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 */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2018-10-15 13:06:39
