Delen via


ALTER TABLE

Van toepassing op:aangevinkt ja Databricks SQL aangevinkt ja Databricks Runtime

Hiermee wijzigt u het schema of de eigenschappen van een tabel.

Zie gegevens herschrijven voor typewijzigingen of kolommen hernoemen in Delta Lake.

Als u de opmerking in een tabel of kolom wilt wijzigen, kunt u ook COMMENT ONgebruiken.

Als u een STREAMING TABLEwilt wijzigen, gebruikt u ALTER STREAMING TABLE.

Als de tabel in de cache is opgeslagen, worden met de opdracht gegevens in de cache van de tabel en alle afhankelijke gegevens gewist die ernaar verwijzen. De cache wordt op een uitgestelde manier gevuld wanneer de tabel of de afhankelijkheden de volgende keer worden geopend.

Notitie

Wanneer u een kolom aan een bestaande Delta-tabel toevoegt, kunt u geen DEFAULT waarde definiëren. Alle kolommen die aan Delta-tabellen worden toegevoegd, worden behandeld als NULL voor bestaande rijen. Nadat u een kolom hebt toegevoegd, kunt u desgewenst een standaardwaarde voor de kolom definiëren, maar dit wordt alleen toegepast op nieuwe rijen die in de tabel zijn ingevoegd. Gebruik de volgende syntaxis:

ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_expression

Op vreemde tabellen kunt u alleen ALTER TABLE SET OWNERALTER TABLE RENAME TOen.

Vereiste toestemmingen

Als u Unity Catalog gebruikt, moet u gemachtigd zijn MODIFY voor het volgende:

  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • SET TBLPROPERTIES
  • UNSET TBLPROPERTIES
  • modificeren PREDICTIVE OPTIMIZATION

Als u Unity Catalog gebruikt, moet u MANAGE machtiging of eigendom hebben voor:

  • SET OWNER TO

Voor alle andere bewerkingen is het eigendom van de tabel vereist.

Syntaxis

ALTER TABLE table_name
    { RENAME TO clause |
      ADD COLUMN clause |
      ALTER COLUMN clause |
      DROP COLUMN clause |
      RENAME COLUMN clause |
      DEFAULT COLLATION clause |
      ADD CONSTRAINT clause |
      DROP CONSTRAINT clause |
      DROP FEATURE clause |
      ADD PARTITION clause |
      DROP PARTITION clause |
      PARTITION SET LOCATION clause |
      RENAME PARTITION clause |
      RECOVER PARTITIONS clause |
      SET { ROW FILTER clause } |
      DROP ROW FILTER |
      SET TBLPROPERTIES clause |
      UNSET TBLPROPERTIES clause |
      SET SERDE clause |
      SET LOCATION clause |
      SET OWNER TO clause |
      SET SERDE clause |
      SET TAGS clause |
      UNSET TAGS clause |
      CLUSTER BY clause |
      PREDICTIVE OPTIMIZATION clause}

