# MySQL/Databases manipulation
## Creation

Require? Privilege.
mysqladmin create is a command-line wrapper for this function.
NB: in MySQL, CREATE SCHEMA is a perfect synonym of CREATE DATABASE, contrarily to some other DBMS like Oracle or SQL Server.

## Deletion

Require ? privilege.
mysqladmin drop is a command-line wrapper for this function. The -f option can be used to suppress the interactive confirmation (useful for unattended scripts).

## Rename

In some 5.1.x versions there was a RENAME DATABASE db1 TO db2; command, but it has been removed because renaming databases via SQL caused some data loss problems.
However, in the command-line, you can create/export/import/delete:
Another option, if you have root access, is to rename the database directory:
You also need to drop privileges on name1 and recreate them on name2:

## Copy

There is no direct copy command in MySQL. However, this can easily be done using some tools.

### With mysqldump

The mysqldump command-line can be used to generate a complete flat-file copy of the database. You can then reinject this copy in another database.
This requires a direct access to the database; if you do not have it, you may need to use phpMyAdmin instead.

#### Backup

To set an automatic backup every day at midnight, in Linux:

### With phpMyAdmin
## Restoration

* With Linux:
* With Windows, the program may not be into the environment variables:
Contrarily to the PhpMyAdmin importations, there is no limit. For example, we can load a 2 GB database in five minutes.

## Migration from other databases

Tools: MySQL Migration Toolkit

## Tools for data modeling

* MySQL Query Browser apparently includes a MySQL Table Editor module.
* Kexi (wikipedia: Kexi)

### DB Designer 4 and MySQL Workbench

DBDesigner begins to be old. It is released under the GNU GPL, but it cannot be fully considered as free software since it requires the non-free Kylix compiler to build.
But MySQL AB acquired fabFORCE, who distributed DB Designer, and MySQL Workbench is the next version. For now the project is still Alpha and not ready for use yet.
Meanwhile, if you use the latest release of DBDesigner, you'll find that it cannot connect to MySQL, with the "unable to load libmysqlclient.so" error. To workaround this,
* Install the MySQL "Shared compatibility libraries" (from http://dev.mysql.com/downloads/mysql/5.0.html#downloads for version 5.0, generic RPMS aka MySQL-shared-compat.i386 will do).
* Replace DBDesigner's version of libmysqlclient.so with the newly installed one:
* Find and install kylixlibs3-unwind-3.0-rh.4.i386.rpm
* Find an old xorg (e.g. xorg-x11-libs-6.8.2-37.FC4.49.2.1.i386.rpm from FC4) and extract it:
* Get libXft.so.1.1 in that package and install it:
You now can connect to your MySQL5 server from DBDesigner4. Consider this a temporary work-around waiting for community (free) and commercial (not free) versions MySQL Workbench.

### OpenOffice Base and ODBC

Typical configuration :
* MySQL database on a host machine (which name is mysqlhost below)
* OOo 2 on a client machine (Debian GNU/Linux for instance)
* Connection via ODBC.
It's a client configuration : we need mysql-client:
Under Fedora/CentOS:
Before installing ODBC, we can test the remote connexion locally:
You must have create the database mysqldatabase and the user user1 on mysqlhost. It seems there is no problem (hope there is not ;-)):
Then, it's possible to test, through different queries :
Fine ! Let's go with OOo and ODBC, on the client machine:
For Fedora/CentOS:
/etc/odbc.ini (empty file) and /etc/odbcinst.ini are created. odbcinst.ini declares the available ODBC driver. Here's the MySQL statement (paths to the .so files may vary depending on the distribution); for Debian:
for CentOS:
Now we can use odbcinst :
For further options : man odbcinst
First of all, we have to create at least one DSN (Data Source Name or Data Set Name), because every ODBC connection is initialized through an existing DSN. It's true in every cases, so it is required for an ODBC connection from OOo.
To create a DSN, one have different possibilities :
* Modify /etc/odbc.ini (concerns all users)
* Modify ~/.odbc.ini (concerns a specific user)
* Use graphical applications such as ODBCConfig (Debian: unixodbc-bin, Fedora: unixODBC-kde). Finally, these graphical applications modify /etc/odbc.ini or ~/.odbc.ini
For instance, a /etc/odbc.ini file (the name of the DSN is between brackets):
In that case, the DSN is called MySQL-test
Then we can test, using isql command:
And now, from OOo:
At that step, one is connected to the mysqldatabase database, under the user user1. Just before accessing the database, for example to create tables, one will give user1 password. Then, through OOo, it is now quite easy to access and manipulate the database. We can just notice that Java is required in the following cases :
* Wizard to create a form (at the opposite, to create a form directly don't need any JRE).
* Wizard to create reports.
* Wizard to create queries (at the opposite, to create a query directly or through a view don't need any JRE).
* Wizard to create tables (at the opposite, to create a table directly or to create a view don't need any JRE).
GNU/Linux distros usually ships OpenOffice with IcedTea (openjdk-6-jre/java-1.6.0-openjdk) or GCJ (java-gcj-compat/java-1.4.2-gcj-compat) so that these Java-based features work.

## References

* ↑ https://dev.mysql.com/doc/refman/5.1/en/rename-database.html
* ↑ http://stackoverflow.com/questions/6645818/how-to-automate-database-backup-using-phpmyadmin
* ↑ In the forums: but we'd need something more official