20

I don't know much about MySQL at all. But I am trying to reverse engineer a MySQL database using Visio. I know what steps I should take to do this, but I can't get my database to show in the 'Databases' section (as below):

enter image description here

How do I create the MySQL database using the .SQL file and get it to show up in this list? I have tried this code: mysql -u username -p password database_name < filename.sql using my own credentials of course. But that doesn't seem to work. In what folder should the .SQL file be placed if this statement is to work?

6
  • 1
    Does it give any error when you run this command: mysql -u username -p password database_name < filename.sql ? Commented May 26, 2012 at 20:06
  • It doesn't do anything, it jumps to the next line with an arrow. But I'm not sure that I'm typing the command in correctly. Should the database_name be what I intend to save the database as or should it already exist? And where should the .sql file be saved? Commented May 26, 2012 at 20:08
  • 1
    @Amy the database_name should already exist. Commented May 26, 2012 at 20:09
  • @Michael Okay, thanks, I see. So can I create a blank database and import the .sql file into that? Commented May 26, 2012 at 20:12
  • 1
    You can run this command from anywhere but make sure to refer the right path of your filename.sql. Try moving to the folder where filename.sql exists and run this command as it is. The database_name is the name of the database that is already created. Commented May 26, 2012 at 20:15

4 Answers 4

46

1) Create a file "filename.sql"

2) Create a database in your DB in which you want to import this file.

3) From command-prompt/terminal, move to the directory where you have created a "filename.sql".

4) Run the command: mysql -u username -p password database_name < filename.sql. (You can also give the proper path of your file and run this command from anywhere). It might be the case that you don't have a password set for MySQL. If so, mysql -u username database_name < filename.sql will also work.

In your case if you have created a database with name ojs and also created a file with name ojs.sql in C: drive then run the following command:

Edit: Put the path inside quotes.

mysql -u username -p password ojs < "C:\ojs.sql"

There is another way of importing tables in mysql. You can do it this way as well:

1) Connect your database

2) Type command "use ojs;"

3) Type command "source C:/ojs.sql"

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks a lot. It says "Unknown command '\o', so I changed it to a forward slash instead. Then it doesn't give an error, but still doesn't respond. Is it supposed to confirm something when the command works?
What is used to actually execute this statement? Is it just hitting Enter? Because when I press Enter, it seems to just jump to the next line and not actually execute the statement.
After executing the command with forward slash do you see the tables in your database that you want to create ?
No, there is no response at all. Like the statement hasn't been executed. Like it just skips to the next line of that statement instead of executing it.
you need to terminate your command with a semi-colon. mysql is waiting for you to continue your statement.
|
26

Most MySQL SQL files that create databases create the database 'on-the-fly', so you typically needn't do anything except:

  1. log-in

    mysql -u [username] -p[password]

(Note: make sure you do NOT include a space (' ') character between the -p and the [password]. MySQL will think that [password] is the name of the database you want to connect to. The 'general' log-in (above) does not assume you want to connect to any particular schema.)

  1. source the file (do not use quotes around filename)

    mysql> source [database_creation_file].sql

Comments

2

you can simply do it using mysql workbench

1> create a new query tab 2> CREATE DATABASE database_name; 3> USE database_name; 4> open the filename.sql file and execute it ctrl + shift + enter 5> all the tables in the filename.sql are created

Comments

2

To create a MySQL database using a SQL file, you can follow these steps:

  1. Log in to your MySQL server using the mysql command-line tool and the appropriate credentials.

  2. Use the CREATE DATABASE command to create a new database with the desired name:

CREATE DATABASE database_name;
  1. Use the USE command to switch to the newly created database:
USE database_name;
  1. Use the SOURCE command to import the SQL file into the database:
SOURCE path/to/sql/file;
  1. The database will now be created and populated with the data from the SQL file. You can verify this by running some SQL queries against the database.

It's important to note that this process assumes that the SQL file contains valid SQL statements compatible with the version of MySQL you are using. If the SQL file contains any errors or unsupported statements, they will be displayed in the mysql command-line tool, and the import process will be interrupted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.