How to export code lines to a .sql file

1

I have created a database, in mariadb, I need to save in my .sql file my database and the code lines I use to create it, for example if I did:

CREATE TABLE ejemplo (  id VARCHAR(20), x INT, PRIMARY KEY ( id,x ) FOREIGN KEY x REFERENCES otratabla(x) );

What I have to do is besides exporting my database, also the code lines of how I created it, with the create table, pks, fks, etc, it is not enough to create the base and after that the one that wants see do a show create table, my goal is that, create the table, put step by step as I did, that's what I mean by exporting the code lines.

The objective is to show my way of programming, looking only for ways to export the base itself, and not also the lines I used to make it, or different queries that I needed to make to it.

I understand that this is how it is exported:

mysqldump -u root -p LIBRARY > library-exit.sql

And responding to:

  

When you export the database, what it does is: It generates a file with SQL extension and if you open that file with a text editor and ... pum! You find all the code of your bd,   ...   tables and what has your tables, columns, data type etc.,   ...   and if you have entered data it also generates them in that same file. So, what do you really need?

Well, if it shows how I believe it line by line, ..., that answers part of my query, I would also like to know if there is any way to also save in my sql file, the queries I want to make to the base, and the results (tables) it would return.

    
asked by Ramiro 03.08.2016 в 23:16
source

1 answer

1

If I interpret your question correctly, you want the sentences that led you from create database MI_BASE_DE_DATOS to the current state of your DB.

Since mysqldump is not going to help you with it, it will give you the current picture of your DB and not the step by step. For example if you modified a table to add a column, you will only see creation of the table with all the columns.

What you want to do, I understand, falls into database versioning. Unfortunately, if you have not saved the scripts that you have been running, there is no tool that you know that allows you to get what you want.

There are different tools that make your task easier, I encourage you to look for them and try them.

But for me the simplest to do and that complies with what you are looking for is to put the SQL scripts that you run and consider useful under some version control system. How can it be git , subversion or Mercurial ).

There are several strategies on how to save the scripts, a very usual one is to prefix the execution order in the name of the ones as well as to put a brief description of what it does. For example:

  • 01_create_database_MiBaseDeDatos.sql
  • 02_create_table_PERSONAS.sql
  • 03_alter_table_PERSONAS_agregar_unique_index.sql

It is also common to keep your scripts as atomic as possible (one SQL statement per script).

For more details I advise you to look at the following article or perform a search on Google .

    
answered by 13.09.2016 в 00:37