DROP DATABASE IF EXISTS `Pendragon`;

CREATE DATABASE `Pendragon`;
USE `Pendragon`;

CREATE TABLE `DVD` (
  `id`           int(10) unsigned NOT NULL auto_increment,
  `name`         VARCHAR(250) NOT NULL,
  `description`  TEXT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE  `TAG` (
  `id`           int(10) unsigned NOT NULL auto_increment,
  `name`         VARCHAR(250) NOT NULL,
  `description`  TEXT,
  `image`        VARCHAR(64),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `TAG_DVD` (
  `id_tag`       int(10) unsigned NOT NULL,
  `id_dvd`       int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_tag`, `id_dvd`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `CATALOGUE` (
  `id`           int(10) unsigned NOT NULL auto_increment,
  `id_dvd`       int(10) unsigned NOT NULL,
  `id_catalogue` int(10) unsigned,
  `path`         TEXT NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`id_dvd`) REFERENCES `DVD` (`id`),
  FOREIGN KEY (`id_catalogue`) REFERENCES `CATALOGUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `FILE` (
  `id`           int(10) unsigned NOT NULL auto_increment,
  `id_catalogue` int(10) unsigned NOT NULL,
  `name`         VARCHAR(250) NOT NULL,
  `size`         bigint DEFAULT 0,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`id_catalogue`) REFERENCES `CATALOGUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `IMAGE` (
  `id`           int(10) unsigned NOT NULL auto_increment,
  `name`         VARCHAR(250) NOT NULL,
  `image`        MEDIUMBLOB,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

GRANT ALL ON * TO `piotr`@`localhost`;
USE `Pendragon`;