Parameterwaarden

  • table_name

    Hiermee wordt de tabel geïdentificeerd die wordt gewijzigd. De naam mag geen tijdelijke specificatie of optiesspecificatie bevatten. Als de tabel niet kan worden gevonden, genereert Azure Databricks een TABLE_OR_VIEW_NOT_FOUND-fout.

  • RENAME TO to_table_name

    Wijzigt de naam van de tabel.

    • to_table_name

      Hiermee wordt de naam van de nieuwe tabel geïdentificeerd. De naam mag geen tijdelijke specificatie of optiesspecificatie bevatten.

      Voor Unity Catalog-tabellen moet de to_table_name tabel zich in dezelfde catalogus bevinden als table_name. Voor andere tabellen moet de to_table_name tabel zich in hetzelfde schema bevinden als table_name.

      Als to_table_name niet-gekwalificeerd is, wordt dit impliciet gekwalificeerd volgens het huidige schema.

  • TOEVOEGEN COLUMN

    Hiermee voegt u een of meer kolommen toe aan de tabel.

  • VERANDEREN COLUMN

    Hiermee wijzigt u een eigenschap of de locatie van een kolom.

  • DRUPPEL COLUMN

    Verwijder een of meer kolommen of velden in een Delta Lake-tabel.

  • naam van COLUMN wijzigen

    Wijzigt de naam van een kolom of veld in een Delta Lake-tabel.

  • ADD CONSTRAINT

    Hiermee voegt u een controleconstraint, informatieve buitenlandse sleutelconstraint of informatieve primaire sleutelconstraint toe aan de tabel.

    Vreemde sleutels en primaire sleutels worden alleen ondersteund voor tabellen in Unity Catalog, niet voor de hive_metastore catalogus.

  • DEFAULT COLLATION collatie_naam

    Van toepassing op:aangevinkt als ja Databricks SQL aangevinkt als ja Databricks Runtime 16.3 en hoger

    Hiermee wijzigt u de standaardsortering van de tabel voor nieuwe STRING kolommen. Bestaande kolommen worden niet beïnvloed door deze component. Als u de sortering van een bestaande kolom wilt wijzigen, gebruikt u ALTER TABLE ... ALTER COLUMN ... COLLATE collation_name.

  • DROP CONSTRAINT

    Hiermee wordt een primaire sleutel, buitenlandse sleutel of check-voorwaarde uit de tabel verwijderd.

  • DROP FEATURE feature_name [ TRUNCATE HISTORY ]

    Van toepassing op:controleren gemarkeerd als Ja Databricks Runtime 14.3 LTS en hoger

    Verouderde ondersteuning voor DROP FEATURE is beschikbaar vanaf Databricks Runtime 14.3 LTS. Zie Drop Delta-tabelfuncties (verouderd) voor documentatie over de verouderde functionaliteit.

    Van toepassing op:aangevinkt als ja Databricks SQL aangevinkt als ja Databricks Runtime 16.3 en hoger

  • Azure Databricks raadt aan om Databricks Runtime 16.3 en hoger te gebruiken voor alle DROP FEATURE opdrachten, waardoor het verouderde gedrag wordt vervangen.

    Hiermee verwijdert u een functie uit een Delta Lake-tabel.

    Als u een functie verwijdert, kan dit leiden tot het toevoegen van de checkpointProtection schrijffunctie in het tabelprotocol. Zie Drop Delta-tablefuncties en Table functies voor protocolcompatibiliteit voor meer informatie.

    • feature_name

      De naam van een functie in de vorm van een STRING letterlijke of id, die moet worden begrepen door Azure Databricks en moet worden ondersteund in de tabel.

      Als de functie niet aanwezig is in de tabel, verhoogt Azure Databricks DELTA_FEATURE_DROP_FEATURE_NOT_PRESENT.

    • TRUNCEREN VAN GESCHIEDENIS

      Het verwijderen van functies door de geschiedenis af te kapen. Hiervoor is een proces met twee fasen vereist:

