22

我想要1 个查询结果中的 MySql 表的所有CREATE语句。

例如,INFORMATION_SCHEMA包含所有表名、注释等,但是CREATE语句存储在 MySql 的哪里?可以在一个查询中检索所有表吗?

目前我正在为 1 个表检索TABLE ddl,如下所示。我有 100 张桌子,所以我可以每次都重复同样的事情,这是一个耗时的过程

show create table row_format;
4

4 回答 4

26

mysqldump -h localhost -u root -p --no-data --compact  some_db


mysqldump -d --compact --compatible=mysql323 ${dbname}|egrep -v "(^SET|^/\*\!)"

如何使用 mysqldump 仅导出 CREATE TABLE 命令

于 2013-08-05T05:16:16.313 回答
0

I needed this today, and the mysqldump answer generated me a .sql file that I could not import: it declared full CREATE TABLE statements with foreign keys pointing to tables it had not declared yet.

Instead, I used Liquibase:

  1. Download MySQL Connector/J (pick Platform Independent): https://dev.mysql.com/downloads/connector/j/

  2. From the directory where you downloaded the connector jar, create a liquibase.properties file and edit it like so:

outputDefaultSchema=true
includeSchema=true
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/replace_with_your_db
username=replace with your MySQL user
password=replace with your MySQL password
classpath=full path of the MySQL connector, for instance C:\mysql-connector-java-8.0.21.jar
  1. Install Liquibase then run:
liquibase --changeLogFile=schema.mysql.sql generateChangeLog

This will generate SQL commands for your schema, all in the right order.

For instance:

CREATE TABLE address (id BIGINT AUTO_INCREMENT NOT NULL, line VARCHAR(128) NULL, owner_id BIGINT NOT NULL);
CREATE TABLE person (id BIGINT AUTO_INCREMENT NOT NULL, name VARCHAR(64) NOT NULL);
ALTER TABLE address ADD CONSTRAINT fk_address_person FOREIGN KEY (owner_id) REFERENCES person (id) ON UPDATE RESTRICT ON DELETE RESTRICT;
于 2021-02-12T22:33:58.143 回答
-2

有人刚刚向我展示了一种更简单的方法。

  1. 突出显示所有表格
  2. 右键点击。
  3. 选择“复制到剪贴板”,然后选择“创建语句”。
  4. 转到文本编辑器并粘贴,一切都在那里。
于 2020-06-15T17:30:56.823 回答
-2
  1. 选择所需的数据库
  2. 选择要显示创建表的表(检查所有 表)
  3. 单击带有已选择的下拉菜单:并选择显示创建
于 2021-07-09T13:35:03.373 回答