Mysql TIMESTAMP trigger doesn't work - mysql

What I do wrong?
I have table1
On INSERT data to table1 I have trigger:
BEGIN
INSERT INTO table2 (`c_id`, `date`, `product_id`, `price`)
VALUES (
NEW.c_id,
NEW.date = CURRENT_TIMESTAMP,
NEW.product_id,
NEW.price
); END
CREATE TABLE `table2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c_id` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`product_id` int(11) NOT NULL,
`price` decimal(9,2) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `c_data` (`c_id`,`date`,`product_id`)
) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;
As result, I get 0000-00-00 00:00:00 in date
MySQL version 5.6.28

Remove date and NEW.date = CURRENT_TIMESTAMP, from insert query. By default mysql will set date for it.
INSERT INTO table2 (`c_id`, `product_id`, `price`)
VALUES (
NEW.c_id,
NEW.product_id,
NEW.price
);

Related

Error when uploading mySQL file

After i used the new answer i uploaded the file and this is what i get now.
SQL query:
CREATE TABLE IF NOT EXISTS `games` ( ) INSERT INTO `games` (`name`, `creatorid`, `gameid`, `id`, `plays) VALUES ('בדיקה', 'לא ידוע', 1,1,0) CREATE TABLE IF NOT EXISTS `gamewall` ( `userid` int(11) NOT NULL, `reason` text NOT NULL, `comment` text NOT NULL, `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `bannedbyid` int(11) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE IF NOT EXISTS `bans_IP` ( `IP` mediumtext NOT NULL, `id` int(11) NOT NULL AUTO_INCREMENT, `bannerid` int(11) NOT NULL, `reason` mediumtext NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; CREATE TABLE IF NOT EXISTS `buysell` ( `buysell` int(11) NOT NULL, `sellerid` int(11) NOT NULL, `price` int(11) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE IF NOT EXISTS `competitions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` mediumtext NOT NULL, `startedbyid` int(11) NOT NULL, `descripti[...]
MySQL said: Documentation
#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 ')
INSERT INTO `games` (`name`, `creatorid`, `gameid`, `id`, `plays) VALUES
('' at line 3
You are receiving this error because you have selected all the columns of your table but values are available for first 11 columns and you missed ) in last
This code will fix your problem
INSERT INTO `accounts` (
`Username`,
`Password`,
`Email`,
`salt`,
`Reebs`,
`online`,
`lastOnline`,
`lastOnlineMinutes`,
`lastOnlineHours`,
`status`,
`blurb`
)
VALUES
(
'AvatarLife',
'df3ed1ad53c4cc2f3b5c6bdf04d33a9f094df53b',
'fawrsnipng#gmail.com',
'jBqnW?Dk9Epzpg4',
'9223372036854775807',
'1',
'205',
'26',
'13',
'',
'Avatar Life is the only good life!')

MYSQL trigger after insert => insert into second table => before insert trigger not working

I got this datebase structure:
CREATE TABLE `user` (
`usr_id` bigint(20) NOT NULL AUTO_INCREMENT,
`usr_mail` varchar(64) NOT NULL,
`usr_creationdate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`usr_id`)
);
CREATE TABLE `user_activation` (
`usract_id` bigint(20) NOT NULL AUTO_INCREMENT,
`usr_id` bigint(20) NOT NULL,
`usract_key` text NOT NULL,
`usract_used` int(11) NOT NULL DEFAULT '0',
`usract_creationdate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`usract_usagedate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`usract_expiredate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TRIGGER `USER_INSERT_ADD_ACTIVATION` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_activation (usr_id, usract_key)
VALUES (NEW.usr_id, UUID());
CREATE TRIGGER `USRACT_INSER_SET_EXPIREDATA` BEFORE INSERT ON `user_activation`
FOR EACH ROW
SET NEW.usract_expiredate = DATE_ADD(NOW(), INTERVAL 1 DAY);
When I insert data into the table user_activation my user_activation.usract_expiredate gets updated to the next day.
If I insert data into the table user a row is insert into user_activation but user_activation.usract_expiredate will not get set to the next day.
Seems like the second trigger is not being executed.
Am I doing something wrong with the triggers?
Well you need only one trigger to achieve this.
DROP TRIGGER `USRACT_INSER_SET_EXPIREDATA`;
DROP TRIGGER `USER_INSERT_ADD_ACTIVATION`;
CREATE TRIGGER `USER_INSERT_ADD_ACTIVATION` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_activation (usr_id, usract_key,usract_expiredate)
VALUES (NEW.usr_id, UUID(),DATE_ADD(NOW(), INTERVAL 1 DAY));

Error executing stored procedure fails

I created a stored procedure in mysql database, which fires multiple insert statemets.
As shown below:
DELIMITER //
DROP PROCEDURE IF EXISTS insert_user_group_info
//
CREATE PROCEDURE insert_group_user_info(groupname varchar(50), groupdesc varchar(100),
createddate varchar(50), createdby varchar(100))
BEGIN
DECLARE RETURN_VAL INT UNSIGNED DEFAULT 0;
DECLARE NEWGROUPID INT UNSIGNED DEFAULT 0;
START TRANSACTION;
Insert into group_tbl (`groupname`,
`groupdesc`,
`groupusers`,
`createdon`,
`createdby`,
`groupstatus`)
values
(groupname,
groupdesc,
'1',
createddate,
createdby,
'1');
SET NEWGROUPID = LAST_INSERT_ID();
INSERT INTO useringroup_tbl
( groupid,
username,
regdate,
joindate,
userstatus,
roleid)
VALUES
( NEWGROUPID,
createdby,
createddate,
createddate,
'1',
'1');
INSERT INTO userinrole_tbl
( username,
groupid,
roleid)
VALUES
(createdby,
NEWGROUPID,
'1');
SET RETURN_VAL = LAST_INSERT_ID();
SELECT RETURN_VAL;
COMMIT;
END//
DELIMITER ;
Schema for tables are as follows
Table 1 group_tbl
CREATE TABLE `group_tbl`
(
`groupid` int(11) NOT NULL auto_increment,
`groupname` varchar(50) NOT NULL,
`groupdesc` varchar(100) NOT NULL,
`groupusers` int(11) NOT NULL,
`createdon` datetime NOT NULL,
`createdby` varchar(50) NOT NULL,
`groupstatus` tinyint(4) NOT NULL,
PRIMARY KEY (`groupid`)
);
Table 2 useringroup_tbl
CREATE TABLE IF NOT EXISTS `useringroup_tbl`
(
`groupid` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
`regdate` datetime NOT NULL,
`joindate` datetime NOT NULL default '0000-00-00 00:00:00',
`userstatus` tinyint(4) NOT NULL,
`closingdate` datetime NOT NULL default '0000-00-00 00:00:00',
`roleid` int(11) NOT NULL default '0',
PRIMARY KEY (`groupid`,`username`)
);
Table 3 userinrole_tbl
CREATE TABLE IF NOT EXISTS `userinrole_tbl`
(
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`groupid` int(11) NOT NULL,
`roleid` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
While executing this procedure by this statement
call insert_group_user_info('test name','test description','2015-05-10 12:10:12','XYZuser')
I get an error
1062 - Duplicate entry '1-XYZuser' for key 1
and each time it the number appended to the username gets incremented
like this
1062 - Duplicate entry '2-XYZuser' for key 1
So please if anybody can give me direction what I am doing wrong,
Thanks in advance.
According to the error you have pasted,
1062 - Duplicate entry '1-XYZuser' - PRIMARY KEY (groupid,username) - It doesn't matter if you truncate the tables or not. You might be inserting the same data again.
You might be using last_insert_id() incorrectly.
Why is there no auto_increment in useringroup_tbl?

SELECT LAST_INSERT_ID() Not working with BIGINT on MySQL 5.6.11

I have a BIGINT field as an auto increment and primary key on a MySQL Innodb table, running MySQL Community Server 5.6.11.
After calling a basic INSERT statement, and then calling SELECT LAST_INSERT_ID(), I'm always returned 0, even though the INSERT statement was sucessful.
Any ideas why this might be happening.
UPDATE: Here is my table definition
CREATE TABLE `Booking` (
`BookingId` bigint(20) NOT NULL AUTO_INCREMENT,
`HotelId` int(11) NOT NULL,
`AgentId` int(11) NOT NULL DEFAULT '0',
`BookedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastChangedBy` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`BookingId`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=71 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Here is my INSERT statement
INSERT INTO Booking
(
HotelId,
AgentId,
BookedOn,
LastChangedBy
)
VALUES
(
iHotelId,
iAgentId,
NOW(),
0
);
SELECT LAST_INSERT_ID() AS BookingId;

Data is not inserted in mysql

I have the following table:
(Yes, the table names are silly... I'm just messing about)
CREATE TABLE `habitat`.`habit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
When I try to do the following sql statement, only 0 shows up in content:
INSERT into habit
(content, user_id)
VALUES (content = 'this is some habit', user_id = 2)
Basically you do not need to include the "columnName = value" in the VALUES portion of an insert statement. It should look like this.
INSERT INTO habit (
content,
user_id)
VALUES (
'this is some habit',
2)