0

Can anyone please help me with writing a query to add constraint on BestFigure column with "%/%" i.e. it should be 3/10 format.

Please refer the image.

Image Link

create table Player(
Player_No int Identity(1,1) Primary Key, Player_Name Varchar(20) Not null,
Category Varchar(20) check (Category='batsman' or Category='bowler' or Category='Allrounder'),
BestFigure Varchar(10) check (Bestfigure like'%/%'))
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786

1 Answers1

0

I think a better solution is to store the two figures separately and then combine them:

BestFigure_left int,
BestFigure_right int,
BestFigure varchar(10) generated always as (concat(BestFigure_Left, '/', BestFigure_Right))

MySQL does not actually enforce check constraints (which is why a trigger is needed). If it did, you would do:

BestFigure Varchar(10) check (Bestfigure regexp '^[0-9]{1,3}/[0-9]{1,6}$')

Or something like that. I am unclear what the "3" and "10" mean in your description.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786