-- MySQL Script generated by MySQL Workbench
-- Wed May 18 23:17:03 2016
-- 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='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema Mondiale2009
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema Mondiale2009
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `Mondiale2009` DEFAULT CHARACTER SET latin1 ;
USE `Mondiale2009` ;

-- -----------------------------------------------------
-- Table `Mondiale2009`.`Pilota`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Mondiale2009`.`Pilota` (
  `CodPilota` INT(11) NOT NULL,
  `Nazionalita` VARCHAR(45) NOT NULL,
  `Nome` VARCHAR(45) NOT NULL,
  `Cognome` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`CodPilota`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;


-- -----------------------------------------------------
-- Table `Mondiale2009`.`Squadra`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Mondiale2009`.`Squadra` (
  `idSQ` INT(11) NOT NULL,
  `NomeSQ` VARCHAR(45) NOT NULL,
  `Motore` VARCHAR(45) NULL DEFAULT NULL,
  PRIMARY KEY (`idSQ`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;


-- -----------------------------------------------------
-- Table `Mondiale2009`.`Appartiene`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Mondiale2009`.`Appartiene` (
  `Pilota` INT(11) NOT NULL,
  `Squadra` INT(11) NOT NULL,
  PRIMARY KEY (`Pilota`, `Squadra`),
  INDEX `fk_squadra2_idx` (`Squadra` ASC),
  CONSTRAINT `fk_pilota2`
    FOREIGN KEY (`Pilota`)
    REFERENCES `Mondiale2009`.`Pilota` (`CodPilota`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_squadra2`
    FOREIGN KEY (`Squadra`)
    REFERENCES `Mondiale2009`.`Squadra` (`idSQ`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;


-- -----------------------------------------------------
-- Table `Mondiale2009`.`GranPremio`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Mondiale2009`.`GranPremio` (
  `DataGP` DATE NOT NULL,
  `Nome` VARCHAR(45) NOT NULL,
  `Giri` INT(11) NOT NULL,
  `Nazione` VARCHAR(45) NULL DEFAULT NULL,
  `Circuito` VARCHAR(45) NULL DEFAULT NULL,
  PRIMARY KEY (`DataGP`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;


-- -----------------------------------------------------
-- Table `Mondiale2009`.`Risultato`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Mondiale2009`.`Risultato` (
  `Pilota` INT(11) NOT NULL,
  `Squadra` INT(11) NOT NULL,
  `GranPremio` DATE NOT NULL,
  `GiriEffettuati` INT(11) NULL DEFAULT NULL,
  `Punti` INT(11) NULL DEFAULT NULL,
  `Posizione` INT(11) NULL DEFAULT NULL,
  `MotivoRitiro` VARCHAR(45) NULL DEFAULT NULL,
  PRIMARY KEY (`Pilota`, `Squadra`, `GranPremio`),
  INDEX `datagp_fk1_idx` (`GranPremio` ASC),
  INDEX `idsquadra_fk1_idx` (`Squadra` ASC),
  CONSTRAINT `cod_pilota_fk1`
    FOREIGN KEY (`Pilota`)
    REFERENCES `Mondiale2009`.`Pilota` (`CodPilota`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `datagp_fk1`
    FOREIGN KEY (`GranPremio`)
    REFERENCES `Mondiale2009`.`GranPremio` (`DataGP`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `idsquadra_fk1`
    FOREIGN KEY (`Squadra`)
    REFERENCES `Mondiale2009`.`Squadra` (`idSQ`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;


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