ALTER TABLE CATRACA DROP CONSTRAINT PK_CATRACA
/
DROP INDEX PK_CATRACA
/
ALTER TABLE CATRACA ADD CDCAIXA VARCHAR2(03)
/
BEGIN
   FOR C_CATRACA IN
       (SELECT DISTINCT(CDFILIAL) AS  CDFILIAL 
	   FROM CATRACA ) LOOP
	   
          UPDATE CATRACA
	   SET CDCAIXA = (SELECT MIN(CDCAIXA) AS CDCAIXA 
	   	             FROM CAIXA 
		     	     WHERE CDFILIAL = C_CATRACA.CDFILIAL)
          WHERE
             CDFILIAL = C_CATRACA.CDFILIAL AND
             CDCAIXA IS NULL;
END LOOP;
COMMIT;		   
END;
/
ALTER TABLE CATRACA MODIFY CDCAIXA NOT NULL
/
ALTER TABLE CATRACA ADD CONSTRAINT PK_CATRACA PRIMARY KEY (CDFILIAL, CDCAIXA, DSENDIPCAT)