Prepared Statements Mysql - mysql

I have a prepared statement I want to execute direct to the MySQL server;
USE `testdb`;
SET #t = (
SELECT GROUP_CONCAT(mytab.TABLE_NAME SEPARATOR ', ')
FROM (SELECT * FROM information_schema.TABLES as `m` WHERE table_schema = 'testdb' AND table_name LIKE 'mi_%' LIMIT 0,1) as `mytab`
);
PREPARE `stmt1` FROM 'DROP TABLE t';
EXECUTE `stmt1` USING #t;
DEALLOCATE PREPARE `stmt1`;
However, when I execute it returns an error on the EXECUTE line saying
Error Code: 1210. Incorrect arguments to EXECUTE
I would appreciate any help on what I'm doing wrong.
I have mysql version 5.1.68

Your issue can be resolved with preparing fully static DDL before executing. Sample:
Tables:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a |
| b |
+----------------+
2 rows in set (0.00 sec)
And your (little modified) SQL:
mysql> SET #t = (SELECT GROUP_CONCAT(mytab.TABLE_NAME SEPARATOR ', ') FROM (SELECT * FROM information_schema.TABLES as `m` WHERE table_schema = 'test'
AND table_name LIKE 'a%' LIMIT 0,1) as `mytab`);
Query OK, 0 rows affected (0.06 sec)
mysql> select #t;
+------+
| #t |
+------+
| a |
+------+
1 row in set (0.00 sec)
Now, DDL:
mysql> set #drop = CONCAT('DROP TABLE ', #t);
Query OK, 0 rows affected (0.00 sec)
mysql> select #drop;
+--------------+
| #drop |
+--------------+
| DROP TABLE a |
+--------------+
1 row in set (0.00 sec)
And, finally, prepared statement:
mysql> PREPARE `stmt1` FROM #drop;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE `stmt1`;
Query OK, 0 rows affected (0.00 sec)
You'll get:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| b |
+----------------+
1 row in set (0.00 sec)
You can use only one variable (I've added #drop to increase readability)

Related

how to use mysql rewriter plugin?

I am trying to use mysql default rewrite plugin as per their instructions but it is giving me some grief. Simply put after activating rules the plugin is simply not working.
Below is my table and rules but simply queries are not reporting correct results. Can someone tell me what am i doing wrong?
Thanks.
mysql> select creditcard_number from mihir_test.creditcard_info where name = "mihir";
+-------------------+
| creditcard_number |
+-------------------+
| 1234567890123456 |
+-------------------+
1 row in set, 1 warning (0.00 sec)
mysql> insert into query_rewrite.rewrite_rules ( pattern, replacement) values ( 'select creditcard_number from mihir_test.creditcard_info where name = ?', 'select name, creditcart_number from mihir_test.creditcard_info where name = ?' );
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql>
mysql> CALL query_rewrite.flush_rewrite_rules();
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> select creditcard_number from mihir_test.creditcard_info where name = "mihir"; +-------------------+
| creditcard_number |
+-------------------+
| 1234567890123456 |
+-------------------+
1 row in set, 1 warning (0.00 sec)
mysql>

Any way of using fully qualified table name with CREATE TABLE

When I use CREATE TABLE tbl_name I can only specify a table in database I currently use.
Is there any way to CREATE TABLE tbl_name in database1 without a prior USE database1?
Try:
mysql> CREATE DATABASE `test_1`;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE `test_2`;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> CREATE TABLE `test_1`.`table_1` (`column_1` BOOL);
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE `test_2`.`table_2` (`column_1` BOOL);
Query OK, 0 rows affected (0.01 sec)
mysql> USE `test_1`;
Database changed
mysql> SHOW TABLES;
+------------------+
| Tables_in_test_1 |
+------------------+
| table_1 |
+------------------+
1 row in set (0.00 sec)
mysql> USE `test_2`;
Database changed
mysql> SHOW TABLES;
+------------------+
| Tables_in_test_2 |
+------------------+
| table_2 |
+------------------+
1 row in set (0.00 sec)

MySQL database() vs database name

I constructed a query where i use:
where table_schema=database()
but if i replace database() with the database-name i get an empty resultset.
Why is that?
Thank you in advance!
EDIT:
The whole query:
SELECT table_name from information_schema.tables where
table_schema=database()
the databasename is
training
I can't reproduce the problem.
mysql> CREATE DATABASE `training`;
Query OK, 1 row affected (0.00 sec)
mysql> USE `training`;
Database changed
mysql> CREATE TABLE `test_table`(`c0` INTEGER);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT
-> `table_name`
-> FROM
-> `information_schema`.`TABLES`
-> WHERE
-> `table_schema` = DATABASE();
+------------+
| table_name |
+------------+
| test_table |
+------------+
1 row in set (0.00 sec)
mysql> SELECT
-> `table_name`
-> FROM
-> `information_schema`.`TABLES`
-> WHERE
-> `table_schema` = 'training';
+------------+
| table_name |
+------------+
| test_table |
+------------+
1 row in set (0.00 sec)

Delete rows from one table in multiple databases?

I have multiple databases, and each of the databases have some tables in common. I'd like to do something like DELETE FROM *.table_name WHERE col_name LIKE 'value'; followed by OPTIMIZE TABLE *.table_name;
Is this possible with a single MYSQL query, or will I need to run these queries individually across each database?
If you have three databases with the table mytable, this might do it
DELETE A.*,B.*,C.*
FROM db1.mytable A,db2.mytable B,db3.mytable C
WHERE A.col_name LIKE 'value'
AND B.col_name LIKE 'value'
AND C.col_name LIKE 'value';
OPTIMIZE TABLE db1.mytable,db2.mytable,db3.mytable;
Give it a Try !!!
UPDATE 2013-05-31 18:27 EDT
I have a solution based on the INFORMATION_SCHEMA database and Dynamic SQL.
SOLUTION
USE db1
SET #GivenTable = 'mytb';
SET #GivenCol = 'dat';
SET #GivenValue = 'Burger King';
SET #x = 0;
SELECT GROUP_CONCAT(dbtb) INTO #OptimizeTableList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(#x:=#X+1) num,column_name col
FROM information_schema.columns WHERE table_name=#GivenTable
and column_name=#GivenCol) AAA) AA;
SET #x = 0;
SELECT GROUP_CONCAT(dbalias,'.*') INTO #DeleteTableList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(#x:=#X+1) num,column_name col
FROM information_schema.columns WHERE table_name=#GivenTable
and column_name=#GivenCol) AAA) AA;
SET #x = 0;
SELECT GROUP_CONCAT(dbtb,' AS ',dbalias) INTO #FromList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(#x:=#X+1) num,column_name col
FROM information_schema.columns WHERE table_name=#GivenTable
and column_name=#GivenCol) AAA) AA;
SET #x = 0;
SELECT GROUP_CONCAT(CONCAT(dbalias,'.',col,'=',
QUOTE(#GivenValue)) SEPARATOR ' AND ') INTO #WhereClause
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias,col
FROM (SELECT table_schema db,table_name tb,(#x:=#X+1) num,column_name col
FROM information_schema.columns WHERE table_name=#GivenTable
and column_name=#GivenCol) AAA) AA;
SET #OptTableSQL = CONCAT('OPTIMIZE TABLE ',#OptimizeTableList);
SET #DeleteSQL = CONCAT('DELETE ',#DeleteTableList,' FROM ',#FromList,' WHERE ',#WhereClause);
PREPARE st FROM #DeleteSQL; EXECUTE st; DEALLOCATE PREPARE st;
PREPARE st FROM #OptTableSQL; EXECUTE st; DEALLOCATE PREPARE st;
SELECT * FROM db1.mytb;
SELECT * FROM db2.mytb;
SELECT * FROM db3.mytb;
SAMPLE DATA
DROP DATABASE IF EXISTS db1;
DROP DATABASE IF EXISTS db2;
DROP DATABASE IF EXISTS db3;
CREATE DATABASE db1;
CREATE DATABASE db2;
CREATE DATABASE db3;
CREATE TABLE db1.mytb
(id int not null auto_increment,
dat varchar(20),
primary key (id),key (dat));
INSERT INTO db1.mytb (dat) VALUES
('McDonald''s'),('Wendy''s'),
('Burger King'),('Jack in the Box');
CREATE TABLE db2.mytb LIKE db1.mytb;
CREATE TABLE db3.mytb LIKE db1.mytb;
INSERT INTO db2.mytb (dat) SELECT dat FROM db1.mytb;
INSERT INTO db3.mytb (dat) SELECT dat FROM db1.mytb;
SELECT * FROM db1.mytb;
SELECT * FROM db2.mytb;
SELECT * FROM db3.mytb;
SAMPLE DATA LOADED
mysql> DROP DATABASE IF EXISTS db1;
Query OK, 1 row affected (0.00 sec)
mysql> DROP DATABASE IF EXISTS db2;
Query OK, 1 row affected (0.00 sec)
mysql> DROP DATABASE IF EXISTS db3;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE db2;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE db3;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE TABLE db1.mytb
-> (id int not null auto_increment,
-> dat varchar(20),
-> primary key (id),key (dat));
Query OK, 0 rows affected (0.08 sec)
mysql> INSERT INTO db1.mytb (dat) VALUES
-> ('McDonald''s'),('Wendy''s'),
-> ('Burger King'),('Jack in the Box');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> CREATE TABLE db2.mytb LIKE db1.mytb;
Query OK, 0 rows affected (0.06 sec)
mysql> CREATE TABLE db3.mytb LIKE db1.mytb;
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO db2.mytb (dat) SELECT dat FROM db1.mytb;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> INSERT INTO db3.mytb (dat) SELECT dat FROM db1.mytb;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM db1.mytb;
+----+-----------------+
| id | dat |
+----+-----------------+
| 1 | McDonald's |
| 2 | Wendy's |
| 3 | Burger King |
| 4 | Jack in the Box |
+----+-----------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM db2.mytb;
+----+-----------------+
| id | dat |
+----+-----------------+
| 1 | Burger King |
| 2 | Jack in the Box |
| 3 | McDonald's |
| 4 | Wendy's |
+----+-----------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM db3.mytb;
+----+-----------------+
| id | dat |
+----+-----------------+
| 1 | Burger King |
| 2 | Jack in the Box |
| 3 | McDonald's |
| 4 | Wendy's |
+----+-----------------+
4 rows in set (0.00 sec)
mysql>
SOLUTION EXECUTED
mysql> USE db1
Database changed
mysql> SET #GivenTable = 'mytb';
Query OK, 0 rows affected (0.00 sec)
mysql> SET #GivenCol = 'dat';
Query OK, 0 rows affected (0.00 sec)
mysql> SET #GivenValue = 'Burger King';
Query OK, 0 rows affected (0.00 sec)
mysql> SET #x = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT GROUP_CONCAT(dbtb) INTO #OptimizeTableList
-> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
-> FROM (SELECT table_schema db,table_name tb,(#x:=#X+1) num,column_name col
-> FROM information_schema.columns WHERE table_name=#GivenTable
-> and column_name=#GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)
mysql> SET #x = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT GROUP_CONCAT(dbalias,'.*') INTO #DeleteTableList
-> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
-> FROM (SELECT table_schema db,table_name tb,(#x:=#X+1) num,column_name col
-> FROM information_schema.columns WHERE table_name=#GivenTable
-> and column_name=#GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)
mysql> SET #x = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT GROUP_CONCAT(dbtb,' AS ',dbalias) INTO #FromList
-> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
-> FROM (SELECT table_schema db,table_name tb,(#x:=#X+1) num,column_name col
-> FROM information_schema.columns WHERE table_name=#GivenTable
-> and column_name=#GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)
mysql> SET #x = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT GROUP_CONCAT(CONCAT(dbalias,'.',col,'=',
-> QUOTE(#GivenValue)) SEPARATOR ' AND ') INTO #WhereClause
-> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias,col
-> FROM (SELECT table_schema db,table_name tb,(#x:=#X+1) num,column_name col
-> FROM information_schema.columns WHERE table_name=#GivenTable
-> and column_name=#GivenCol) AAA) AA;
Query OK, 1 row affected (0.03 sec)
mysql> SET #OptTableSQL = CONCAT('OPTIMIZE TABLE ',#OptimizeTableList);
Query OK, 0 rows affected (0.00 sec)
mysql> SET #DeleteSQL = CONCAT('DELETE ',#DeleteTableList,' FROM ',#FromList,' WHERE ',#WhereClause);
Query OK, 0 rows affected (0.00 sec)
mysql> PREPARE st FROM #DeleteSQL; EXECUTE st; DEALLOCATE PREPARE st;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
Query OK, 3 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> PREPARE st FROM #OptTableSQL; EXECUTE st; DEALLOCATE PREPARE st;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
+----------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------+----------+----------+----------+
| db1.mytb | optimize | status | OK |
| db2.mytb | optimize | status | OK |
| db3.mytb | optimize | status | OK |
+----------+----------+----------+----------+
3 rows in set (0.05 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM db1.mytb;
+----+-----------------+
| id | dat |
+----+-----------------+
| 1 | McDonald's |
| 2 | Wendy's |
| 4 | Jack in the Box |
+----+-----------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM db2.mytb;
+----+-----------------+
| id | dat |
+----+-----------------+
| 2 | Jack in the Box |
| 3 | McDonald's |
| 4 | Wendy's |
+----+-----------------+
3 rows in set (0.01 sec)
mysql> SELECT * FROM db3.mytb;
+----+-----------------+
| id | dat |
+----+-----------------+
| 2 | Jack in the Box |
| 3 | McDonald's |
| 4 | Wendy's |
+----+-----------------+
3 rows in set (0.01 sec)
mysql>
WHAT DO THE QUERIES LOOK LIKE
mysql> SELECT #OptimizeTableList,#DeleteTableList,#FromList,
-> #WhereClause,#OptTableSQL,#DeleteSQL\G
*************************** 1. row ***************************
#OptimizeTableList: db1.mytb,db2.mytb,db3.mytb
#DeleteTableList: dbalias1.*,dbalias2.*,dbalias3.*
#FromList: db1.mytb AS dbalias1,db2.mytb AS dbalias2,db3.mytb AS dbalias3
#WhereClause: dbalias1.dat='Burger King' AND dbalias2.dat='Burger King' AND dbalias3.dat='Burger King'
#OptTableSQL: OPTIMIZE TABLE db1.mytb,db2.mytb,db3.mytb
#DeleteSQL: DELETE dbalias1.*,dbalias2.*,dbalias3.* FROM db1.mytb AS dbalias1,db2.mytb AS dbalias2,db3.mytb AS dbalias3 WHERE dbalias1.dat='Burger King' AND dbalias2.dat='Burger King' AND dbalias3.dat='Burger King'
1 row in set (0.00 sec)
mysql>
CAVEAT
You have to be standing in one of the three databases for it to work
Are you surprised it worked ??? Hey, so am I !!!
For more information on multiple-table DELETE, see the MySQL Documentation. Search for the phrase multiple-table DELETE statement.

mysql delete triggers

I think I read that the delete trigger doesn't know what data was deleted and loops over the whole table applying the trigger. Is that true?
Does that mean that the before delete loops over the whole table before the data is deleted and after delete loops over the whole table after the delete occurs?
Is there no way to loop over just the deleted records? So If 10 records are deleted loop over them?
DELIMITER $$
DROP TRIGGER `before_delete_jecki_triggername`$$
CREATE TRIGGER before_delete_triggername
BEFORE DELETE ON table
FOR EACH ROW
BEGIN
/*do stuff*/
END$$
DELIMITER ;
Thanks,
Mat
I think it was due to a confusion with FOR EACH ROW statement.
It is only for "matched records for the statement issued before trigger is invoked."
If there exists N number of records in a table and matches records for where id=x,
assuming x causes a result of less than N records, say N-5, then
FOR EACH ROW causes a loop for N-5 times only.
UPDATE:
A sample test run on the rows affected due to FOR EACH ROW statement is shown below.
mysql> -- create a test table
mysql> drop table if exists tbl; create table tbl ( i int, v varchar(10) );
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.06 sec)
mysql> -- set test data
mysql> insert into tbl values(1,'one'),(2,'two' ),(3,'three'),(10,'ten'),(11,'eleven');
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from tbl;
+------+--------+
| i | v |
+------+--------+
| 1 | one |
| 2 | two |
| 3 | three |
| 10 | ten |
| 11 | eleven |
+------+--------+
5 rows in set (0.02 sec)
mysql> select count(*) row_count from tbl;
+-----------+
| row_count |
+-----------+
| 5 |
+-----------+
1 row in set (0.00 sec)
mysql>
mysql> -- record loop count of trigger in a table
mysql> drop table if exists rows_affected; create table rows_affected( i int );
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.05 sec)
mysql> select count(*) 'rows_affected' from rows_affected;
+---------------+
| rows_affected |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)
mysql>
mysql> set #cnt=0;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> -- drop trigger if exists trig_bef_del_on_tbl;
mysql> delimiter //
mysql> create trigger trig_bef_del_on_tbl before delete on tbl
-> for each row begin
-> set #cnt = if(#cnt is null, 1, (#cnt+1));
->
-> /* for cross checking save loop count */
-> insert into rows_affected values ( #cnt );
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> delimiter ;
mysql>
mysql> -- now let us test the delete operation
mysql> delete from tbl where i like '%1%';
Query OK, 3 rows affected (0.02 sec)
mysql>
mysql> -- now let us see what the loop count was
mysql> select #cnt as 'cnt';
+------+
| cnt |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
mysql>
mysql> -- now let us see the table data
mysql> select * from tbl;
+------+-------+
| i | v |
+------+-------+
| 2 | two |
| 3 | three |
+------+-------+
2 rows in set (0.00 sec)
mysql> select count(*) row_count from tbl;
+-----------+
| row_count |
+-----------+
| 2 |
+-----------+
1 row in set (0.00 sec)
mysql> select count(*) 'rows_affected' from rows_affected;
+---------------+
| rows_affected |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec)
mysql>