Copying a MySQL table from one database to another
To copy a MySQL table, both the structure and data, from one database to
another, the following two commands can be used inside MySQL where
db1
is the original database and
db2
is the
new database and
table
is the name of the particular table
you wish to copy.
CREATE TABLE db2.table LIKE db1.table;
INSERT INTO db2.table SELECT * FROM db1.table;
[/software/database/mysql]
permanent link
Showing all MySQL databases or all tables in a database
For MySQL on a Linux system, if you need a list of all MySQL databases
you can display a list of the databases by logging into MySQL and
then issuing the command
show tables;
or from a shell
prompt you can issue the command
mysqlshow -u username -p
where
username
is a MySQL username. The
-p
option will
result in a prompt for the password associated with the username.
$ mysqlshow -u jdoe -p
Enter password:
+--------------------+
| Databases |
+--------------------+
| information_schema |
| jdoedbf |
| test |
| tokyo |
+--------------------+
If you need a list of all tables in a MySQL database, you can display a
list of tables by logging into MySQL with an account that has access
to the database and then issuing the command
show tables;
or from a shell prompt you can issue the command
mysqlshow -u
username -p dbname
where
username
is the account with access to the database and
dbname
is
the name of the database. The
-p
option will prompt for
the password for the account.
$ mysqlshow -u jdoe -p products
If you need to find a table with particular text in the name, you can
pipe the output of the mysqlshow command into grep. E.g., if you were looking
for a table in a database named "products" with many tables that contained
"cat" as part of the name of the table, you could use something like
the following:
$ mysqlshow -u jdoe -p products | grep cat
[/software/database/mysql]
permanent link