简体   繁体   中英

How to use CONSTRAINT CHECK in SQLDEVELOPER on a specific DATE?

I'm trying to set a CONSTRAINT on column Nr_AnoLetivo which is a DATE, which only can be NOT NULL AND greater than the year 2000.

I've been trying this:

CREATE TABLE Classe(
  Cd_Classe     NUMBER(8),   
  Nr_AnoLetivo  NUMBER(4) CONSTRAINT CLASSE_NR_ANOLETIVO_NN NOT NULL,
  Cd_Escola     NUMBER(5),
  Cd_Grau       NUMBER(2),
  Nr_Serie      NUMBER(2) CONSTRAINT CLASSE_NR_SERIE_NN NOT NULL, 
  Sg_Turma      VARCHAR2(2) CONSTRAINT CLASSE_SG_TURMA_NN NOT NULL,
  Cd_Periodo    NUMBER(2),
  CONSTRAINT CLASSE_CD_CLASSE_PK PRIMARY KEY (CD_CLASSE),
  CONSTRAINT CLASSE_NR_ANOLETIVO_CK CHECK (NR_ANOLETIVO IN DATE ('2000/01/01')),
  CONSTRAINT ESCOLA_CD_ESCOLA_FK FOREIGN KEY (CD_ESCOLA) REFERENCES Escola (CD_ESCOLA),
  CONSTRAINT GRAU_CD_GRAU_FK FOREIGN KEY (CD_GRAU) REFERENCES Grau (CD_GRAU),
  CONSTRAINT PERIODO_CD_PERIODO_FK FOREIGN KEY (CD_PERIODO) REFERENCES Periodo (CD_PERIODO)
);

And the Error message is:

  1. 00000 - "missing expression"

Something tells me this error is generated by the DATE row and I don't get it why.

Can anyone tell why is this happening?

You got it wrong.

If you declared NR_ANOLETIVO column as NUMBER(4) , it seems that you expect year only in that column (such as 1957, 1998, 2010, etc.), not the whole date (such as 16.09.2018 (dd.mm.yyyy)).

Therefore, setting a constraint to check some date value is wrong - you should check that value you put into that column is larger than 2000. Something like this (your code, simplified):

SQL> create table classe
  2    (cd_classe    number(8) constraint pk_cla primary key,
  3     --
  4     nr_anoletivo number(4) constraint ch_nra_2k check (nr_anoletivo > 2000)
  5                            not null,
  6     --
  7     cd_grau      number(2));

Table created.

SQL> insert into classe (cd_classe, nr_anoletivo, cd_grau) values (1, 1990, 23);
insert into classe (cd_classe, nr_anoletivo, cd_grau) values (1, 1990, 23)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_NRA_2K) violated


SQL> insert into classe (cd_classe, nr_anoletivo, cd_grau) values (2, 2018, 33);

1 row created.

SQL>

If you really meant to check the full date, then you should modify datatype column (to DATE ), as well as the constraint:

SQL> create table classe
  2    (cd_classe    number(8) constraint pk_cla primary key,
  3     --
  4     nr_anoletivo date      constraint ch_nra_2k check (nr_anoletivo > date '2000-01-01')
  5                            not null,
  6     --
  7     cd_grau      number(2));

Table created.

SQL> insert into classe (cd_classe, nr_anoletivo, cd_grau) values (1, date '1990-12-25', 23);
insert into classe (cd_classe, nr_anoletivo, cd_grau) values (1, date '1990-12-25', 23)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_NRA_2K) violated


SQL> insert into classe (cd_classe, nr_anoletivo, cd_grau) values (2, date '2018-09-16', 33);

1 row created.

SQL>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM