skip to Main Content

I get an Error because of the Check Constraint and i dont know why.

create table TStudenten (
StudId int unsigned auto_increment primary key,
StudVorname varchar(25) not null,
StudNachname varchar(25) not null,
StudStrasse varchar(40),
StudPLZ char(4),
StudOrt varchar(20),
StudGebDatum date,
StudEmail varchar(30) not null,
constraint GebDatumCheck check ('2023-01-01' - StudGebDatum > 18));

And then i insert Data in it

insert into TStudenten values (null, 'Werner', 'Döbele', 'Bergstrasse 15', '8278', 'Pfyn', '1974-09-13', '[email protected]');

And i get a Error Code: ‘Error Code: 3819. Check constraint ‘GebDatumCheck’ is violated.’

I tried without the Check and then it worked but i need the Check.

2

Answers


  1. '2023-01-01' - StudGebDatum > 18
    means that you compare the date difference to 18 days not years.

    You can use DATE_SUB('2023-01-01', INTERVAL 18 YEAR) or simply '2005-01-01' for comparison instead.

    Login or Signup to reply.
  2. In your constraint check you are looking for something to allow only dates from the last 18 years.

    Try this instead :

    constraint GebDatumCheck check (YEAR('2023-01-01') - YEAR(StudGebDatum) > 18));
    

    Demo here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search