datagrip转换mysql建表语句
时间: 2023-11-23 12:54:52 浏览: 131
通过使用all_tab_columns和all_col_comments两个数据字典,可以获取对应的表名、列表、类型、长度、注释,然后将其转换成mysqlDDL语句,从而实现datagrip转换mysql建表语句的功能。具体步骤如下:
```sql
-- SQL
SELECT
CONCAT(
'CREATE TABLE `',
a.table_name,
'` (',
GROUP_CONCAT(
CONCAT(
'`',
a.column_name,
'` ',
a.data_type,
IF(a.data_type IN ('varchar', 'char'), CONCAT('(', a.data_length, ')'), ''),
IF(a.nullable = 'N', ' NOT NULL', ''),
IF(a.column_id = b.column_id, ' PRIMARY KEY', ''),
' COMMENT ''',
b.comments,
''''
)
ORDER BY a.column_id
SEPARATOR ',\n'
),
'\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;'
) AS ddl
FROM
all_tab_columns a
INNER JOIN all_col_comments b ON a.owner = b.owner AND a.table_name = b.table_name AND a.column_name = b.column_name
WHERE
a.owner = 'schema_name'
AND a.table_name = 'table_name'
GROUP BY
a.owner,
a.table_name;
```
其中,schema_name和table_name需要替换成要转换的库名和表名。此外,需要注意表名大小写。将以上代码复制到datagrip中执行即可得到mysql建表语句。
阅读全文