How to add Multiple Column in table in alter command - db2

i want to add multiple column in existng table with the help of alter query.
ALTER TABLE users.UM_REG_HPTL_DET_DOC
ADD column HPTL_TADDRESS3 vargraphic(50),
ADD column APPLICANT_ADDRESS3 varchar(50),
ADD column APPLICANT_TADDRESS3(50),
ADD column aadhar_no varchar(12),
ADD column Registration_no varchar(50);
than that error comes
An unexpected token "," was found following "RESS3 vargraphic(50)". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.18.60

Remove "," and than run
ALTER TABLE users.UM_REG_HPTL_DET_DOC
ADD column HPTL_TADDRESS3 vargraphic(50)
ADD column APPLICANT_ADDRESS3 varchar(50)
ADD column APPLICANT_TADDRESS3(50)
ADD column aadhar_no varchar(12)
ADD column Registration_no varchar(50);

Related

Is it possible to add a column with unique constraint in one go?

I want to add an email column to an existing table with the UNIQUE constraint. Is it possible to do this with one statement?
When I try:
ALTER TABLE "Corporates"
ADD COLUMN "email" varchar(100) NOT NULL
CONSTRAINT "Corporates_email_key" UNIQUE ("email")
the query fails with the error:
ERROR: syntax error at or near "("
LINE 3: CONSTRAINT "Corporates_email_key" UNIQUE ("email")
^
SQL state: 42601
Character: 120
OTOH I could run the following two statements individually:
ALTER TABLE "Corporates"
ADD COLUMN "email" varchar(100) NOT NULL
and then:
ALTER TABLE "Corporates"
ADD CONSTRAINT "Corporates_email_key" UNIQUE ("email")
and it worked.
You need a second ADD option:
ALTER TABLE corporates
ADD COLUMN email varchar(100) NOT NULL,
ADD CONSTRAINT corporates_email_key UNIQUE (email);

Postgresql alter table column type to unique not null

ALTER TABLE users ALTER COLUMN email VARCHAR(50) UNIQUE NOT NULL;
ERROR: syntax error at or near "VARCHAR"
LINE 1: ALTER TABLE users ALTER COLUMN email VARCHAR(50) UNIQUE NOT ...
I want to alter column email to add its type as UNIQUE NOT NULL in Postgresql and get this error. Can you explain to me what's wrong?
You cannot create 2 constraints with one single statement. And you have to use PostgreSQL syntax.
alter table users alter column email set not null;
alter table users add constraint email_unique unique (email);

postgres error that sequence doesn't exist but nextval returns value for sequence

I am trying to alter a table and need to drop primary key column and then recreate it. This is slightly more complicated because I need to insert a column that is 4th from last, the last 3 columns need to stay last.
I ma executing the following script:
DO$$
BEGIN
ALTER TABLE logging.audit_study
DROP CONSTRAINT audit_study_pkey,
DROP COLUMN indication,
ADD COLUMN indication INT,
ADD COLUMN audit_study_id_tmp INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
ADD COLUMN aud_action_tmp VARCHAR,
ADD COLUMN transaction_id_tmp BIGINT DEFAULT (TXID_CURRENT());
UPDATE logging.audit_study SET audit_study_id_tmp = audit_study_id;
UPDATE logging.audit_study SET aud_action_tmp = aud_action;
UPDATE logging.audit_study SET transaction_id_tmp = transaction_id;
ALTER TABLE logging.audit_study
DROP COLUMN audit_study_id,
DROP COLUMN aud_action,
DROP COLUMN transaction_id;
ALTER TABLE logging.audit_study RENAME audit_study_id_tmp TO audit_study_id;
ALTER TABLE logging.audit_study RENAME aud_action_tmp TO aud_action;
ALTER TABLE logging.audit_study RENAME transaction_id_tmp TO transaction_id;
PERFORM SETVAL('logging.audit_study_audit_study_id_tmp_seq', (SELECT MAX(audit_study_id)+1 FROM logging.audit_study), true);
END $$
I get the following error:
[42P01] ERROR: relation "logging.audit_study_audit_study_id_tmp_seq" does not exist Where: PL/pgSQL function inline_code_block line 26 at PERFORM
However, if i execute
SELECT nextval('logging.audit_study_audit_study_id_tmp_seq')
I get an integer back for the next sequence. The sequence is also listed in the table properties.
I can't seem to understand why during this script, the sequence seems to disappear.
Dropping a primary key column declared as SERIAL data type (that creates automatically the related sequence) also drops the related sequence.

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