MySQL Workbench: Refactoring Script - mysql

We have to make some changes to our databases (creating tables, Running special SQL queries).
Is there some way in MySQL Workbench to import a structure, edit it with a graphical interface and then getting some piece of SQL Code that I can use to transform my old structure into the new structure?
To explain my problem a bit further:
I need a way to transform my MySQL database structure to a new structure (adding databases and values into those tables) without interrupting the production environment for too long.
I would prefer a solution in MySQL Workbench (I'm using version 6.3)
but also accept any other answer to do this (for free)

Okay, I actually found a solution!!
What I was looking for is called an "SQL ALTER Script" or a Schema Sync.
Steps to generate a SQL Alter Script with MySQL Workbench:
Open MySQL Workbench
Click File -> New Model
Click Database -> Synchronize With Any Source
Click Next
Select a SQL Dump File as Source (this is the new structure)
Select a SQL Dump File as Destination (this is the old structure)
At Send Updates To select a Folder and Filename where the ALTER Script will be placed.
Click Next
Click Next
Make sure default_schema is checked and Click Next
Click Next
Preview your SQL Code and Click Next
You now successfully generated your SQL Alter Script and it got saved into the specified file.
But Note that you will have to open the SQL File in an editor and replace default_schema with your database name

Related

MySQL Workbench not showing schemas in Models

I am new to MySQL Workbench, so this could be a basic error on my part, but I am not sure what to do.
I have imported data (both using Data Import as well as running a script that includes schema creation) and I can see the schema exists in the Navigator. Likewise, I can run SQL queries on the schema with no problems.
However, when I go to File -> New Model, the schema is not there. I tried downloading another .sql file, created a schema for it and have the same problem.
Summary: I have schemas in MySQL Workbench that are not showing up in the Model section.
Chris M Perry answered this on Twitter for me:
"In any tab, you can click database -> reverse engineer and then follow the steps in selecting the schema."

How to export a data base from DataGrip at one SQL file?

How I can export a data base from DataGrip at one SQL file as phpmyadmin export?
If I use Dump Data To Files, DataGrip save one table at one file.
And how I can create backup of all MySQL databases, which open in DataGrip?
Old thread here, but for those who may stumble upon it, the newer versions of DataGrip have a new option, "dump with mysqldump", that will let you generate a dump contained in a single .sql output file.
Running DataGrip 2016.2.5 you can select sever tables and right click->Copy DDL. That puts a drop/create script on the clipboard, which you can then paste into a text file.
I just got the DataGrip 2016.2.1 update this morning. It looks like the feature has been added (at least for SQL SERVER, the only one I tested). It wasn't available in the previous version - I tried.
Select a range of tables and right-click on them, or right-click on the database name. The Dump Data to Files option is now there.
Unfortunately, it still dumps to multiple files. But it beats the heck out of right-clicking on each table in turn.
You can now tick "Single File" while choosing "Dump Data to File(s)" in DataGrip 2018.3.1

Import a database to DataGrip(0xDBE)

How do I import a database just like in phpmyadmin at DataGrip?
I have the .sql exported from phpmyadmin... but those are lots of lines so that the IDE stops working when trying to run the whole .sql
In DataGrip go to File > Open and select your mysql dump file. Then right click the tab for the file to get the context menu, and select "Run [your filename...]" option. It may ask you to select your schema to apply the run to. But this is how I accomplished importing a dump from phpMyadmin using DataGrip.
Jetbrains documentation on running SQL scripts does not provide a ton of information on processing large insert statements. There is a discussion in the Datagrip community forums and apparently upcoming features to make working with large scripts easier.
Quote from thread:
Huge SQL files can be executed from Files view (use a context menu action).
I assume you are attempting to import a database export which is a series of SQL statements saved to a file. There could be a memory issue if you are attempting to run a large SQL file in memory. Try the following.
Insert commit statements in your SQL file in a text editor. This can even be done from within datagrip. Every couple of hundred statements you can place the line
commit;
which should purge the previous statements from memory. I strongly recommend saving the file which you edit separately from the export script. This method is not applicable if you need an all or nothing import, meaning if even one statement or block fails you want all of the statement to be rolled back.
1 - Going to View->Tool Windows->Files
2 - Going to schema folder and open it in windows explorer after that past your dump file in my example i will past MyDump.dmp .
3 - Right click on the MyDump.dmp and run it .
To import data from a script file, run the file as it is described in Run database code. In addition to script files, you can import a CSV, TSV, or any other text file that contains delimiter-separated values.
https://www.jetbrains.com/help/datagrip/import-data.html

How to generate the whole database script in MySQL Workbench?

I want to take the whole database. Where do I find the database file?
And is there a way to write the whole database with all data to a text file (like the one in SQL Server)?
How to generate SQL scripts for your database in Workbench
In Workbench Central (the default "Home" tab) connect to your MySQL instance, opening a SQL Editor tab.
Click on the SQL Editor tab and select your database from the SCHEMAS list in the Object Browser on the left.
From the menu select Database > Reverse Engineer and follow the prompts. The wizard will lead you through connecting to your instance, selecting your database, and choosing the types of objects you want to reverse engineer.
When you're all done, you will have at least one new tab called MySQL Model. You may also have a tab called EER Diagram which is cool but not relevant here.
Click in the MySQL Model tab
Select Database > Forward Engineer
Follow the prompts. Many options present themselves, including Generate INSERT Scripts for Tables which allows you to script out the data contained within your tables (perfect for lookup tables).
Soon you will see the generated script in front of you. At this point you can Copy to Clipboard or Save to Text File.
The wizard will take you further, but if you just want the script you can stop here.
A word of caution: the scripts are generated with CREATE commands. If you want ALTER you'll have to (as far as I can tell) manually change the CREATEs to ALTERs.
This is guaranteed to work, I just did it tonight.
Q#1: I would guess that it's somewhere on your MySQL server?
Q#2: Yes, this is possible. You have to establish a connection via Server Administration. There you can clone any table or the entire database.
This tutorial might be useful.
EDIT
Since the provided link is no longer active, here's a SO answer outlining the process of creating a DB backup in Workbench.
In MySQL Workbench 6, commands have been repositioned as the "Server Administration" tab is gone.
You now find the option "Data Export" under the "Management" section when you open a standard server connection.
there is data export option in MySQL workbech
I found this question by searching Google for "mysql workbench export database sql file". The answers here did not help me, but I eventually did find the answer, so I am posting it here for future generations to find:
Answer
In MySQLWorkbench 6.0, do the following:
Select the appropriate database under MySQL Connections
On the top-left hand side of screen, under the MANAGEMENT heading, select "Data Export".
Here is a screenshot for reference:
None of these worked for me. I'm using Mac OS 10.10.5 and Workbench 6.3. What worked for me is Database->Migration Wizard... Flow the steps very carefully
In the top menu of MySQL Workbench click on database and then on forward engineer. In the options menu with which you will be presented, make sure to have "generate insert statements for tables" set.
Try the export function of phpMyAdmin.
I think there is also a possibility to copy the database files from one server to another, but I do not have a server available at the moment so I can't test it.
Using Windows 10 and MySql Workbench 8.0
Go to Server tab
Go to Database Export
This opens up something like this
Select the schema to export in the Tables to export
Click the button Start Export
Surprisingly the Data Export in the MySql Workbench is not just for data, in fact it is ideal for generating SQL scripts for the whole database (including views, stored procedures and functions) with just a few clicks. If you want just the scripts and no data simply select the "Skip table data" option. It can generate separate files or a self contained file. Here are more details about the feature: http://dev.mysql.com/doc/workbench/en/wb-mysql-connections-navigator-management-data-export.html
in mysql workbench server>>>>>>export Data
then follow instructions it will generate insert statements for all tables data each table will has .sql file for all its contained data

Import initial table data to a .mwb model file

How can I import initial table data to a .mwb file? I know that there is an inserts tab for each table, but I would like to import around 200 records and I don't want to do this by hand.
It is not possible with the modern version of MySQL Workbench. There is no way, essentially, to model data - you can only upload it to the server (not the model). The only way currently is to edit one by one which isn't practical. Even if you reverse engineer a table filed with data, the inserts table of the EER model will be blank. You'll note that right-clicking on the row of the inserts tab gives a number of greyed out options including "load from file". I suspect the team didn't have time to implement them or something. Anyway, there is a simple work around if you know phpMyAdmin, which seems to handle CSV files well, or MySQL Workbench, which I have not gotten to work at all with CSV files.
Solution:
Draw your DB model in MySQL Workbench, structure only. Place all your data in associated CSV files - I use Excel and save as CSV - very easy. Once you have your data modeled in Excel and the structure modeled in Workbench, forward engineer the DB, then use some other tool or technique to upload your Excel modeled data.
Not the greatest solution, but bug them to provide data modeling and maybe we'll be lucky in the next version.
Currently this seems not to be possible. I too was hoping to be able to Reverse engineer from the insert statements in a script file, but 1. it didn't work :P and 2. actually the documentation explicitly states that these will be ignored:
http://download.oracle.com/docs/cd/E19078-01/mysql/mysql-workbench/wb-data-modeling.html#wb-reverse-engineering
7.7.9.1. Reverse Engineering Using a Create Script
Reverse engineering using a create script is done by using the File, Import, Reverse Engineer MySQL Create Script ... menu options. Doing this opens a file open dialog box with the default file type set to an SQL script file, a file with the extension sql.
You can create a data definition (DDL) script by executing the mysqldump db_name --no-data > script_file.sql command. Using the --no-data option ensures that the script contains DDL statements only. However, if you are working with a script that also contains DML statements you need not remove them; they will be ignored.
It seems that the lesson is that we ought to handle such resources (that are too large to be manually inserted) through some other medium, such as a versioned sql file. :(