What's wrong with this MySQL INSERT query - mysql

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 (...)

Related

MySQL trigger is preventing INSERT if condition isn't met

I have a trigger before insert that replaces a string for a field with the new insert id. This is wrapped in a condition and works very well when the condition is met. However when an ordinary insert is attempted and the string isn't present the trigger stops the insert from executing at all, and yet there is nothing in the trigger to do this, any clues why this might be happening? Trigger below:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `delivery_management_vehicle_placeholder_title`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `delivery_management_vehicle_placeholder_title` BEFORE INSERT ON `delivery_management_vehicle`
FOR EACH ROW BEGIN
DECLARE id_delivery_management_vehicle INT DEFAULT 0;
DECLARE mask VARCHAR(10) DEFAULT '--id--';
IF LOCATE(mask, NEW.`title`) > 0 THEN
SELECT
`auto_increment` INTO id_delivery_management_vehicle
FROM
`information_schema`.`tables`
WHERE
`table_name` = 'delivery_management_vehicle'
AND
`table_schema` = DATABASE();
SET NEW.`title`=REPLACE(NEW.`title`, mask, id_delivery_management_vehicle);
END IF;
END;
$$
DELIMITER ;
The table structure is as follows:
CREATE TABLE `delivery_management_vehicle` (
`id_dmv` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_system_user` int(11) NOT NULL,
`id_organisation` int(11) NOT NULL DEFAULT '0',
`id_depth1` int(11) NOT NULL DEFAULT '0',
`id_depth2` int(11) NOT NULL DEFAULT '0',
`id_type` int(11) NOT NULL DEFAULT '1',
`id_approver` int(11) NOT NULL DEFAULT '0',
`id_author` int(11) NOT NULL DEFAULT '0',
`id_owner` int(11) NOT NULL DEFAULT '0',
`id_responsible` int(11) NOT NULL DEFAULT '0',
`id_administrator` int(11) NOT NULL DEFAULT '0',
`id_category` int(11) NOT NULL DEFAULT '1',
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`keywords` text NOT NULL,
`colour` char(6) DEFAULT NULL,
`scope` text NOT NULL,
`assumptions` text NOT NULL,
`objectives` text NOT NULL,
`reference` varchar(255) NOT NULL,
`source` varchar(255) NOT NULL DEFAULT '',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_updated` datetime NOT NULL,
`last_updated_by` int(11) NOT NULL,
`approved` enum('0','1') NOT NULL DEFAULT '0',
`priority` enum('undefined','low','medium','high','critical') NOT NULL DEFAULT 'undefined',
`approved_date` datetime NOT NULL,
`actual_start_date` date NOT NULL,
`planned_start_date` date NOT NULL,
`projected_start_date` date NOT NULL,
`planned_completion_date` date NOT NULL,
`projected_completion_date` date NOT NULL,
`actual_completion_date` date NOT NULL,
`tolerance` int(11) NOT NULL,
`tolerance_period` enum('days','weeks','months') NOT NULL,
`planned_implementation` bigint(19) DEFAULT NULL,
`planned_management` bigint(19) DEFAULT NULL,
`planned_budget` bigint(19) DEFAULT NULL,
`projected_management` bigint(19) DEFAULT NULL,
`projected_budget` bigint(19) DEFAULT NULL,
`actual_implementation` bigint(19) DEFAULT NULL,
`actual_management` bigint(19) DEFAULT NULL,
`actual_budget` bigint(19) DEFAULT NULL,
`financial_summary_tolerance` int(11) DEFAULT '0',
`use_rag` enum('D','R','A','G','B') NOT NULL DEFAULT 'D',
`mandatory` enum('0','1') NOT NULL DEFAULT '0',
`active` enum('0','1') NOT NULL DEFAULT '1',
PRIMARY KEY (`id_dmv`),
KEY `id_system_user` (`id_system_user`),
KEY `id_type` (`id_type`),
KEY `id_approver` (`id_approver`),
KEY `id_author` (`id_author`),
KEY `id_owner` (`id_owner`),
KEY `id_responsible` (`id_responsible`),
KEY `id_administrator` (`id_administrator`),
KEY `id_system_user_2` (`id_system_user`,`active`),
KEY `id_author_2` (`id_author`,`active`),
KEY `id_owner_2` (`id_owner`,`active`),
KEY `id_approver_2` (`id_approver`,`active`),
KEY `id_responsible_2` (`id_responsible`,`active`),
KEY `id_type_2` (`id_type`,`active`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT='Delivery Management Vehicle';
and the insert statement I am trying to execute is:
INSERT INTO `delivery_management_vehicle` (
`last_updated`,
`title`,
`reference`,
`description`,
`keywords`,
`source`,
`priority`,
`scope`,
`assumptions`,
`objectives`,
`use_rag`,
`id_category`,
`id_type`,
`id_organisation`,
`id_depth1`,
`id_depth2`,
`colour`,
`id_system_user`,
`actual_start_date`,
`planned_start_date`,
`projected_start_date`,
`planned_completion_date`,
`projected_completion_date`,
`tolerance`,
`tolerance_period`,
`last_updated_by`
) VALUES (
NOW(),'G Test 3','','','','','undefined','','','','D',1,1,0,0,0,NULL,'3','','','','','',0,'days','3');
Sorry the table declaration is huge, but the insert is only for certain columns.
So looking at the title field I am expecting this to just insert and the trigger bypasses because the string '--id--' does not exist in the title field.
Cheers
You are trying to insert '' into an INT column. You can't do that.

Data too long for column 'Subscribe_Specialty' at row 1 - Mysql error

Here is the insert statement -
INSERT INTO `Newsletter_Subscriber_Uploaded_Data`(`File_Id`, `First_Name`, `Last_Name`, `Email`,`Country`,`Country_Id`,`Subscribe_Specialty`,Is_Valid, `Subscription_Id`, `User_Id`, `Created_Date`)
VALUES (2, 'Adolphus','Bonar', 'a.bonar#endocrinology.org', 'United States','2', 'General Practice/Family Medicine', 1 ,20, '89',CURRENT_TIMESTAMP)
For column Subscribe_Specialty, the value I'm trying to insert is 'General Practice/Family Medicine' which has only 32 characters, but still it is giving me the above error.
Please help me guys, as this is a production error.
More Information:
Collation : utf8
Storage Engine : InnoDB
Below is the Table structure -
CREATE TABLE `Newsletter_Subscriber_Uploaded_Data` (
`File_Id` int(11) NOT NULL,
`Row_Id` int(11) NOT NULL AUTO_INCREMENT,
`First_Name` varchar(50) NOT NULL,
`Last_Name` varchar(50) NOT NULL,
`Email` varchar(100) DEFAULT NULL,
`Country` varchar(20) NOT NULL,
`Country_Id` int(11) NOT NULL DEFAULT '0',
`Subscribe_Specialty` varchar(100) NOT NULL,
`Is_Valid` tinyint(4) DEFAULT '0',
`Subscription_Id` int(100) NOT NULL DEFAULT '0',
`Duplicate` tinyint(4) DEFAULT '0',
`Subscriber` tinyint(4) DEFAULT '0',
`Created_Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`User_Id` int(11) NOT NULL COMMENT 'Panel user id',
PRIMARY KEY (`Row_Id`),
KEY `File_Id` (`File_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=21511 DEFAULT CHARSET=utf8;

#1054-Unknown column 'userid' in 'field list' in mysql. Getting error when i am importing a file containing this table in mysql through xampp

CREATE TABLE IF NOT EXISTS `user` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL DEFAULT '',
`password` varchar(100) NOT NULL DEFAULT '',
`authtype` varchar(20) NOT NULL DEFAULT 'mysql',
`disabled` int(1) NOT NULL DEFAULT '0',
`logonchange` int(1) NOT NULL DEFAULT '0',
`expiry` tinyint(1) NOT NULL DEFAULT '0',
`ldapdn` varchar(250) DEFAULT NULL,
`cookieid` varchar(32) DEFAULT NULL,
`lastchanged` int(11) DEFAULT '0',
`first_name` varchar(250) DEFAULT NULL,
`last_name` varchar(250) DEFAULT NULL,
`email` varchar(250) DEFAULT NULL,
`payroll_number` varchar(50) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2234 ;
INSERT INTO `user` (`userid`, `username`, `password`, `authtype`, `disabled`, `logonchange`, `expiry`, `ldapdn`, `cookieid`, `lastchanged`, `first_name`, `last_name`, `email`, `payroll_number`) VALUES
(1, 'administrator', '200ceb26807d6bf99fd6f4f0d1ca54d4', 'mysql', 0, 0, 0, '', 'e6f33989529daf141bd1406702b995fc', 1155190068, NULL, NULL, NULL, ''),
(2224, 'staff', '1253208465b1efa876f982d8a9e73eef', 'mysql', 0, 0, 0, 'NULL', '972b26e91f1a089cb0cafb16cbeb422c', 1156372109, NULL, NULL, NULL, ''),
(2233, 'guest', '', 'mysql', 0, 0, 0, NULL, NULL, 0, NULL, NULL, NULL, '');
I know it is late. :) See AUTO_INCREMENT=2234 in your code?
It says auto increment starts with 2234 which is correct for any further entries but when importing we are inserting with userid = 1 for the first entry which breaks the rule. This is the reason why you are getting error. You can remove the auto increment part from the create table statement and use the below statement after the Insert statements
ALTER TABLE user AUTO_INCREMENT = 2234;
Errors like: #1054-Unknown column 'xxxx' in 'field list' can be solved by simple editing the import file.

MySQL Extremely Slow Query

I am trying to learn about optimizations to MySQL, table engines and when to use them, etc.
I have a query that is running up against the time-out limit of 10 minutes and which needs to complete in seconds because its function is a user-generated report.
The Query:
SELECT em.employeeId, tsk.taskId
FROM employee em INNER JOIN
task tsk
ON tsk.employeeId = em.employeeId
WHERE em.employeeId <> 'Not Done'
AND tsk.employeeId (
SELECT employeeId FROM task
WHERE templateId
IN ( '5', '6', '7', '8' )
AND tsk.status = 'Done'
)
AND tsk.employeeId IN
(
SELECT employeeId FROM task
WHERE templateId IN
( '55', '56', '57', '58' )
AND status = 'Not Done'
)
Explain:
# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, PRIMARY, tsk, ALL, , , , , 61326, Using where
1, PRIMARY, em, eq_ref, PRIMARY, PRIMARY, 4, newhire.tsk.employeeId, 1, Using index
3, DEPENDENT SUBQUERY, task, ALL, , , , , 61326, Using where
2, DEPENDENT SUBQUERY, task, ALL, , , , , 61326, Using where
The DB server uses MyISAM as default, so most schemas including this one are MyISAM.
I also realize that the text searches (status=Done or status LIKE 'Done') are adding a lot to the query.
EDIT1:
# Table, Create Table
employee, CREATE TABLE `employee` (
`employeeId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lastName` varchar(255) NOT NULL,
`firstName` varchar(255) NOT NULL,
`applicantId` varchar(255) NOT NULL,
`fEmployeeId` varchar(255) DEFAULT NULL,
`opId` varchar(255) DEFAULT NULL,
`rehire` tinyint(3) unsigned NOT NULL DEFAULT '0',
`sDate` date DEFAULT NULL,
`oDate` date DEFAULT NULL,
`cDate` date DEFAULT NULL,
`additionalDate` date DEFAULT NULL,
`additionalType` varchar(255) DEFAULT NULL,
`processingDate` date DEFAULT NULL,
`created` datetime NOT NULL,
`recruiterId` int(10) unsigned NOT NULL,
`processorId` int(10) unsigned DEFAULT NULL,
`position` tinyint(3) unsigned NOT NULL DEFAULT '1',
`status` varchar(255) NOT NULL,
`campus` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`requisition` varchar(255) DEFAULT NULL,
`Position` varchar(255) DEFAULT NULL,
`department` varchar(255) DEFAULT NULL,
`jobClass` varchar(255) DEFAULT NULL,
`hiringManager` varchar(255) DEFAULT NULL,
`badge` varchar(255) DEFAULT NULL,
`currentAddress` varchar(255) DEFAULT NULL,
`holding` tinyint(3) unsigned DEFAULT '0',
PRIMARY KEY (`employeeId`)
) ENGINE=MyISAM AUTO_INCREMENT=3959 DEFAULT CHARSET=latin1
EDIT 2:
# Table, Create Table
task, CREATE TABLE `task` (
`taskId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`templateId` int(10) unsigned NOT NULL,
`employeeId` int(10) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`description` text,
`naAvailable` tinyint(3) unsigned DEFAULT '0',
`fileRequired` tinyint(3) unsigned DEFAULT '0',
`fileHrCatalog` int(10) unsigned DEFAULT NULL,
`quickFileName` varchar(255) DEFAULT NULL,
`fileUploaded` tinyint(3) unsigned DEFAULT '0',
`fileExt` varchar(255) DEFAULT NULL,
`level` tinyint(3) unsigned NOT NULL,
`status` varchar(255) NOT NULL,
`due` date DEFAULT NULL,
`daysDue` int(10) unsigned DEFAULT NULL,
`routeIncentives` tinyint(3) unsigned DEFAULT '0',
`requiresAudit` tinyint(3) unsigned DEFAULT '0',
`auditStatus` varchar(255) DEFAULT NULL,
`auditUser` int(10) unsigned DEFAULT NULL,
`auditDate` datetime DEFAULT NULL,
`stampOption` tinyint(3) unsigned DEFAULT '0',
`done` tinyint(3) unsigned DEFAULT '0',
`doneBy` int(10) unsigned DEFAULT NULL,
`doneWhen` datetime DEFAULT NULL,
`sortOrder` tinyint(3) unsigned NOT NULL DEFAULT '255',
PRIMARY KEY (`taskId`),
KEY `status` (`status`,`templateId`)
) ENGINE=MyISAM AUTO_INCREMENT=176802 DEFAULT CHARSET=latin1
I would write the query as below, but to help the optimization, have a covering indexes on your tables.
Employee table -- index on ( status, employeeID )
Task table -- index on ( employeeid, templateid, status )
By the first join, you are prequalifying to get the first task as a "Done" status.
The second join is looking for the OTHER task you are interested in that is NOT Done.
Doing subqueries (especially correlated sub queries) can be harder on performance. By doing a JOIN, it's either there or its not...
SELECT
em.employeeId,
tsk.taskId
FROM
employee em
INNER JOIN task tsk1
ON em.employeeId = tsk1.employeeId
AND tsk1.templateID in ( '5', '6', '7', '8' )
AND tsk1.status = 'Done'
INNER JOIN task tsk2
ON em.employeeId = tsk2.employeeId
AND tsk2.templateID in ( '55', '56', '57', '58' )
AND tsk2.status = 'Not Done'
WHERE
em.status <> 'Not Done'
Your first change should be to create an index on task that covers both the status and templateId columns:
ALTER TABLE task ADD INDEX (status, templateId);
That'll prevent the full-table scans of 61326 rows each time that table is accessed in your query.
Also, it looks like you might have made a typo here:
SELECT employeeId FROM task
WHERE templateId
IN ( '5', '6', '7', '8' )
AND tsk.status = 'Done'
That tsk.status should be just status like the 2nd subquery.

Getting a duplicate key error in MYSQL. No duplicate found

I have a table. (Code taken from table generation code, I did not write this)
DROP TABLE IF EXISTS `CatalogueBasket`;
CREATE TABLE `CatalogueBasket` (
`ID` int(11) NOT NULL auto_increment,
`Shopper` char(35) NOT NULL default '',
`ItemLink` int(11) NOT NULL default '0',
`Quantity` int(11) NOT NULL default '0',
`Created` datetime NOT NULL default '0000-00-00 00:00:00',
`ExpectedDelivery1` datetime default NULL,
`ExpectedDelivery2` datetime default NULL,
`Comments` char(255) default NULL,
`Status` int(10) unsigned default NULL,
`QuantityShipped` int(10) unsigned default NULL,
`HarmonyNumber` int(10) unsigned default NULL,
`StartDate` datetime default NULL,
KEY `ID` (`ID`),
KEY `Shopper` (`Shopper`),
KEY `ItemLink` (`ItemLink`),
KEY `Quantity` (`Quantity`),
KEY `Created` (`Created`)
) TYPE=MyISAM;
When trying to insert a new Row at the end of this table I am getting the following message.
Duplicate entry '116604' for key 1
The insert statement is:
INSERT INTO CatalogueBasket (Shopper,ItemLink,Quantity,Created, Status, StartDate)
VALUES ('0.80916300 1338507348',58825,1,'2012-06-01 09:58:23', 0, '0-0-0')
I'm assuming it is talking about the ID column.
If I run the following query I get 116603 as the last key
SELECT * FROM `CatalogueBasket` order by ID desc limit 1
Any insight / help into this is appreciated.