text: -- phpMyAdmin SQL Dump
  -- version 5.1.0
  -- https://www.phpmyadmin.net/
  --
  -- Host: localhost
  -- Generation Time: May 12, 2021 at 04:17 AM
  -- Server version: 10.4.18-MariaDB
  -- PHP Version: 8.0.3
  
  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 */;
  
  --
  -- Database: `games_cryteria`
  --
  
  DELIMITER $$
  --
  -- Procedures
  --
  CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_rating` (IN `accountId` INT, IN `gameId` INT, IN `gameRating` INT)  begin
  	if ((select id from games_cryteria.games where id=gameId) is null) THEN
  		insert into games_cryteria.games values
  		(gameId, 0);
  	end if;
  
  	insert into games_cryteria.account_ratings values
  	(accountId, gameId, gameRating);
  
  	update games_cryteria.games
  	set
  		rating = (select avg(rating) from games_cryteria.account_ratings where game_id = gameId)
  	where
  		id = gameId;
  end$$
  
  DELIMITER ;
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `accounts`
  --
  
  CREATE TABLE `accounts` (
    `id` int(11) NOT NULL,
    `username` varchar(20) NOT NULL,
    `name` varchar(255) NOT NULL,
    `email` varchar(255) NOT NULL,
    `password` varchar(255) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `account_ratings`
  --
  
  CREATE TABLE `account_ratings` (
    `account_id` int(11) NOT NULL,
    `game_id` int(11) NOT NULL,
    `rating` float NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `games`
  --
  
  CREATE TABLE `games` (
    `id` int(11) NOT NULL,
    `rating` float NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  
  --
  -- Dumping data for table `games`
  --
  
  INSERT INTO `games` (`id`, `rating`) VALUES
  (1, 1);
  
  --
  -- Indexes for dumped tables
  --
  
  --
  -- Indexes for table `accounts`
  --
  ALTER TABLE `accounts`
    ADD PRIMARY KEY (`id`),
    ADD UNIQUE KEY `username` (`username`),
    ADD UNIQUE KEY `email` (`email`);
  
  --
  -- Indexes for table `account_ratings`
  --
  ALTER TABLE `account_ratings`
    ADD PRIMARY KEY (`account_id`,`game_id`),
    ADD KEY `game_id` (`game_id`);
  
  --
  -- Indexes for table `games`
  --
  ALTER TABLE `games`
    ADD PRIMARY KEY (`id`);
  
  --
  -- AUTO_INCREMENT for dumped tables
  --
  
  --
  -- AUTO_INCREMENT for table `accounts`
  --
  ALTER TABLE `accounts`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
  
  --
  -- Constraints for dumped tables
  --
  
  --
  -- Constraints for table `account_ratings`
  --
  ALTER TABLE `account_ratings`
    ADD CONSTRAINT `account_ratings_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON UPDATE CASCADE,
    ADD CONSTRAINT `account_ratings_ibfk_2` FOREIGN KEY (`game_id`) REFERENCES `games` (`id`) ON UPDATE CASCADE;
  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 */;
  