-- --------------------------------------------------------
-- Server OS:                    Win64
-- HeidiSQL Version:             11.1.0.6116
-- --------------------------------------------------------


-- Dumping structure for table test.kaste_lietotajs
CREATE TABLE IF NOT EXISTS `kaste_lietotajs` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Lietotāja ID',
  `name` varchar(50) NOT NULL DEFAULT '' COMMENT 'Lietotāja NIK vārds',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Spēles lietotāji';

-- Dumping data for table test.kaste_lietotajs: ~0 rows (approximately)

-- Dumping structure for view test.kaste_retings
DROP VIEW IF EXISTS `kaste_retings`;
-- Creating temporary table to overcome VIEW dependency errors
CREATE TABLE `kaste_retings` (
	`id` INT(10) UNSIGNED NOT NULL COMMENT 'Lietotāja ID',
	`name` VARCHAR(50) NOT NULL COMMENT 'Lietotāja NIK vārds' COLLATE 'utf8mb4_general_ci',
	`time` DECIMAL(32,0) NULL
) ENGINE=MyISAM;

-- Dumping structure for table test.kaste_rezultati
CREATE TABLE IF NOT EXISTS `kaste_rezultati` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Rezultātu tabula ID',
  `id_lietotajs` int(10) unsigned NOT NULL COMMENT 'Lietotāja ID',
  `lvl` int(11) NOT NULL COMMENT 'Llīmeņa laiks',
  `level` smallint(2) unsigned NOT NULL DEFAULT 1 COMMENT 'Līmenis',
  PRIMARY KEY (`id`),
  KEY `id_lietotajs` (`id_lietotajs`),
  CONSTRAINT `FK_kaste_rezultati_kaste_lietotajs` FOREIGN KEY (`id_lietotajs`) REFERENCES `kaste_lietotajs` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Rezultatu tabula';

-- Dumping data for table test.kaste_rezultati: ~0 rows (approximately)

-- Dumping structure for view test.kaste_retings
DROP VIEW IF EXISTS `kaste_retings`;
-- Removing temporary table and create final VIEW structure
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `kaste_retings` AS SELECT
	u.id,
	u.name,
	SUM(r.lvl) AS time
FROM 
	kaste_lietotajs u
LEFT JOIN
	kaste_rezultati r
ON 
	r.id_lietotajs=u.id
GROUP by
	u.id
ORDER by
	u.name ;

