MySQL Error - #1046 - No database selected

mysqlmysqli database更新于 2024/1/28 14:05:00

当我们创建表但忘记选择数据库时,可能会发生错误 - #1046。假设我们已经启动了 MySQL,如下所示 −

Command Line

输入正确的密码后,将打开上述窗口。现在创建一个表而不选择任何数据库。这将显示错误 −

mysql> CREATE table TblUni
-> (
-> id int,
-> Name varchar(100)
-> );

ERROR 1046 (3D000): No database selected

The following screenshot is showing the same error −

No_database_selected

Now, choose any database to get rid of the above error. Firstly, let us check how many databases are present in MySQL with the help of SHOW command −

mysql> SHOW databases;

以下是输出 −

+--------------------+
| Database           |
+--------------------+
| business           |
| hello              |
| information_schema |
| mybusiness         |
| mysql              |
| performance_schema |
| sample             |
| sys                | 
| test               |
+--------------------+
9 rows in set (0.00 sec)

现在,我们可以选择任意数据库。假设我使用的是数据库"business",因此我们可以借助"use"命令进行选择。

mysql> use business;
Database changed

使用数据库"business"后,我们可以创建上述表格,并且不会出现任何错误。

mysql> CREATE table TblUni
-> (
-> id int,
-> Name varchar(100)
-> );
Query OK, 0 rows affected (0.50 sec)

相关文章