Create trigger on insert - mysql

I have the following table:
CREATE TABLE `loteria_loterias` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fecha_ini` datetime NOT NULL,
`fecha_fin` datetime DEFAULT NULL,
`ganador` varchar(60) CHARACTER SET utf8 DEFAULT NULL,
`coste` int(11) NOT NULL,
`premium` int(11) NOT NULL,
`duracion` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `ganador` (`ganador`),
CONSTRAINT `loteria_loterias_ibfk_1` FOREIGN KEY (`ganador`) REFERENCES `tegm_users` (`user_login`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1
Is it posible to create a trigger that fill in 'fecha_fin' with 'fecha_ini + duracion' (duracion is in hours) in every insert?

delimiter |
CREATE TRIGGER `some_name` BEFORE INSERT ON loteria_loterias
FOR EACH ROW BEGIN
SET NEW.fecha_fin = NEW.fecha_ini + interval NEW.duracion hour;
END;
|
delimiter ;

Related

Transaction within a stored procedure always failing

I've created a stored procedure via phpmyadmin which carries out a transaction as follows:
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SELECT -1;
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment) VALUES (name, gender, house_id, date_from,date_to, clean_rating,comments);
UPDATE rental SET reviewed = 1 WHERE renter_id = renter_id AND house_id = house_id AND date_from = dateFrom AND date_to = dateTo ;
COMMIT;
SELECT 1;
END
The parameters were set like this:
parameters to the stored procedure
Each time I execute the stored procedure with valid values via phpmyadmin, I get -1 returned and no tables are affected. Not quite sure what's wrong here.
Edit: Added some info
SHOW CREATE PROCEDURE sp_save_review:
| sp_save_review | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_save_review`(IN `name` VARCHAR(200), IN `gender` VARCHAR(50), IN `house_id` INT(11), IN `date_from` DATE, IN `date_to` DATE, IN `clean_rating` FLOAT, IN `comments` VARCHAR(1000), IN `renter_id` INT(11))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SELECT -1;
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment) VALUES (name, gender, house_id, date_from,date_to, clean_rating,comments);
UPDATE rental SET reviewed = 1 WHERE renter_id = renter_id AND house_id = house_id AND date_from = dateFrom AND date_to = dateTo ;
COMMIT;
SELECT 1;
SHOW CREATE TABLE review:
| review | CREATE TABLE `review` (
`review_id` int(11) NOT NULL AUTO_INCREMENT,
`reviewer_name` varchar(200) CHARACTER SET utf32 NOT NULL,
`reviewer_gender` varchar(50) CHARACTER SET utf32 NOT NULL,
`house_id` int(11) NOT NULL,
`rental_date_from` date NOT NULL,
`rental_date_to` date NOT NULL,
`house_rating` float NOT NULL,
`house_comment` varchar(1000) CHARACTER SET utf32 NOT NULL,
`flagged` tinyint(1) NOT NULL DEFAULT 0,
`banned` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`review_id`),
KEY `house_id` (`house_id`),
CONSTRAINT `review_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `rental` (`house_id`)
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=latin1 COLLATE=latin1_bin |
SHOW CREATE TABLE rental:
| rental | CREATE TABLE `rental` (
`renter_id` int(11) NOT NULL,
`house_id` int(11) NOT NULL,
`date_from` date NOT NULL,
`date_to` date NOT NULL,
`price` double NOT NULL,
`reviewed` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`renter_id`,`house_id`,`date_from`),
KEY `house_id` (`house_id`),
CONSTRAINT `rental_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `house` (`house_id`),
CONSTRAINT `rental_ibfk_2` FOREIGN KEY (`renter_id`) REFERENCES `renter` (`renter_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin |
You seem to have got into a fankle with your naming try this
DROP TABLE IF EXISTS REVIEW;
DROP TABLE IF EXISTS RENTAL;
CREATE TABLE `rental` (
`renter_id` int(11) NOT NULL,
`house_id` int(11) NOT NULL,
`date_from` date NOT NULL,
`date_to` date NOT NULL,
`price` double NOT NULL,
`reviewed` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`renter_id`,`house_id`,`date_from`),
KEY `house_id` (`house_id`) #,
#CONSTRAINT `rental_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `house` (`house_id`),
#CONSTRAINT `rental_ibfk_2` FOREIGN KEY (`renter_id`) REFERENCES `renter` (`renter_id`)
) ;
CREATE TABLE `review` (
`review_id` int(11) NOT NULL AUTO_INCREMENT,
`reviewer_name` varchar(200) CHARACTER SET utf32 NOT NULL,
`reviewer_gender` varchar(50) CHARACTER SET utf32 NOT NULL,
`house_id` int(11) NOT NULL,
`rental_date_from` date NOT NULL,
`rental_date_to` date NOT NULL,
`house_rating` float NOT NULL,
`house_comment` varchar(1000) CHARACTER SET utf32 NOT NULL,
`flagged` tinyint(1) NOT NULL DEFAULT 0,
`banned` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`review_id`),
KEY `house_id` (`house_id`),
CONSTRAINT `review_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `rental` (`house_id`)
) ;
DROP PROCEDURE IF EXISTS P;
DELIMITER $$
CREATE PROCEDURE P(IN `Pname` VARCHAR(200), IN `Pgender` VARCHAR(50), IN `Phouse_id` INT(11),
IN `Pdate_from` DATE, IN `Pdate_to` DATE, IN `Pclean_rating` FLOAT,
IN `Pcomments` VARCHAR(1000), IN `Prenter_id` INT(11))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SELECT -1;
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment)
VALUES (Pname, Pgender, Phouse_id, Pdate_from,Pdate_to, Pclean_rating,Pcomments);
UPDATE rental
SET reviewed = 1
WHERE renter_id = Prenter_id AND house_id = Phouse_id AND date_from = Pdate_From AND date_to = Pdate_To ;
COMMIT;
SELECT 1;
END $$
DELIMITER ;
INSERT INTO RENTAL( `renter_id`, `house_id` , `date_from` , `date_to` , `price`, `reviewed` )
VALUES (1,1,'2022-01-01','2022-01-01',10,0);
CALL P('AAA','F',1,'2022-01-01','2022-01-01',1,'BBB',1);

Join inside a trigger

I'm having problems trying to create the following trigger:
CREATE TRIGGER loteria_loterias AFTER UPDATE ON loteria_loterias
FOR EACH ROW BEGIN
UPDATE
loteria_loterias l
JOIN loteria_tipos t
ON l.tipo = t.id
SET
NEW.fecha_fin = NEW.fecha_ini + interval t.duracion hour
WHERE c.cID=NEW.cID;
END
I have this definitions for the tables:
CREATE TABLE `loteria_loterias` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`tipo` int(11) unsigned NOT NULL,
`fecha_ini` datetime NOT NULL,
`fecha_fin` datetime DEFAULT NULL,
`ganador` varchar(60) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ganador` (`ganador`),
KEY `tipo` (`tipo`),
CONSTRAINT `loteria_loterias_ibfk_2` FOREIGN KEY (`tipo`) REFERENCES `loteria_tipos` (`id`),
CONSTRAINT `loteria_loterias_ibfk_1` FOREIGN KEY (`ganador`) REFERENCES `tegm_users` (`user_login`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1
CREATE TABLE `loteria_tipos` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`nombre` varchar(60) NOT NULL,
`coste` int(11) NOT NULL,
`premio` int(11) NOT NULL,
`duracion` int(11) NOT NULL COMMENT '(en horas)',
`activa` tinyint(1) NOT NULL,
`x` int(11) NOT NULL COMMENT '(coordenadas del cartel)',
`y` int(11) NOT NULL COMMENT '(coordenadas del cartel)',
`z` int(11) NOT NULL COMMENT '(coordenadas del cartel)',
UNIQUE KEY `id` (`id`),
KEY `id_2` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
According to MySQL:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 9
You're missing the ON keyword:
CREATE TRIGGER loteria_loterias AFTER UPDATE ON loteria_loterias
^^

Mysql statement insert, if inserted, insert another

I have the following statement:
INSERT INTO `Properties`(`Id`, `Url`, `BrokerId`, `LastFound`) VALUES
(#Id,#Url,#BrokerId,#LastFound)
ON DUPLICATE KEY UPDATE LastFoundOn = #LastFoundOn;
INSERT INTO `Events`(`Id`, `Type`, `DateTime`, `PropertyId`) VALUES
(#EventId,#EventType,#Now,#Id);
There is a foreign key constraint between Properties.Id and Events.PropertyId. And the Url is unique.
This works - almost. When a recod is not inserted, but updated because of duplicate key (Url), then the insert into event will fail, because the foreign key simply doesn't exist. Like this:
Eg:
exists: 1 | http://test1.com | 2 | 2013-03-13
to insert: 2 | http://test2.com | 2 | 2013-03-14
When trying to insert, it updates instead, because of the unique url. When afterwards trying to insert the event, a foreign key (2) doesn't exist in the Properties table. How can I make an if then statement to handle this scenario?
Something like (?):
INSERT INTO `Properties`(`Id`, `Url`, `BrokerId`, `LastFound`) VALUES
(#Id,#Url,#BrokerId,#LastFound)
ON DUPLICATE KEY UPDATE LastFoundOn = #LastFoundOn;
IF LastInserted = #Id THEN
INSERT INTO `Events`(`Id`, `Type`, `DateTime`, `PropertyId`) VALUES
(#EventId,#EventType,#Now,#Id);
END IF;
UPDATE:
A trigger might be the solution, but I'm struggeling making it work. What's wrong here?
DELIMITER $$
CREATE TRIGGER Event_Submitted_Trigger AFTER INSERT ON Properties
FOR EACH ROW
BEGIN
INSERT INTO Events VALUES(SELECT(UUID()), 'PropertySubmitted', SELECT(NOW()), new.Id);
END$$
I get the following error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT(NOW()), new.Id); END$$' at line 4
Best regards,
Søren
UPDATE:
Here is my schema:
CREATE TABLE IF NOT EXISTS `Events` (
`Id` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Type` enum('PropertySubmitted','PropertyChanged','PropertyRemoved') NOT NULL,
`DateTime` datetime NOT NULL,
`Attribute` varchar(128) NOT NULL,
`From` varchar(512) NOT NULL,
`To` varchar(512) NOT NULL,
`PropertyId` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`Id`),
KEY `IX_FK_PropertyEvent` (`PropertyId`),
KEY `DateTimeIndex` (`DateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `Properties`
--
CREATE TABLE IF NOT EXISTS `Properties` (
`Id` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Url` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Type` varchar(64) NOT NULL,
`ExtractedAddress` varchar(192) NOT NULL,
`ExtractedPostcode` varchar(8) NOT NULL,
`ExtractedCity` varchar(64) NOT NULL,
`StreetName` varchar(128) DEFAULT NULL,
`StreetNumber` varchar(8) DEFAULT NULL,
`Floor` varchar(8) DEFAULT NULL,
`Side` varchar(8) DEFAULT NULL,
`DoorNo` varchar(8) DEFAULT NULL,
`Postcode` int(4) DEFAULT NULL,
`City` varchar(64) DEFAULT NULL,
`Latitude` double DEFAULT NULL,
`Longitude` double DEFAULT NULL,
`ImageUrl` varchar(512) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`Price` int(8) NOT NULL,
`Payout` int(8) NOT NULL,
`GrossPrice` int(6) NOT NULL,
`NetPrice` int(6) NOT NULL,
`Area` int(5) NOT NULL,
`GroundArea` int(5) NOT NULL,
`Rooms` int(2) NOT NULL,
`Year` int(4) NOT NULL,
`PriceChange` int(11) NOT NULL,
`FirstFoundOn` datetime NOT NULL,
`SubmittedOn` datetime NOT NULL,
`LastFoundOn` datetime NOT NULL,
`FoundAt` varchar(256) DEFAULT NULL,
`Validated` tinyint(1) NOT NULL,
`BrokerId` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Archived` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`),
UNIQUE KEY `Url` (`Url`),
KEY `IX_FK_PropertyBroker` (`BrokerId`),
KEY `UrlIndex` (`Url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Triggers `Properties`
--
DROP TRIGGER IF EXISTS `Event_Submitted_Trigger`;
DELIMITER //
CREATE TRIGGER `Event_Submitted_Trigger` AFTER INSERT ON `Properties`
FOR EACH ROW BEGIN
INSERT INTO `Events` VALUES(UUID(), 'PropertySubmitted', NOW(), NEW.Id);
END
//
DELIMITER ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `Events`
--
ALTER TABLE `Events`
ADD CONSTRAINT `Events_ibfk_1` FOREIGN KEY (`PropertyId`) REFERENCES `Properties` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `Properties`
--
ALTER TABLE `Properties`
ADD CONSTRAINT `Properties_ibfk_2` FOREIGN KEY (`BrokerId`) REFERENCES `Brokers` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
Assuming the following structure:
CREATE TABLE Properties (
id INT,
url VARCHAR(100),
lastFound DATETIME,
UNIQUE (url)
) ;
CREATE TABLE Events (
id VARCHAR(36),
type VARCHAR(20),
t DATETIME,
propertyId INT
) ;
Here is a working trigger:
DELIMITER $$
CREATE TRIGGER Event_Submitted_Trigger AFTER INSERT ON Properties
FOR EACH ROW BEGIN
INSERT INTO Events VALUES( UUID(), 'PropertySubmitted', NOW(), new.Id);
END $$
DELIMITER ;
See it in action here. Notice the NOW()+SLEEP(1) hack, only meant to delay execution in order to get a significant result (SLEEP() returns 0 if not interrupted).

Mysql Trigger Loop for query result with many rows

hi i have a database with many tables and foreign keys like this
CREATE TABLE IF NOT EXISTS `articulos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nombre` varchar(63) NOT NULL,
`contenido` text NOT NULL,
`normas_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=138 ;
CREATE TABLE IF NOT EXISTS `aspectosambientales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nombre` varchar(63) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ;
CREATE TABLE IF NOT EXISTS `aspectosambientales_articulos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`aspectosambientales_id` int(11) NOT NULL,
`articulos_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_aspaspectosambientales1` (`aspectosambientales_id`),
KEY `fk_aspee` (`articulos_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 UTO_INCREMENT=225 ;
CREATE TABLE IF NOT EXISTS `empresas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`razonsocial` varchar(127) DEFAULT NULL,
`nit` varchar(63) DEFAULT NULL,
`direccion` varchar(127) DEFAULT NULL,
`telefono` varchar(15) DEFAULT NULL,
`web` varchar(63) DEFAULT NULL,
`auth_user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
CREATE TABLE IF NOT EXISTS `articulos_empresas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`empresas_id` int(11) NOT NULL,
`articulo_id` int(11) NOT NULL,
`acciones` text,
`responsable` varchar(255) DEFAULT NULL,
`plazo` date DEFAULT NULL,
`cumplido` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_normas_empresas_empresas1` (`empresas_id`),
KEY `fk_normas_empresas_normas1` (`normas_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
and i need to create a trigger to fill the 'articulos_empresas' after insert in 'empresas' for all rows in 'articulos' that match with 'aspectosambientals' that the new 'empresas' selected.
I get all 'articulos' with this query
SELECT articulos_id FROM aspectosambientales_articulos
WHERE aspectosambientales_id = ID
-- ID is the aspectosambientales_id selected when the 'empresas' row is created
-- maybe something like NEW.aspectosambientales_id
but i dont know how create a loop like ' for loop' in trigger for every result in the query
some like this:
CREATE TRIGGER 'filltableae' AFTER INSERT ON 'empresas'
FOR EACH ROW
BEGIN
DECLARE arrayresult = (SELECT articulos_id FROM aspectosambientales_articulos
WHERE aspectosambientales_id = NEW.aspectosambientales_id)
--- here is when i have to do the loop for all the results
--- for ids in arrayresults
--- insert into articulos_empresas ('',NEW.id, ids, '', '' ,'','')
--- endfor
END
thanks!!!
Based on #Razvan answer i left here the code for the trigger, so maybe can help somebody
DROP TRIGGER IF EXISTS AEINST;
DELIMITER //
CREATE TRIGGER AEINST AFTER INSERT ON procesos_aspectos
FOR EACH ROW
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE ids INT;
DECLARE cur CURSOR FOR SELECT articulos_id FROM aspectosambientales_articulos WHERE aspectosambientales_id = NEW.aspectosambientales_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
ins_loop: LOOP
FETCH cur INTO ids;
IF done THEN
LEAVE ins_loop;
END IF;
INSERT INTO articulos_empresas VALUES (null,ids, NEW.empresas_id,null,null,null,null);
END LOOP;
CLOSE cur;
END; //
DELIMITER ;
thanks again!
As far as I know you can iterate through the result of a SELECT query using cursors.
See here : http://dev.mysql.com/doc/refman/5.0/en/cursors.html

What could cause a foreign key to not be able to be dropped even when foreign_key_checks = 0?

I'm attempting to rebuild my database, but I'm unable to get past dropping any foreign keys from tables, even though I also call SET foreign_key_checks = 0; From the MySQL docs, it seems that's all that I should need to do. What else might I need to do?
SET foreign_key_checks=0;
alter table galleries drop foreign key fk_page_gallery ;
alter table photos drop foreign key fk_photo_gallery ;
create table galleries (
id int(11) auto_increment not null ,
page_id int(11) ,
cover_id int null ,
title varchar(1024) ,
slug varchar(1024) not null ,
description text null ,
sort_order int(11) ,
published tinyint(1) default 0,
created varchar(20) ,
modified datetime ,
constraint pk_galleries primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
create table pages (
id int(11) auto_increment not null ,
menu_id int(11) not null ,
title varchar(1024) not null ,
slug varchar(1024) not null ,
body text not null ,
short_description varchar(1024) ,
published tinyint(1) default 0,
created datetime ,
modified datetime ,
constraint pk_pages primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SET foreign_key_checks=1;
And for reference:
mysql> SHOW CREATE TABLE galleries;
+-----------+-------------------------------------------+
| Table | Create Table |
+-----------+-------------------------------------------+
| galleries | CREATE TABLE `galleries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`page_id` int(11) DEFAULT NULL,
`cover_id` int(11) DEFAULT NULL,
`title` varchar(1024) DEFAULT NULL,
`slug` varchar(1024) NOT NULL,
`description` text,
`sort_order` int(11) DEFAULT NULL,
`published` tinyint(1) DEFAULT '0',
`created` varchar(20) DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_page_gallery` (`page_id`),
KEY `fk_gallery_cover` (`cover_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> SHOW CREATE TABLE pages;
+-------+-----------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------+
| pages | CREATE TABLE `pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`menu_id` int(11) NOT NULL,
`title` varchar(1024) NOT NULL,
`slug` varchar(1024) NOT NULL,
`body` text NOT NULL,
`short_description` varchar(1024) DEFAULT NULL,
`published` tinyint(1) DEFAULT '0',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_pages_menu` (`menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------+
The foreign keys in your create table statements are different than those in your drop statements. What's more is that your table definitions don't seem to have foreign keys at all. MySQL may throw a weird error if you try to drop a foreign key that is not there.