MySql 1062 error that has me scratching my head - mysql

I created table as follows:
CREATE TABLE IF NOT EXISTS `products` (
`ID` tinyint(3) NOT NULL AUTO_INCREMENT,
`SKU` varchar(30) NOT NULL,
`Title` varchar(100) NOT NULL,
`Description` text NOT NULL,
`Price` decimal(3,2) NOT NULL,
`Image1` varchar(100) NOT NULL,
`Image2` varchar(100) NOT NULL,
`Keywords` varchar(150) NOT NULL,
`Shop` tinyint(2) NOT NULL,
`lmlCat` tinyint(3) NOT NULL,
`VinylCat` tinyint(3) NOT NULL,
`FancyCat` tinyint(3) NOT NULL,
`Active` tinyint(1) NOT NULL,
`SizeDescription` varchar(50) NOT NULL,
`Size` varchar(250) NOT NULL,
PRIMARY KEY (`ID`)
);
When importing data from CSV, it imports 127 lines then I get this error:
#1062 - Duplicate entry '127' for key 'PRIMARY'

You made the ID column a tinyint which can only take values from -127 to 127, larger values are truncated. Make it a regular int and things will work.

Related

Sql error syntaxes with phpmyadmin

Good day,
I have an issue using the phpmyadmin for my database, I'm in new in this and this is the mysql structure from previewmysql:
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM(0) NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15) NULL,
`marital_status` ENUM(0) NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM(0) NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM(0) NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
The error is:
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 '0) NOT NULL , `email` VARCHAR(20) NOT NULL , `phone` INT(15) NULL , `marital_sta' at line 1
And also attached is my phpmyadmin table structure.
Will appreciate any help.
Try it.Hope errors goes boom.I just fixed your errors. But your table structure is not good enough. give time, then day by day you will also be expert on it.
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM('0','1') NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15),
`marital_status` ENUM('0','1') NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM('0','1') NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM('0','1') NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
Modify ENUM declaration as ENUM ('male', 'female') for gender column and others also as shown in your table. It will not accept ENUM(0).
ENUM(0) is wrong format , if you want that for gender roles then you can use :-
ENUM('Male', 'Female') i.e you can run this query :-
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM('Male', 'Female') NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15) NULL,
`marital_status` ENUM('Single','Married','divorced') NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM('no','yes') NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM('Cape Town','Woodstock') NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
You have used ENUM data type in your table. And provided 0 as argument but
An enumeration value must be a quoted string literal
You can refer the mySql documentation for more information
http://dev.mysql.com/doc/refman/5.7/en/enum.html

Mysql error 1215 - Cannot add foreign key constraint

