How to order INSERTs after TRIGGER-statements with MySQL Workbench - mysql

Okay guys, my problem is kind of a first world problem but it bugs me...
I'm designing my MySQL database with MySQL Workbench and after drawing all tables, triggers and INSERTs I let the Workbench generate the CREATE-SQL for me. That is really cool but here is the problem:
One of my triggers is a AFTER INSERT trigger that relies on a INSERT I
defined in Workbench, too. But because the triggers are created after the INSERTs, the trigger doesn't fire.
Of course I can just open a text editor and copy all of my INSERTs after the triggers, but yeah thats the point that bugs me. I could also write a script, but my question is:
Can I change the order of the exported CREATE-SQL in MySQL Workbench?

Short answer: no, you can't reorder these statements.
Long answer: the INSERTs data in a model are not to be used as general data store for your database, they are simply not made for that. Their purpose is to allow entering test data after synchronisation/forward engineering. A better approach is to add a separate SQL script to your model (see the overview page for notes and scripts). Once you are done with your sync you can then use the script to run additional code. I have to admit however that the integration of those scripts is not so good currently. You have to copy the sql text and paste it in an editor of an open connection.

Related

Moving and Copying Columns Between Tables in MySQL Workbench

I'm using the latest version of MySQL Workbench 5.2.40.
In the EER Diagram, is there a way to copy/paste columns between tables, as well as move columns from one table to another using drag and drop?
I don't see any any such feature now. If this is the case, it would be difficult to do preliminary design using Workbench considering that it will be tedious to modify or correct your designs later on. I might as well go straight to the physical design using PhpMyAdmin since I can copy-paste portions of SQL statements when altering tables.
Any work-around modifying columns in Workbench? Thanks
The current version of MySQL Workbench is 6.0.7 and yes, you can copy and paste column definitions using the context menu in the table editor (not directly in the diagram). Just open the tables you want to edit. Usually, the editor is reused if an object of the same type is opened for editing, but there's an entry in the context menu of the objects that allows to open the object in a new tab. This way you can have two table editors on 2 tabs and can easily switch back and forth for copying e.g. column definitions.
I handle these cases this way :
do the modifications on the physical tables (using pma, or the "schema editor" tab of mwb, or the command-line)
then synchronize database and model
In fact, I'd find useful to be able to manipulate the schema directly in SQL; GUI would then (as pma is) simply be a more convenient way to to some actions ('Create a table' would generate, then execute, an SQL 'CREATE TABLE' statement)
But it would probably mean a redesign from scratch - at least for the way the schema is stored.

How to execute SQL queries inside MySQL workbench

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

Put trigger in MySQL database to update Oracle database?

I want to create an insert trigger on MySQL which will automatically insert the record into an Oracle database. I would like to know if there are people that have experience to share on this topic.
Cheers
Invoke a script as is done in this example that calls the Oracle code.
Note: you lose support for transactions (there will be no built-in rollback for the Oracle database) when you perform this type of cascading, and you also will take a likely very large performance hit in doing so. The script could turn around and simply call Java code or some other executable that invokes your some generic code to insert into Oracle, or it could be a raw query that gets passed arguments from the script.
This is almost certainly a bad idea because of the odd side-effect behavior, but it's one that can be implemented. I think that you would be much better off having the code to do this against two different DataSources (in Java/.NET speak) rather than have a hidden script in a MySQL trigger that screams unmaintainable, as well as hidden failure for future developers.

How do I automatically execute a MySQL script or routine after forward engineering from model

