If you have noticed there is an exclamatory sign (!) after the post title, let me tell you I have deliberately placed it to express my surprise on the whole thing which I’m going to share. I have been working with MySQL since early 2005, if I remember correctly it was version 4.1 which for the first time supported Sub Queries. I was a pure MySQL newbie back then and did not have any idea about how good or bad MySQL was. Honestly I switched from Microsoft Technologies to Open Source Technologies because my employer decided to switch to Open Source to avoid license costs, for obvious reasons I was skeptical about Free and Open Source MySQL and though very highly of MSSQL & MMC Console Microsoft offered! Since then till I faced the MySQL ORDER BY issue in Cloud I have used MySQL ORDER BY to solve all kinds of ranking problems, though I always knew MySQL sorting algorithm is not a great one, inside it uses qsort (quicksort) algorithm but if use the ORDER BY on a indexed field / key then it won’t cause much of an optimization issue. [Read more…]
A Few Useful MySQL Commands / Queries for Amateur DBAs – Part 1
I consider myself an amateur DBA because I’m not a professional MySQL DBA but at times I have to work with some MySQL Commands / Queries which are piece of cake for professional DBAs but require me to Google every time I need to use them. Currently some of them are lying in my GMail’s draft and some are lying in a Google Drive Doc and for the rest of them I still need to Google!! One Monday morning decided to create a ready reference out of those Commands / Queries and publish under my Blog for all future requirements and for other amateur MySQL DBAs out there in the World Wide Web!
1. Login to MySQL Command Prompt as root user
#mysql -uroot -p (this will require you to enter password after you press the return key)
OR
#mysql -uroot -prootpwd (here “rootpwd” is the password for root user)
Using the full MySQL Server path: /usr/local/mysql/bin/mysql -uroot -p OR /usr/local/mysql/bin/mysql -uroot -prootpwd
How to rename a stored procedure in MySQL?
If you have a situation in hand where you need to rename a STORED PROCEDURE in MySQL and not DROP it then there are 2 ways I could figure out to tackle it. Before I start describing the methods I would like to state why I needed to rename a STORED PROCEDURE in MySQL?
I always have a STORED PROCEDURE named as “x” in all my projects databases where I write temporary code to tackle a particular problem and when the experiment is successful I create a new STORED PROCEDURE and copy & paste code from “x” PROCEDURE. I was following this process for quite some time happily, but then came a situation where it looked time consuming and I needed a faster method!
I found Method 1 by Google-ing and Method 2 was my sudden discovery!
Method 1: Rename a STORED PROCEDURE
SET name = ‘<new_proc_name>’,
specific_name = ‘<new_proc_name>’
WHERE db = ‘<database>’ AND
name = ‘<old_proc_name>’;
So if I want to rename my “x” PROCEDURE to “new_x” the statement would look like the following:
UPDATE `mysql`.`proc`
SET name = ‘new_x’,
specific_name = ‘new_x’
WHERE db = ‘db_mydb’ AND
name = ‘x’;
Please Also note: If you have granted privileges to users for this procedure you will need to update the procedure name in procs_priv as well.
UPDATE `mysql`.`procs_priv`
SET Routine_name = ‘<new_proc_name>’
WHERE Db = ‘<database>’ AND
Routine_name = ‘<old_proc_name>’;
FLUSH PRIVILEGES;
You can execute the above queries from MySQL GUI Tool like MySQL Query Browser or MySQL Command Prompt.
Method 2: Rename a STORED PROCEDURE with a backup
This method is my sudden discovery and I figured it before I found Method 1 by Google-ing and I execute it in MySQL Query Browsers (needless to say it is still my favorite tool to operate MySQL!)
– Select the PROCEDURE “x” and press F2 or right click and select “Edit Procedure”
– PROCEDURE opens in the Query Browser Editor
– Modify DROP PROCEDURE IF EXISTS `db_mydbs`.`x` to DROP PROCEDURE IF EXISTS `db_mydbs`.`new_x`
– Modify CREATE PROCEDURE `db_mydb`.`x` () to CREATE PROCEDURE `db_mydb`.`new_x` ()
– Click to Continue button
The above steps create a new PROCEDURE named “new_x” and also keeps the PROCEDURE named “x” intact.
I generally use Method 2 because it lets me keep my handy “x” PROCEDURE intact and create another PROCEDURE with the same code in a mouse click. Method 2 may not sound very scientific or authentic but since MySQL respond to it quite nicely I guess it is not as bad :).
© Kamalika Guha Roy