触发后删除DELETE不起作用

时间:2016-12-15 03:43:18

标签: mysql triggers

我试图在我的代码中指定的日期之前删除购买,并且还小于7.99。我可以成功创建我的程序,但它不能正常运行。它不会从我的表中删除任何字段。

 DELIMITER $$

USE BrendaStoreFinal $$


CREATE TRIGGER PurchaseUpdate
AFTER DELETE ON Purchase
FOR EACH ROW
BEGIN

DELETE from Purchase where PurchaseDate<=2016-11-01 AND PurchaseAmount>7.99;

END $$

DELIMITER ;

drop trigger if exists PurchaseUpdate;

SELECT 
    *
FROM
    Purchase;

我的数据库:

 -- MySQL Workbench Forward Engineering

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';

-- -----------------------------------------------------
-- Schema BrendaStoreFinal
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `BrendaStoreFinal` ;

-- -----------------------------------------------------
-- Schema BrendaStoreFinal
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `BrendaStoreFinal` DEFAULT CHARACTER SET utf8 ;
SHOW WARNINGS;
USE `BrendaStoreFinal` ;

-- -----------------------------------------------------
-- Table `Vendor`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Vendor` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Vendor` (
  `idVendor` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NOT NULL,
  `Address` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idVendor`))
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Shop`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Shop` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Shop` (
  `idShop` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NOT NULL,
  `Address` VARCHAR(45) NOT NULL,
  `PhoneNumber` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idShop`))
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Product`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Product` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Product` (
  `idProduct` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NOT NULL,
  `ProductPrice` DECIMAL(4,2) NOT NULL,
  `Shop_idShop` INT NOT NULL,
  PRIMARY KEY (`idProduct`),
  INDEX `fk_Product_Shop1_idx` (`Shop_idShop` ASC),
  CONSTRAINT `fk_Product_Shop1`
    FOREIGN KEY (`Shop_idShop`)
    REFERENCES `Shop` (`idShop`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Recipe`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Recipe` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Recipe` (
  `idRecipe` INT NOT NULL AUTO_INCREMENT,
  `IngredientAmount` VARCHAR(45) NOT NULL,
  `Product_idProduct` INT NOT NULL,
  PRIMARY KEY (`idRecipe`),
  INDEX `fk_Recipe_Product1_idx` (`Product_idProduct` ASC),
  CONSTRAINT `fk_Recipe_Product1`
    FOREIGN KEY (`Product_idProduct`)
    REFERENCES `Product` (`idProduct`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Ingredient`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Ingredient` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Ingredient` (
  `idIngredient` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NOT NULL,
  `Price` DECIMAL(4,2) NOT NULL,
  `Vendor_idVendor` INT NOT NULL DEFAULT 1,
  `Recipe_idRecipe` INT NOT NULL DEFAULT 1,
  `Product_idProduct` INT NOT NULL DEFAULT 1,
  PRIMARY KEY (`idIngredient`),
  INDEX `fk_Ingredient_Vendor_idx` (`Vendor_idVendor` ASC),
  INDEX `fk_Ingredient_Recipe1_idx` (`Recipe_idRecipe` ASC),
  INDEX `fk_Ingredient_Product1_idx` (`Product_idProduct` ASC),
  CONSTRAINT `fk_Ingredient_Vendor`
    FOREIGN KEY (`Vendor_idVendor`)
    REFERENCES `Vendor` (`idVendor`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Ingredient_Recipe1`
    FOREIGN KEY (`Recipe_idRecipe`)
    REFERENCES `Recipe` (`idRecipe`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Ingredient_Product1`
    FOREIGN KEY (`Product_idProduct`)
    REFERENCES `Product` (`idProduct`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Client`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Client` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Client` (
  `idClient` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NOT NULL,
  `PhoneNumber` VARCHAR(45) NOT NULL,
  `Email` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idClient`))
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Purchase`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Purchase` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Purchase` (
  `idPurchase` INT NOT NULL AUTO_INCREMENT,
  `PurchaseDate` DATETIME NOT NULL,
  `PurchaseAmount` DECIMAL(2,2) NOT NULL,
  `Client_idClient` INT NOT NULL,
  PRIMARY KEY (`idPurchase`),
  INDEX `fk_Purchase_Client1_idx` (`Client_idClient` ASC),
  CONSTRAINT `fk_Purchase_Client1`
    FOREIGN KEY (`Client_idClient`)
    REFERENCES `Client` (`idClient`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Orders`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Orders` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Orders` (
  `idOrders` INT NOT NULL AUTO_INCREMENT,
  `Date` DATETIME NOT NULL,
  `Quantity` VARCHAR(45) NOT NULL,
  `Price` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idOrders`))
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Employee` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Employee` (
  `idEmployee` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NOT NULL,
  `Address` VARCHAR(45) NOT NULL,
  `PhoneNumber` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idEmployee`))
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Shop_has_Employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Shop_has_Employee` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Shop_has_Employee` (
  `Shop_idShop` INT NOT NULL,
  `Employee_idEmployee` INT NOT NULL,
  PRIMARY KEY (`Shop_idShop`, `Employee_idEmployee`),
  INDEX `fk_Shop_has_Employee_Employee1_idx` (`Employee_idEmployee` ASC),
  INDEX `fk_Shop_has_Employee_Shop1_idx` (`Shop_idShop` ASC),
  CONSTRAINT `fk_Shop_has_Employee_Shop1`
    FOREIGN KEY (`Shop_idShop`)
    REFERENCES `Shop` (`idShop`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Shop_has_Employee_Employee1`
    FOREIGN KEY (`Employee_idEmployee`)
    REFERENCES `Employee` (`idEmployee`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Ingredient_has_Orders`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Ingredient_has_Orders` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Ingredient_has_Orders` (
  `Ingredient_idIngredient` INT NOT NULL,
  `Orders_idOrders` INT NOT NULL,
  PRIMARY KEY (`Ingredient_idIngredient`, `Orders_idOrders`),
  INDEX `fk_Ingredient_has_Orders_Orders1_idx` (`Orders_idOrders` ASC),
  INDEX `fk_Ingredient_has_Orders_Ingredient1_idx` (`Ingredient_idIngredient` ASC),
  CONSTRAINT `fk_Ingredient_has_Orders_Ingredient1`
    FOREIGN KEY (`Ingredient_idIngredient`)
    REFERENCES `Ingredient` (`idIngredient`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Ingredient_has_Orders_Orders1`
    FOREIGN KEY (`Orders_idOrders`)
    REFERENCES `Orders` (`idOrders`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `Product_has_Purchase`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Product_has_Purchase` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Product_has_Purchase` (
  `Product_idProduct` INT NOT NULL,
  `Purchase_idPurchase` INT NOT NULL,
  PRIMARY KEY (`Product_idProduct`, `Purchase_idPurchase`),
  INDEX `fk_Product_has_Purchase_Purchase1_idx` (`Purchase_idPurchase` ASC),
  INDEX `fk_Product_has_Purchase_Product1_idx` (`Product_idProduct` ASC),
  CONSTRAINT `fk_Product_has_Purchase_Product1`
    FOREIGN KEY (`Product_idProduct`)
    REFERENCES `Product` (`idProduct`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Product_has_Purchase_Purchase1`
    FOREIGN KEY (`Purchase_idPurchase`)
    REFERENCES `Purchase` (`idPurchase`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

0 个答案:

没有答案