26

Using Toad for Oracle, I can generate full DDL files describing all tables, views, source code (procedures, functions, packages), sequences, and grants of an Oracle schema. A great feature is that it separates each DDL declaration into different files (a file for each object, be it a table, a procedure, a view, etc.) so I can write code and see the structure of the database without a DB connection. The other benefit of working with DDL files is that I don't have to connect to the database to generate a DDL each time I need to review table definitions. In Toad for Oracle, the way to do this is to go to Database -> Export and select the appropriate menu item depending on what you want to export. It gives you a nice picture of the database at that point in time.

Is there a "batch" tool that exports
- all table DDLs (including indexes, check/referential constraints)
- all source code (separate files for each procedure, function)
- all views
- all sequences
from SQL Server?

What about PostgreSQL?
What about MySQL?
What about Ingres?

I have no preference as to whether the tool is Open Source or Commercial.

1
  • I would have liked to award the answer to more than one answer, as each answer is DB specific and the question was asked of most major DB systems.
    – user78706
    Apr 29, 2010 at 3:28

8 Answers 8

44

For SQL Server:

In SQL Server Management Studio, right click on your database and choose 'Tasks' -> 'Generate Scripts'.

You will be asked to choose which DDL objects to include in your script.

3
  • While I was looking for a fully scripted option, this is the best answer I have ever found!
    – CoveGeek
    Feb 10, 2016 at 21:12
  • Thanks! I first downvoted and added a reaction, but then I found out I misread your answer.
    – Leonard
    Sep 12, 2019 at 11:01
  • How to make the export sorted alphabetically? May 4, 2020 at 9:46
9

In PostgreSQL, simply use the -s option to pg_dump. You can get it as a plain sql script (one file for the whole database) on in a custom format that you can then throw a script at to get one file per object if you want it.

The PgAdmin tool will also show you each object's SQL dump, but I don't think there's a nice way to get them all at once from there.

6

For mysql, I use mysqldump. The command is pretty simple.

$ mysqldump [options] db_name [tables]

$ mysqldump [options] --databases db_name1 [db_name2 db_name3...]

$ mysqldump [options] --all-databases

Plenty of options for this. Take a look here for a good reference.

5

In addition to the "Generate Scripts" wizard in SSMS you can now use mssql-scripter which is a command line tool to generate DDL and DML scripts.

It's an open source and Python-based tool that you can install via: pip install mssql-scripter.

Here's an example of what you can use to script the database schema and data to a file. mssql-scripter -S localhost -d AdventureWorks -U sa --schema-and-data > ./adventureworks.sql More guidelines: https://github.com/Microsoft/sql-xplat-cli/blob/dev/doc/usage_guide.md

And here is the link to the GitHub repository: https://github.com/Microsoft/sql-xplat-cli

3

MySQL has a great tool called MySQL workbench that lets you reverse and forward engineer databases, as well as synchronize, which I really like. You can view the DDL when executing these functions.

3

I wrote SMOscript which does what you are asking for (referring to MSSQL Server)

1

Following what Daniel Vassallo said, this worked for me:

pg_dump -f c:\filename.sql -C -n public -O -s -d Moodle3.1 -h localhost -p 5432 -U postgres -w
0

try this python-based tool: Yet another script to split PostgreSQL dumps into object files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.