How to execute SQL queries inside MySQL workbench - mysql

I am developing an application using Django framework. As you may know the workflow is you first describe your objects in Python classes and then you synchronize the database.
I made a MySQL Workbench EER diagram. Since then I continued to develop the application, so the database model is not updated in the EER diagram nor the MySQL Workbench model.
I tried to synchronize it using the built-in feature "Synchronize with Any Source" of MySQL Workbench, but this feature is not working for some reason and causes a segmentation fault. The queries to be executed inside the MySQL Workbench model are displayed but at the last step I get an empty SQL alter script. I tried manually copying the queries in that script and clicking the "Execute" button, but I had no luck with that. I think MySQL stores queries internally. Anyway. I submitted the bug to MySQL Workbench developers here and now it is fixed, but not yet released. I am now looking for a workaround while waiting for the next release.
Although I have a specific problem, the question remains generic.
Is it possible to execute queries on the MySQL Workbench model in order to alter it?

Did you try the "Forward Engineer" option? It allows you to reflect all the changes that were made to your table relations directly to the database which is a pretty useful functionality.
There are some catches though like the inability to maintain existing data every time forward engineering is performed however this can be compensated by entering some example data which will be shipped with the ER diagram the next time you perform "Forward engineer".

Related

How to do a query in Mysql WorkBench?

I have an .sql script that contains inserts and creates tables. I used the "Create EER Model From Script"
It created the tables but I can't see the data inside these tables.
I went to the query menu and tried to make a query but it gives me an error about not being able to connect to localhost.
Am I doing it right?
As documented under Create EER Model from SQL Script:
Clicking this action item launches the Reverse Engineer SQL Script wizard. This is a multi-stage wizard that enables you to select the script you want to create your model from.
For further information, see Section 7.7.9.1, “Reverse Engineering Using a Create Script”.
Following that link:
However, if you are working with a script that also contains DML statements you need not remove them; they will be ignored.
Instead, you want the Manage Data Import/Export option under Server Administration (within the Workspace section of the Home window).
You are confusing things here. Creating a model from a script is a process where meta data is examined and a model is created that you can then use to modify your schema structure, further design your db objects and all that. Modeling is a design process for the structure of your schema/db so it only deals with meta data. It's also used for documentation (e.g. in teams).
On the other hand there's normal sql work with existing db objects and/or actually creating/deleting/modifying db objects. In order to do the latter you must have an understanding of the design of the schema (which you could get by using the modeling part of MySQL Workbench, but not only by that). This is also the place to load a script, run it to insert data and such.
The error you mentioned regarding the connection is yet another problem and you need to solve this first to be able to even access your server. And yes, you have to install a server first somewhere. MySQL Workbench is a tool to visually work with your server(s) in opposition to the MySQL command line client which is a pure text interface (but still also a client application for your MySQL servers).
If you are on Windows and want a MySQL server installed locally (e.g. for testing) your best option is to download the MySQL Installer which greatly simplifies installing any of the tools from the MySQL family (server, client tools, connectors, documentation and more).

Mysql workbench synchronize different databases

I'm using Mysql workbench to develop my database for my application.
I use at least two databases,for example:
my_local : my local testing database that it's always synchronized with mysql workbench
myserver_database : the final database in the server,keep in mind that this database is in production and users WILL update it and i can't loose any information stored into it.
Now i can synchronyze my database every time i want but i can't find a way to update the scheme to the final server because they have different names,i get something like:
my_local => N/A
N/A <= myserver_database
in the past i simply renamed the database in mysql workbench but it doesen't seem to work anymore,probably because of a bug.
I want to be able to synchronize the same workbench scheme with different databases,regardless of the database name,i didn't find a way to force the database name even by modifying the default_scheme.
Please keep in mind i'll do it a lot of times so it's better to avoid triky or dangerous solutions if possible.
I know this question is quite old but I was able to do this on workbench 5.2.40 and there are not many updated resources online explaining how.
First I got a script of my old database:
mysqldump -no-data myolddb > script.sql
(I only want to synch the schemas, this can be done on the workbench too)
now the trick is to modify the script by adding use mynewdb; as its first line, this way the workbench won't say N/A or default schema nonsense.
On the workbench I created a EER model of mynewdb which is on my server, and then "Database->Synchronize with any source" and select from "model Schemadata" to "Script file" in the wizard using the script I modified initially. And then the Synch wizard worked like it should.

test and production server deployment with yii framework - syncing DB changes

