MYSQL wrong data inserted - mysql

I have a problem with my SQL query. I'm trying to insert data into table, but it seems like it put wrong value for one column.
Here is my query:
INSERT INTO va_users_points (user_id, order_id, points_amount, points_action, points_type, admin_id_added_by, admin_id_modified_by, date_added)
VALUES ('16772', '152424', '49', '1', '2', '0', '0', '2015-04-20 12:22:03')
The problem with column points_amount. In my query, it's 49. The DATA Type of this column in MYSQL table is INT(11). But, it inserts 490 value. Why it could be?
Here is my database structure
'points_id' INT(11) NOT NULL AUTO_INCREMENT,
'user_id' INT(11) NULL DEFAULT '0',
'order_id' INT(11) NULL DEFAULT '0',
'order_item_id' INT(11) NULL DEFAULT '0',
'points_amount' INT(11) NULL DEFAULT '0',
'points_action' INT(11) NULL DEFAULT '0',
'points_type' INT(11) NULL DEFAULT '0',
'admin_id_added_by' INT(11) NULL DEFAULT '0',
'admin_id_modified_by' INT(11) NULL DEFAULT '0',
'date_added' DATETIME NULL DEFAULT NULL,
'date_modified' DATETIME NULL DEFAULT NULL

As I see, there is no way this situation can occur except if there is a AFTER INSERT trigger defined on this table va_users_points which is doing this manipulation behind the scene as already commented by #fancypants.
Check this fiddle (http://sqlfiddle.com/#!9/4b1fb/1) which proves that there is nothing wrong with your INSERT statement.
To check if really a trigger is defined or not use the below SQL statement
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_SCHEMA='your_database_name'
AND EVENT_OBJECT_TABLE='va_users_points';

Related

MySQL INSERT results in duplicate key, but no duplicate exists

I have read through many entries that people have claimed to have this problem and they've solved their issue but none of their answers solve MY issue. I am using phpMyAdmin to update the email address of a user. The "user_email" field is marked as UNIQUE. Whenever I update the email address to his actual email, I get:
Duplicate entry 'user#example.com' for key 'user_email'
I have Analyzed, Optimized, and Repaired the table and no errors appear -- everything comes up as OK.
I have run several SQL statements to find any duplication only to find out that none exists.
I even exported the table and imported the records again. I add the indexes and try and update... duplicate entry message. Here's the table structure:
CREATE TABLE IF NOT EXISTS `users` (
`id` bigint(20) NOT NULL,
`md5_id` varchar(200) NOT NULL DEFAULT '',
`full_name` tinytext,
`user_name` varchar(200) DEFAULT NULL,
`user_email` varchar(220) DEFAULT NULL,
`user_level` tinyint(4) NOT NULL DEFAULT '1',
`pwd` varchar(220) NOT NULL DEFAULT '',
`address` text COLLATE latin1_general_ci,
`country` varchar(200) DEFAULT NULL,
`tel` varchar(200) NOT NULL DEFAULT '',
`fax` varchar(200) DEFAULT NULL,
`website` text,
`date` date NOT NULL DEFAULT '0000-00-00',
`users_ip` varchar(200) NOT NULL DEFAULT '',
`approved` int(1) NOT NULL DEFAULT '0',
`activation_code` int(10) NOT NULL DEFAULT '0',
`banned` int(1) NOT NULL DEFAULT '0',
`ckey` varchar(220) NOT NULL DEFAULT '',
`ctime` varchar(220) NOT NULL DEFAULT '',
`location` tinyint(4) NOT NULL DEFAULT '9'
) ENGINE=MyISAM AUTO_INCREMENT=210 DEFAULT CHARSET=latin1;
ALTER TABLE `users` ADD PRIMARY KEY (`id`);
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=210;
Even now that I have REMOVED the UNIQUE index from the 'user_email' field, the error is STILL coming up. I REALLY don't understand that (Maybe something residual...? I'm just guessing).
Picture me with wads of hair in my hands. Can anyone please help?
UPDATE:
Here's the output from SHOW CREATE TABLE users
Here's the output from SHOW INDEX FROM users
Error message while editing:
Error message without using database name:
Output of: SHOW CREATE TABLE proctor.users

Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

