Join inside a trigger - mysql

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

Related

MySQL query error #1064?

I'm trying to import my old database but this gave me some errors what makes it impossible im also searching on google for 30 minuts and i can't find any solution?
SQL-query:
CREATE TABLE `UG_blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ownerid` int(11) NOT NULL,
`content` text NOT NULL,
`date` datetime NOT NULL DEFAULT TIMESTAMP,
PRIMARY KEY (`id`), KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
MySQL meldt: Documentatie
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '
PRIMARY KEY (`id`), KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT C' at line 6
The correct default value is CURRENT_TIMESTAMP:
CREATE TABLE `UG_blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ownerid` int(11) NOT NULL,
`content` text NOT NULL,
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
The SQL Fiddle is here.
EDIT:
You must be using an old-ish version of MySQL (okay, not that old, just pre-5.6). Well, you can't default a datetime value (without a trigger), so you have to live with a TIMESTAMP value and learn to love the timestamp functions:
CREATE TABLE `UG_blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ownerid` int(11) NOT NULL,
`content` text NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
First solution : change datatype into timestamp like this
CREATE TABLE `UG_blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ownerid` int(11) NOT NULL,
`content` text NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
The problem is : The Timestamp data type has a range of 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07.
So the second solution is, don't change datetime datatype into timestamp. But you need a trigger to set default value.

What Is Possible Use One Reference Field To Multiple Foreign Key Constraint

I want to make 3 Tables like this :
wc_groups Table
CREATE TABLE IF NOT EXISTS `wc_groups` (
`id` int(2) unsigned NOT NULL AUTO_INCREMENT,
`idgroup` int(7) NOT NULL,
`title` varchar(10) NOT NULL,
`content` text,
`status` smallint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `idgroup` (`idgroup`),
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
wc_matches Table
CREATE TABLE IF NOT EXISTS `wc_matches` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`time` date NOT NULL,
`group_id` int(2) unsigned NOT NULL,
`place` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`team_id_1` int(10) unsigned NOT NULL,
`team_id_2` int(10) unsigned NOT NULL,
`edituser` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `group_id_foreign` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
wc_teams Table
CREATE TABLE IF NOT EXISTS `wc_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`group_id` int(2) unsigned NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
ALTER TABLE `wc_teams`
ADD CONSTRAINT `group_id_foreign` FOREIGN KEY (`group_id`) REFERENCES `wc_groups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Why i got an error when execution Code to create table wc_team ?
What's possible to use What Is Possible Use One Reference Field ( wc_groups (id) ) To Multiple Foreign Key Constraint ?
I don't understand what you mean with multiple foreign keys.
But the problem in your wc_teams create query is that you have a lost , behind
`group_id` int(2) unsigned NOT NULL,
But you also miss your primary key so i guess you need this
CREATE TABLE IF NOT EXISTS `wc_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`group_id` int(2) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

mysql table creation Error Code: 1005 in this specific case

I have tried everything in solving this issue and yes I know that this type of question is already asked here but I could not solve my issue
It is mysql database
Error Code: 1005
Can't create table '.\project\comments.frm' (errno: 150)
the foreign keys are matching in structure (i.e length and type) then what can be the possible problem in the table creation
Table which is giving error is comments:
CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts`.`id`,
FOREIGN KEY (`user_id`) REFERENCES `users`.`id`,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;`
Here is posts table which is already created in the databas
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(30) default NULL,
`description` longtext,
`image` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Here is the users table which is also already created in the database
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`user_name` varchar(33) default NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) default NULL,
`type` varchar(255) NOT NULL,
`registrationDate` date NOT NULL,
PRIMARY KEY (`id`,`email`,`type`)
)
Your syntax is incorrect. The REFERENCES keyword should be followed by table (columns):
CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

It it possible to add a foreign key constraint to a composite key in mysql?

So I have 2 tables.
subject_schedule:
CREATE TABLE IF NOT EXISTS `subject_schedule` (
`subject` varchar(10) NOT NULL,
`schedule_id` int(11) NOT NULL,
`id` int(11) NOT NULL,
PRIMARY KEY (`subject`,`schedule_id`),
KEY `schedule_id` (`schedule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
and appointment:
CREATE TABLE IF NOT EXISTS `appointment` (
`work_plan` varchar(1000) DEFAULT NULL,
`date` date DEFAULT NULL,
`homework_given` varchar(1000) DEFAULT NULL,
`tutor_comments` varchar(1000) DEFAULT NULL,
`admin_comments` varchar(1000) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`schedule_id` int(11) NOT NULL,
`attended` tinyint(1) NOT NULL DEFAULT '1',
`arrival_time` time DEFAULT NULL,
`departure_time` time DEFAULT NULL,
`homework_completed` tinyint(1) NOT NULL DEFAULT '0',
`subject` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `schedule_id` (`schedule_id`),
KEY `subject` (`subject`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10004 ;
I want to create a foreign key which references the composite key in appointment. I have tried:
ALTER TABLE 'appointment'
ADD CONSTRAINT 'appointment_fk' FOREIGN KEY (`schedule_id`, `subject`)
REFERENCES 'subject_schedule' ('schedule_id', 'subject');
but it returns an error in PhpMyAdmin:
#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 ''appointment' ADD CONSTRAINT 'appointment_fk' FOREIGN KEY
(schedule_id, `su' at line 1
What am I doing wrong?
Is it better to just have an id as a primary key and reference that instead of using a composite key?
the columnNames shouldn't be wrap with single quotes because it will be converted to a string (not a column anymore)
ALTER TABLE appointment
ADD CONSTRAINT appointment_fk FOREIGN KEY (`schedule_id`, `subject`)
REFERENCES subject_schedule (schedule_id, subject);
SQLFiddle Demo

mysql error 1064

Im trying to create a table with this code:
CREATE TABLE IF NOT EXISTS `entries` (
`id` int(10) NOT NULL auto_increment,
`atom_id` varchar(512) NOT NULL,
`title` varchar(256) NOT NULL,
`author` varchar(128) NOT NULL,
`link` varchar(512) NOT NULL,
`content` longtext NOT NULL,
`updated` varchar(25) NOT NULL,
`inserted` varchar(25) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `atom_id` (`atom_id`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `topics` (
`id` int(10) NOT NULL auto_increment,
`status` varchar(32) NOT NULL,
`hub` varchar(512) NOT NULL,
`topic` varchar(512) NOT NULL,
`lease` varchar(25) NOT NULL,
`secret` varchar(256) NOT NULL,
`token` varchar(40) NOT NULL,
`date` varchar(25) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
but i got this 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 ') ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1' at line 12
I can't figure what's going on, any advice?
Remove the comma after UNIQUE KEY 'atom_id' ('atom_id'), in line 11
UNIQUE KEY `atom_id` (`atom_id`),
^
Try losing the ","