I am working on a Yii framework based app where I have to test the app on my local machine and then when ready move the changes to the production server.
the app will be developed as people are using it and ask for new features. So when I make changes to my DB schema on the test machine I have to apply these to the schema of the production DB without destroying data there.
is there a recommended and convenient way to deal with this? syncing source code is less of an issue, i am using svn and can do svn export ; rsync ...
MySQLWorkbench can be helpful for syncing db schema as well as other database design tasks.
Yii does support Migrations (since v1.1.6), although it can be more trouble than it's worth depending on how often you make changes and how collaborative your project is.
Another approach I've used is to keep a log of update statements in svn and basically handled the migrations manually.
The best approach is going to depend on the cost/benefits to your particular project/workflow.
You can try SQLyog's Schema Synchronization Tool, which is a visual comparison/synchronization tool designed for developers who work between different MySQL servers or need to keep databases between two MySQL servers in sync. This means reporting the differences between tables, indexes, columns and routines of two databases, and generating scripts to bring them in Sync. Only the Schema will be synced in the target.
For a similar project we
use MySQLWorkbench (MWB) to design and edit the schema
share the .mwb file through a VCS.
When one of us is comfortable with a change he uses mysqldump --complete-insert... on the production and test schemas to generate a copy of the existing test and production data with field names
pull out all the production server insert statements in (3) and put them in protected/data/insert.sql
use the "forward engineer" menu item in MWB on the modified model to generate sql to save to a file called protected/data/create.sql, hand-editing as appropriate (be sure to use the if exists clause to reduce errors)
write a drop.sql file based on drop statements in (3)
use MWB, run the sql (drop.sql, create.sql, insert.sql) after issuing the appropriate "use database" command that identifies the production database
deal with all the errors in (7) by getting rid of any invalid inserts due to columns/fields that are not needed in the new models/schema. Rerun (7)
deal with new fields in (7) that need data other than Null. Rerun (7)
Now you have a snapshot of your schema (drop.sql create.sql) and your data that should revive either your test or production server if you ever have a problem. And you have a "fixture" of the data that was in your production server (insert.sql) that can be used to bring your test server up to speed, or as a backup of the production server data (that will quickly be outdated). Obviously all the foreign key relationships are what are really painful, so it's rare that the insert.sql is useful for anything except upgrading the schema and restoring the production data after this change. Clearly it takes some time to work out the kinks in the process so that the delay between (3) and (9) is small enough that the production server users don't notice the downtime.
Clearly "Rerun (7)" gets repetitive and quickly turns into a shell script that calls mysql directly. Also other steps in the sql editing process become sed scripts.
Have a look at schema comparison tool in dbForge Studio for MySQL.
This tool will help you to compare and synchronize two databases or a database project with specified database.
Also there is separate tool - dbForge Schema Compare for MySQL.

Migration from MySQL to Postgresql with auto-increments - how?

I'm considering a MySQL to Postgresql migration for my web application, but I'm having a really hard time converting my existing MySQL database to Postgresql.
I tried :
mysldump with --compatible=postgresql
migration wizard from EnterpriseDB
Postgresql Data Wizard from EMS
DBConvert from DMSoft
and NONE of the above programs do a good job converting my database!
I saw some Perl and Python scripts for converting mysql to postgresql, but I can't figure out how to use them....(I installed ActivePerl and don't understand what I'm supposed to do next to run that script!)
I use Auto Increment fields (as a primary key) all the time, and these are just ignored... I understand that Postgresql does auto-increments in another way (with sequences), but it can't be THAT hard for MIGRATION software to implement that, or is it?
Did anybody have better luck converting a MySQL database with auto-increments as primary keys?
I know this is probably not the answer you are looking for, but: I don't believe in "automated" migration tools.
Take your existing SQL Scripts that create your database schema, do a search and replace for the necessary data types (autonumber maps to serial which does all the sequence handling automagically for you), remove all the "engine=" stuff and then run the new script against Postgres.
Dump the old database into flat files and import them into the target.
I have done this several times with sample databases that were intended for MySQL and it really doesn't take that long.
Probably just as long as trying all the different "automated" tools.
Why not use an ETL Tool? you dont have to worry about dumps or stuff like that.
I have migrated to PostgresSQL and MySQL and have had no problems with the auto increment fields.
You just need to know the connection credentials and thats it. I personally use Pentaho ( it's open source ).
Download Pentaho ETL from http://kettle.pentaho.org/
Unzip and run Pentaho (using .bat file spoon.bat)
Create a new Job:
Create DB connection for source data base (PostgreSQL) - using menu: Tools→Wizard→Create DataBase Connection (F3) Create DB connection for destination data base (Mysql) - using technique described above.
Run the Wizard: Tools → Wizard → Copy Tables (Ctrl-F10).
Select source (left dialog panel), and destination (left dialog panel). Click Finish.
The Job will be generated - Run the job.
If you need any help let me know.
Even when you familiar with all "PostgreSQL gotchas", doing every step by hand may take a lot of time, especially when your db is "big".
Try some other scripts/tools.
I know this is an old question but I just ran into the same problem migrating from MySQL to Postgres. After trying several migration tools out the very best one I could find, which will migrate your database structure as cleanly as possible, was Pgloader https://github.com/dimitri/pgloader/ it will take care of changing the Auto Increment to Postgres sequences no problem and it's super fast.

How to collaborate on mysql schema?

I'm working with another dev and together we're building out a MySQL database. We've each got our own local instances of MySQL 5.1 on our dev machines. We've not yet been able to identify a way for us to be able to make a local schema change (eg: add a field and some values for that field) and then export some kind of script or diff file that the other can import in. I've looked into Toad and Navicat's synchronization features but they seem oriented towards synchronizing between two instances, not an instance and an intermediate file. We thought MySQL Workbench would be great but this but the synchronization feature just seems plain broken. Any other ideas? How do you collaborate with others on the schema?
First of all put your final SQL schema into version control. So you'll always have a version of it with all changes. It can be a plain SQL file. Every developer in the team can use it as starting point to created his copy database. All changes must be applied to it. This will help you to find conflicts faster.
Also I used such file to create a test database to run unit-tests after each submit. So we were always sure that production code is working.
Then you can use any migration tool to move changed between developers. Here is similar question about this:
Mechanisms for tracking DB schema changes
If you're using PHP then look at Doctrine migrations.