Getting an invalid ALTER TABLE option - oracle

This is my exact query which is erroring out
alter table INDIL_MCAR drop constraint ABOB.INDI_MCAR_PK;
I m trying to remove the unique key constring from the table. It gives me the following error.
ORA-01735: invalid ALTER TABLE option

You can't prefix the constraint name... the table name yes, but not the constraint name. Remove the ABOB.:
alter table INDIL_MCAR drop constraint INDI_MCAR_PK;

Related

Why do I keep getting this error message ORA-01735: invalid ALTER TABLE option

I am trying to use this syntax to alter the table to define the primary key:
ALTER TABLE student ADD CONSTRAINT studentid_pk PRIMARY KEY (studentid);
but I keep getting this error message:
ORA-01735: invalid ALTER TABLE option
What syntax would you recommend?

Add notnull constraint to a column of an existing table

How to add notnull constraint to a column of an existing table in which no value is yet inserted i.e. only table is created and now i want to alter the table
i am writing the following query
ALTER TABLE TABLENAME MODIFY COLUMNAME DATATYPE NOT NULL;
but oracle is throwing error "invalid alter table option"
In an alter table you only have to specify what changes
ALTER TABLE TABLENAME MODIFY COLUMNAME NOT NULL;
As you don't change the datatype, you don't have to specify it again.

Is "Constraint" keyword required in Alter table Add [Constraint] syntax?

I came across an example with the following query
ALTER TABLE T ADD UNIQUE(col1)
Normally I would write
ALTER TABLE T ADD CONSTRAINT UNIQUE(col1)
ALTER TABLE T ADD CONSTRAINT UQ_1 UNIQUE(col1)
for unnamed and named constraint.
My question is whether the keyword CONSTRAINT is required ? I could not find reference on this. Can we write
ALTER TABLE T ADD FOREIGN KEY(col1) REFERENCE T1(col2)
ALTER TABLE T ADD PRIMARY KEY(col1)
Yes, we can use:
ALTER TABLE T ADD FOREIGN KEY(col1) REFERENCES T1(col2);
ALTER TABLE T ADD PRIMARY KEY(col1);
LiveDemo
or if you want to name them:
ALTER TABLE T ADD CONSTRAINT fk_T_T1 FOREIGN KEY(col1) REFERENCES T1(col2);
ALTER TABLE T ADD CONSTRAINT pk_T PRIMARY KEY(col1);
ALTER TABLE:
Image from: http://docs.oracle.com/cd/B28359_01/server.111/b28286/img/constraint_clauses.gif
and:
Image from: http://docs.oracle.com/cd/B19306_01/server.102/b14200/img/out_of_line_constraint.gif
As shown in the documentation (link below), the keyword CONSTRAINT is optional:
https://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#CJAEDFIB
However, it is required if you want to name your constraint.
The constraint <constraint_name> subclause is not required.

Not able to add a new column to the existing primary key

I have a primary key PK1 for the table TABLE1. Need to add one more column to the existing primary key.
I was tying the following alter script
ALTER TABLE TABLE1
ADD CONSTRAINT "PRIMARYKEYS" PRIMARY KEY
("PK1",
"PK2");
Error report:
SQL Error: ORA-02260: table can have only one primary key
02260. 00000 - "table can have only one primary key"
*Cause: Self-evident.
*Action: Remove the extra primary key.
How can I add one more column to the primary key, without affecting the data(data has been verified , there are no duplication.)
If you have concern about new data that violates PK can be added during the time interval when the old PK dropped, but the new one is not created, you can create a unique index first :
CREATE UNIQUE INDEX IDXU_TABLE1_PK ON TABLE1(PK1,PK2);
ALTER TABLE TABLE1 DROP CONSTRAINT [old_pk_constraint_name] ;
ALTER TABLE TABLE1 ADD CONSTRAINT "PRIMARYKEYS" PRIMARY KEY
(PK1,PK2) USING INDEX IDXU_TABLE1_PK;
Another option is to keep index associated with the old PK constraint until new PK is created :
ALTER TABLE TABLE1 DROP CONSTRAINT [old_pk_constraint_name] KEEP INDEX;
ALTER TABLE TABLE1 ADD CONSTRAINT "PRIMARYKEYS" PRIMARY KEY
(PK1,PK2) ;
DROP INDEX [name of unique index associated with the old PK constraint];
You need to drop old primary key first.

Oracle drop constraint unique and re-add it

I've drop a constraint unique key with command:
ALTER TABLE table
DROP CONSTRAINT UNIQUE uk_nome;
it's removed because I don't see it, but when I try to re-add it with different parameter:
ALTER TABLE tale ADD CONSTRAINT UK_name UNIQUE (uk_1, uk_2);
I receive the error:
Errore SQL: ORA-00955: name is already used by an existing object.
Where is the problem?
You drop uk_nome and then add uk_name. I mean that there is a typo in uk_nome.
Resolved, the unique key is present, like index, into the system in the table dba_objects

Resources