Importing Data and Schema to MySQL Workbench - mysql

I'm trying to learn SQL and I downloaded a database to practice. I downloaded two files of extension .sql, one is the schema and the other one the actual data. I've also installed MySQL Workbench. I've been googling and I've been trying things to solve this but I don't understand Workbench and I can't load the database.
Where do I import the schema and the data in order to try queries ?
Any help would be really appreciated.

This is simple in Workbench, and I'll use the freely available sakila database as an example. Feel free to apply this to your situation:
Download "sakila" from here: http://dev.mysql.com/doc/index-other.html
Extract it somewhere, in my case, onto the Desktop into a new sakila-db/ directory
Open Workbench
In the Schema Navigator, right-click an empty area and choose "Create Schema"
In the schema creation wizard, name it "sakila", keep the defaults, hit "Apply", finish wizard
Go to "File" -> "Run SQL Script..."
Choose "sakila-schema.sql", make sure "Default Schema Name" is blank or select "sakila", execute
Go to "File" -> "Run SQL Script..."
Choose "sakila-data.sql", execute
Click the "refresh" icon in the Workbench Schema Navigator (or restart Workbench)
Now, use the populated sakila database :)
Steps (4) and (5) are optional in this case (as executing sakila-schema.sql creates the schema), but the idea is worth mentioning.
Here's how it would look when loading th script into the SQL IDE:

The accepted answer is from 4 years ago, so I thought I'd give an update as in MySQL Workbench 6.3 the procedure is a bit different.
You have to select the menu item Server -> Data Import -> Import from Self-Contained File and select the SQL file containing the database you want to import.
In Default Target Schema, select the database you want to import the SQL dump to, or create a new empty database via New...
Then click on Start Import.

You could use mysql console from terminal. Login through the user id and pass. Then create a Database from the following command is the .sql file does not have one such command to create so.
Create database db-name
use db-name;
SOURCE xyz.sql;
Source command would load the the content from xyz.sql to your database created. This would be reflected later in workbench indeed.

Its very easy on Linux platform just follow below mentioned steps, After downloading zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and other one is sakila-data.sql.
Open terminal
Enter command mysql -u root -p < sakila-schema.sql
Enter command mysql -u root -p < sakila-data.sql
Now enter command mysql -u root -p and enter your password, now you have entered into mysql system with default database.
To use sakila database, use this command use sakila;
To see tables in sakila-db, use show tables command
Please take care that extracted files are present in home directory else provide the absolute path of these files in all above commands.

Related

MySQL Workbench - How to clone a database on the same server with different name?

I am using MYSQL Workbench and I want to clone a database on the same server with different name. It should duplicate the all the tables structure and data into the new database.
I know the usual way is probably using data export to generate a sql script of the database and then run the script on the new database but I encounter some issues with it.
Anyway, is there any better way or easier way to do so?
You can use migration wizard from MySQL Workbench. Just choose the same local connection in both source and target selection, then change schema name on manual editing step. If nothing appears on manual editing step click next and the source and targets will appear. Click slowly on the source database name and edit to the correct name. Go thorough to the end and voilĂ  - you have two identical databases with different names. Note you must have created the target database already and granted permissions to it for the MySQL Workbench user.
I tried to do it in MySQL Workbench 8.0. However I kept receiving an error regarding column-statics. The main idea is to use mysqldump.exe, located in the installation directory of MySQL Workbench, to export the data. So, supposing a Windows oriented platform:
Open Powershell, navigate to mysqldump.exe directory. In my case the command is:
cd C:\Program Files\MySQL\MySQL Workbench 8.0 CE
Export database by executing mysqldump providing the right arguments:
./mysqldump.exe --host=[hostServerIP] --protocol=tcp --user=[nameOfUser] --password=[yourPassword] --dump-date=FALSE --disable-keys=FALSE --port=[portOfMysqlServer] --default-character-set=utf8 --skip-triggers --column-statistics=0 "[databaseName]"
Without changing directory, import the exported file (.sql) by using the following command in Powershell:
Get-Content "[pathToExportedDataFile]" | ./mysql.exe --user=[nameOfUser] --password=[yourPassword] --port=[portOfMysqlServer] --host=[hostServerIP] --database=[nameOfNewDatabase] --binary-mode=1
You can check in the documentation here for more information regarding the mysqldump options.
Please note the following:
Do not forget to replace the values in [] with your own values and remove the []. Do not remove the quotes("") where the are present.
Do not switch Powershell for cmd or something like git-bash, since the above will not work.
As far as step 3 is concerned, I created the new database from MySQL Workbench and then ran the powershell command.
List item First, create a new database using CREATE DATABASE statement.
Second, export all the database objects and data of the database from which you want to copy using mysqldump tool.
Third, import the SQL dump file into the new database.

