steph
steph

Reputation: 4357

Error 1046 No database selected, how to resolve?

Error SQL query:

--
-- Database: `work`
--
-- --------------------------------------------------------
--
-- Table structure for table `administrators`
--
CREATE TABLE IF NOT EXISTS `administrators` (

`user_id` varchar( 30 ) NOT NULL ,
`password` varchar( 30 ) NOT NULL ) ENGINE = InnoDB DEFAULT CHARSET = latin1;

MySQL said:

#1046 - No database selected

need some help here.

Upvotes: 434

Views: 1283842

Answers (21)

Ayham AlKawi
Ayham AlKawi

Reputation: 179

  • Edit your SQL file using Notepad or Notepad++

  • Add the following 2 lines:

    CREATE DATABASE NAME;
    USE NAME;
    

Upvotes: 16

Mina Fawzy
Mina Fawzy

Reputation: 21462

I faced the same error when I tried to import a database created from before. Here is what I did to fix this issue:

  1. Create new database

  2. Use it by use command

enter image description here

  1. Try again

Upvotes: 44

cs075
cs075

Reputation: 1

  1. Create a new DB in MySQL.
  2. Select that new DB (if you are using MySQL phpmyadmin now on the top it'll be like 'Server:...* >> Database ).
  3. Now go to import tab select file.Import!

Upvotes: -1

iversoncru
iversoncru

Reputation: 579

Quoting ivan n : "If importing a database, you need to create one first with the same name, then select it and then IMPORT the existing database to it. Hope it works for you!"

These are the steps: Create a Database, for instance my_db1, utf8_general_ci. Then click to go inside this database. Then click "import", and select the database: my_db1.sql

Upvotes: 2

veeru666
veeru666

Reputation: 21

First select database : USE db_name

Then creat table:CREATE TABLE tb_name ( id int, name varchar(255), salary int, city varchar(255) );

This is for MySQL 5.5 version syntax

Upvotes: 1

moolsbytheway
moolsbytheway

Reputation: 1292

If you are searching for the solution when this error occurs with mysqldump instead of mysql, try this solution that I found on a German website out there by chance.

So the problem occurs because the lack -databases parameter before the database name

So your command must look like this:

mysqldump -pdbpass -udbuser --databases dbname

Another cause of the problem in my case was that I'm developing on local and the root user doesn't have a password, so in this case you must use --password= instead of -pdbpass, so my final command was:

mysqldump -udbuser --password= --databases dbname

Link to the complete thread (in German) : https://marius.bloggt-in-braunschweig.de/2016/04/29/solution-mysqldump-no-database-selected-when-selecting-the-database/

Upvotes: 1

ivan n
ivan n

Reputation: 109

If importing a database, you need to create one first with the same name, then select it and then IMPORT the existing database to it.

Upvotes: 10

Roanna
Roanna

Reputation: 271

