Tag Archives: mysql

Mysql autoincrement column that resets every month

By | July 26, 2020

The autoincrement feature in mysql allows us to create numeric fields that automatically increment by 1 for every row inserted. It does it atomically so we don’t have to worry about any duplicates or collision. However, there are situations when we need an autoincrement field to reset at regular intervals like every month or so…. Read More »

List foreign keys in mysql

By | September 5, 2009

information_schema The following query will list out the foreign keys in mysql. It finds it out from the information_schema database. select concat(table_name, ‘.’, column_name) as ‘foreign key’, concat(referenced_table_name, ‘.’, referenced_column_name) as ‘references’ from information_schema.key_column_usage where referenced_table_name is not null; The output is a clean table listing out all foreign keys from all databases +———————–+————-+ |… Read More »

Restore Mysql Database from a sql or zip file using PHP

By | May 1, 2009

In a previous post we learnt how to inside php. In this example the same zip file would be used to restore the database. Code // Function to restore from a file function restore($path) { $f = fopen(‘restore/temp.sql’ , ‘w+’); if(!$f) { echo "Error While Restoring Database"; return; } $zip = new ZipArchive(); if ($zip->open($path)… Read More »

Backup mysql database with php and zip it

By | May 1, 2009

Many php applications prefer to backup the mysql database from within the application and save it as an archive. The mysqldump commandline utility can be used to perform this function of backing up a mysql database as sql file. The command would be like this : mysqldump –user=$username –password=$password –opt $db_name > $dir/backup.sql The above… Read More »