A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table below:

ALTER TABLE | Databricks Documentation

ALTER TABLE

Applies to: Databricks SQL Databricks Runtime

Alters the schema or properties of a table.

For type changes or renaming columns in Delta Lake see rewrite the data.

To change the comment on a table or a column, you can also use COMMENT ON.

To alter a STREAMING TABLE, use ALTER STREAMING TABLE.

If the table is cached, the command clears cached data of the table and all its dependents that refer to it. The cache will be lazily filled when the table or the dependents are accessed the next time.

note

When you add a column to an existing Delta table, you cannot define a DEFAULT value. All columns added to Delta tables are treated as NULL for existing rows. After adding a column, you can optionally define a default value for the column, but this is only applied for new rows inserted into the table. Use the following syntax:

SQL

ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_expression

On foreign tables, you can perform only ALTER TABLE SET OWNER and ALTER TABLE RENAME TO.

Required permissions​

If you use Unity Catalog you must have MODIFY permission to:

If you use Unity Catalog you must have MANAGE permission or ownership to:

All other operations require ownership of the table.

Syntax​
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}
Parameters​

The removal of features by truncating history requires a two-step process:

Examples​

For Delta Lake add constraints and alter column examples, see

SQL


> DESCRIBE student;
col_name data_type comment

name string NULL
rollno int NULL
age int NULL


age int NULL

> ALTER TABLE Student RENAME TO StudentInfo;


> DESCRIBE StudentInfo;
col_name data_type comment

name string NULL
rollno int NULL
age int NULL


age int NULL


> SHOW PARTITIONS StudentInfo;
partition

age=10
age=11
age=12

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


> SHOW PARTITIONS StudentInfo;
partition

age=11
age=12
age=15


> DESCRIBE StudentInfo;
col_name data_type comment

name string NULL
rollno int NULL
age int NULL


age int NULL

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


> DESCRIBE StudentInfo;
col_name data_type comment

name string NULL
rollno int NULL
LastName string NULL
DOB timestamp NULL
age int NULL


age int NULL


> SHOW PARTITIONS StudentInfo;
partition

age=11
age=12
age=15

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


> SHOW PARTITIONS StudentInfo;
partition

age=11
age=12
age=15
age=18


> SHOW PARTITIONS StudentInfo;
partition

age=11
age=12
age=15
age=18

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


> SHOW PARTITIONS StudentInfo;
partition

age=11
age=12
age=15


> SHOW PARTITIONS StudentInfo;
partition

age=11
age=12
age=15

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


> SHOW PARTITIONS StudentInfo;
partition

age=11
age=12
age=15
age=18
age=20


> DESCRIBE StudentInfo;
col_name data_type comment
+
name string NULL
rollno int NULL
LastName string NULL
DOB timestamp NULL
age int NULL


age int NULL

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


> DESCRIBE StudentInfo;
col_name data_type comment

name string new comment
rollno int NULL
LastName string NULL
DOB timestamp NULL
age int NULL


age int NULL


> ALTER TABLE StudentInfo RENAME COLUMN name TO FirstName;


> DESCRIBE StudentInfo;
col_name data_type comment

FirstName string new comment
rollno int NULL
LastName string NULL
DOB timestamp NULL
age int NULL


age int NULL


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


> 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');


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


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


> ALTER TABLE my_table DROP FEATURE deletionVectors;


> ALTER TABLE my_table DROP FEATURE deletionVectors TRUNCATE HISTORY;


> ALTER TABLE test SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');


> ALTER TABLE test UNSET TAGS ('tag1', 'tag2', 'tag3');


> ALTER TABLE main.schema1.test ALTER COLUMN col1 SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');


> ALTER TABLE main.schema1.test ALTER COLUMN col1 UNSET TAGS ('tag1', 'tag2', 'tag3');


> ALTER TABLE my_table ENABLE PREDICTIVE OPTIMIZATION;



> 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


> 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


> 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
Related articles​

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4