1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
-- Sauvegarde de la table affaire
CREATE TABLE affaire2 AS SELECT * FROM affaire;
-- Création de la table statut
DROP TABLE IF EXISTS statut;
CREATE TABLE statut (
`StatutId` int(11) NOT NULL AUTO_INCREMENT,
`StatutNom` varchar(45) NOT NULL,
PRIMARY KEY (`StatutId`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
INSERT INTO statut VALUES (1, "En cours");
INSERT INTO statut VALUES (2, "Gagnée");
INSERT INTO statut VALUES (3, "Perdue");
INSERT INTO statut VALUES (4, "Reportée");
INSERT INTO statut VALUES (5, "No Go");
-- Création de la table associative entre les affaires et les contacts
DROP TABLE IF EXISTS affaire_contact;
CREATE TABLE affaire_contact (
`AC_AffaireId` int(11) NOT NULL,
`AC_ContactId` int(11) NOT NULL,
PRIMARY KEY (`AC_AffaireId`,`AC_ContactId`),
KEY `AC_ContactId_fkey` (`AC_ContactId`),
CONSTRAINT `AC_AffaireId_fkey` FOREIGN KEY (`AC_AffaireId`) REFERENCES `affaire` (`AffaireId`),
CONSTRAINT `AC_ContactId_fkey` FOREIGN KEY (`AC_ContactId`) REFERENCES `contact` (`ContactId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Suppression et création de la nouvelle table affaire.
DROP TABLE IF EXISTS affaire;
CREATE TABLE affaire (
`AffaireId` int(11) NOT NULL DEFAULT '0',
`AffaireTypeAffaireId` int(11) NOT NULL,
`AffaireNom` varchar(64) NOT NULL,
`AffaireDateCreation` date DEFAULT NULL,
`AffaireDateActualisation` date DEFAULT NULL,
`AffaireDateFin` date DEFAULT NULL,
`AffaireStatutId` int(11) DEFAULT 1,
`AffaireCommentaire` text DEFAULT NULL,
`AffaireSocieteId` int(11) NOT NULL,
PRIMARY KEY (`AffaireId`),
KEY `Affaire_TypeAffaire_fkey` (`AffaireTypeAffaireId`),
KEY `Affaire_Statut_fkey` (`AffaireStatutId`),
KEY `Affaire_Societe_fkey` (`AffaireSocieteId`),
CONSTRAINT `Affaire_Societe_fkey` FOREIGN KEY (`AffaireSocieteId`) REFERENCES `societe` (`SocieteId`),
CONSTRAINT `Affaire_Statut_fkey` FOREIGN KEY (`AffaireStatutId`) REFERENCES `statut` (`StatutId`),
CONSTRAINT `Affaire_TypeAffaire_fkey` FOREIGN KEY (`AffaireTypeAffaireId`) REFERENCES `typeaffaire` (`TypeAffaireId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Vérification : on compte les affaires ayant plusieurs sociétés (optionnel pour le suivi).
SELECT * FROM societe_affaire WHERE SA_AffaireId IN (SELECT SA_AffaireId FROM societe_affaire GROUP BY SA_AffaireId HAVING COUNT(SA_AffaireId)>1);
-- Insertion des données dans la table Affaire sans les affaires affiliées à plusieurs sociétés.
INSERT INTO affaire
SELECT A2.AffaireId, A2.AffaireTypeAffaireId, A2.AffaireNom, A2.AffaireDateCreation, A2.AffaireDateActualisation, A2.AffaireDateFin, 1, "", SA_SocieteId
FROM societe_affaire SA, affaire A2
WHERE A2.AffaireId = SA_AffaireId GROUP BY SA_AffaireId;
-- Activation de l'auto_increment sur la clé primaire de la table affaire pour l'insertion des doublons.
ALTER TABLE affaire MODIFY COLUMN `AffaireId` INTEGER NOT NULL DEFAULT NULL AUTO_INCREMENT;
-- Insertion des doublons non enregistrés auparavant, avec génération automatique de la clé primaire par l'auto_increment.
INSERT INTO affaire (AffaireTypeAffaireId, AffaireNom, AffaireDateCreation, AffaireDateActualisation, AffaireDateFin, AffaireStatutId, AffaireCommentaire, AffaireSocieteId)
SELECT A2.AffaireTypeAffaireId, A2.AffaireNom, A2.AffaireDateCreation, A2.AffaireDateActualisation, A2.AffaireDateFin, 1, "", SA.SA_SocieteId
FROM societe_affaire SA, affaire2 A2
WHERE SA_AffaireId IN (SELECT SA_AffaireId FROM societe_affaire GROUP BY SA_AffaireId HAVING COUNT(SA_AffaireId)>1)
AND SA_SocieteId NOT IN (SELECT SA_SocieteId FROM societe_affaire GROUP BY SA_AffaireId HAVING COUNT(SA_AffaireId)>1)
AND A2.AffaireId = SA_AffaireId
GROUP BY SA_SocieteId;
-- Désactivation de l'auto_increment sur la clé primaire de la table affaire pour l'insertion des doublons.
ALTER TABLE affaire MODIFY COLUMN `AffaireId` INTEGER NOT NULL DEFAULT '0';
-- Attribution des commerciaux aux nouvelles affaires récemment créées.
INSERT INTO commercial_affaire
SELECT CA.CA_CommercialId, A.AffaireId
FROM commercial_affaire CA, affaire A
WHERE CA.CA_AffaireId IN (SELECT SA_AffaireId FROM societe_affaire GROUP BY SA_AffaireId HAVING COUNT(SA_AffaireId)>1)
AND A.AffaireId NOT IN (SELECT CA_AffaireId FROM commercial_affaire)
GROUP BY A.AffaireId;
-- Suppression de la table affaire2.
DROP TABLE affaire2; |
Partager