I faced a very wierd problem that in my db i set my id as primary key and it stores those ids too that are identical e.g:
+--------+ | ID | +--------+ | ABC94 | | ABC94 | +--------+
When I deleted them then from phpmyadmin, then it generates following queries for deletion in which you can see one id contains \r which makes its unique but is not visible on select view. Kindly let me know how can i make sure when ids are getting saved then it should only take ABC94 part and ignore any parts like \r or \d or the like . Thanks,
DELETE FROM `Db` WHERE `ID` = 'ABC94\r';
DELETE FROM `DB` WHERE `ID` = 'ABC94';
I tried following code but it gave me error due to \ i guess cause it worked fine when i used - instead of \
SELECT SUBSTRING_INDEX( "ABC94\r", "\", 1 )
If there is any possibility of extra white space, you must handle it either in your application code before insert, or trim it off with the RDBMS at INSERT time.
You cannot update your existing rows without key violations unless you devise a scheme of replacing whites pace with other printable characters.
For example, replace the carriage return characters with
x, and they will still be unique:http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_trim
Your attempt with
SUBSTRING_INDEX()was nearly valid, but you should have omitted the backslash and instead supplied the empty string''.But using
TRIM()as I have above is more reliable. If at all possible, the best approach is to fix the source of the input data to avoid generating these in the first place.