Voor het verwijderen van functies door het afkappen van de geschiedenis is een proces in twee stappen vereist:

  • Met de eerste aanroep worden sporen van de functie gewist en wordt u geïnformeerd over gedeeltelijk succes.

  • Wacht tot de bewaarperiode is verstreken voordat u de opdracht opnieuw uitvoert om de verwijdering te voltooien.

    Als u de tweede aanroep te vroeg start, genereert Azure Databricks DELTA_FEATURE_DROP_WAIT_FOR_RETENTION_PERIOD of DELTA_FEATURE_DROP_HISTORICAL_VERSIONS_EXIST.

    Door de tabelgeschiedenis af te kappen, beperkt u uw vermogen om DESCRIBE HISTORY uit te voeren en tijdreisquery's uit te voeren.

  • TOEVOEGEN PARTITION

    Voegt een of meer partities toe aan de tabel.

  • DRUPPEL PARTITION

    Hiermee verwijdert u een of meer partities uit de tabel.

  • PARTITION ... SET LOCATIE

    Hiermee stelt u de locatie van een partitie in.

  • naam van PARTITION wijzigen

    Vervangt de sleutels van een partitie.

  • PARTITIES HERSTELLEN

    Geeft Azure Databricks opdracht om de locatie van de tabel te scannen en bestanden toe te voegen aan de tabel die rechtstreeks aan het bestandssysteem zijn toegevoegd.

  • SET ROW FILTER clausule

    Van toepassing op:aangevinkt als ja Databricks SQL aangevinkt als ja Databricks Runtime 12.2 LTS en hoger aangevinkt als ja en alleen Unity Catalog

    Hiermee voegt u een rijfilterfunctie toe aan de tabel. Alle volgende query's voor de tabel ontvangen een subset van de rijen waarin de functie resulteert in de booleaanse waarde WAAR. Dit kan handig zijn voor verfijnde toegangsbeheerdoeleinden, waarbij de functie de identiteit of groepslidmaatschappen van de aanroepende gebruiker kan inspecteren om te bepalen of bepaalde rijen moeten worden gefilterd.

  • DROP ROW FILTER

    Van toepassing op:aangevinkt als ja alleen op Unity Catalog

    Hiermee wordt het rijfilter uit de tabel verwijderd, indien van toepassing. Toekomstige query's retourneren alle rijen uit de tabel zonder automatisch filteren.

  • SET TBLPROPERTIES

    Hiermee stelt u een of meer door de gebruiker gedefinieerde eigenschappen in of stelt u deze opnieuw in.

  • VERWIJDER TBLPROPERTIES

    Hiermee verwijdert u een of meer door de gebruiker gedefinieerde eigenschappen.

  • SET LOCATION

    Hiermee verplaatst u de locatie van een tabel.

    SET LOCATION path
    
    • LOCATION path

      path moet een STRING letterlijke waarde zijn. Specificeert de nieuwe locatie voor de tabel.

      Bestanden op de oorspronkelijke locatie worden niet verplaatst naar de nieuwe locatie.

  • [ SET ] OWNER TO directeur

    Hiermee wordt het eigendom van de tabel overgedragen naar principal.

    Van toepassing op:vinkje als ja aan Databricks SQL vinkje als ja aan Databricks Runtime 11.3 LTS en hoger

    SET is toegestaan als een optioneel trefwoord.

  • SET TAGS ( { tag_name = tag_value } [, ...] )

    Van toepassing op:vinkje als ja aan Databricks SQL vinkje als ja aan Databricks Runtime 13.3 LTS en hoger

    Tags toepassen op de tabel. U moet gemachtigd zijn APPLY TAG om tags toe te voegen aan de tabel.

    • tag_name

      Een letterlijke STRING. De tag_name naam moet uniek zijn binnen de tabel of kolom.

    • tag_value

      Een letterlijke STRING.

  • UNSET TAGS ( tag_name [, ...] )

    Van toepassing op:vinkje als ja aan Databricks SQL vinkje als ja aan Databricks Runtime 13.3 LTS en hoger

    Verwijder tags uit de tabel. U moet gemachtigd zijn APPLY TAG om tags uit de tabel te verwijderen.

    • tag_name

      Een letterlijke STRING. De tag_name naam moet uniek zijn binnen de tabel of kolom.

  • CLUSTER BY clausule

    Van toepassing op:vinkje als ja aan Databricks SQL vinkje als ja aan Databricks Runtime 13.3 LTS en hoger

    Hiermee wordt de clusterstrategie voor een Delta Lake-tabel toegevoegd, gewijzigd of verwijderd.

  • { ENABLE | DISABLE | INHERIT } PREDICTIVE OPTIMIZATION

    Van toepassing op:aangevinkt als ja Databricks SQL aangevinkt als ja Databricks Runtime 12.2 LTS en hoger aangevinkt als ja en alleen Unity Catalog

    Hiermee wijzigt u de beheerde Delta Lake-tabel in de gewenste instelling voor voorspellende optimalisatie.

    Wanneer tabellen worden gemaakt, is het standaardgedrag om INHERIT vanuit het schema te gebruiken.

    Wanneer voorspellende optimalisatie expliciet wordt ingeschakeld of als ingeschakeld wordt overgenomen, worden OPTIMIZE en VACUUM automatisch in de tabel aangeroepen, zoals passend wordt geacht door Azure Databricks. Zie voor meer informatie: Voorspellende optimalisatie voor beheerde tabellen in Unity Catalog.

Voorbeelden

Zie voor Delta Lake beperkingen toevoegen en kolomvoorbeelden wijzigen

-- RENAME table
> DESCRIBE student;
                col_name data_type comment
 ----------------------- --------- -------
                    name    string    NULL
                  rollno       int    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

> ALTER TABLE Student RENAME TO StudentInfo;

-- After Renaming the table
> DESCRIBE StudentInfo;
                col_name data_type comment
 ----------------------- --------- -------
                    name    string    NULL
                  rollno       int    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

-- RENAME partition
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=10
    age=11
    age=12

> ALTER TABLE default.StudentInfo PARTITION (age='10') RENAME TO PARTITION (age='15');

-- After renaming Partition
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15

-- Add new columns to a table
> DESCRIBE StudentInfo;
                col_name data_type comment
 ----------------------- --------- -------
                    name    string    NULL
                  rollno       int    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

> ALTER TABLE StudentInfo ADD columns (LastName string, DOB timestamp);