Installing (minimal) MySQL Workbench to simply join data from two .csv files (connections???)

I previously used MySQL Workbench to do this, in an environment that was already set up.
How do I set up a minimal working environment to just create and join tables on my own computer? (Connections???)
More details:
I downloaded and installed MySQL Workbench, and I can't even run SELECT sysdate();. There's a red x next to it. If I try "CREATE DATABASE MY_DATABASE; there's a green check, but the execute button is grey.
Doing some reading I apparently need "connections." Reading about that, I apparently need to also install MySQL Database Server. Who knows what else.
So, again, the question is how do I set up a minimally working environment to just create tables from .csv files, join them with MySQL commands, and export the results to another .csv file? (I know the syntax of the command to import a .csv file, and how to join tables.)
Thanks.
Install MySQL WorkBench AND MySQL Server.
From the command line, in the directory where MySQL server is installed, execute "mysqld --initialize" (One time only.)
execute "mysqld" from the command line, after the initialization given in step 1, and after any reboots. (It runs in the background, and doesn't exit when you exit MySQL WorkBench. (It can optionally be installed as an automatically running Windows service during installation.)
Execute Database -> Connect to Database upon starting MySQL WorkBench (each time you start the application). The default local host connection works fine.
After doing File -> New Model and setting up table(s), do Database -> Forward Engineer. This will place your new database in the Schemas section on the home/main window.
Double click on the Schema you created (default name is mydb) and it changes to bold font. Now scripts you run from that main window will run against the database you created.

how to migrate mysql database using gui between two non connected pc running windows

i have my windows form application in my home pc. i want to migrate it to my college PC.how to transfer MySQL database using GUI from one pc to another both running windows
I've never worked with mysql-workbench, but here's a fast and simple solution using the commandline:
Export
mysqldump -u [uname] -p db_name > db_backup.sql
Import
mysql -u username -p -h localhost DATA-BASE-NAME < db_backup.sql
Only for MyISAM tables, you can simply copy and paste the three files that you will find in the database folder inside 'C:\xampp\mysql\data' or similar.
These files have the extensions, .frm, .MYI and .MYD
Once you've pasted the files flush the tables via command or a GUI tool and you have a duplicate of the table on the original machine.
For more about these files see, http://en.wikipedia.org/wiki/MyISAM
If you prefer a more graphically oriented way of doing that use MySQL Workbench -> Management -> Data Export on the source machine. Likewise -> Data Import on the target box. It allows you to easily select individual tables/schemas, creates a self-contained dump file or individual files for each table in case you want to have greater control over what to import etc.
i finally figured out the problem..
i first exported the data using the tab on the left hand side
i selected the appropriate location for the exported file to be saved.(it created an sql query files of the database)
i then copied it to my pen drive and pasted it on the other PC
i installed workbench on the destination PC and imported the sql files but couldnt execute them. it said you dont have a connection something like that. i then searched for servies on my pc in that i got a list of all the services on my pc. search for mysqlxx(where x= integer) right click on it and select start.
then go back to the workbench and execute the sql file the file will execute successfully but you wont be able to see the database.
close workbench and reopen it you now will be able to see the database

How to copy a database using HeidiSQL?

I have a local installation of MariaDB on a Windows XP.
I created an empty database db_y which I wanted to populate with the tables of the database db_x which I exported as a dump-file from a MySQL-instance (with HeidiSQL). When I imported the dump-file db_x.sql into the the MariaDB instance:
c:\ > mysql -u root -h localhost -p db_y < "X:/archive/db_x.sql"
I got the following:
- MariaDB-inst
+db_x
+db_y
db_y remains empty and db_x from the dump-file was added (db_x is the database name of the original database I exported). What I have to do to get the desired database name? I thought I could change the database name in the db_x.sql file but I didn't want to open such a large file. Can I change the import command above in such a way that it change the database name?
I'm also interested in this kind of solution:
CREATE DATABASE y FROM DATABASE x
Is something like this possible?
In the net I find the solution RENAME DATABASE which was not recommended and ALTER DATABASE db_x UPGRADE DATA DIRECTORY NAME
but sincerely, I preferred to create a new database with the new name.
Thanks for any help.
Consider you have two databases: source_db and target_db. If you want to copy the database contents from source_db to target_db you should do as follow in HeidiSQL:
Right click on source_db then select: Export database as SQL.
Now change the value of Output and select Database.
A select box will appear, select target_db and that's all.
There is an easy way to transfer a database from one instance to another with HeidiSQL:
Create the database db_y in instance y
Click on dump icon (or right click). The instance y should be activated.
At "Output" option choose Database
At "Database" option choose db_y
Select on the left the instance x and database x
Export
Try MySQL Workbench. It's made by MySQL and I've found it excellent for backing up a database and restoring it under a different name.
http://dev.mysql.com/downloads/workbench/
HeidiSQL's export dialog recently got a new option called "Max INSERT size". This controls the number of rows in bulk/multiple INSERT commands.
Also, there is a documentation for this export dialog.

How do I register an mysql database?

Sorry for a noob question regarding MySQL. I downloaded FlightStats to learn about mysql but I can't figure out how to register it with my localhost mysql db. I know in MS SQL you can simply register any sql db using sql studio. I tried to google but come up with no result. Perhaps, my search phrase is wrong. I'm searching with "how to register a mysql database, register a mysql database...etc.". How do you register or setup an database from existing database like FlightStats? I'm using DBVisualizer. Is there a way in dbVis that I'm not aware of to regsiter a database?
Thanks
edit: sorry for the bad wording. I found this. I have the .myd, .myi and .frm and I want to get it to restore(?) with my local mysql instance. I look at all the answers but I'm still confuse as how you restore the database from those 3 files.
A little background first. The FlightStats download page linked to in the original question appears to provide zipped tarballs of the binary table storage files from the MySQL data directory. Given that this is considered a viable means of distribution, and combined with the use of MERGE tables, I would surmise that this tarball contains a bunch of MyISAM data files (.myi, .myd). Jack's edit confirms that this is the situation.
This is an atypical means of distributing a MySQL data set, although not at all uncommon when backing up MyISAM storage, and probably not all that unheard of for moving large data sets around; it likely works out considerably more space-efficient than a corresponding dump file. Of course, in SQL Server land, it's pretty common to attach database files into an instance.
Broadly speaking, you'd recover the database as follows:
Locate the MySQL data directory; typically /var/mysql or similar
Create a new directory with the desired database name e.g. flightdata
Extract the .myi, .myd and other files from the tarball into this directory
Make sure the entire directory is owned by the user MySQL runs as (usually mysql) - use chmod -R to make sure you get everything
Open a MySQL console
USE <database-name>
SHOW TABLES
You should see some tables listed. In addition, the downloads page linked includes a couple of SQL scripts, which contain SQL commands that you need to run against your database once it's in place. These will cause the merge definitions and table indexes to be rebuilt. You can pipe these into the command-line client, e.g. mysql -u<username> -p<password> <database-name> < <sql-file>.
It may be a good idea to shut down the MySQL server while you're doing this; use e.g. /etc/init.d/mysql stop or similar, and restart once the files are extracted in place.
There's generally a way to import sql files using a GUI database tool. I'm not familiar with DBVisualizer, but as long as you have a MySQL command line client installed you can do it there as well. It's pretty easy:
Create a blank schema. You can do this in your GUI tool or on the command line client. Just use CREATE DATABASE flightstats;, or whatever name you want.
Use the following command line syntax to import/run an sql file on the new schema: mysql -u <username> -p flightstats < /path/to/file.sql
The -p option prompts for a password. I generally set up the database using step 1 as the root user, then GRANT some permissions on it to a new user id, then use that user id to run the SQL file.
This process is pretty much what a GUI tool will do in the background.
Registering a database? dont know what that means however mysql gui tools can help you creating a database. Have a look at it or better you download phpmyadmin.
Google WAMP for Windows.
Google MAMP for Mac.
Google LAMP for Linux.
Any questions?