-- MySQL Script generated by MySQL Workbench
-- qui 10 out 2024 19:55:51
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema db_agenda
-- -----------------------------------------------------
-- drop schema db_agenda;
-- drop database db_agenda;
-- -----------------------------------------------------
-- Schema db_agenda
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `db_agenda` DEFAULT CHARACTER SET utf8 ;
USE `db_agenda` ;

-- -----------------------------------------------------
-- Table `db_agenda`.`tb_usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `db_agenda`.`tb_usuario` (
  `cd_usuario` SMALLINT NOT NULL auto_increment,
  `nm_usuario` VARCHAR(60) NOT NULL,
  `nm_email` VARCHAR(60) NOT NULL,
  `cd_senha` VARCHAR(64) NOT NULL,
  `st_usuario` CHAR(1) NOT NULL DEFAULT '1',
  `dt_registro_usuario` DATETIME NOT NULL DEFAULT current_timestamp,
  `id_tipo_usuario` TINYINT NOT NULL,
  PRIMARY KEY (`cd_usuario`),
  UNIQUE INDEX `nm_email_UNIQUE` (`nm_email` ASC) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `db_agenda`.`tb_ambiente`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `db_agenda`.`tb_ambiente` (
  `cd_ambiente` TINYINT NOT NULL auto_increment,
  `nm_ambiente` VARCHAR(60) NOT NULL,
  PRIMARY KEY (`cd_ambiente`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `db_agenda`.`tb_curso`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `db_agenda`.`tb_curso` (
  `cd_curso` TINYINT NOT NULL AUTO_INCREMENT,
  `nm_curso` VARCHAR(60) NOT NULL,
  PRIMARY KEY (`cd_curso`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `db_agenda`.`tb_turma`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `db_agenda`.`tb_turma` (
  `cd_turma` INT NOT NULL AUTO_INCREMENT,
  `nm_turma` VARCHAR(45) NOT NULL,
  `nm_periodo` VARCHAR(45) NOT NULL,
  `id_curso` TINYINT NOT NULL,
  PRIMARY KEY (`cd_turma`),
  INDEX `fk_tb_turma_tb_curso1_idx` (`id_curso` ASC) ,
  CONSTRAINT `fk_tb_turma_tb_curso1`
    FOREIGN KEY (`id_curso`)
    REFERENCES `db_agenda`.`tb_curso` (`cd_curso`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `db_agenda`.`tb_agenda`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `db_agenda`.`tb_agenda` (
  `cd_agenda` INT NOT NULL AUTO_INCREMENT,
  `dt_agenda_ambiente` DATE NOT NULL,
  `hr_agenda_ambiente` TIME NOT NULL,
  `id_responsavel_agenda` SMALLINT NOT NULL,
  `id_ambiente` TINYINT NOT NULL,
  `id_turma` INT NULL,
  `st_agenda` CHAR(1) NOT NULL DEFAULT '1',
  `dt_registro_agenda` DATETIME NOT NULL DEFAULT current_timestamp,
  PRIMARY KEY (`cd_agenda`),
  INDEX `fk_tb_agenda_tb_usuario_idx` (`id_responsavel_agenda` ASC) ,
  INDEX `fk_tb_agenda_tb_ambiente1_idx` (`id_ambiente` ASC) ,
  INDEX `fk_tb_agenda_tb_turma1_idx` (`id_turma` ASC) ,
  CONSTRAINT `fk_tb_agenda_tb_usuario`
    FOREIGN KEY (`id_responsavel_agenda`)
    REFERENCES `db_agenda`.`tb_usuario` (`cd_usuario`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_tb_agenda_tb_ambiente1`
    FOREIGN KEY (`id_ambiente`)
    REFERENCES `db_agenda`.`tb_ambiente` (`cd_ambiente`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_tb_agenda_tb_turma1`
    FOREIGN KEY (`id_turma`)
    REFERENCES `db_agenda`.`tb_turma` (`cd_turma`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

alter table tb_agenda add column ds_agenda text;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

insert into tb_usuario set 
nm_usuario = 'claudio',
nm_email = 'claudio@mail.com',
cd_senha = sha2("123", 256),
id_tipo_usuario = 1;

SELECT * FROM tb_usuario;