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

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.

Related

Still Getting Errors After Applying All I've Read About the Error 02270

I'm creating a couple of Tables for an assignment.
So I created a Gardener Table and an Offering Table, with all the appropriate data types and NULL statuses, as well as the Primary Key constraint for each. In the Gardener table I've included offeringID, and vice versa.
When I try to add Foreign Key constraint offeringID to the Gardener Table I get an error.
After checking online, I realized I had forgotten to make offeringID and gardenerID in each other's tables UNIQUE, hence I altered table to add uniqueness.
Tried adding Foreign Key constraint and I get the same error. I reckon I may be understanding something wrongly, but I can't seem to put my finger on it.
Create Table Gardener
(gardenerID NUMBER(10) NOT NULL,
offeringID NUMBER(10) NOT NULL,
CONSTRAINT gardener_pk PRIMARY KEY(gardenerID)
);
Create Table Offering
(offeringID NUMBER(10) NOT NULL,
gardenerID NUMBER(10) NOT NULL,
CONSTRAINT offering_pk PRIMARY KEY(offeringID)
);
Alter Table Gardener
add CONSTRAINT offering_fk FOREIGN KEY(offeringID)
REFERENCES Offering(offeringID);
Alter Table Gardener
add Unique(offeringID);
Alter Table Offering
add Unique(gardenerID);
This is the error:
ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement gives a column-list
for which there is no matching unique or primary key constraint in the referenced table.
Like, I still don't get it. Isn't offeringID a Primary Key hence pointing to it from Gardener shouldn't be an issue still?
Since you're trying to add a a foreign key constraint for offering.offeringID column within the Gardener table, whereas that column has no unique/primary key key when you try to add a foreign key. i.e. operation stops at the 3rd command.
So, just exchange the order of commands as :
Alter Table Gardener
add Unique(offeringID); -- should be prior to the below command
Alter Table Gardener
add CONSTRAINT offering_fk FOREIGN KEY(offeringID)
REFERENCES Offering(offeringID);
Demo

Getting an invalid ALTER TABLE option

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;

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

how to drop and add multi constraints by 1 sql statement for H2 database

I'm working on H2 database, and I meet this problem -
to drop one constraint is fine, I can use this statement
alter table customer drop constraint if exists fk_customer_order ;
for add one constraint is fine too, I can use this statement.
alter table customer add constraint fk_customer_order foreign key (order_id) references order (id) on delete cascade on update cascade;
but the problems is, in customer table I have more foreign key and I want delete them in one query statement.
Something like this
alter table customer drop constraint fk_customer_order
drop constraint fk_customer_information
drop constraint ....
but this seem can not be done in h2 database, anyone can tell me can or not add or drop multi constraint by 1 sql statment? Any answer are welcome and I appreciate much.
I think it can not be done. Why don't you use multiple statements?

oracle 10g foreign key constraints

ALTER TABLE employees
ADD CONSTRAINT emp_dno_fk FORIEGN KEY(Dno) REFERENCES Departments(Dno);
When I use this command it shows an error like
CONSTRAINT Specification not allowed here.
But when I use this command it works:
ALTER TABLE employees
ADD CONSTRAINT emp_dno_fk Dno REFERENCES Departments(Dno);
Can anyone tell me why Oracle doesn't allow FOREIGN KEY KEYWORD in the first command?
The error message, admittedly, is not very helpful. The following are examples of how a referential integrity constraint may be created in Oracle:
The following examples assume that the column Dno already exists in employees:
ALTER TABLE employees ADD
CONSTRAINT emp_dno_fk FOREIGN KEY (Dno) REFERENCES Departments (Dno);
ALTER TABLE employees ADD
FOREIGN KEY (Dno) REFERENCES Departments (Dno);
The following examples assume that the column Dno does not already exist in employees:
ALTER TABLE employees ADD
CONSTRAINT emp_dno_fk Dno REFERENCES Departments (Dno);
ALTER TABLE employees ADD
Dno REFERENCES Departments (Dno);
Personally, I avoid the syntax versions which add the column and prefer to add it myself.

Resources