Johan Känngård » MySQL http://johankanngard.net Useful tips for developers Thu, 19 May 2011 18:40:56 +0000 en hourly 1 http://wordpress.org/?v=3.0.1 MySQL Commands Cheat Sheet http://johankanngard.net/2008/07/31/mysql-commands-cheat-sheet/ http://johankanngard.net/2008/07/31/mysql-commands-cheat-sheet/#comments Thu, 31 Jul 2008 12:03:48 +0000 Johan Känngård http://johankanngard.net/?p=255 delete from TABLE where id=X Removes the specified row mysql> describe TABLE; Shows the structure of the specified table mysql> select * from TABLE; Shows all rows in the specified table :-O mysql> show procedure status; Shows stored procedures mysql> show table status; [...]]]> Another memory-dump. The manual contains more information (that’s a surprise :-).

mysql> delete from TABLE where id=X
Removes the specified row
mysql> describe TABLE;
Shows the structure of the specified table
mysql> select * from TABLE;
Shows all rows in the specified table :-O
mysql> show procedure status;
Shows stored procedures
mysql> show table status;
Shows tables in current database
mysql> use MYDATABASE;
Changes the database to act on
]]>
http://johankanngard.net/2008/07/31/mysql-commands-cheat-sheet/feed/ 0
MySQL 5 Stored Procedures Cheat Sheet http://johankanngard.net/2007/07/05/mysql-5-stored-procedures-cheat-sheet/ http://johankanngard.net/2007/07/05/mysql-5-stored-procedures-cheat-sheet/#comments Thu, 05 Jul 2007 10:37:57 +0000 Johan Känngård http://johankanngard.net/2007/07/05/mysql-5-stored-procedures-cheat-sheet/ SHOW PROCEDURE STATUS; Example output (with cropped dates and times for readable table): Db Name Type Definer Modified Created Security_type Comment mydb store_user PROCEDURE root 2007-0.. 2007-0.. DEFINER mydb store_cart PROCEDURE root 2007-0.. 2007-0.. [...]]]> I will add more when I stumble upon commands I can’t remember the syntax of.

To list all stored procedures:


mysql> SHOW PROCEDURE STATUS;

Example output (with cropped dates and times for readable table):

Db Name Type Definer Modified Created Security_type Comment
mydb store_user PROCEDURE root 2007-0.. 2007-0.. DEFINER
mydb store_cart PROCEDURE root 2007-0.. 2007-0.. DEFINER
mydb store_settings PROCEDURE root 2007-0.. 2007-0.. DEFINER


To create a new procedure


mysql> DELIMITER $$
mysql> CREATE PROCEDURE adduser(IN username VARCHAR(32), IN emailaddress VARCHAR(32))
BEGIN
INSERT INTO users VALUES (username, emailaddress);
END
$$

To show a stored procedure


mysql> SHOW CREATE PROCEDURE `userdb`.`adduser`\\\\G

Example output:

*************************** 1. row ***************************
Procedure: adduser
sql_mode:
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `adduser`(IN username varchar(32), IN emailaddress(32))
BEGIN
INSERT INTO users VALUES (username, emailaddress);
END
1 row in set (0.00 sec)

To call a stored procedure from Java


Connection con = DriverManager.getConnection(url, user, password);
CallableStatement statement = con.prepareCall(\\\"{call userdb.adduser(?, ?)}\\\");
int i = 0;
statement.setString(i++, \\\"myuser\\\");
statement.setString(i++, \\\"[email protected]\\\");
statement.execute();

For this to work you must give the user SELECT privilege to the proc table in the MySQL database, where the stored procedures are stored. Do it like this:

GRANT SELECT ON `mysql`.`proc` TO \\\'mysqluser\\\'@\\\'myhost\\\';

For a list of more commands, see my MySQL Commands Cheat Sheet.

]]>
http://johankanngard.net/2007/07/05/mysql-5-stored-procedures-cheat-sheet/feed/ 0