Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams -- -------------------------------------------------------- -- 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.

In case anyone is interested, you can also specify the database name via the CLI command without editing the import file. mysql -u root -p databasename < import.sql – Sam Dufel Nov 24, 2014 at 15:36 what happened to me was: create a schema, filled in the name of the database, then it says "fail, no database selected". reopen workbench, i see database that i have just failed to created. Then I choose the database that i have just created, and open a .sql file and run, with both create database and use database statements, it complains "fail no database selected" again. reopen workbench, the tables are all built. MAC system, workbench 6.3.3 – Tiina Jun 9, 2016 at 9:12 Remember if you are on *nix systems and your database name has a $, wrap the name with single quotes. – dataman Feb 16 at 23:49

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

CREATE DATABASE database_name;

followed by:

USE database_name;
                In my case, I had used mysqldump to export a database and was running into this upon import. I just had to edit the exported sql file to add these two commands to the top of it.
– bh5k
                Oct 11, 2012 at 17:01
                Thanks! This is what I needed. I had a script to create my database and tables but the tables weren't getting created because I needed to insert the USE tablename command.
– ckpepper02
                Apr 21, 2014 at 15:23
                Write: CREATE DATABASE IF NOT EXISTS database_name ; USE database_name; This works whether the database is already created or not.
– Stian
                Jun 26, 2018 at 12:29
                USE database_name make selected database as default but what if you want to use other than you have run it again right?
– Sanket H patel
                Apr 7, 2021 at 12:03

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

3- Try again

This works for me.

one more note - make sure that the jdbc connection string contains the schema name and that schema is created already. – Gaurav Sep 3, 2020 at 21:16

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;
                Using MySQL Workbench: Could you please provide an image with the location of the drop down?
– AdrianRM
                Feb 2, 2012 at 16:00
                I'm thinking this might refer to an older version of MySQL Workbench. I just pulled the most current version and I don't see an "object browser" tab anywhere.
– Tim Keating
                Mar 22, 2012 at 5:43
                Looks like you need to use the USE database; command, even in MySQL Workbench (I'm in v5.2.47) if you're getting this error. Only need to run it one time per query tab - all subsequent executions on that tab seem to use the database correctly.
– DACrosby
                Nov 5, 2013 at 3:23
                Hi, So for wordpress sql how can we "use" in SQL file. Can you please clarify that once more. I am having the issue now.
– Rahul S
                Jul 15, 2015 at 5:02
  • 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.

  • That did the trick for me. Really hope that helps

    You'd think that if you weren't already on the database, there would be an option on the page to allow you to select one. – Kelderic Jan 6, 2017 at 13:43 The best answer and simple, u need to select the database table at the left after logging into the database on live server – Eben Watts Aug 27, 2022 at 10:08 In MySQL Workbench 8.0, you have to select your Default Schema to be Imported to in the Data Import window. – shiuu Jul 22, 2020 at 6:58

    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".

    How can we write in case for wordpress database file as am trying to import using MySql Workbench but getting no database selected – Rahul S Jul 15, 2015 at 5:03

    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!

    will ask for a password and complain with mysqldump: Got error: 1046: "No database selected" when selecting the database

    the problem is that the -p option requires that there be no space between -p and the password.

    mysqldump [options] -p'' --databases database_name
    

    solved the problem (quotes are not needed anymore).

    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)
    

    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;
    

    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

    That should be all.

    This should be a comment underneath Ivan's answer, so he can change his answer to include your steps. – Navigatron May 30, 2013 at 10:44

    I'm late i think :] soory,

    If you are here like me 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 i wanted to share with homeless people who got headaches like me.

    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 developping 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
    

    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à.

    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.