How to change a table indicating that the data of a certain field can only vary from date 1 to date 2?

1

I have a table called "LOC_VEICULO" where the vehicle data is contained. In it I have the following field: DT_FABRICATION (DATE), where the manufacturing dates of the vehicles are stored.

I need to change the vehicles, indicating the dates of manufacture that should vary from 02/01/2008 until 02/01/2010.

How should I proceed?

    
asked by anonymous 10.09.2017 / 05:25

1 answer

2

To prevent data entries that are not on those dates you can create a contraint in the table, like this:

alter table LOC_VEICULO 
add constraint check_DT_FABRICACAO 
CHECK (DT_FABRICACAO between date'2008-01-02' and date'2010-01-02');

Or create a trigger that shows you the error:

CREATE OR REPLACE TRIGGER trg_valida_data
BEFORE INSERT OR UPDATE 
ON LOC_VEICULO
FOR EACH ROW
BEGIN
    if (:new.DT_FABRICACAO not between date'2008-01-02' and date'2010-01-02') then
        raise_application_error(-20000,'O valor de Data de fabricação deve variar entre 02/01/2008 e 02/01/2010');
    end if;
END;

/

    
10.09.2017 / 06:11