If you are doing this through phpMyAdmin:

  • I'm assuming you already Created a new MySQL Database on Live Site (by live site I mean the company your hosting with (in my case Bluehost)).

  • Go to phpMyAdmin on live site - log in to the database you just created.

  • Now IMPORTANT! Before clicking the "import" option on the top bar, select your database on the left side of the page (grey bar, on the top has PHP Myadmin written, below it two options:information_schema and name of database you just logged into.

  • Once you click the database you just created/logged into it will show you that database and then click the import option.

Upvotes: 26

I got the same error below:

ERROR 1046 (3D000): No database selected

When I ran these SQL below:

SELECT * FROM person;

Or:

SHOW EVENTS;

So, I selected apple database with use(USE) as shown below, then the error was solved. *You can omit ;:

use apple;

Or:

USE apple;

Upvotes: 1

Joukhar
Joukhar

Reputation: 872

Simple

  1. click on database
  2. and then click on import

without modifying .sql file

Upvotes: 1

David
David

Reputation: 1141

In Amazon RDS, merely writing use my-favorite-database does not work if that database's name includes dashes. Furthermore, none of the following work, either:

use "my-favorite-database"
use `my-favorite-database`
use 'my-favorite-database'

Just click the "Change Database" button, select the desired database, and voilà.

Upvotes: 2

sargupta
sargupta

Reputation: 1043

Solution with an Example

  • Error 1046 occurs when we miss to connect our table with a database. In this case, we don't have any database and that’s why at first we will create a new database and then will instruct to use that database for the created table.
# At first you have to create Database 
CREATE DATABASE student_sql;

# Next, specify the database to use
USE student_sql;

# Demo: create a table 
CREATE TABLE student_table(
    student_id INT PRIMARY KEY,
    name VARCHAR(20),
    major VARCHAR(20)
);

# Describe the table 
describe student_table;

Upvotes: 5

Sanket H patel
Sanket H patel

Reputation: 121

Check you have created the database first which you want.

If you have not created the dataBase you have to fire this query:

CREATE DATABASE data_base_name

If you have already created the database then you can simply fire this query and you will be able to create table on your database:

CREATE TABLE `data_base_name`.`table_name` (
 _id int not null,
 LastName varchar(255) NOT NULL,
 FirstName varchar(255),
 Age int,
 PRIMARY KEY (_id)
);

Upvotes: 5

BenKoshy
BenKoshy

Reputation: 35731

Assuming you are using the command line:

1. Find Database

show databases;

Example of a database list

2. Select a database from the list

e.g. USE classicmodels; and you should be off to the races! (Obviously, you'll have to use the correctly named database in your list.

Why is this error occurring?

Mysql requires you to select the particular database you are working on. I presume it is a design decision they made: it avoids a lot of potential problems: e.g. it is entirely possible, for you to use the same table names across multiple databases e.g. a users table. In order to avoid these types of issues, they probably thought: "let's make users select the database they want".

Upvotes: 19

Eric Korolev
Eric Korolev

Reputation: 743

For MySQL Workbench

  1. Select database from Schemas tab by right mouse clicking.
  2. Set database as Default Schema

enter image description here

Upvotes: 26

William T. Mallard
William T. Mallard

Reputation: 1660

For an added element of safety, when working with multiple DBs in the same script you can specify the DB in the query, e.g. "create table my_awesome_db.really_cool_table...".

Upvotes: 0

the10thplanet
the10thplanet

Reputation: 27

Just wanted to add: If you create a database in mySQL on a live site, then go into PHPMyAdmin and the database isn't showing up - logout of cPanel then log back in, open PHPMyAdmin, and it should be there now.

Upvotes: 0

Shay Anderson
Shay Anderson

Reputation: 994

You can also tell MySQL what database to use (if you have it created already):

 mysql -u example_user -p --database=example < ./example.sql

Upvotes: 69

zipzit
zipzit

Reputation: 4027

Although this is a pretty old thread, I just found something out. I created a new database, then added a user, and finally went to use phpMyAdmin to upload the .sql file. total failure. The system doesn't recognize which DB I'm aiming at...

When I start fresh WITHOUT first attaching a new user, and then perform the same phpMyAdmin import, it works fine.

Upvotes: 0

codaddict
codaddict

Reputation: 455410

You need to tell MySQL which database to use:

USE database_name;

before you create a table.

In case the database does not exist, you need to create it as:

CREATE DATABASE database_name;

followed by:

USE database_name;

Upvotes: 606

OMG Ponies
OMG Ponies

Reputation: 332741

If you're trying to do this via the command line...

If you're trying to run the CREATE TABLE statement from the command line interface, you need to specify the database you're working in before executing the query:

USE your_database;

Here's the documentation.

If you're trying to do this via MySQL Workbench...

...you need to select the appropriate database/catalog in the drop down menu found above the :Object Browser: tab. You can specify the default schema/database/catalog for the connection - click the "Manage Connections" options under the SQL Development heading of the Workbench splash screen.

Addendum

This all assumes there's a database you want to create the table inside of - if not, you need to create the database before anything else:

CREATE DATABASE your_database;

Upvotes: 34

Related Questions