-- After Adding New columns to the table
> DESCRIBE StudentInfo;
                col_name data_type comment
 ----------------------- --------- -------
                    name    string    NULL
                  rollno       int    NULL
                LastName    string    NULL
                     DOB timestamp    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

-- Add a new partition to a table
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15

> ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18);

-- After adding a new partition to the table
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15
    age=18

-- Drop a partition from the table
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15
    age=18

> ALTER TABLE StudentInfo DROP IF EXISTS PARTITION (age=18);

-- After dropping the partition of the table
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15

-- Adding multiple partitions to the table
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15

> ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18) PARTITION (age=20);

-- After adding multiple partitions to the table
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15
    age=18
    age=20

-- ALTER or CHANGE COLUMNS
> DESCRIBE StudentInfo;
                col_name data_type comment
+-----------------------+---------+-------
                    name    string    NULL
                  rollno       int    NULL
                LastName    string    NULL
                     DOB timestamp    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

ALTER TABLE StudentInfo ALTER COLUMN name COMMENT "new comment";

--After ALTER or CHANGE COLUMNS
> DESCRIBE StudentInfo;
                col_name data_type     comment
 ----------------------- --------- -----------
                    name    string new comment
                  rollno       int        NULL
                LastName    string        NULL
                     DOB timestamp        NULL
                     age       int        NULL
 # Partition Information
              # col_name data_type     comment
                     age       int        NULL

-- RENAME COLUMN
> ALTER TABLE StudentInfo RENAME COLUMN name TO FirstName;

--After RENAME COLUMN
> DESCRIBE StudentInfo;
                col_name data_type     comment
 ----------------------- --------- -----------
               FirstName    string new comment
                  rollno       int        NULL
                LastName    string        NULL
                     DOB timestamp        NULL
                     age       int        NULL
 # Partition Information
              # col_name data_type     comment
                     age       int        NULL

-- Change the file Location
> ALTER TABLE dbx.tab1 PARTITION (a='1', b='2') SET LOCATION '/path/to/part/ways';

-- SET SERDE/ SERDE Properties (DBR only)
> ALTER TABLE test_tab SET SERDE 'org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe';

> ALTER TABLE dbx.tab1 SET SERDE 'org.apache.hadoop' WITH SERDEPROPERTIES ('k' = 'v', 'kay' = 'vee');

-- SET TABLE PROPERTIES
> ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('winner' = 'loser');

-- DROP TABLE PROPERTIES
> ALTER TABLE dbx.tab1 UNSET TBLPROPERTIES ('winner');

-- Drop the "deletion vectors" from a Delta table
> ALTER TABLE my_table DROP FEATURE deletionVectors;

-- 24 hours later
> ALTER TABLE my_table DROP FEATURE deletionVectors TRUNCATE HISTORY;

-- Applies three tags to the table named `test`.
> ALTER TABLE test SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');

-- Removes three tags from the table named `test`.
> ALTER TABLE test UNSET TAGS ('tag1', 'tag2', 'tag3');

-- Applies three tags to table `main.schema1.test` column `col1`.
> ALTER TABLE main.schema1.test ALTER COLUMN col1 SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');

-- Removes three tags from table `main.schema1.test` column `col1`.
> ALTER TABLE main.schema1.test ALTER COLUMN col1 UNSET TAGS ('tag1', 'tag2', 'tag3');

-- Enables predictive optimization for my_table
> ALTER TABLE my_table ENABLE PREDICTIVE OPTIMIZATION;

-- Alter multiple columns in a single statement
-- Create a table with 3 columns
> CREATE TABLE my_table (num INT, str STRING, bool BOOLEAN) TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported')
> DESCRIBE TABLE my_table;
  col_name    data_type     comment
  --------    ---------     -------
       num          int        null
       str       string        null
       bool      boolean       null

-- Update comments on multiple columns
> ALTER TABLE table ALTER COLUMN
   num COMMENT 'number column',
   str COMMENT 'string column';

> DESCRIBE TABLE my_table;
  col_name    data_type      comment
  --------    ---------   -------------
       num          int   number column
       str       string   string column
      bool      boolean            null

-- Can mix different types of column alter
> ALTER TABLE table ALTER COLUMN
   bool COMMENT 'boolean column',
   num AFTER bool,
   str AFTER num,
   bool SET DEFAULT true;

> DESCRIBE TABLE my_table;
  col_name    data_type      comment
  --------    ---------   --------------
      bool      boolean   boolean column
       num          int    number column
       str       string    string column