How to Delete column from Hive table?

Viewing 2 reply threads
  • Author
    Posts
    • #5259
      DataFlair TeamDataFlair Team
      Spectator

      how should we delete column in Hive Table? and how delete work?

    • #5262
      DataFlair TeamDataFlair Team
      Spectator

      Hive allows you to change the definition for columns, add new columns, or even replace all existing columns in a table with a new set.
      ADD COLUMNS lets you add new columns to the end of the existing columns but before the partition columns.
      REPLACE COLUMNS removes all existing columns and adds the new set of columns. This can be done only for tables with a native SerDe (DynamicSerDe, MetadataTypedColumnsetSerDe, LazySimpleSerDe and ColumnarSerDe).

      REPLACE COLUMNS can also be used to drop columns. For example,
      ALTER TABLE test REPLACE COLUMNS (a int, b int);will remove column ‘c’ from test schema.

      You cannot drop column directly from a table using command “ALTER TABLE table_name drop col_name;”

      The only way to drop column is using replace command. Lets say, I have a table TEST with id, name and case column. We want to drop id column of table TEST. So provide all those columns which you want to be the part of table in replace columns clause. Below command will drop id column from TEST table.

      ALTER TABLE TEST REPLACE COLUMNS( name string, case string);

    • #5265
      DataFlair TeamDataFlair Team
      Spectator

      There is no delete column in hive. however the below commands can work.
      CREATE TABLE test_change (a int, b int, c int);
      ALTER TABLE test_change REPLACE COLUMNS (a int, b int);” will remove column ‘c’ from test_change’s schema.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.