Mysql-хранимая процедура не может подавать данные в таблицу

Tables-

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//

Я выполняю вышеуказанную процедуру в следующих таблицах. Первые два запроса в процедуре работают должным образом, что дает правильный бренд_id и category_id, но запросы на вставку данных в таблицу brand_category, product & price table не могут выполняться.

mysql,stored-procedures,

1

Ответов: 0

Mysql-хранимая процедура не может подавать данные в таблицу

Tables-

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//

Я выполняю вышеуказанную процедуру в следующих таблицах. Первые два запроса в процедуре работают должным образом, что дает правильный бренд_id и category_id, но запросы на вставку данных в таблицу brand_category, product & price table не могут выполняться.

01MySQL, хранимые-процедуры,
Похожие вопросы
Яндекс.Метрика