Created these two tables successfully
First table
CREATE TABLE IF NOT EXISTS `lawncare_user` (
`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`FirstName` varchar(255) NOT NULL,
`LastName` varchar(255) NOT NULL,
`Email` varchar(255) NOT NULL,
`UserType` varchar(30) NOT NULL,
`UserName` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
`AddedBy` int(11) NOT NULL,
`AddedOn` date NOT NULL,
`ModifiedOn` date DEFAULT NULL,
`Status` BOOLEAN NOT NULL DEFAULT '0',
`QuestionID` int(11) DEFAULT NULL,
`QuestionAnswer` text DEFAULT NULL,
`Params` text NOT NULL,
`Address` text NOT NULL,
`Country` varchar(300) NOT NULL,
`State` varchar(300) NOT NULL,
`City` varchar(300) NOT NULL,
`ContactNo` double DEFAULT NULL,
`Activation` BOOLEAN NOT NULL DEFAULT '0',
`ActivatedOn` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Second table
CREATE TABLE IF NOT EXISTS `lawncare_customer` (
`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`FirstName` varchar(255) NOT NULL,
`LastName` varchar(255) NOT NULL,
`Email` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
`ContactNo` varchar(20) NOT NULL,
`Address` varchar(255) NOT NULL,
`Params` text NOT NULL,
`Province` varchar(255) NOT NULL,
`ZipCode` varchar(255) NOT NULL,
`Status` Boolean NOT NULL DEFAULT '0',
`AddedBy` int(11) NOT NULL,
`AddedOn` date NOT NULL,
`ModifiedOn` date DEFAULT NULL
) ENGINE =InnoDB DEFAULT CHARSET=latin1;
But while creating third table as
CREATE TABLE IF NOT EXISTS `lawncare_message` (
`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Reason` int(5) NOT NULL,
`Subject` text NOT NULL,
`Description` text NOT NULL,
`Customer` int(11) NOT NULL,
`CustomerUser` varchar(255) NOT NULL,
`CustomerEmail` varchar(255) NOT NULL,
`SendTo` int(11) NOT NULL,
`SendToUser` varchar(255) NOT NULL,
`SendToEmail` varchar(255) NOT NULL,
`Status` int(5) NOT NULL DEFAULT '0',
`AddedBy` int(11) NOT NULL,
`AddedOn` date NOT NULL
FOREIGN KEY (SendTo, SendToUser, SendToEmail)
REFERENCES lawncare_user(ID, UserName, Email)
ON UPDATE CASCADE ,
FOREIGN KEY (Customer, CustomerUser, CustomerEmail)
REFERENCES lawncare_customer(ID, FirstName,Email)
ON UPDATE CASCADE
) ENGINE =InnoDB DEFAULT CHARSET=latin1
I get #1215 - Cannot add foreign key constraint , error in mysql tried adding foreign keys after creating table but it still gives the same error. I don't know what I'm doing wrong here.
First of all Check whether you have applied indexes on the keys.
As per your code their is no point in referencing id,UserName and Email.
Only id is enough for referencing.
Check the following code
CREATE TABLE IF NOT EXISTS `lawncare_message` (
`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Reason` int(5) NOT NULL,
`Subject` text NOT NULL,
`Description` text NOT NULL,
`Customer` int(11) NOT NULL,
`CustomerUser` varchar(255) NOT NULL,
`CustomerEmail` varchar(255) NOT NULL,
`SendTo` int(11) NOT NULL,
`SendToUser` varchar(255) NOT NULL,
`SendToEmail` varchar(255) NOT NULL,
`Status` int(5) NOT NULL DEFAULT '0',
`AddedBy` int(11) NOT NULL,
`AddedOn` date NOT NULL,
FOREIGN KEY (SendTo)
REFERENCES lawncare_user(ID)
ON UPDATE CASCADE ,
FOREIGN KEY (Customer)
REFERENCES lawncare_customer(ID)
ON UPDATE CASCADE
) ENGINE =InnoDB DEFAULT CHARSET=latin1

MySQL SELECT query with joins takes too long

