Total Pageviews

Tuesday, July 19, 2011

Mysql tricky stuff

1) Delete duplicate entry/data from mysql database.

delete from table1
USING table1, table1 as aliastable
WHERE (NOT table1.ID=aliastable.ID)
AND (table1.field_name=aliastable.field_name).
above query is a nice example of self-join.

2) Import large database files
run below command on command prompt.
mysql -p -u username database_name < file.sql

3) if you get following error
#1467 - Failed to read auto-increment value from storage engine
this happens because of 2 reasons your auto increment field length is too small
or it is a bug in mysql.
follow these steps
1)show table status;
if it shows a very large value for your auto increment column
use the query
ALTER TABLE table_name AUTO_INCREMENT =2009;

Wednesday, July 13, 2011

How to get last insert id in MYSQL

There are 3 ways of getting last insert in mysql

1) mysql_insert_id();
2) LAST_INSERT_ID();
3) my fetching max(id) from column, this is generally not a good method for high traffic websites