Get Constraint and drop name next

Asked

Viewed 109 times

1

I have a constraint UNIQUE in the PERSON table and is from the field CPF. To get the name of this contraint I use the query:

SELECT UC.CONSTRAINT_NAME FROM USER_CONSTRAINTS UC
INNER JOIN USER_TAB_COLUMNS UT ON UT.TABLE_NAME = UC.TABLE_NAME
WHERE UC.TABLE_NAME = 'PESSOA' AND UC.CONSTRAINT_TYPE = 'U' AND UT.COLUMN_NAME = 'CPF';

How do a procedure where first I take this name and give the drop in it?

1 answer

2

Just perform your query and make an "alter table drop Constraint", ex:

create or replace procedure DropConstraint(aTabela          varchar2,
                                           aCONSTRAINT_TYPE varchar2,
                                           aCOLUMN_NAME     varchar2) is
  cursor cConstraints is
    SELECT UC.CONSTRAINT_NAME
      FROM USER_CONSTRAINTS UC
     INNER JOIN USER_TAB_COLUMNS UT
        ON UT.TABLE_NAME = UC.TABLE_NAME
     WHERE UC.TABLE_NAME = aTabela
       AND UC.CONSTRAINT_TYPE = aCONSTRAINT_TYPE
       AND UT.COLUMN_NAME = aCOLUMN_NAME;
begin
  for c in cConstraints loop
    execute immediate ('ALTER TABLE ' || aTabela || '  DROP CONSTRAINT ' ||
                      c.CONSTRAINT_NAME);
  end loop;
end;
  • Where I put the table name, column, etc ?

  • Ué, as you asked, I set up a procedure that makes the constraints drop, just call this in an anonymous block passing by parameter as your need.

  • Explain your question better @Douglas

Browser other questions tagged

You are not signed in. Login or sign up in order to post.