skip to Main Content

I need help regarding my sql project.

I have a table in which football results in column as VARCHAR. And I want to make it impossible to insert data into them other than in this format: number:number

For example: 2:1, 0:0, 10:0 and so on. Is it possible to set it to only this way?

Thank you.

2

Answers


  1. As suggested in the comments, it would be better to put the values in two separate INT columns. But if you really must do it this way, you can use a CHECK constraint to test the format using a regular expression.

    CONSTRAINT CHECK (result REGEXP '^[0-9]+:[0-9]+$')
    
    Login or Signup to reply.
  2. try this
    ALTER TABLE football_results
    ADD CONSTRAINT score_format CHECK (score LIKE ‘[0-9]:[0-9]’)

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