I always get the error:
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
DROP TABLE IF EXISTS `characters`;
CREATE TABLE `characters`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`accId` int(11) NOT NULL,
`charId` int(11) NOT NULL,
`charType` int(11) NOT NULL,
`level` int(11) NOT NULL,
`exp` int(11) NOT NULL,
`fame` int(11) NOT NULL,
`items` varchar(128) NOT NULL,
`hp` int(11) NOT NULL,
`mp` int(11) NOT NULL,
`stats` varchar(64) NOT NULL DEFAULT '"1,2,3,4,5,6,7,8"',
`dead` tinyint(1) NOT NULL DEFAULT '0',
`tex1` int(11) NOT NULL DEFAULT '0',
`tex2` int(11) NOT NULL DEFAULT '0',
`pet` int(11) NOT NULL DEFAULT '0',
`fameStats` varchar(128) NOT NULL DEFAULT 'eNoVytkRgCAMRdGH4IIbgmsPdmNVNmZf5n7kzM0kksLjJN2V4b30vcHK1YYam9hCxxqh5zpQI0wwQ4IFMhRYYeNjpw444YIfA3kDIA',
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`totalFame` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
This is my first time using MySQL stuff. The reason I am using it because I downloaded a source for a game with server and this is needed.
The problem is isolated to these two lines
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
To get rid of the error, you need to modify one of those lines to remove the DEFAULT CURRENT_TIMESTAMP. MySQL only allows one timestamp column to be defined that way. For example:
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL,
Because the deathTime column is defined as NOT NULL and doesn't have a default, you'll need to supply a value for deathTime whenever you insert a row. You could get the current date and time assigned to that column for an INSERT using a BEFORE INSERT trigger.
You are trying to, well, define more than "one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause". Specifically, createTime and deathTime.
You Can't Do That.™

INSERT INTO using SET in SQL

