---
title: create_table
description: Create a managed table written by append flows in Lakeflow pipeline with the `create_table()` Python function.
canonical_url: https://docs.databricks.com/gcp/en/ldp/developer/ldp-python-ref-create-table
---

# create\_table

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

Use the `create_table()` function in a pipeline to create a managed table, written by one or more [append\_flow](https://docs.databricks.com/gcp/en/ldp/developer/ldp-python-ref-append-flow.md) declarations. Pair the `create_table()` call with one or more `@append_flow(target=...)` decorators that write into the table. Multiple flows can target the same managed table.

For the SQL equivalent, see [CREATE TABLE ... FLOW](https://docs.databricks.com/gcp/en/ldp/developer/ldp-sql-ref-create-table-flow.md).

## Syntax

```python
from pyspark import pipelines as dp

dp.create_table(
  name = "<table-name>",
  comment = "<comment>",
  spark_conf={"<key>" : "<value>", "<key>" : "<value>"},
  table_properties={"<key>" : "<value>", "<key>" : "<value>"},
  partition_cols=["<partition-column>", "<partition-column>"],
  path="<storage-location-path>",
  schema="schema-definition",
  expect_all = {"<key>" : "<value>", "<key>" : "<value>"},
  expect_all_or_drop = {"<key>" : "<value>", "<key>" : "<value>"},
  expect_all_or_fail = {"<key>" : "<value>", "<key>" : "<value>"},
  cluster_by = ["<clustering-column>", "<clustering-column>"],
  cluster_by_auto = False,
  row_filter = "row-filter-clause",
  private = False,
  override_schema = "partial-schema-definition",  # Mutually exclusive with schema.
  copy = [dp.CopyOptions.<option>, ...],
  nocopy = [dp.CopyOptions.<option>, ...]
)
```

## Parameters

| Parameter                                                | Type                   | Description                                                                                                                                                                                                                                                                                                                                                                                                               |
| -------------------------------------------------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`                                                   | `str`                  | Required. The table name.                                                                                                                                                                                                                                                                                                                                                                                                 |
| `comment`                                                | `str`                  | A description for the table.                                                                                                                                                                                                                                                                                                                                                                                              |
| `spark_conf`                                             | `dict`                 | A list of Spark configurations for the execution of this query.                                                                                                                                                                                                                                                                                                                                                           |
| `table_properties`                                       | `dict`                 | A `dict` of [table properties](https://docs.databricks.com/gcp/en/ldp/properties.md) for the table.                                                                                                                                                                                                                                                                                                                       |
| `partition_cols`                                         | `list`                 | A list of one or more columns to use for partitioning the table.                                                                                                                                                                                                                                                                                                                                                          |
| `path`                                                   | `str`                  | A storage location for table data. If not set, use the managed storage location for the schema containing the table.                                                                                                                                                                                                                                                                                                      |
| `schema`                                                 | `str` or `StructType`  | The complete schema definition for the table. Schemas can be defined as a SQL DDL string or with a Python `StructType`.                                                                                                                                                                                                                                                                                                   |
| `override_schema`                                        | `str` or `StructType`  | ([Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md)) In Python workspace pipelines, declares selected column definitions and infers the remaining query output columns. Accepts a SQL DDL string or `StructType`. Cannot be combined with `schema`. See [Use partial schema inference in Python Lakeflow pipelines](https://docs.databricks.com/gcp/en/ldp/developer/partial-schema-inference.md). |
| `expect_all`, `expect_all_or_drop`, `expect_all_or_fail` | `dict`                 | Data quality constraints for the table. Provides the same behavior and uses the same syntax as expectation decorator functions, but implemented as a parameter. See [Expectations](https://docs.databricks.com/gcp/en/ldp/developer/ldp-python-ref-expectations.md).                                                                                                                                                      |
| `cluster_by`                                             | `list`                 | Enable liquid clustering on the table and define the columns to use as clustering keys. See [Use liquid clustering for tables](https://docs.databricks.com/gcp/en/tables/clustering.md).                                                                                                                                                                                                                                  |
| `cluster_by_auto`                                        | `bool`                 | Enable automatic liquid clustering on the table. Can be combined with `cluster_by` to define the initial clustering keys. See [Use automatic liquid clustering](https://docs.databricks.com/gcp/en/tables/automatic-liquid-clustering.md).                                                                                                                                                                                |
| `row_filter`                                             | `str`                  | (Public Preview) A row filter clause for the table. See [Publish tables with row filters and column masks](https://docs.databricks.com/gcp/en/ldp/unity-catalog.md#row-filters-and-column-masks).                                                                                                                                                                                                                         |
| `private`                                                | `bool`                 | When `True`, creates a private table that is not published to the catalog and is only accessible within the pipeline. Defaults to `False`.                                                                                                                                                                                                                                                                                |
| `copy`                                                   | `list` of `CopyOption` | ([Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md)) Existing table metadata for refreshes to keep, even when the definition doesn't set it. Equivalent to the SQL `COPY` clause. See [Preserve table metadata across refreshes](https://docs.databricks.com/gcp/en/ldp/developer/ldp-python-ref-create-table.md#copy-nocopy).                                                                     |
| `nocopy`                                                 | `list` of `CopyOption` | ([Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md)) Existing table metadata for refreshes to drop. Equivalent to the SQL `NOCOPY` clause. `dp.CopyOptions.ALL` is not supported in `nocopy`.                                                                                                                                                                                                      |

## <a id="copy-nocopy" />Preserve table metadata across refreshes

> [!NOTE]
> **Beta**
>
> `copy` and `nocopy` are in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

Use `copy` and `nocopy` to control which existing table metadata, such as properties or comments added with `ALTER TABLE`, a refresh keeps. Build each list from `dp.CopyOptions` values:

| `dp.CopyOptions` value                                     | SQL equivalent                                          |
| ---------------------------------------------------------- | ------------------------------------------------------- |
| `ALL`                                                      | `*`                                                     |
| `TBLPROPERTIES`                                            | `TBLPROPERTIES`                                         |
| `tblproperty("<key>")`                                     | `TBLPROPERTY '<key>'`                                   |
| `COMMENT`                                                  | `COMMENT`                                               |
| `COLUMN_COMMENTS`                                          | `COLUMN COMMENTS`                                       |
| `CONSTRAINTS`, `CHECK_CONSTRAINTS`, `constraint("<name>")` | `CONSTRAINTS`, `CHECK CONSTRAINTS`, `CONSTRAINT <name>` |
| `DELETE_AFTER`                                             | `DELETE AFTER`                                          |
| `CLUSTER_BY`                                               | `CLUSTER BY`                                            |

`dp.CopyOptions` also defines `GRANTS`, `TAGS`, `tag()`, `COLUMN_TAGS`, `ROW_FILTER`, `COLUMN_MASKS`, `PRIMARY_KEY`, `FOREIGN_KEYS`, and `UNIQUE_CONSTRAINTS`. Managed tables don't support them. A pipeline update that uses them fails with `COPY_NO_COPY_UNSUPPORTED_OPTION`. List each option one time.

The behavior, default, requirements, and enablement match the SQL clauses. Errors match too, except that without the pipeline setting a Python definition fails with `UNSUPPORTED_FEATURE.COPY_NOCOPY`. See [Preserve table metadata across refreshes](https://docs.databricks.com/gcp/en/ldp/developer/ldp-sql-ref-create-table-flow.md#copy-nocopy).

## Limitations

* Managed tables do not support change data capture (CDC) change flows. `create_auto_cdc_flow()` or `create_auto_cdc_from_snapshot_flow()` targeting a managed table fails. Use [create\_streaming\_table()](https://docs.databricks.com/gcp/en/ldp/developer/ldp-python-ref-streaming-table.md) for CDC targets.
* Managed tables support only `append_flow`. REPLACE USING (`replace_flow`) and REPLACE WHERE flows are not supported.
* Managed tables are supported only in pipelines with Unity Catalog.
* You cannot reuse the name of an existing streaming table for a managed table.
* When a refresh drops a table or column comment, Unity Catalog can return the old comment. See [Limitations](https://docs.databricks.com/gcp/en/ldp/developer/ldp-sql-ref-create-table-flow.md#limitations).

## Example

```python
from pyspark import pipelines as dp

dp.create_table("combined")

@dp.append_flow(target="combined")
def from_a():
    return spark.readStream.table("source_a")

@dp.append_flow(target="combined")
def from_b():
    return spark.readStream.table("source_b")
```

The following example keeps table properties and comments added outside the pipeline, but drops the auto time-to-live setting:

```python
from pyspark import pipelines as dp

dp.create_table(
  "orders",
  copy=[dp.CopyOptions.TBLPROPERTIES, dp.CopyOptions.COMMENT, dp.CopyOptions.COLUMN_COMMENTS],
  nocopy=[dp.CopyOptions.DELETE_AFTER],
)

@dp.append_flow(target="orders")
def orders_flow():
    return spark.readStream.table("samples.tpch.orders")
```
