ClickHouce 查询某个数据库中的所有表名、某些表的字段名、字段类型、字段注释

有时我们需要对数据进行探究,首先想到的就是该表有哪些字段,以及字段的类型和注释,那么我们该如何快速的知道呢?

1、查询 Test 数据库 的 所有表,以及对应的建表语句

select database,name,create_table_query
from `system`.tables
where database = 'Test'

2、查询 某些表的 所有字段名、字段类型以及字段注释:

select table,name,type,comment
from `system`.columns
where table in('test_1','test_2','test_3')

仅供参考

可以使用如下的SQL语句来获取dwd库所有的信息: ``` SELECT table_name, table_comment FROM information_schema.tables WHERE table_schema = 'dwd' ``` 这个语句会返回dwd库所有表名注释。 接下来,可以使用以下的SQL语句来获取每个字段名字段注释: ``` SELECT column_name, column_comment FROM information_schema.columns WHERE table_schema = 'dwd' AND table_name = '表名' ``` 将上面的SQL语句的“表名”替换成具体的表名,就可以获取该的所有字段名字段注释。 如果需要将所有的信息都导出来,可以使用脚本来自动化执行以上的SQL查询,并将结果保存到文件。以下是一个Python脚本的示例: ``` import pymysql # 连接数据库 db = pymysql.connect(host='localhost', user='root', password='密码', db='dwd') # 获取所有的信息 cursor = db.cursor() cursor.execute("SELECT table_name, table_comment FROM information_schema.tables WHERE table_schema = 'dwd'") tables = cursor.fetchall() # 获取每个字段信息并输出到文件 with open('output.txt', 'w', encoding='utf-8') as f: for table in tables: table_name = table[0] table_comment = table[1] f.write(f'Table Name: {table_name}\nTable Comment: {table_comment}\n') cursor.execute(f"SELECT column_name, column_comment FROM information_schema.columns WHERE table_schema = 'dwd' AND table_name = '{table_name}'") columns = cursor.fetchall() for column in columns: column_name = column[0] column_comment = column[1] f.write(f'Column Name: {column_name}\nColumn Comment: {column_comment}\n') f.write('\n') # 关闭数据库连接 db.close() ``` 这个脚本会将所有的信息输出到一个名为“output.txt”的文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值