START TRANSACTION;
INSERT INTO
  `contrainte` (`idcontrainte`, `couleur`)
SELECT DISTINCT
  id_contrainte,
  couleur
FROM
  donnees_fournies.instances2;

INSERT INTO
  `carte` (`idcarte`, `niveau`, `img`, `points`)
SELECT DISTINCT
  `id_carte`,
  `niveau`,
  `fichier`,
  `points`
FROM
  donnees_fournies.instances2;

INSERT INTO
  `classement` (`idclassement`, `nom`, `portee`)
SELECT DISTINCT
  `idC`,
  `nom`,
  `portée`
FROM
  donnees_fournies.classement;

INSERT INTO
  `equipe` (`idequipe`, `nom`)
SELECT DISTINCT
  `idE`,
  `nom`
FROM
  donnees_fournies.équipe;

INSERT INTO
  `tournoi` (`idtournoi`, `nom`, `date_deb`, `date_fin`)
SELECT
  `idT`,
  `nom`,
  `dateDeb`,
  `dateFin`
FROM
  donnees_fournies.tournoi;

INSERT INTO
  `joueur` (
    `idjoueur`,
    `nom`,
    `prenom`,
    `pseudo`,
    `annee_nais`,
    `email`,
    `idequipe`
  )
SELECT DISTINCT
  idJ,
  nom,
  prénom,
  pseudo,
  YEAR (date_naiss),
  email,
  id_équipe
FROM
  donnees_fournies.joueur;

INSERT INTO
  `joueur` (
    `idjoueur`,
    `nom`,
    `prenom`,
    `pseudo`,
    `annee_nais`,
    `email`
  )
SELECT DISTINCT
  id_joueur,
  nom,
  prénom,
  pseudo,
  YEAR (date_naiss),
  email
FROM
  donnees_fournies.instances1
WHERE
  id_joueur > 10;

INSERT INTO
  `phase` (`idtournoi`, `niveau`, `date_p`)
SELECT
  `idT`,
  `niveau`,
  `dateP`
FROM
  donnees_fournies.phase;

INSERT INTO
  `classement_equipe` (`idclassement`, `idequipe`, `rang`)
SELECT
  `idC`,
  `idE`,
  `rang`
FROM
  donnees_fournies.classement_equipe;

INSERT INTO
  `partie` (
    `idpartie`,
    `date_partie`,
    `horaire`,
    `duree`,
    `etat`,
    `idplateau`,
    `idtournoi`,
    `niveau`
  )
SELECT DISTINCT
  id_partie,
  dateP,
  heureP,
  ROUND(durée_secondes / 60),
  CASE état
    WHEN 'T' THEN 'terminée'
    WHEN 'AV' THEN 'a venir'
    WHEN 'EC' THEN 'en cours'
  END,
  NULL,
  NULL,
  NULL
FROM
  donnees_fournies.instances1;

INSERT INTO
  `participe` (
    `idtournoi`,
    `niveau`,
    `idjoueur`,
    `a_joue`,
    `est_qualifie`
  )
SELECT
  `idT`,
  `niveau`,
  `idJ`,
  `a_joué`,
  `est_qualifié`
FROM
  donnees_fournies.participe;

INSERT INTO
  `est_en_lien` (`idclassement_1`, `idclassement_2`, `rang`)
SELECT
  `idC1`,
  `idC2`,
  `type_lien`
FROM
  donnees_fournies.est_en_lien;

INSERT INTO
  `tour` (`idpartie`, `num_tour`)
SELECT DISTINCT
  `id_partie`,
  `numTour`
FROM
  donnees_fournies.instances3;

INSERT INTO
  `choisit_main` (
    `idjoueur`,
    `idpartie`,
    `num_tour`,
    `nb_de_rouge`,
    `nb_de_jaune`,
    `nb_de_bleu`
  )
SELECT DISTINCT
  `id_joueur`,
  `id_partie`,
  `numTour`,
  `main_nb_rouge`,
  `main_nb_jaune`,
  `main_nb_bleu`
FROM
  donnees_fournies.instances3;

INSERT INTO
  `classement_individuel` (`idclassement`, `idjoueur`, `rang`)
SELECT
  `idC`,
  `idJ`,
  `rang`
FROM
  donnees_fournies.classement_individuel;

INSERT INTO
  `est_contrainte` (`idcarte`, `idcontrainte`)
SELECT
  `id_carte`,
  `id_contrainte`
FROM
  donnees_fournies.instances2;

INSERT INTO
  `face_de_de` (`idcontrainte`, `valeur`)
SELECT DISTINCT
  `id_contrainte`,
  `valeur`
FROM
  donnees_fournies.instances2
WHERE
  nom = 'face_de_dé';

INSERT INTO
  `serie_au_choix` (`idcontrainte`, `nombre`)
SELECT DISTINCT
  `id_contrainte`,
  `valeur`
FROM
  donnees_fournies.instances2
WHERE
  nom = 'suite_au_choix';

INSERT INTO
  `meme_au_choix` (`idcontrainte`, `nombre`, `couleur`)
SELECT DISTINCT
  `id_contrainte`,
  `valeur`,
  `couleur`
FROM
  donnees_fournies.instances2
WHERE
  nom = 'meme_au_choix';

INSERT INTO
  `seuil_de_des` (`idcontrainte`, `valeur`, `sens`)
SELECT DISTINCT
  `id_contrainte`,
  `valeur`,
  `sens`
FROM
  donnees_fournies.instances2
WHERE
  nom = 'seuil_de_dé';

INSERT INTO
  `tente_validation` (
    `idpartie`,
    `num_tour`,
    `idjoueur`,
    `idcarte`,
    `nb_tentatives`
  )
SELECT
  `id_partie`,
  `numTour`,
  `id_joueur`,
  `carteAvalider`,
  COUNT(`numL`) as nb_tentatives
FROM
  donnees_fournies.instances3
GROUP BY
  `id_partie`,
  `id_joueur`,
  `carteAvalider`;

SET @row_number = 0;

INSERT INTO `comprend`(`idpartie`, `numTour`, `idlancer`) SELECT `id_partie`, `numTour`, @row_number:=@row_number+1 AS idlancers
FROM donnees_fournies.instances3 t;

SET @row_number = 0;
INSERT INTO `effectue`(`idjoueur`, `idlancer`)

SELECT inst3.id_joueur, @row_number:=@row_number+1 AS idlancer
FROM donnees_fournies.instances3 inst3;

SET @idlancer = 0;
INSERT INTO `de_lance`(`idlancer`, `rang_couleur_valeur`)
SELECT @idlancer:=@idlancer+1 AS idlancer, CONCAT('1;', de1) AS rang_couleur_valeur FROM `donnees_fournies`.`instances3` 
UNION ALL SELECT @idlancer:=@idlancer+1, CONCAT('2;', de2) FROM `donnees_fournies`.`instances3` 
UNION ALL SELECT @idlancer:=@idlancer+1, CONCAT('3;', de3) FROM `donnees_fournies`.`instances3` 
UNION ALL SELECT @idlancer:=@idlancer+1, CONCAT('4;', de4) FROM `donnees_fournies`.`instances3` 
UNION ALL SELECT @idlancer:=@idlancer+1, CONCAT('5;', de5) FROM `donnees_fournies`.`instances3` 
UNION ALL SELECT @idlancer:=@idlancer+1, CONCAT('6;', de6) FROM `donnees_fournies`.`instances3`;
COMMIT;