Constrait Oracle Garden

Asked

Viewed 127 times

1

I have a question, when I create a CONSTRAINT UNIQUE , she measures all the columns together?

For example, I want a country not to have names or acronyms equal, in which case I must create CONSTRAINT different?

For I did in only one, she lets me enter countries since name and acronym are not equal, however, I want to block even in case only one of these columns are equal.

I did so:

CREATE TABLE PAIS
(
  IdPais INTEGER NOT NULL,
  NomePais VARCHAR(60) NOT NULL,
  SiglaPais VARCHAR(3) NOT NULL,
  StatusPais CHAR(1) NOT NULL, 
  CONSTRAINT PAIS PRIMARY KEY (IdPais),
  CONSTRAINT PAIS_UNIQUE UNIQUE (NomePais, SiglaPais)
);
/
CREATE SEQUENCE SEQ_ID_PAIS
MINVALUE 1
MAXVALUE 9999999999
START WITH 1
INCREMENT BY 1
NOCACHE
CYCLE;
/
CREATE OR REPLACE TRIGGER TRG_ID_PAIS BEFORE INSERT ON PAIS FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN IF :NEW.IDPAIS IS NULL THEN SELECT SEQ_ID_PAIS.NEXTVAL INTO :NEW.IDPAIS FROM DUAL; END IF; END COLUMN_SEQUENCES; END;
/

1 answer

2


In the case of:

CONSTRAINT PAIS_UNIQUE UNIQUE (NomePais, SiglaPais)

The columns will be analyzed together yes, which will result in:

'Brasil' | 'BR' <- Permitido
'Brasil' | 'BR' <- Violação
'Brazil' | 'BR' <- Permitido

Now To:

CONSTRAINT PAIS_UNIQUE UNIQUE (SiglaPais)

'Brasil' | 'BR' <- Permitido
'Brasil' | 'BR' <- Violação
'Brazil' | 'BR' <- Violação
  • then, when I want separate, I must make a Constraint for each column, correct ?

  • yes, if the given column cannot repeat the value in any condition, the Constraint is individual

  • Siglapais could be the PK then.

  • depending on the case, yes, but you can’t know the specification of the colleague’s software, so I didn’t even get into this question.

Browser other questions tagged

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