Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Oct 152010
 

How to create a transaction in MySQL

Answer:

A simple transaction in MySQL looks like the following:

mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO my_table VALUES (1, 2, 3);
Query OK, 1 row affected (0.02 sec)

mysql> ROLLBACK;
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO my_table VALUES (4, 5, 6);
Query OK, 1 row affected (0.01 sec)

mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)

At the end of the queries, the row (4, 5, 6) was inserted into the table, but not (1, 2, 3).

Oct 092010
 

Drop a database in MongoDB

Answer:

Assuming you are going to drop the `test` database in MongoDB, follow the steps below to drop (delete) it.

>  use test
switched to db test

> db.dropDatabase()
{ "dropped" : "test.$cmd", "ok" : 1 }
Oct 062010
 

Show all tables started with a prefix in MySQL

Answer:

If your database has a lot of tables, and you want to list all the tables started with a name prefix, you can try:

mysql> SHOW TABLES LIKE 'cache%';
+--------------------------------+
| Tables_in_mydb (cache%) |
+--------------------------------+
| cache                          |
| cache_block                    |
| cache_bootstrap                |
| cache_field                    |
| cache_filter                   |
| cache_form                     |
| cache_image                    |
| cache_menu                     |
| cache_page                     |
| cache_path                     |
| cache_update                   |
+--------------------------------+
11 rows in set (0.00 sec)

Oct 012010
 

Drop all tables in MySQL database

Answer:

MySQL does not have a command for removing all database table(s) without dropping the database, to do so, you need the following tricks:

# mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] 
| grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]