是否有为 SQL Server 生成完整数据库 DDL 的工具?Postgres 和 MySQL 呢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1929815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a tool to generate a full database DDL for SQL Server? What about Postgres and MySQL?
提问by Daniel Vassallo
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.
使用 Toad for Oracle,我可以生成完整的 DDL 文件,描述 Oracle 模式的所有表、视图、源代码(过程、函数、包)、序列和授权。一个很棒的功能是它将每个 DDL 声明分成不同的文件(每个对象的文件,可以是表、过程、视图等),因此我可以编写代码并查看数据库的结构,而无需数据库连接. 使用 DDL 文件的另一个好处是,每次我需要查看表定义时,我都不必连接到数据库来生成 DDL。在 Toad for Oracle 中,执行此操作的方法是转到 Database -> Export 并根据您要导出的内容选择相应的菜单项。它为您提供了当时数据库的精美图片。
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?
是否有一个“批处理”工具可以导出
——所有表 DDL(包括索引、检查/引用约束)
——所有源代码(每个过程、函数的单独文件)
——所有视图
——
来自 SQL Server 的所有序列?
What about PostgreSQL?
What about MySQL?
What about Ingres?
PostgreSQL 怎么样?
MySQL呢?
安格尔呢?
I have no preference as to whether the tool is Open Source or Commercial.
对于该工具是开源的还是商业的,我没有偏好。
采纳答案by Magnus Hagander
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.
在 PostgreSQL 中,只需对 pg_dump 使用 -s 选项。您可以将其作为自定义格式的普通 sql 脚本(整个数据库的一个文件)获取,然后您可以根据需要抛出一个脚本以获取每个对象的一个文件。
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.
PgAdmin 工具还会向您显示每个对象的 SQL 转储,但我认为没有什么好方法可以从那里一次性获取它们。
回答by Daniel Vassallo
For SQL Server:
对于 SQL 服务器:
In SQL Server Management Studio, right click on your database and choose 'Tasks' -> 'Generate Scripts'.
在 SQL Server Management Studio 中,右键单击您的数据库并选择“任务”->“生成脚本”。
You will be asked to choose which DDL objects to include in your script.
系统会要求您选择要包含在脚本中的 DDL 对象。
回答by Carl
For mysql, I use mysqldump. The command is pretty simple.
对于 mysql,我使用 mysqldump。命令非常简单。
$ mysqldump [options] db_name [tables]
$ mysqldump [options] --databases db_name1 [db_name2 db_name3...]
$ mysqldump [options] --all-databases
$ mysqldump [选项] db_name [表]
$ mysqldump [选项] --databases db_name1 [db_name2 db_name3...]
$ mysqldump [选项] --all-databases
Plenty of options for this. Take a look herefor a good reference.
对此有很多选择。看看这里作为一个很好的参考。
回答by Tara Raj
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.
除了 SSMS 中的“生成脚本”向导,您现在还可以使用 mssql-scripter,它是一个命令行工具来生成 DDL 和 DML 脚本。
It's an open source and Python-based tool that you can install via: pip install mssql-scripter.
它是一个基于 Python 的开源工具,您可以通过以下方式安装: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
下面是一个示例,说明您可以使用什么脚本将数据库架构和数据写入文件。mssql-scripter -S localhost -d AdventureWorks -U sa --schema-and-data > ./adventureworks.sql 更多指南:https: //github.com/Microsoft/sql-xplat-cli/blob/dev/doc/用法指南.md
And here is the link to the GitHub repository: https://github.com/Microsoft/sql-xplat-cli
这是 GitHub 存储库的链接:https: //github.com/Microsoft/sql-xplat-cli
回答by Andy West
MySQL has a great tool called MySQL workbenchthat lets you reverse and forward engineer databases, as well as synchronize, which I really like. You can view the DDL when executing these functions.
MySQL 有一个很棒的工具叫做MySQL workbench,它可以让你反向和正向工程数据库,以及同步,我真的很喜欢。您可以在执行这些功能时查看 DDL。
回答by devio
回答by shcherbak
try this python-based tool: Yet another script to split PostgreSQL dumps into object files
试试这个基于 python 的工具:另一个脚本将 PostgreSQL 转储拆分为目标文件
回答by Karl
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