My first post so bear with me.
I need to restore a DB Table from a backup. I have cleaned the SQL up so I only have the the DROP TABLE, CREATE TABLE and INSERT INTO commands. The query is failing when I run it in MYSQl on my server.
The error message is....
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 'id='2',
unitid='1',
optionname='Baby Chair',
customtype='0',
customfeild' at line 3
The query is.....
DROP TABLE IF EXISTS `jos_resman_options`;
CREATE TABLE `jos_resman_options`
(
`id` int(4) NOT NULL AUTO_INCREMENT,
`unitid` int(9) NOT NULL DEFAULT '0',
`optionname` varchar(255) NOT NULL DEFAULT '',
`customtype` tinyint(1) NOT NULL DEFAULT '0',
`customfeild` varchar(150) NOT NULL DEFAULT '',
`moreinformation` varchar(255) NOT NULL DEFAULT '',
`totaloptions` tinyint(1) NOT NULL DEFAULT '0',
`price` decimal(10,2) NOT NULL DEFAULT '0.00',
`minvalue` int(2) NOT NULL DEFAULT '0',
`maxvalue` int(2) NOT NULL DEFAULT '0',
`formobject` int(1) NOT NULL DEFAULT '0',
`enabled` tinyint(1) NOT NULL DEFAULT '0',
`priceoption` int(1) NOT NULL DEFAULT '0',
`taxfree` tinyint(1) NOT NULL DEFAULT '0',
`compulsory` tinyint(1) NOT NULL DEFAULT '0',
`istax` tinyint(1) NOT NULL DEFAULT '0',
`isinsure` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=75;
INSERT INTO `jos_resman_options`
VALUES
id='2',
unitid='1',
optionname='Baby Chair',
customtype='0',
customfeild='',
moreinformation='Baby Chair',
totaloptions='0',
price='0.00',
minvalue='1',
maxvalue='2',
formobject='1',
enabled='1',
priceoption='2',
taxfree='1',
compulsory='0',
istax='0',
isinsure='0';
The table is created, When the CREATE TABLE part runs on it's own there is no error message. I only see the error message when the INSERT INTO part of the query runs.
The INSERT INTO data is directly from a backup of the database and so the data SHOULD be OK??
Many Thanks for any input
Have Tried all of the suggestions...Changing the Auto INCREMENT to DEFAULT Field, Put in the Brackets, All of them fail with error messages. Here's the latest error message... Thanks again all.!
Error
SQL query:
INSERT INTO `jos_resman_options`
SET unitid = '1',
optionname = 'Baby Chair',
customtype = '0',
customfeild = '',
moreinformation = 'Baby Chair',
totaloptions = '0',
price = '0.00',
minvalue = '1',
maxvalue = '2',
formobject = '1',
enabled = '1',
priceoption = '2',
taxfree = '1',
compulsory = '0',
istax = '0',
isinsure = '0'
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 'maxvalue='2',
formobject='1',
enabled='1',
priceoption='2',
taxfree='1',' at line 12
INSERT INTO `jos_resman_options`
VALUES (
id='2',
unitid='1',
optionname='Baby Chair',
customtype='0',
customfeild='',
moreinformation='Baby Chair',
totaloptions='0',
price='0.00',
minvalue='1',
maxvalue='2',
formobject='1',
enabled='1',
priceoption='2',
taxfree='1',
compulsory='0',
istax='0',
isinsure='0');
try to delete id fields becouse id was defined AutoNumber.
So you can not insert a value because it handles the database engine, if you delete the columba will tell you different amount of columns to insert, then try putting id = DEFAULT or omit the id field

Mysql Query Search

I need to search columns (shown below) in a table called t_npc , for a specific value i but have failed many times.
example (does not work)
SELECT * FROM t_npc WHERE a_item_0 LIKE '%a_item_%' = 123;
SELECT * FROM t_npc WHERE 'a_item_0' <=> 'a_item_19' = 123;
SELECT a_index FROM t_npc WHERE a_item_0 LIKE 'a_item_%' = 123;
SELECT a_index, a_name FROM t_npc WHERE t_npc.a_item_0 or t_npc.a_item_1 or t_npc.a_item_2 or t_npc.a_item_4 = 44;
and many others to search the column names below for a value but it never works. I have tried wild cards with these columns still with not luck.
`a_item_0` int(11) NOT NULL DEFAULT '-1',
`a_item_1` int(11) NOT NULL DEFAULT '-1',
`a_item_2` int(11) NOT NULL DEFAULT '-1',
`a_item_3` int(11) NOT NULL DEFAULT '-1',
`a_item_4` int(11) NOT NULL DEFAULT '-1',
`a_item_5` int(11) NOT NULL DEFAULT '-1',
`a_item_6` int(11) NOT NULL DEFAULT '-1',
`a_item_7` int(11) NOT NULL DEFAULT '-1',
`a_item_8` int(11) NOT NULL DEFAULT '-1',
`a_item_9` int(11) NOT NULL DEFAULT '-1',
`a_item_10` int(11) NOT NULL DEFAULT '-1',
`a_item_11` int(11) NOT NULL DEFAULT '-1',
`a_item_12` int(11) NOT NULL DEFAULT '-1',
`a_item_13` int(11) NOT NULL DEFAULT '-1',
`a_item_14` int(11) NOT NULL DEFAULT '-1',
`a_item_15` int(11) NOT NULL DEFAULT '-1',
`a_item_16` int(11) NOT NULL DEFAULT '-1',
`a_item_17` int(11) NOT NULL DEFAULT '-1',
`a_item_18` int(11) NOT NULL DEFAULT '-1',
`a_item_19` int(11) NOT NULL DEFAULT '-1',
select value from table where field = 'value'
or
select value from table where field like '%value%'
You are doing both, which will not work. Provide more detail on what you want to accomplish, but from what you asked, that is why your query is not working.
You can, however do:
select value from table where field_a = 'valuea' AND field_b like '%valueb'
You should be using a different table structure.
Try something like this
CREATE TABLE npc (
Npc_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (Npc_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE item (
Npc_id bigint(20) unsigned NOT NULL,
Item_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
Item_value int(11) NOT NULL,
PRIMARY KEY (Item_id),
KEY Npc_id (Npc_id),
CONSTRAINT Item_ibfk_1 FOREIGN KEY (Npc_id) REFERENCES Npc (Npc_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The long way:
SELECT a_index, a_name FROM t_npc WHERE
t_npc.a_item_0 = 44 or t_npc.a_item_1 = 44 or t_npc.a_item_2 = 44
...
or t_npc.a_item19 = 44;
Slightly better you can use IN :
select a_index, a_name from t_npc where 44 in
(a_item_0, a_item_1, a_item_2, a_item3,
...
a_item18, a_item19);
You should also create an t_npc_items table instead of having so many fields on t_npc.
[You could generate the sql by querying the field names from the information_schema and then executing that command, but metasql makes unicorns cry.]

What's wrong with this MySQL INSERT query

I'm trying to insert the data below into an existing table, and it gives me sql error 1064, you have an error in your sql syntax at line 3.
INSERT INTO `static_contract` (`ID`, `contractID`, `name`, `mobbaseID`, `classID`, `dialogID`, `menuoptions`, `iconID`, `notes`, `vendorID`, `pTable`, `sTable`, `itemModTable`, `allowedBuildingTypeID`)
VALUES
(2026, 2026, 'Premium Vendor', 15312, 1906, 600, '1 2 15 18', 68, 'vendor', 0, '0', '0', '', 0),
Here's the table schema:
CREATE TABLE `static_contract` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`contractID` int(10) unsigned NOT NULL,
`name` varchar(100) NOT NULL DEFAULT '',
`mobbaseID` int(10) unsigned NOT NULL,
`classID` int(10) unsigned NOT NULL,
`dialogID` int(10) unsigned NOT NULL,
`menuoptions` varchar(20) NOT NULL DEFAULT '',
`iconID` tinyint(3) unsigned NOT NULL DEFAULT '0',
`notes` varchar(50) NOT NULL DEFAULT '',
`vendorID` int(10) NOT NULL DEFAULT '0',
`pTable` varchar(50) NOT NULL DEFAULT '0',
`sTable` varchar(50) NOT NULL DEFAULT '0',
`itemModTable` varchar(50) NOT NULL DEFAULT '',
`allowedBuildingTypeID` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `fk_contracts_mobbase` (`mobbaseID`)
) ENGINE=InnoDB AUTO_INCREMENT=302900 DEFAULT CHARSET=latin1
The comma outside of your parenthesis at the end is the problem. Please select an answer to mark this question as complete.
First off, I believe it is complaining about the comma at the end of your statement on line 3.
Second, you are putting a value into an auto-increment column ID.
Third, why are you specifying every column to insert into if you're inserting something in every column? Just do:
INSERT INTO static_contract VALUES (...)