不要使用会话。相反,添加一个连接用户和投票主题的新表。当用户对某个主题进行投票时,将用户 ID 和主题 ID 添加到此表中。在允许用户投票之前,请检查此表。
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';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`vote_topics`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`vote_topics` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`users_has_vote_topics`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`users_has_vote_topics` (
`users_id` INT NOT NULL ,
`vote_topics_id` INT NOT NULL ,
PRIMARY KEY (`users_id`, `vote_topics_id`) ,
INDEX `fk_users_has_vote_topics_vote_topics1_idx` (`vote_topics_id` ASC) ,
INDEX `fk_users_has_vote_topics_users_idx` (`users_id` ASC) ,
CONSTRAINT `fk_users_has_vote_topics_users`
FOREIGN KEY (`users_id` )
REFERENCES `mydb`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_has_vote_topics_vote_topics1`
FOREIGN KEY (`vote_topics_id` )
REFERENCES `mydb`.`vote_topics` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;