使用Databricks SQL如何提取表名和列名列表?

huangapple go评论72阅读模式
英文:

How do I pull table name with column name list using databricks sql?

问题

我正在使用Azure Databricks INFORMATION_SCHEMA。需要使用SQL列出所有表和列。

我使用SHOW TABLES命令来查看数据库中的所有表。使用SHOW COLUMNS命令来查看表中的所有列。我漏掉了什么?如何使用SQL获取表名和列名信息的组合列表?

英文:

I am using Azure Databricks INFORMATION_Schema. Need list all tables and columns using SQL.

I used SHOW TABLES command for viewing all tables within a database. SHOW COLUMNS command for viewing all columns within a table. What am I missing? How do I get combine list of table name, and column name information using sql?

答案1

得分: 1

Ideally you are using Unity Catalog, in which case Databricks has you covered with a built-in system table containing all table and column information in the system.information_schema schema.

The system.information_schema.columns table has information about the columns, but also has the table_name column that relates it to the table the column is part of.

https://docs.databricks.com/sql/language-manual/sql-ref-information-schema.html

TL;DR With Unity Catalog:

SELECT 
  CONCAT(table_catalog, '.', table_schema, '.', table_name) AS `table`,
  column_name,
  data_type
FROM system.information_schema.columns
ORDER BY 1
英文:

Ideally you are using Unity Catalog, in which case Databricks has you covered with a built-in system table containing all table and column information in the system.information_schema schema.

The system.information_schema.columns table has information about the columns, but also has the table_name column that relates it to the table the column is part of.

https://docs.databricks.com/sql/language-manual/sql-ref-information-schema.html

TL;DR With Unity Catalog:

SELECT 
  CONCAT(table_catalog, '.', table_schema, '.', table_name) AS `table`,
  column_name,
  data_type
FROM system.information_schema.columns
ORDER BY 1

答案2

得分: 0

以下是您要翻译的内容:

database_list = ["database_1", "database_2"]

for database in database_list:
  for row in spark.sql(f"show tables from {database}").collect():
    full_table_name = row["database"] + '.' + row["tableName"]
    print(full_table_name)
    spark.sql(f"describe {full_table_name}").show()

请告诉我如果您需要更多的翻译帮助。

英文:

Example python notebook (without Unity Catalog)

database_list = ["database_1", "database_2"]

for database in database_list:
  for row in spark.sql(f"show tables from {database}").collect():
    full_table_name = row["database"] + '.' + row["tableName"]
    print(full_table_name)
    spark.sql(f"describe {full_table_name}").show()

huangapple
  • 本文由 发表于 2023年7月18日 09:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定