Set Databricks Table Description From Code

Problem

I’d like to add table descriptions when they are viewed in data explorer in Databricks or Databricks SQL, like right here:

Solution

Table descriptions are stored in table properties and can be accessed via SQL. The actual property that is used to display description is called comment, therefore we just need to set it. Here is some code to do that:

def set_table_property(spark: SparkSession, table_name: str, property_name: str, value: str) -> None:
    spark.sql(f"""ALTER TABLE {table_name} SET TBLPROPERTIES ("{property_name}" = "{value}")""")


def set_table_comment(spark: SparkSession, table_name: str, comment: str) -> None:
    set_table_property(spark, table_name, "comment", comment)
    
set_table_comment(spark, "mytable", "Wonderful table that solves all your issues!")

Effect:

Em, excuse me! Have Android 📱 and use Databricks? You might be interested in my totally free (and ad-free) Pocket Bricks . You can get it from Google Play too: Get it on Google Play

Have a question⁉ Contact me.