使用CREATE TABLE的任何方式使用完全限定的表名

时间:2022-08-18 08:01:06

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?

当我使用CREATE TABLE tbl_name时,我只能在我当前使用的数据库中指定一个表。有没有任何方法在没有先前的USE数据库1的情况下在database1中创建表tbl_name?

1 个解决方案

#1


1  

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)

#1


1  

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)