MySQL Update Query Successfull But Values Doesn't Changing At All - mysql

I have a problem, I created a stored procedure to activate users by sending their email id and activation key
this is the SQL syntax (in MySQL)
DELIMITER $$
CREATE PROCEDURE `dbname`.`sp_userActivate` (
IN email VARCHAR(140),
IN ac VARCHAR(64)
)
BEGIN
SET SQL_SAFE_UPDATES=0;
UPDATE user SET activated = 1
WHERE user.email = email AND
user.activation_key=ac;
commit;
END
When I execute the syntax, it's successfully executed, but when I take a look in the table, the value activated still remains = 0 (not updated at all)
I have looked more than twice to ensure the email and activation is equal with is stored in table,
What's wrong with this Query?
UPDATE
This is my table structure in schema
Thanks in advance for your help.
SQL Statement for Create table
CREATE TABLE IF NOT EXISTS `db`.`user` (
`no` INT(11) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(140) NOT NULL ,
`password` VARCHAR(128) NOT NULL ,
`firstname` VARCHAR(300) NOT NULL ,
`lastname` VARCHAR(300) NULL DEFAULT NULL ,
`email` VARCHAR(140) NOT NULL ,
`pepper` CHAR(128) NOT NULL ,
`activation_key` VARCHAR(64) NULL DEFAULT NULL ,
`gender` CHAR(1) NULL DEFAULT NULL ,
`activated` CHAR(1) CHARACTER SET 'latin1' COLLATE 'latin1_bin' NULL DEFAULT '0' ,
PRIMARY KEY (`no`) ,
UNIQUE INDEX `username` (`username` ASC) ,
UNIQUE INDEX `email` (`email` ASC) ,
INDEX `user_firstname_idx` (`firstname` ASC) ,
INDEX `user_lastname_idx` (`lastname` ASC) )
For Example Row
insert into
user(username, password,firstname, lastname, email,pepper,activation_key,gender)
values("usr1test","c781bf44a464a5946ef36a7250f5504388914bbf6287fabaf938472f46c413d71cd7bf2b3077eeac8675419d5f022ff3652ba7e13e8","user1","test","usr1test#localhost","af41bfa3c9324f39fd82f84125967b38969662256cf8249e73e3bd2cef3928b5","OGE4Y2E2OWUtMmM2Mi00MjJkLWI0NTQtNzJkZDQ1OTcxNjUx",'M');

Here is the procedure, that worked for me:
DROP PROCEDURE IF EXISTS sp_userActivate;
DELIMITER $$
CREATE PROCEDURE sp_userActivate (
IN email VARCHAR(140),
IN ac VARCHAR(64)
)
BEGIN
SET SQL_SAFE_UPDATES=0;
UPDATE `user`
SET activated = 1
WHERE
`user`.`email` = email
AND
`user`.`activation_key` = ac;
COMMIT;
END $$
DELIMITER ;
Run this, then try:
CALL sp_userActivate('usr1test#localhost',
'OGE4Y2E2OWUtMmM2Mi00MjJkLWI0NTQtNzJkZDQ1OTcxNjUx');
It worked correctly on my machine.

okay, after see the table data in my table. It seems that the key is have BLANK space after the last character.
example :
-- with blank space:
ZWRhNzZmMzMtMmI2Ny00NDBlLTkxNTUtNmQ2MWIwYjg3MzA4_ (see _ ?)
-- without blank space: ZWRhNzZmMzMtMmI2Ny00NDBlLTkxNTUtNmQ2MWIwYjg3MzA4
so, the MySQL see point no 1 and 2 are different.
I hope this makes my problem clear.
thank you for all of the contributor

Related

Why when I use select statement like "select * from table_name where character='⺎' " then return '尢','⼪','⺎' and '⺐'

Mysql query returns mismatched records, such as the query field character='兀' but matches out, 尢, ⺎, ⺐?
I update my mysql from 5.7 to 8.0,in 5.7,this problem is more common
drop database if exists `chinese_sort`;
create database `chinese_sort` default charset utf8mb4;
use `chinese_sort`;
create table `chinese_character_dict_standard_version`(
`id` bigint primary key auto_increment,
`character` char(1) not null comment '字',
`chinese_pinyin` varchar(50) default '' comment '拼音',
`stroke_num` tinyint not null default 0 comment '笔画数',
`four_corner_num` varchar(5) default '' comment '四角号码',
`unicode` varchar(12) not null comment 'unicode码',
`utf16` varchar(12) not null comment 'utf16码'
);
insert into chinese_character_dict_standard_version(`character`, unicode, utf16)
values('尢','5c22','\u5c22'),
('⼪','2f2a','\u2f2a'),
('⺎','2e8e','\u2e8e'),
('⺐','2e90','\u2e90');
SET NAMES utf8mb4 collate utf8mb4_unicode_ci;
select * from chinese_character_dict_standard_version
where `character` ='⺎';
I expect the output of the select statement to be '兀', but the actual output is 兀', '尢', '⺎', '⺐'

