How to drop multiple columns in PostgreSQL - postgresql

I want to drop 200 columns in my table in PostgreSQL. I tried:
ALTER TABLE my_table
DROP COLUMN col1, col2
But I get an error like this:
ERROR: syntax error at or near "col2"

As per the docs, you can do this:
ALTER TABLE table DROP COLUMN col1, DROP COLUMN col2;
(You may need to wrap some of your column names in " quotes if they happen to be keywords.)

Thanks long & Ondrej - below commands worked for me,
ALTER TABLE table_name DROP COLUMN column_name_1,DROP COLUMN column_name_2,DROP COLUMN column_name_3;
ALTER TABLE table_name DROP column_name_1,DROP column_name_2,DROP column_name_3;

This worked for me:
alter table your_table_name drop column your_column_name;

Related

PostgreSQL DROP COLUMN: Remove One or More Columns of a Table

To drop a column of a table, you use the DROP COLUMN clause in the ALTER TABLE statement as follows:
ALTER TABLE res_partner rs
DROP COLUMN rs.miss_schedule;
ERROR: syntax error at or near "rs"
LINE 1: ALTER TABLE res_partner rs
Try this query with no alias
ALTER TABLE res_partner DROP COLUMN miss_schedule;

PGSQL - Change all columns of a specific datatype to text

I'm trying to change all the USER-DEFINED columns to TEXT in a specific view using pgsql.
Is it possible to do that in a single alter table query ou do I need to check first what columns contain that dataype and then perform the dataype change one by one?
This is what I'm trying:
ALTER TABLE if exists "schemaName"."Table_A"
ALTER COLUMN (
select column_name
from information_schema.columns inf
where table_name = 'Table_A' and inf.data_type = 'USER-DEFINED')
TYPE TEXT;
I'm getting and error in the subquery start "("
You need to do this one by one. Generally speaking such DDL statements cannot work on several objects in one statement.
For ALTER TABLE, see: https://www.postgresql.org/docs/12/sql-altertable.html.
FOR ALTER VIEW, see: https://www.postgresql.org/docs/current/sql-alterview.html

Delete column in hive table

I am working with hive version 0.9 and I need delete columns of a hive table. I have searched in several manuals of hive commands but I only I have found commands to version 0.14. Is possible to delete a column of a hive table in hive version 0.9? What is the command?
Thanks.
We can’t simply drop a table column from a hive table using the below statement like sql.
ALTER TABLE tbl_name drop column column_name ---- it will not work.
So there is a shortcut to drop columns from a hive table.
Let’s say we have a hive table.
From this table I want to drop the column Dob. You can use the ALTER TABLE REPLACE statement to drop a column.
ALTER TABLE test_tbl REPLACE COLUMNS(ID STRING,NAME STRING,AGE STRING); you have to give the column names which you want to keep in the table
There isn't a drop column or delete column in Hive.
A SELECT statement can take regex-based column specification in Hive releases prior to 0.13.0, or in 0.13.0 and later releases if the configuration property hive.support.quoted.identifiers is set to none.
That being said you could create a new table or view using the following:
drop table if exists database.table_name;
create table if not exists database.table_name as
select `(column_to_remove_1|...|column_to_remove_N)?+.+`
from database.some_table
where
...
;
This will create a table that has all the columns from some_table except the columns named column_to_remove_1, ... , to column_to_remove_N. You can also choose to create a view instead.
ALTER TABLE table_name REPLACE COLUMNS ( c1 int, c2 String);
NOTE: eliminate column from column list. It will keep matched columns and removed unmentioned columns from table schema.
we can not delete column from hive table . But droping a table(if its external) in hive and the recreating table(with column excluded) ,wont delete ur data .
so what can u do is(if u dont have table structure) run this command :
show create table database_name.table_name;
Then you can copy it and edit it (with column eliminated).Afterwards you can do as per invoke the shell
table details are empid,name,dept,salary ,address. i want remove address column. Just write REPLACE COLUMNS like below query
jdbc:hive2://> alter table employee replace columns(empid int, name string,dept string,salary int);
As mentioned before, you can't drop table using an alter statement.
Alter - replace is not guaranteed to work in all the cases.
I found the best answer for this here:
https://stackoverflow.com/a/48921280/4385453

deleting multiple columns sql, brackets cause failure

i'm trying to alter my table but it doesn't work, although according to 1 hour of google this should be the way to do it... the error seems to be with the brackets. I've tried them all () {} []...
ALTER TABLE all_in_one DROP (age, occupation, salary);
ALTER TABLE all_in_one ADD COLUMN sex char(7);
ALTER TABLE all_in_one ALTER COLUMN sex char(7) SET DEFAULT 'male';
SQL error:
FEHLER: Syntaxfehler bei „(“
LINE 2: ALTER TABLE all_in_one DROP (age, occupation, salary);
^
i also have a problem with the data type. Seems char(7) just won't work.
I'm using phpPgAdmin.
EDIT: i used "ALTER TABLE all_in_one DROP COLUMN age, DROP COLUMN occupation, DROP COLUMN salary;", but is this the only way to do that? Or is there no way i could use brackets in phppgadmin?(if i need to delete 10-100 or more columns, writing DROP COLUMN seems very tedious)
Next problem would be defining the data type.
ALTER TABLE all_in_one ADD COLUMN sex char(7);
doesn't work.
EDIT:
ALTER TABLE all_in_one DROP COLUMN age, DROP COLUMN occupation, DROP COLUMN salary;
ALTER TABLE all_in_one ADD COLUMN sex character(7);
ALTER TABLE all_in_one ALTER COLUMN sex SET DEFAULT 'male';
this works. although the documentation says i could use char, i couldn't. but with character it works.
I think the right syntax is
ALTER TABLE all_in_one DROP COLUMN age, DROP COLUMN occupation, DROP COLUMN salary;
and for alter table:
ALTER TABLE all_in_one ADD COLUMN sex char(7);
ALTER TABLE all_in_one ALTER COLUMN sex TYPE char(7);
ALTER TABLE all_in_one ALTER COLUMN sex SET DEFAULT 'male';

How to add multiple columns to a table in Postgres?

How do I add multiple columns in one query statement in PostgreSQL using pgadmin3?
Try this :
ALTER TABLE table ADD COLUMN col1 int, ADD COLUMN col2 int;
Use ALTER TABLE with ADD COLUMN subcommand.
ALTER TABLE:
This changes the definition of an existing table using a subcommand, for example: ADD COLUMN.
ADD COLUMN [ IF NOT EXISTS ]:
This form adds a new column to the table, using the same syntax as CREATE TABLE. If IF NOT EXISTS is specified and a column already exists with this name, no error is thrown.
Adding a Column
The most basic syntax is:
ALTER TABLE table_name
ADD COLUMN new_column_name data_type constraint;
In this syntax:
First, specify the name of the table that you want to add a new column to after the ALTER TABLE keyword.
Second, specify the name of the new column as well as its data type and constraint after the ADD COLUMN keywords.
Adding a Column using IF NOT EXIST clause
ALTER TABLE table_name
ADD COLUMN IF NOT EXISTS new_column_name data_type constraint;
This option gives PostgreSQL instructions to add the new column only in case the column name does not already exist in the table. If it does, you will receive a corresponding response; otherwise, it will create one.
ALTER TABLE IF EXISTS TABLEname
add ADD COLUMN IF NOT EXISTS column_name data_type [column_constraint];
detailed query where column_constraints are optional