通过在 MySQL 和 SQL Server 上查询生成表 DDL

发布于 2024-08-07 18:03:17 字数 311 浏览 7 评论 0原文

有没有一种简单的方法可以使用 Ms 或 My SQL 服务器通过查询来提取表 DDL 信息? (最好两者都?)

例如,使用 MySQL Administrator / Navicat for MySql,有一个“DDL”函数,它生成“create table foo (....)”脚本。

有什么方法可以从查询本身获取此信息,例如:

Select DDL from foo where table_name='bar';

有“创建表栏(.....)”返回给我吗?

如果没有 - 有什么建议吗?

Is there an easy way to extract table DDL information, via a query, using either Ms or My SQL server? (preferably both?)

For example, using MySQL Administrator / Navicat for MySql, there is a "DDL" function, which generates the "create table foo (....)" script.

Is there any way to get this information from a query itself, such as:

Select DDL from foo where table_name='bar';

Any have the "Create table bar (.....)" returned to me?

If not - any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

z祗昰~ 2024-08-14 18:03:17

它是 mysql 特定的,但是 SHOW CREATE TABLE 为您提供表的 DDL。

it's mysql-specific, but SHOW CREATE TABLE <table-name> gives you the DDL for a table.

北座城市 2024-08-14 18:03:17

你必须自己创造它。

您可以查询 INFORMATION_SCHEMA.COLUMNS 以获取列名称和数据类型。

You have to create that yourself.

You can query INFORMATION_SCHEMA.COLUMNS for the column name and data type.

陌生 2024-08-14 18:03:17

您无法以跨平台方式获取 CREATE Table 文本,但您可以从 INFORMATION_SCHEMA 视图获取足够的信息来自行构建它。

You can't get the CREATE Table text in a cross platform way, but you can get enough information to build it yourself from the INFORMATION_SCHEMA views.

笑忘罢 2024-08-14 18:03:17

我想你正在寻找这样的东西。

这些查询生成的结果可以复制和粘贴,也可以用于自动化:

基于 "在 MySQL 每个表的字段中搜索文本的查询数据库”

-- KEY
-- CHAR(96) IS BACKTICK
-- CHAR(46) IS PERIOD
-- CHAR(59) IS SEMI-COLON


SELECT
CONCAT('SHOW CREATE TABLE '
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `TABLE_SCHEMA`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(46) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `TABLE_NAME`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(59) AS CHAR(8) CHARACTER SET LATIN1)
      ) AS QUERY_GENERATOR
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE
      UPPER(`TABLE_SCHEMA`)
        NOT IN (
                'INFORMATION_SCHEMA'
              , 'MYSQL'
              , 'PERFORMANCE_SCHEMA'
              , 'SAKILA'
              , 'SYS'
              , 'WORLD'
               )
  AND UPPER(`TABLE_TYPE`) = 'BASE TABLE'
ORDER BY 1;

视图


SELECT
CONCAT('SHOW CREATE VIEW '
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `TABLE_SCHEMA`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(46) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `TABLE_NAME`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(59) AS CHAR(8) CHARACTER SET LATIN1)
      ) AS QUERY_GENERATOR
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE
      UPPER(`TABLE_SCHEMA`)
        NOT IN (
                'INFORMATION_SCHEMA'
              , 'MYSQL'
              , 'PERFORMANCE_SCHEMA'
              , 'SAKILA'
              , 'SYS'
              , 'WORLD'
               )
  AND UPPER(`TABLE_TYPE`) = 'VIEW'
ORDER BY 1
;

存储过程/例程


SELECT
CONCAT('SHOW CREATE PROCEDURE '
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `ROUTINE_SCHEMA`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(46) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `ROUTINE_NAME`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(59) AS CHAR(8) CHARACTER SET LATIN1)
      ) AS QUERY_GENERATOR
FROM `INFORMATION_SCHEMA`.`ROUTINES`
WHERE
      UPPER(`ROUTINE_SCHEMA`)
        NOT IN (
                'INFORMATION_SCHEMA'
              , 'MYSQL'
              , 'PERFORMANCE_SCHEMA'
              , 'SAKILA'
              , 'SYS'
              , 'WORLD'
               )
ORDER BY 1
;

I think you are looking for something like this.

These queries generate results that you can copy and paste or use to automate:

Queries based off of "Search text in fields in every table of a MySQL database"

-- KEY
-- CHAR(96) IS BACKTICK
-- CHAR(46) IS PERIOD
-- CHAR(59) IS SEMI-COLON

Tables


SELECT
CONCAT('SHOW CREATE TABLE '
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `TABLE_SCHEMA`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(46) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `TABLE_NAME`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(59) AS CHAR(8) CHARACTER SET LATIN1)
      ) AS QUERY_GENERATOR
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE
      UPPER(`TABLE_SCHEMA`)
        NOT IN (
                'INFORMATION_SCHEMA'
              , 'MYSQL'
              , 'PERFORMANCE_SCHEMA'
              , 'SAKILA'
              , 'SYS'
              , 'WORLD'
               )
  AND UPPER(`TABLE_TYPE`) = 'BASE TABLE'
ORDER BY 1;

Views


SELECT
CONCAT('SHOW CREATE VIEW '
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `TABLE_SCHEMA`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(46) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `TABLE_NAME`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(59) AS CHAR(8) CHARACTER SET LATIN1)
      ) AS QUERY_GENERATOR
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE
      UPPER(`TABLE_SCHEMA`)
        NOT IN (
                'INFORMATION_SCHEMA'
              , 'MYSQL'
              , 'PERFORMANCE_SCHEMA'
              , 'SAKILA'
              , 'SYS'
              , 'WORLD'
               )
  AND UPPER(`TABLE_TYPE`) = 'VIEW'
ORDER BY 1
;

Stored Procedures/Routines


SELECT
CONCAT('SHOW CREATE PROCEDURE '
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `ROUTINE_SCHEMA`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(46) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1), `ROUTINE_NAME`, CAST(CHAR(96) AS CHAR(8) CHARACTER SET LATIN1)
     , CAST(CHAR(59) AS CHAR(8) CHARACTER SET LATIN1)
      ) AS QUERY_GENERATOR
FROM `INFORMATION_SCHEMA`.`ROUTINES`
WHERE
      UPPER(`ROUTINE_SCHEMA`)
        NOT IN (
                'INFORMATION_SCHEMA'
              , 'MYSQL'
              , 'PERFORMANCE_SCHEMA'
              , 'SAKILA'
              , 'SYS'
              , 'WORLD'
               )
ORDER BY 1
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文