Mysql存储过程无法在表中提供数据

时间:2016-10-14 09:52:03

标签: mysql stored-procedures

表 -

DROP TABLE IF EXISTS `brands`;
CREATE TABLE IF NOT EXISTS `brands` (
    `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

DROP TABLE IF EXISTS `categories`;
CREATE TABLE IF NOT EXISTS `categories` (
    `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    `parent_id` tinyint(3) unsigned NOT NULL DEFAULT '0',
    `comment` text COMMENT 'USP for a specific category. Would be displayed on the webpages',
    PRIMARY KEY (`id`),
    UNIQUE KEY `name` (`name`),
    KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='List of all categories'
DROP TABLE IF EXISTS `brand_category`;
CREATE TABLE IF NOT EXISTS `brand_category` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `brand_id` smallint(5) unsigned NOT NULL,
    `category_id` tinyint(3) unsigned NOT NULL,
    `added` date NOT NULL,
    PRIMARY KEY (`id`),
    KEY `brand_name` (`brand_id`),
    KEY `brand_category_name` (`category_id`),
    CONSTRAINT `brand_category_name` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `brand_name` FOREIGN KEY (`brand_id`) REFERENCES `brands` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Relations between brands and category';


DROP TABLE IF EXISTS `prices`;
CREATE TABLE IF NOT EXISTS `prices` (
    `product_id` int(10) unsigned NOT NULL,
    `price` float unsigned NOT NULL,
    `date` date NOT NULL,
    UNIQUE KEY `product_id_date` (`product_id`,`date`),
    CONSTRAINT `price_product_id` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Prices for products set on specific dates';

DROP TABLE IF EXISTS `products`;
CREATE TABLE IF NOT EXISTS `products` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `brand_category_id` int(10) unsigned NOT NULL,
    `model_name` varchar(255) NOT NULL,
    `video_url` varchar(150) DEFAULT NULL,
    `display_name` text NOT NULL,
    `description` text DEFAULT NULL,
    `retail_price` float unsigned NOT NULL,
    `current_price` float unsigned NOT NULL DEFAULT '0',
    `launch_date` date DEFAULT NULL,
    `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `brand_category_id_model_name` (`brand_category_id`,`model_name`),
    CONSTRAINT `product_brand_id` FOREIGN KEY (`brand_category_id`) REFERENCES `brand_category` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='List of all products available';

Mysql存储过程 -

    DELIMITER //

    CREATE PROCEDURE `product_entry`(
        IN `category_name` VARCHAR(100),
        IN `brand_name` VARCHAR(100),
        IN `model_name` VARCHAR(255),
        IN `video_url` VARCHAR(150),
        IN `display_name` text,
        IN `description` text,
        IN `retail_price` float,
        OUT `category_id` tinyint(3),
        OUT `product_id` int(10)
    )
    BEGIN
        DECLARE brand_id smallint(5);
        DECLARE brand_category_id int(10);

        DECLARE EXIT HANDLER FOR SQLEXCEPTION
        BEGIN
            ROLLBACK;
        END;

        START TRANSACTION;

        INSERT INTO categories (name)
        VALUES (category_name)
        ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id);

        SET category_id = LAST_INSERT_ID();

        INSERT INTO brands (name)
        VALUES (brand_name)
        ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id);

        SET brand_id = LAST_INSERT_ID();

        INSERT INTO brand_category (brand_id, category_id, added)
        VALUES (brand_id, category_id, CURDATE())
        ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id);

        SET brand_category_id = LAST_INSERT_ID();

        INSERT INTO product (brand_category_id, model_name, video_url, display_name, description, retail_price, launch_date)
        VALUES (brand_category_id, model_name, video_url, display_name, description, retail_price, CURDATE())
        ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id);

        SET product_id = LAST_INSERT_ID();

        INSERT INTO prices (product_id, price, date)
        VALUES (product_id, retail_price, CURDATE());

        COMMIT;

END//

我正在执行以下表格的上述程序。程序中的前两个查询工作正常,给出了正确的brand_id& category_id但查询在brand_category表,product&amp ;;中插入数据价格表无法执行。

0 个答案:

没有答案