Search code examples
sqlconstraintscheck-constraints

How to make a check-constraint to control the data was inserted


I have a column named "ppl". How do I make a constraint for this column that the data inserted got to have 5 characters and start with "ppl" (the next 2 characters got to be numbers)

I tried using CHECK but I don't know how to and not sure if it works (I'm new to SQL)


Solution

  • Assuming you're using Microsoft SQL Server, you can achieve your goal by adding constraint to your ppl column.

    Something like this:

    ALTER TABLE [dbo].[YourTable]  
        WITH CHECK 
           ADD  CONSTRAINT [CK_YourTable] CHECK (([ppl] like 'ppl[0-9][0-9]'))
    GO