DEV Community

Cover image for Generating a whole database script in MySQL workbench #databases #MySQL #MySQL_workbench
Mohammad Mehadi Hasan
Mohammad Mehadi Hasan

Posted on

Generating a whole database script in MySQL workbench #databases #MySQL #MySQL_workbench

Hello there devs! I hope you guys are doing well and staying safe in this pandemic. So, as I took a database course at my university this semester and our teacher asked us to go for SQL first then learning NoSQL databases such as MongoDB.After the first half of the course, we had to create&design a whole working database in SQL and present it to the teacher as a project. As I knew MySQL syntax before I preferred to make my project in MySQL and as I am primarily a Windows user I am writing my SQL scripts in MySQL workbench.But it took me by surprise when our teacher asked us to send him only one SQL script which has everything he asked for in the first place.So to describe it a bit more I had to send him only one SQL script when executed it should create the database at the first place then the tables, views,stored procedures and functions as well.I was a little bit surprised because usually in the professional work environment you are supposed to make them in separate scripts and put them under source control so that your teammates can also work on it without messing up with the data inside. Anyway as I had to do it for my grades I had to look for this up on the internet and took me some time to get how to do it properly even tho I thought I knew how to use MySQL Workbench.So I am writing this just in case you are looking for it for the first time.
so at first we will go to the MySQL Workbench and connect a server you connected to before.Then go to the main menu and go to Server and then go to Data Export
Alt Text
After that, you will see the list of Database schemas you can select to export.In my case, I had to select only one.
Alt Text
So after selecting the Database Schema you can select the associated objects with it like the tables,views and stored procedures.Go ahead and select the objects you want to export in the SQL script.
Alt Text
Okay. This part is a little bit important.
You have 3 choices.

  • Dump Structure and Data
  • Dump Data only
  • Dump Structure only

So you probably can understand by the names what each option would do for you.If you only want to export the structure with no data you can select that or you can only export the data if that's what your task is.But in my case, I had to export both the structure and the data along with it so I went with Dump Structure and Data.
Alt Text
okay, we are almost done. Now we have the choice of putting our script in a folder or in a single file(depends however you prefer) and there is also a box where you can check it to include create schema meaning the Data Definition Language(DDL) or not.
Alt Text
So after selecting according to your needs you just have to click the

Start Export

button to generate a sql script of the whole schema and you can either put it under source control and share the link or you can directly send the .sql file by email.
I hope it helped you in some way.

Top comments (0)