Home > Enterprise >  Error Code: 3819. Check constraint 'checker_1' is violated
Error Code: 3819. Check constraint 'checker_1' is violated

Time:11-02

My question says that it wants me to add a constraint so that the company cannot purchase any more car that were made before 2018.The problem is that my database already has info about cars that were made before 2018. I cant delete the table because it will mess with some of my other queries . Can i get some advice on it?

My code below for vehicle table

create table Vehicle(
LicensePlate varchar(48) primary key,  /*Primary key for table Vehicle for License plate, variable can hold up to 48 characters*/
Make varchar (48),  
CarYear int 
);

insert into Vehicle (LicensePlate,Make,CarYear) values ('1234 AA','Toyota',1970);
insert into Vehicle (LicensePlate,Make,CarYear) values ('1237 AB','Mazda',1995);
insert into Vehicle (LicensePlate,Make,CarYear) values ('1892 BG','Toyota',2000);
insert into Vehicle (LicensePlate,Make,CarYear) values ('1876 FA','Nissan',1999);
insert into Vehicle (LicensePlate,Make,CarYear) values ('3021 AA','Mazda',1950);
insert into Vehicle (LicensePlate,Make,CarYear) values ('2134 FF','Toyota',1992);
-- Company has decided not to purchase any more car that were made before 2018. Add appropriate constraints to the Vehicle table to enforce this requirement.

alter table Vehicle add constraint checker_1 check (CarYear > 2018);

CodePudding user response:

Try this query if it's feasible for your operation:

SELECT LicensePlate, Make, CarYear,
         CASE WHEN CarYear > 2018 THEN 1 ELSE 0 END AS car_year_chk
FROM Vehicle;

On your current sample data, you'll get this result:

LicensePlate Make CarYear car_year_chk
1234 AA Toyota 1970 0
1237 AB Mazda 1995 0
1876 FA Nissan 1999 0
1892 BG Toyota 2000 0
2134 FF Toyota 1992 0
3021 AA Mazda 1950 0

Demo fiddle

  • Related