-1

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)

  • 2
    So, why don't you show us what you tried? And please explain why you are insecure about whether it works. Have you not just tried to insert some valid and some invalid data into your table to see what happens? – Thorsten Kettner Nov 08 '22 at 08:24
  • 2
    If the first 3 characters are *always* `ppl`, why are you bothering to store that? You can easily re-derive the 5 character identifier just from storing the 2 digits. – Damien_The_Unbeliever Nov 08 '22 at 08:28
  • Which dbms are you using? – jarlh Nov 08 '22 at 09:42

1 Answers1

1

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
Nino
  • 6,931
  • 2
  • 27
  • 42
  • 1
    Product specific answer to a question with no dbms specified. At least tell us which dbms this is for. – jarlh Nov 08 '22 at 08:52