Bonjour !
Je souhaite écrire une procédure stockée, mais je n'en ai jamais fais, du coup je souhaiterais avoir l'avis d'experts si possible
J'explique la situation avant.
J'ai 5 tables, que voici :
En gros, quand mon utilisateur ajoute un thème, celui-ci est ajouté dans la table 'attente_theme', et un nouvel enregistrement est créé dans la table 'contributions' (et dans la table hist_contributions à l'aide d'un trigger).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 CREATE TABLE `themes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL, `created` datetime NOT NULL, `modified` datetime NOT NULL, `description` text NULL, `icone_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `hist_user` varchar(20) NOT NULL, `etat` char(1) NOT NULL, `commentaire` varchar(450)NULL, `date_valid` datetime NULL, `admin_valid` varchar(20) NULL, PRIMARY KEY(`id`), KEY `user_id` (`user_id`), KEY `icone_id` (`icone_id`) ); CREATE TABLE `hist_themes` ( `hist_id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL, `name` varchar(32) NOT NULL, `created` datetime NOT NULL, `modified` datetime NOT NULL, `description` text NOT NULL, `icone_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `etat` char(1) NOT NULL, `commentaire` varchar(450)NOT NULL, `hist_date` date NOT NULL, `hist_time` time NOT NULL, `hist_user` varchar(20) NOT NULL, `hist_comm` char(6) NOT NULL, PRIMARY KEY(`hist_id`) ); CREATE TABLE `contributions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `created` datetime NOT NULL, `modified` datetime NOT NULL, `user_id` int(11) NOT NULL, `type` varchar(30) NOT NULL, `attente_theme_id` int(11) NOT NULL, `attente_item_id` int(11) NULL, `etat` char(1) NOT NULL, `hist_user` varchar(20) NOT NULL, PRIMARY KEY(`id`) ); CREATE TABLE `hist_contributions` ( `hist_id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL, `created` datetime NOT NULL, `modified` datetime NOT NULL, `user_id` int(11) NOT NULL, `type` varchar(30) NOT NULL, `attente_theme_id` int(11) NOT NULL, `attente_item_id` int(11) NULL, `etat` char(1) NOT NULL, `hist_user` varchar(20) NOT NULL, `hist_date` date NOT NULL, `hist_time` time NOT NULL, PRIMARY KEY(`hist_id`) ); CREATE TABLE `attente_themes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL, `created` datetime NOT NULL, `modified` datetime NOT NULL, `description` text NULL, `icone_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `hist_user` varchar(20) NOT NULL, `etat` char(1) NOT NULL, `commentaire` varchar(450)NULL, `date_valid` datetime NULL, `admin_valid` varchar(20) NULL, PRIMARY KEY(`id`), KEY `user_id` (`user_id`), KEY `icone_id` (`icone_id`) );
A cet instant le champ "etat" dans ma table attente_theme est à "A".
Quand mon administrateur va cliquer sur la contributions correspondante au thème ajouté, il va pouvoir valider ou refuser le thème.
Pour le moment je me concentre sur la situation où il valide.
Quand il validera le thème dans la table "attente_themes", le champ "etat" prendra la valeur "V". Ce qui impliquera plusieurs actions que je souhaite mettre dans une procédure. Pour le moment j'ai fais ça :
Ca explique globalement ce que je souhaite faire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 DELIMITER | CREATE PROCEDURE validAddTheme(IN idThemeCree INT) BEGIN IF attente_themes.etat = 'V' INSERT INTO `themes` (`id`, `name`, `created`, `modified`, `description`, `icone_id`, `user_id`, `hist_user`, `etat`, `commentaire`, `date_valid`, `admin_valid`) VALUES (attente_themes.id, attente_themes.client, attente_themes.created, attente_themes.modified, attente_themes.description, attente_themes.icone_id, attente_themes.user_id, attente_themes.hist_user, attente_themes.etat, attente_themes.commentaire, attente_themes.date_valid, attente_themes.admin_valid); INSERT INTO `hist_contributions` (`id`, `created`, `modified`, `user_id`, `type`, `attente_theme_id`, `hist_etat`, `hist_admin`, `hist_date`, `hist_time`) VALUES (contributions.id, contributions.created, contributions.modified, contributions.user_id, contributions.type, contributions.attente_theme_id, attente_themes.etat, attente_themes.admin_valid, current_date(), current_time()); DELETE FROM `attente_themes` WHERE attente_themes.id = idThemeCree; DELETE FROM `contributions` WHERE contributions.attente_theme_id = idThemeCree; END |
Donc quand le champ "etat" prendra la valeur "V", je souhaite copier mon enregistrement de "attente_themes" à "themes" (ce qui va déclencher le trigger pour hist_themes), je souhaite qu'un nouvel enregistrement se créé dans ma table hist_contributions. Puis que le thème ajouté disparaisse de ma table attente_themes, de même que la contribution qui le concerne.
Je sais pas trop si je suis claire xD
Bref. Pour le moment, j'ai fais ça, mais je ne sais pas si c'est vraiment possible d'utiliser plusieurs tables comme ça :/
Merci d'avance pour vos conseils, et votre aide
Ju'
Partager