Hello tout le monde,


Je développe un site de vote

1 utilisateur vote pour 5 produits

insert into de la table utilisateur OK en sgbd (mysql)
update nb_vote= nb_vote+1 de la table produit OK

mais comment implémenter ma table de votes constituée de FK?
Puisque les FK sont en CASCADE sur DELETE ou UPDATE, mais pas sur un INSERT INTO.

Dois-je me servir de pdo::lastInsertId()?

voici un exemple de ma base de données.


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
CREATE DATABASE test COLLATE utf8_general_ci;
 
 
CREATE TABLE IF NOT EXISTS categories(
    id int(10) unsigned NOT NULL AUTO_INCREMENT,
    label varchar(50) NOT NULL,
    PRIMARY KEY (id)
    )ENGINE=InnoDB;
 
 
CREATE TABLE IF NOT EXISTS produits(
    id int(10) unsigned NOT NULL AUTO_INCREMENT,
    id_cat int(10) unsigned NOT NULL,
    label varchar(50) NOT NULL,
    nb_vote int(10) unsigned NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (id_cat) references categories
    )ENGINE=InnoDB;
 
CREATE TABLE IF NOT EXISTS users(
    id int(10) unsigned NOT NULL AUTO_INCREMENT,
    email varchar(50) NOT NULL,
    nom varchar(50) NOT NULL,
    PRIMARY KEY (id)
    )ENGINE=InnoDB;
 
 CREATE TABLE IF NOT EXISTS votes(
    id int(10) unsigned NOT NULL AUTO_INCREMENT,
    id_user int(10) unsigned NOT NULL,
    id_produit int(10) unsigned NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (id_user) references users(id),
    FOREIGN KEY (id_produit) references produits(id)
    )ENGINE=InnoDB;
 
CREATE UNIQUE INDEX inx_email ON users(email)

Bien à vous