Skip to Main Content

Oracle Database Discussions

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to check if a constraint existed in the table and drop it?

793476Aug 24 2010 — edited Aug 24 2010
Hi all,
I want to drop a constraint from a table. I do not know if this constraint already existed in the table. So I want to check if this exists first.
Below is my query:

DECLARE
itemExists NUMBER;
BEGIN
itemExists := 0;

SELECT COUNT(CONSTRAINT_NAME) INTO itemExists
FROM ALL_CONSTRAINTS
WHERE UPPER(CONSTRAINT_NAME) = UPPER('my_constraint');

IF itemExists > 0 THEN
ALTER TABLE my_table DROP CONSTRAINT my_constraint;
END IF;

END;

Here is the error I got when I executed the above query:
ORA-06550: line 11, column 5: PLS-00103: Encountered the symbol "ALTER" when expecting one of the following: ( begin case declare exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge

Please help me with this!

Greatly appreciate!

Khoi Le
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2010
Added on Aug 24 2010
8 comments
17,630 views