I have the following SELECT query with table joins and it taking about a minute to return 6 records:
SELECT * FROM specimen, topography_index, morphology, specimen_image_lookup, image
WHERE
SUBSTRING(specimen.topography_index, 2, 2) = topography_index.topography_index_code
AND
morphology.morphology_code = specimen.snop_code
AND
specimen_image_lookup.specimen_fk = specimen.specimen_pk
AND
image.image_pk = specimen_image_lookup.image_fk
AND
specimen.topography_index, 2, 2) IN('".implode("','",$system)."')
Any ideas what I should here?
Table structures are:
CREATE TABLE `specimen` (
`specimen_pk` int(4) NOT NULL AUTO_INCREMENT,
`number` varchar(20) NOT NULL,
`unit_number` varchar(10) NOT NULL,
`topography_index` varchar(5) NOT NULL DEFAULT '',
`snop_axis` char(1) NOT NULL,
`snop_code` varchar(4) NOT NULL,
`example` int(2) NOT NULL,
`gender` char(1) NOT NULL,
`age` varchar(3) NOT NULL DEFAULT 'NA',
`clinical_history` text NOT NULL,
`specimen` text NOT NULL,
`macroscopic` text NOT NULL,
`microscopic` text NOT NULL,
`conclusion` text NOT NULL,
`comment` text NOT NULL,
`room` char(1) NOT NULL,
`position` varchar(8) NOT NULL,
`created` datetime NOT NULL,
`created_by` int(3) NOT NULL,
`updated` datetime NOT NULL,
`updated_by` int(3) NOT NULL,
PRIMARY KEY (`specimen_pk`),
FULLTEXT KEY `clinical_history` (`clinical_history`),
FULLTEXT KEY `specimen` (`specimen`),
FULLTEXT KEY `macroscopic` (`macroscopic`),
FULLTEXT KEY `microscopic` (`microscopic`),
FULLTEXT KEY `conclusion` (`conclusion`),
FULLTEXT KEY `comment` (`comment`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=500 ;
CREATE TABLE `topography_index` (
`topography_index_pk` int(3) NOT NULL AUTO_INCREMENT,
`topography_index_code` varchar(2) DEFAULT NULL,
`topography_index_nomen` text,
PRIMARY KEY (`topography_index_pk`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=138 ;
CREATE TABLE `specimen_image_lookup` (
`specimen_image_lookup_pk` int(8) NOT NULL AUTO_INCREMENT,
`specimen_fk` int(4) NOT NULL,
`image_fk` int(4) NOT NULL,
PRIMARY KEY (`specimen_image_lookup_pk`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=141 ;
CREATE TABLE `morphology` (
`morphology_pk` int(6) NOT NULL AUTO_INCREMENT,
`morphology_code` varchar(4) NOT NULL,
`morphology_nomen` varchar(120) NOT NULL,
PRIMARY KEY (`morphology_pk`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2295 ;
CREATE TABLE `image` (
`image_pk` int(4) NOT NULL AUTO_INCREMENT,
`image_title` varchar(80) NOT NULL,
`image_description` text NOT NULL,
`image_thumbnail` varchar(100) NOT NULL,
`image_small` varchar(100) NOT NULL,
`image_large` varchar(100) NOT NULL,
`created` datetime NOT NULL,
`created_by` int(3) NOT NULL,
`updated` datetime NOT NULL,
`updated_by` int(3) NOT NULL,
PRIMARY KEY (`image_pk`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=138 ;
By performing a substring on specimen.topography_index, you're asking the database to perform that calculation on every row in the specimen table before finding if the value exists in topography_index. One way to address this is to store the actual integer value that will match with topography_index, rather than a string with that value embedded.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'xxx' for key 'group_key'

On my web page there is no insert or update but already to this I'm getting this error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0-Bekoo-2449.10' for key 'group_key'
This is surprising because the web page does not modify any rows, and the Product table does not have a group_key column or key.
My query :
SELECT Product._like,
comment_count,
title,
price_lower,
price,image,
AffiliateOffers.name,
p‌​‌​ayout_yuzde,
payout_nakit,
payout_type,
xml_id,
brand,model,
currency,url,
Product.af‌​f_‌​id,
Product.offer_id,
pb_share_1,
pb_share_2,
pb_share_1_payda,
pb_share_2_payda,
A‌​ffil‌​iateOffers.seo_title,
afo_offer_id,
r_category,
seo_description
FROM Product
inner Join AffiliateOffers on Product.aff_id = AffiliateOffers.af_id
AND Product.offer_id = AffiliateOffers.af_offer_id
where Product.status = 1
and Product.aff_id = 1
and Product.offer_id = 1290
and my table structure :
` CREATE TABLE `_product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`xml_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`aff_id` int(11) NOT NULL,
`offer_id` int(11) NOT NULL,
`r_category` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`category_id1` int(11) NOT NULL,
`category_id2` int(11) NOT NULL,
`category_id3` int(11) NOT NULL,
`brand` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`model` varchar(75) COLLATE utf8_unicode_ci DEFAULT NULL,
`title` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`description_1` mediumtext COLLATE utf8_unicode_ci,
`price` decimal(18,2) NOT NULL,
`price_lower` decimal(18,2) NOT NULL,
`percentage_lower` int(11) NOT NULL,
`image` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`url` text COLLATE utf8_unicode_ci,
`feature` tinyint(4) NOT NULL,
`city` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`sex` tinyint(4) NOT NULL,
`stock` tinyint(4) NOT NULL,
`start_date` int(25) NOT NULL,
`finish_date` int(25) NOT NULL,
`update_date` int(25) NOT NULL,
`status` tinyint(4) NOT NULL DEFAULT '1',
`hit` int(11) NOT NULL DEFAULT '1',
`_like` int(11) NOT NULL DEFAULT '0',
`comment_count` int(11) DEFAULT NULL,
`like_count` int(11) DEFAULT NULL,
`lastlike_time` int(25) DEFAULT NULL,
`visit_count` int(11) DEFAULT NULL,
`lastvisit_count` int(25) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `xml_id` (`xml_id`),
KEY `lastlike_time` (`lastlike_time`),
KEY `offer_id` (`offer_id`),
KEY `r_category` (`r_category`),
KEY `status` (`status`)
) ENGINE=MyISAM AUTO_INCREMENT=3816867 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`
Why am I getting this error?
Unless you say otherwise, group_key is an internal field used by MySQL when aggregating things so you won't find it in your own tables or view. A duplicate entry error in there means you've got some sort of problem with your aggregation.
Have a look at any GROUP BY statements you're using, and ensure you've got their syntax correct, particularly in any aggregate functions and group expressions.
In Phpmyadmin you have to truncate all the logtables.
That should fix it.
I ran into the same problem, i was forgotten to clear the visitor and online log.
Both of the tables are generate a log/id for the user ( thats where the XXX for stands )
If you truncate both of the tables it will fix this problem.

How to optimize a MySQL JOIN Query

I have this MySQL query that I want to optimize:
SELECT r.WarehouseLocation,sum(sir.qty)
FROM repairableissue as r
INNER JOIN SIR ON r.sirno=sir.sirno
AND r.region=sir.region
AND r.ItemName=sir.Itemdesc
AND r.SerialNo=sir.Serialno
WHERE r.status='Pending'
GROUP BY r.warehouseLocation
How do I optimize this query? I read about optimization and found out that indexes might help but still could not achieve the desired performance.
Which index should be used and which should be removed?
Below is the explain of query:
Repairableissue
CREATE TABLE `repairableissue` (
`Vendor` varchar(40) NOT NULL,
`ItemName` varchar(200) NOT NULL,
`SerialNo` varchar(50) NOT NULL,
`person` varchar(200) NOT NULL,
`siteid` varchar(10) NOT NULL,
`invuser` varchar(50) NOT NULL,
`region` varchar(50) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`Dated` date NOT NULL,
`Sirno` varchar(50) NOT NULL,
`status` varchar(30) NOT NULL DEFAULT 'Pending',
`trackthrough` varchar(30) NOT NULL,
`reason` varchar(100) NOT NULL,
`ckh` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`WarehouseType` varchar(20) NOT NULL,
`WarehouseLocation` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `I1` (`status`),
KEY `ind2` (`ItemName`),
KEY `ind3` (`region`),
KEY `ind5` (`SerialNo`),
KEY `ind4` (`Sirno`)
) ENGINE=MyISAM AUTO_INCREMENT=63029 DEFAULT CHARSET=latin1
sir
CREATE TABLE `sir` (
`SirNo` varchar(50) NOT NULL,
`SiteId` varchar(80) NOT NULL,
`Vendor` varchar(70) NOT NULL,
`Type` varchar(15) NOT NULL,
`ItemDesc` varchar(200) NOT NULL,
`ItemCode` varchar(25) NOT NULL,
`SerialNo` varchar(50) NOT NULL,
`Unit` varchar(15) NOT NULL,
`AssetCode` varchar(50) NOT NULL,
`Qty` decimal(11,0) NOT NULL,
`Region` varchar(15) NOT NULL,
`Status` varchar(20) NOT NULL DEFAULT 'Installed',
`FaultInfo` varchar(100) NOT NULL DEFAULT 'date()',
`chk` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`Phase` varchar(15) NOT NULL,
`Category` varchar(200) NOT NULL,
`Issue_Vendor` varchar(30) NOT NULL,
`AssetName` varchar(150) NOT NULL,
`Ownership` varchar(20) NOT NULL,
`Dated` date NOT NULL,
`PersonName` varchar(150) NOT NULL,
`Remarks` varchar(300) NOT NULL,
`po` varchar(100) NOT NULL,
`invuser` varchar(50) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`grnno` varchar(30) NOT NULL,
`WarehouseType` varchar(20) NOT NULL,
`WarehouseLocation` varchar(20) NOT NULL,
`mainpartserial` varchar(200) NOT NULL,
PRIMARY KEY (`Vendor`,`Type`,`ItemCode`,`ItemDesc`,`SerialNo`,`Ownership`,`SirNo`,`Region`,`WarehouseType`,`WarehouseLocation`,`po`,`Qty`,`id`),
KEY `id` (`id`),
KEY `ind4` (`ItemDesc`),
KEY `ind6` (`SerialNo`),
KEY `ind7` (`SerialNo`)
) ENGINE=MyISAM AUTO_INCREMENT=228007 DEFAULT CHARSET=latin1
One multi-column index on r.status + r.warehouseLocation, in that order.
One multi-column index on sir.sirno + sir.region + sir.Itemdesc + sir.Serialno, in order of most cardinality to least cardinality, with sir.qty tacked on the end.
This assumes the fields are small enough to fit (combined) into an index.
Still, join seeks are unavoidable. The number of records that match r.status='Pending' is going to dictate the speed of this query.