Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop Unique Constraint on Table Column without Knowing the Constraint Name

In Oracle 10g, how can I drop a unique constraint on a column without knowing the name of the constraint (e.g. a system generated name, which won't necessarily be the same across database instances)? Dropping and recreating the table isn't an option. Is it possible?

like image 607
Stephen Swensen Avatar asked Oct 15 '10 17:10

Stephen Swensen


1 Answers

You can retrieve the constraint's name with:

SELECT CONSTRAINT_NAME
FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'xxx'
AND CONSTRAINT_TYPE = 'U'

You can for instance create a stored procedure that executes the previous sql, stores its result in a variable and uses this variable in ALTER TABLE DROP CONSTRAINT

EDIT: e.g.:

BEGIN
  FOR r IN (
    SELECT TABLE_NAME, CONSTRAINT_NAME
    FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'xxx'
    AND CONSTRAINT_TYPE = 'U') LOOP
    EXECUTE IMMEDIATE REPLACE(REPLACE(
      'ALTER TABLE #TABLE# DROP CONSTRAINT #CON#'
      ,'#TABLE#',r.TABLE_NAME)
      ,'#CON#',r.CONSTRAINT_NAME);
  END LOOP;
END;
like image 190
vc 74 Avatar answered Oct 01 '22 08:10

vc 74