Is there a tool to generate a full database DDL fo

2020-06-30 15:17发布

问题:

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:

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.



回答2:

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:

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.



回答4:

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



回答5:

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.



回答6:

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



回答7:

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



回答8:

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