I've built a EER Model in MySQL Workbench that I forward engineer to create the database. The forward engineering works perfectly, and the database is created from the diagram as expected.
Apart from tables, there are also some Stored Procedures (aka Routines) that I've included in the model. These routines are designed to only be run once, as soon as the database has been set up. They automatically insert necessary data into the tables.
My question is, how can I get the forward engineering process to automatically call/execute one of these routines once the tables have been created.
At the moment, I have to forward engineer the database, and then manually call the stored procedures?
In your EER diagram on the workbench right click on a table and select edit table. This will open a pane at the bottom with a couple of tabs. The tabs are table, columns, indexes, foreign key, etc. There is a tab called insert. This tab allows you to insert records into the Model database.
When you click on the insert tab it will show a grid. Add the records you want to insert onto this grid. Make sure you commit these records. See screenshot for example.
Now when you forward engineer the database on the very first screen there is an option to Generate insert statements. Tick the option forward engineer and the data you want inserted will be scripted when you create the model. Save the script so you can run it over and over without going into the MySQL workbench.
I have not found options to update, delete or do other data manipulation in the workbench but I think this is what you are looking for.
NOTE : To directly import the records, you won't be able to do that via the workbench; there is no option. You can save the records to file. However to import/create them you would need to add them one at a time (from the modeller). You could however make a backup of the MySQL database with the records in already. Then copy those INSERT Statements from the MYSQL backup script into your setup script.
Steps would be:
Create database.
Import the files with the setup/config records into newly created database
Backup database
Open backup file, then copy and paste the INSERT statements you are looking for into the setup script created by the MySQL Workbench
UPDATE:
I did some experimenting when you get to the review script to be executed step in the forward engineering you can also at the end call the stored procedures (as you mentioned) by editing the script. Once done save the script to file and test.
Hope that helps!

How to synchronize development and production database

Do you know any applications to synchronize two databases - during development sometimes it's required to add one or two table rows or new table or column.
Usually I write every sql statement in some file and during uploading path I evecute those lines on my production database (earlier backing it up).
I work with mySQL and postreSQL databases.
What is your practise and what applications helps you in that.
You asked for a tool or application answer, but what you really need is a a process answer. The underlying theme here is that you should be versioning your database DDL (and DML, when needed) and providing change scripts to be able to update any version of your database to a higher version.
This set of links provided by Jeff Atwood and written by K. Scott Allen explain in detail what this ought to look like - and they do it better than I can possibly write up here: http://www.codinghorror.com/blog/2008/02/get-your-database-under-version-control.html
For PostgreSQL you could use Another PostgreSQL Diff Tool . It can diff two SQL Dumps very fast (a few seconds on a db with about 300 tables, 50 views and 500 stored procedures). So you can find your changes easily and get a sql diff which you can execute.
From the APGDiff Page:
Another PostgreSQL Diff Tool is simple PostgreSQL diff tool that is useful for schema upgrades. The tool compares two schema dump files and creates output file that is (after some hand-made modifications) suitable for upgrade of old schema.
Have scripts (under source control of course) that you only ever add to the bottom off. That combined with regular restores from your production database to dev you should be golden. If you are strict about it, this works very well.
Otherwise I know lots of people use redgate stuff for SQLServer.
Another vote for RedGate SQL Compare
http://www.red-gate.com/products/SQL_Compare/index.htm
Wouldn't want to live without it!
Edit: Sorry, it seems this is only for SQL Server. Still - if any SQL Server users have the same question I'd definitely recommend this tool.
If you write your SQL statements for your development database (which are, I imagine, series of DDL instructions such as CREATE, ALTER and DROP), why don't you keep track of them by recording them in a table, with a "version" index? You will then be able to:
track your version changes
make a small routine allowing the "automatic" update of your production database by sending the recorded instructions to the database.
I really like the EMS tools.
There tools are available for all popular DB's and you have the same user experience for every type of DB.
One of the tools is the DB Comparer.
TOAD
saved many an ass several times in the past. Why do people run sql with no exit strategy?
the redgate one is good also.
Siebel (CRM, Sales, etc. management product) has a built-in tool to align the production database with the development one (dev2prod).
Otherwise, you've got to stick with manually executed scripts.
Navicat has a structure synchronisation wizard that handles this.
I solve this by using Hibernate. It can detect and autocreate missing tables, columns, etc.
You could add some automation to your current way of doing things by using dbDeploy or a similar script. This will allow you to keep track of your schema changes and to upgrade/rollback your schema as you see fit.
Here's a straight linux bash script I wrote for syncing Magento databases... but you can easily modify it for other uses :)
http://markshust.com/2011/09/08/syncing-magento-instance-production-development
DBV - "Database version control, made easy!" (PHP)