MySQL How to reset the field to old value that has a trigger when the trigger fails?

I have a field called currentItem , this field has a trigger that calls a stored procedure (with transaction) sp_TransferData to perform some transfers of information to a worktable. If the stored procedure fails - I would like to restore the old value of currentItem - as it has not effectively changed.
I am using MySQL and I would like this logic to be in my trigger - obviously we do not want an endless loop so how do I accomplish this ?
Trigger Pseudo Code :
Call sp_TransferData(#ResultCode);
Select #ResultCode into Result;
If Result < 0 THEN
thisField Value = OLD.Value;
End If;
// EDIT 9-16-2016 13:00 //
There is only 1 Row in this table never any thing more! Column list edited for brevity.
table global_items
Id INT PK,
lsize INT,
wsize INT,
currentItem INT
Trigger is on currentItem After Update or Before I do not care which as long as it works and does not refire the trigger:
If the value has changed CALL sp_TransferData(#ResultCode);
If (SELECT #ResultCode) < 0 THEN
Reset currentItem to old value but do not cycle the trigger since we are only resetting it.
EndIf;
Just to add this is what I have in my trigger code which is not correct.
The Table definitions are supplied.
BEGIN
IF NEW.currentItem <> OLD.currentItem THEN
call sp_CurrentItemChanged(NEW.currentItem, #ResultCode, #ResultMsg);
IF ((Select #ResultCode) < 0) THEN
NEW.currentItem = OLD.currentItem;
END IF;
END IF;
END
CREATE TABLE working_table (
Id int(11) NOT NULL AUTO_INCREMENT,
Mbf float DEFAULT NULL,
Width float DEFAULT NULL,
Pulse int(11) DEFAULT NULL,
PRIMARY KEY (Id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci
ROW_FORMAT = DYNAMIC;
CREATE TABLE recipe (
Id int(11) NOT NULL AUTO_INCREMENT,
Name varchar(80) NOT NULL DEFAULT 'UnAssigned',
IsDefault tinyint(1) DEFAULT 0,
PRIMARY KEY (Id),
UNIQUE INDEX Id_UNIQUE (Id),
UNIQUE INDEX Name_UNIQUE (Name)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci
ROW_FORMAT = DYNAMIC;
CREATE TABLE Packs (
Id int(11) NOT NULL AUTO_INCREMENT,
Name varchar(45) NOT NULL DEFAULT 'UNDEFINED',
Width float NOT NULL DEFAULT 0,
Pulse int(11) NOT NULL DEFAULT 0,
Mbf float NOT NULL DEFAULT 0,
RecipeID int(11) NOT NULL DEFAULT 0,
SetupID int(11) DEFAULT 1,
PRIMARY KEY (Id),
INDEX SetupID_ndx (SetupID),
INDEX FK_PackRecipeID_Recipe_ID_idx (RecipeID),
INDEX FK_RecipeID_PackS_idx (RecipeID),
CONSTRAINT FK_PackRecipeID_Recipe_ID FOREIGN KEY (RecipeID)
REFERENCES recipe (Id) ON DELETE CASCADE ON UPDATE CASCADE
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci
ROW_FORMAT = DYNAMIC;
CREATE TABLE global_items (
Id int(11) NOT NULL AUTO_INCREMENT,
PackSys_Count int(11) DEFAULT NULL,
Active_Recipe int(11) DEFAULT 1,
PRIMARY KEY (Id)
)
ENGINE = INNODB
AUTO_INCREMENT = 2
CHARACTER SET utf8
COLLATE utf8_general_ci
ROW_FORMAT = DYNAMIC;
When global_items.Active_recipe changes the trigger fires .. The data that is moved is in packs and its related tables (brevity here) to the working_table. The global_items table is NEVER touched by anything in the lengthy Stored Procedure or any other triggers or any other sql code. It is never modified by anything internal to the SQL storage - it is touched only by an outside applications. I am not sure how I restore the value to the original value on failure of the stored procedure.
I think I might be understanding what you are getting at. But since you did not show your trigger entirely I guess, and not the Stored Proc, I just winged it and investigated.
I think the problem is that your stored proc does not have an OUT qualifier to it to make it write-able. The below works fine and I think it captures how to solve your issue.
Schema:
-- drop table global_items;
create table global_items
( Id INT primary key,
lsize INT not null,
wsize INT not null,
currentItem INT not null,
theCount int not null
);
insert global_items(Id,lsize,wsize,currentItem,theCount) VALUES
(1,1,1,100,0);
select * from global_items;
Trigger:
DROP TRIGGER IF EXISTS giBeforeUpdate;
DELIMITER $$
CREATE TRIGGER giBeforeUpdate
BEFORE UPDATE
ON global_items FOR EACH ROW
BEGIN
DECLARE tRet INT;
SET tRet=0;
SET NEW.theCount=OLD.theCount+1;
CALL uspDoSomething7(tRet);
IF tRet=1 THEN
-- stored proc said FAILURE
SET NEW.currentItem=OLD.currentItem;
END IF;
END;$$
DELIMITER ;
Stored Procedure:
DROP PROCEDURE IF EXISTS uspDoSomething7;
DELIMITER $$
CREATE PROCEDURE uspDoSomething7(OUT retVal INT)
BEGIN
DECLARE rndNum INT;
SET rndNum=FLOOR(RAND()*2)+1; -- random number 1 or 2
-- sets retVal to 1 on FAILURE
IF rndNum=2 THEN
SET retVal=1; -- FAIL
ELSE
SET retVal=0; -- SUCCESS
END IF;
END$$
DELIMITER ;
Test:
Repeatedly call this confirm that it fails roughly half the time
that is, currentItem retains its old value
but chg the update stmt below each time for the currentItem= part of it
update global_items set currentItem=410,lsize=2 where Id=1;
select * from global_items;

How to run the sql script from the EER designed on MySQL Workbench to MySQL Server (Ubuntu)

I have generated this script from MySQL WorkBench but when i try to run it in my MySQL Server, i get the following error:
'17:28:11 DROP SCHEMA IF EXISTS MYA_Database_Schema Error Code: 1044. Access denied for user ''#'localhost' to database 'MYA_Database_Schema' 0.000 sec'
The first 2 tables are created without any issues, but when it comes to BMCDetail, i keep getting errors.
Any help is appreciated since i am designing database for a new project and i need to get this database schema on the server as soon as possible. Any help would be great, even if you point me in the right direction.
Operating system Ubuntu Linux 12.04
MySQL version 5.5.22
I tried to replicate this on a local xampp server that i installed through phpmyadmin and still similar issue.
'SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
DROP SCHEMA IF EXISTS `MYA_Database_Schema` ;
CREATE SCHEMA IF NOT EXISTS `MYA_Database_Schema` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
SHOW WARNINGS;
USE `MYA_Database_Schema` ;
-- Table BMCSupervisor
DROP TABLE IF EXISTS `BMCSupervisor` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `BMCSupervisor` (
`idBMCSupervisor` INT NOT NULL AUTO_INCREMENT ,
`SupervisorName` VARCHAR(30) NULL ,
`EmployeeCode` INT NULL ,
`PhoneNumber` INT NULL ,
`EmailID` VARCHAR(100) NULL ,
`BMCSupervisorID` VARCHAR(5) NOT NULL ,
PRIMARY KEY (`idBMCSupervisor`, `BMCSupervisorID`) )
ENGINE = InnoDB;
SHOW WARNINGS;
-- Table BMCSupplierEnrolManager
DROP TABLE IF EXISTS `BMCSupplierEnrolManager` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `BMCSupplierEnrolManager` (
`idBMCSupplierEnrolManager` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(25) NULL ,
`EmployeeCode` INT NULL ,
`PhoneNumber` INT NULL ,
`EmailID` VARCHAR(100) NULL ,
`SupplierEnrolManagerID` INT NOT NULL ,
PRIMARY KEY (`idBMCSupplierEnrolManager`, `SupplierEnrolManagerID`) )
ENGINE = InnoDB;
SHOW WARNINGS;
-- Table BMCDetail
DROP TABLE IF EXISTS `BMCDetail` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `BMCDetail` (
`idBMCDetail` INT NOT NULL AUTO_INCREMENT ,
`BMCCode` VARCHAR(5) NOT NULL ,
`BMCName` VARCHAR(10) NULL ,
`State` VARCHAR(25) NOT NULL ,
`District` VARCHAR(20) NOT NULL ,
`Block` VARCHAR(15) NULL ,
`Taluk` VARCHAR(15) NULL ,
`Village` VARCHAR(20) NULL ,
`BMCCapacity` DECIMAL(10,2) NULL ,
`DateOfCommissioning` DATE NULL ,
`SupervisorID` VARCHAR(5) NOT NULL ,
`SupplierEnrolManagerID` INT NOT NULL ,
PRIMARY KEY (`idBMCDetail`, `BMCCode`, `State`, `District`) ,
CONSTRAINT `SupervisorID`
FOREIGN KEY (`SupervisorID` )
REFERENCES `BMCSupervisor` (`BMCSupervisorID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `SupplierEnrolManagerID`
FOREIGN KEY (`SupplierEnrolManagerID` )
REFERENCES `BMCSupplierEnrolManager` (`SupplierEnrolManagerID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;'

Operand should contain 1 column(s)

Getting a Operand should contain 1 column(s) mysql error whenever I try to insert into the table sets.
I googled and found a hoard of similar questions but they are always pin point specific to solving their immediate problem. I have mysql 5.6 by the way. I am allowed multiple TIMESTAMPS.
Here is my code:
INSERT INTO `sets` (`tabler_name`) VALUES ("leads_auto");
Here is my table:
CREATE TABLE IF NOT EXISTS `lms`.`sets` (
`set_id` BIGINT NOT NULL AUTO_INCREMENT,
`on_off` SMALLINT NOT NULL DEFAULT 0,
`tabler_name` VARCHAR(45) NULL,
`origin_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`original_count` INT NULL,
`current_count` INT NULL,
`source_type` VARCHAR(45) NULL,
`source` VARCHAR(45) NULL,
`method` VARCHAR(45) NULL,
`agent` VARCHAR(45) NULL,
`dupes` INT NULL,
`bads` INT NULL,
`aged` INT NULL COMMENT 'This table keeps track of the record sets that enter the system. Example: a set of leads imported into the database.',
PRIMARY KEY (`set_id`)
) ENGINE = InnoDB;
Stored Procedure:
DELIMITER //
CREATE PROCEDURE `lms`.`leads_to_bak` ()
BEGIN
SET #table1 = (SELECT `tabler_name` FROM sets WHERE on_off=0 LIMIT 1);
SET #table2 = CONCAT(#table1, '_bak');
SET #SQL1 = CONCAT('INSERT INTO ',#table2, '(', (SELECT
REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #table2), ')', ' SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = #table1), ' FROM ', #table1);
PREPARE stmt FROM #sql1;
EXECUTE stmt;
END//
DELIMITER ;
USE `lms`;
Trigger
DELIMITER $$
USE `lms`$$
CREATE TRIGGER `lms`.`after_insert_into_leads`
AFTER INSERT ON `sets` FOR EACH ROW
BEGIN
IF (SELECT * FROM sets WHERE on_off=0 LIMIT 1) THEN
CALL lms.leads_to_bak();
END IF;
END$$
DELIMITER ;
USE `lms`;
I don't see anything wrong with my routines. Removing the routines and trigger seems to make the problem go away.
In your trigger, did you mean to put EXISTS after IF? Like this:
CREATE TRIGGER `lms`.`after_insert_into_leads`
AFTER INSERT ON `sets` FOR EACH ROW
BEGIN
IF EXISTS (SELECT * FROM sets WHERE on_off=0 LIMIT 1) THEN
CALL lms.leads_to_bak();
END IF;
END$$
Escape your table name, it seems to be a reserved function. I'm not sure if you've defined one locally.
INSERT INTO `sets` (tabler_name) VALUES ("leads_auto");
Also, you can't have two timestamp fields in a single database afaik. Change one of the two timestamps to a DATETIME field if it caused you issues as well
Besides escaping the field name in your INSERT-statement, it cannot be improved very much. But it doesn't generate any error in my test enviroment. Is this really the exact statement throwing you an error?
However, there's a slight problem in your table definition, it will throw you an
Incorrect table definition; there can be only one TIMESTAMP column
with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.
As the error message indicates, you can only use one timestamp column with CURRENT_TIMESTAMP, if you need more than one, you can do this using a trigger.

MySQL assertion-like constraint

I'm a MySQL newbie, I just discovered that it doesn't support assertions.
I got this table:
CREATE TABLE `guest` (
`ssn` varchar(16) NOT NULL,
`name` varchar(200) NOT NULL,
`surname` varchar(200) NOT NULL,
`card_number` int(11) NOT NULL,
PRIMARY KEY (`ssn`),
KEY `card_number` (`card_number`),
CONSTRAINT `guest_ibfk_1` FOREIGN KEY (`card_number`) REFERENCES `member` (`card_number`)
)
What I need is that a member can invite maximum 2 guests.
So, in table guest I need that a specific card_number can appear maximum 2 times.
How can I manage it without assertions?
Thanks.
This definitly smells of a BEFORE INSERT trigger on the table 'guest':
DELIMITER $$
DROP TRIGGER IF EXISTS check_guest_count $$
CREATE TRIGGER check_guest_count BEFORE INSERT ON `guest`
FOR EACH ROW BEGIN
DECLARE numguests int DEFAULT 0;
SELECT COUNT(*) INTO numguests FROM `guest` WHERE card_number=NEW.card_number;
if numguests>=2 THEN
SET NEW.card_number = NULL;
END IF;
END;
$$
DELIMITER ;
This basically looks up the current guest count, and if it is already >=2 sets card_number to NULL. Since card_number is declared NOT NULL, this will reject the insert.
Tested and works for me on MySQL 5.1.41-3ubuntu12.10 (Ubuntu Lucid)