Archive

Posts Tagged ‘mysql’

Problem uploading big sql file via phpMyAdmin

November 11th, 2009 admin No comments

I faced problem uploading a huge sql file to my shared hosting server. The size was not that big. It was only 1.8MB in size with about 30,000 records but phpMyAdmin (provided in cpanel) still failed to fully upload and run the file.

I then googled for a solution.

One of the solutions suggest to use SSH but I’m not familiar with it.

Another solution is by using mysqldumper but the configuration seems quite tedious.

Then I found another solution called BigDump which is very straight forward. You upload a PHP script together with your huge sql file via FTP. Then just run the PHP script as usual. In a second all the data has been restored in your database.

SQL statement to list duplicate contents

July 10th, 2009 admin No comments

You will need this SQL statement to list all duplicate contents in a table

SELECT email,
COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) > 1 )

source

Categories: database Tags: , , ,

How to backup MySQL table in phpMyAdmin

July 9th, 2009 admin 2 comments

Sometimes when you are going to execute an SQL statement to your production database, you need to be very careful especially when you are using DELETE or UPDATE command.

To ensure your original data will be available, you can backup the whole table that you are going to do the operation.

To backup the whole table is very easy in phpMyAdmin (even in other SQL tool).

phpmyadmin

After you done with the operation and it is successful, you can easily drop the temporary backup table.

Categories: database Tags: , ,

Full mysql backup with cron job in cpanel

May 28th, 2009 admin No comments

This is how to do daily mysql backup with cron job in cpanel. This cron job will backup your mysql database as dump file (and gzip) daily. The files will be replaced on the same day next week.

Command to run to dump your mysql database, gzip it and replace it the following week as follow

1
mysqldump -uDBUSERNAME -pPASSWORD --opt DBNAME > /home/USER/dbbackup/FILENAME.sql; gzip -f /home/USER/dbbackup/FILENAME.sql

Replace DBUSERNAME, PASSWORD, DBNAME, USER, FILENAME respectively.

Insert into table with select statement

May 25th, 2009 admin No comments

Sometimes you may need to populate a table (usually a temporary or new table) from data of another table (usually huge table).

Here’s is the statement that you may use

INSERT INTO “table1″ (”column1″, “column2″, …)

SELECT “column3″, “column4″, …FROM “table2″

source

Categories: technology Tags: ,

How to fix Error with database: Table ‘…is marked as crashed and should be repaired

May 24th, 2009 admin No comments

I got this error message in my application

Error with database: Table ‘table_name’ is marked as crashed and should be repaired

Went to the net and look for the solution. The simplest one is to click on the “Repair Database” button provided in cpanel

scrshot_21

scrshot_11


Categories: technology Tags: ,

Updating multiple records in one sql statement (MySQL)

May 21st, 2009 admin No comments

If you want to update multiple records, usually you will have to do a loop and generate multiple SQL statement like this

UPDATE table_name SET fieldname = value1 WHERE fieldname = field_id1;
UPDATE table_name SET fieldname = value2 WHERE fieldname = field_id2;
UPDATE table_name SET fieldname = value3 WHERE fieldname = field_id3;

But to save some resource (perhaps), you can also update multiple records in just one single MySQL statement

UPDATE table_name
SET fieldname = CASE value_id
WHEN value_id1 THEN ‘value1′
WHEN value_id2 THEN ‘value2′
WHEN value_id3 THEN ‘value3′
END
WHERE value_id IN (value_id1,value_id2,value_id3)

Categories: technology Tags: ,

MySQL search and replace

May 20th, 2009 admin No comments

I’ve been doing data cleansing of one of my project. Then there was a need to find some abbreviation to be replaced by the real words.

Here I did with mysql statement.

update property set town = replace(town,’tmn’,'taman’)

In general:

update TABLENAME set FIELDNAME = replace (FIELDNAME, searchstr, replacestr)

Categories: technology Tags: ,