CREATE SCHEMA

適用於:檢查標示為是 Databricks SQL 檢查標示為是 Databricks Runtime

建立具有指定名稱的架構(資料庫)。 如果具有相同名稱的架構已經存在,則會擲回例外狀況。

語法

CREATE SCHEMA [ IF NOT EXISTS ] schema_name
    [ COMMENT schema_comment ]
    [ LOCATION schema_directory | MANAGED LOCATION location_path ]
    [ WITH DBPROPERTIES ( { property_name = property_value } [ , ... ] ) ]

參數

  • schema_name

    要建立之架構的名稱。

    目錄中建立的 hive_metastore 架構只能包含英數位元 ASCII 字元和底線(INVALID_SCHEMA_OR_RELATION_NAME)。

  • 如果不存在

    如果架構不存在,則建立具有指定名稱的架構。 如果具有相同名稱的架構已經存在,則不會發生任何動作。

  • 位置 schema_directory

    LOCATION Unity 目錄不支援。 如果您要在 Unity 目錄中指定架構的儲存位置,請使用 MANAGED LOCATION

    schema_directory 是常 STRING 值。 要在其中建立指定架構之文件系統的路徑。 如果基礎文件系統中沒有指定的路徑,請建立具有路徑的目錄。 如果未指定位置,則會在預設倉儲目錄中建立架構,其路徑是由靜態組態 spark.sql.warehouse.dir所設定。

    警告

    如果在工作區層級 Hive 中繼存放區中註冊架構(資料庫),則使用 CASCADE 選項卸除該架構會導致該架構位置中的所有檔案以遞歸方式刪除,而不論數據表類型(Managed 或 external)。

    如果架構註冊至 Unity 目錄中繼存放區,則會以遞歸方式刪除 Unity 目錄 受控數據表 的檔案。 不過,不會刪除外部資料表檔案。 您必須直接使用雲端記憶體提供者來管理這些檔案。

    因此,為了避免意外遺失數據,您絕對不應該在Hive中繼存放區中向具有現有數據的位置註冊架構。 您也不應該在 Hive 中繼存放區架構或包含 Unity 目錄受控數據表的位置中建立新的外部數據表。

  • schema_comment

    STRING 值。 架構的描述。

  • 受控位置 location_path

    MANAGED LOCATION 是選擇性的,而且需要 Unity 目錄。 如果您要為工作區層級 Hive 或第三方中繼存放區中註冊的架構指定儲存位置,請改用 LOCATION

    location_path 必須是 STRING 常值。 指定與目錄或中繼存放區記憶體根位置不同之架構之記憶體根位置的路徑。 此路徑必須在外部位置設定中定義,而且您必須具有CREATE MANAGED STORAGE外部位置設定的許可權。 您可以使用外部位置組態或子路徑中定義的路徑(換句話說, 'abfss://container@storageaccount.dfs.core.windows.net/finance''abfss://container@storageaccount.dfs.core.windows.net/finance/product')。 在 Databricks SQL 或執行 Databricks Runtime 11.3 LTS 和更新版本之叢集上支援。

    請參閱受控數據表建立 Unity 目錄中繼存放區

  • WITH DBPROPERTIES ( { property_name = property_value } [ , ... ] )

    索引鍵/值組中架構的屬性。

  • OPTIONS

    設定連線時識別目錄所需的連線類型特定參數。

    • option

      選項索引鍵。 索引鍵可以包含一或多個 以點分隔的標識碼 ,或 STRING 常值。

      選項索引鍵必須是唯一的,而且區分大小寫。

    • value

      選項的值。 值必須是 BOOLEANSTRINGINTEGERDECIMAL 常數表達式。 此值也可能是 SQL 函式的 SECRET 呼叫。 例如,的 valuepassword 可能會組成 secret('secrets.r.us', 'postgresPassword') ,而不是輸入常值密碼。

範例

-- Create schema `customer_sc`. This throws exception if schema with name customer_sc
-- already exists.
> CREATE SCHEMA customer_sc;

-- Create schema `customer_sc` only if schema with same name doesn't exist.
> CREATE SCHEMA IF NOT EXISTS customer_sc;

-- Create schema `customer_sc` only if schema with same name doesn't exist with
-- `Comments`,`Specific Location` and `Database properties`. LOCATION is not supported in Unity Catalog.
> CREATE SCHEMA IF NOT EXISTS customer_sc COMMENT 'This is customer schema' LOCATION '/samplepath'
    WITH DBPROPERTIES (ID=001, Name='John');

-- Create schema with a different managed storage location than the metastore's. MANAGED LOCATION is supported only in Unity Catalog.
> CREATE SCHEMA customer_sc MANAGED LOCATION 'abfss://container@storageaccount.dfs.core.windows.net/finance';

-- Verify that properties are set.
> DESCRIBE SCHEMA EXTENDED customer_sc;
database_description_item database_description_value
------------------------- --------------------------
            Database Name                customer_sc
              Description  This is customer schema
                  Location      hdfs://hacluster/samplepath
                Properties    ((ID,001), (Name,John))