Search code examples
sql-serverformatcheck-constraint

Check constraint for string with a format in SQL Server


I have a table which is defined like:

create table HLV (
id nvarchar(7) check (id LIKE 'HLV[0-9][0-9][0-9][0-9]') Primary key,
birthday date,
full_name nvarchar(20) NOT NULL,
win int check (win > 0),
lose int check (lose > 0),
draw int check (draw > 0)
) 

I execute the code and it works, but when I insert the data into it with

insert into HLV values ('HLV0001',GETDATE(), 10,5,6)

I got the error

Column name or number of supplied values does not match table definition.

Is it because the check is wrong?


Solution

  • No it is because you missed full_name in your insert statement:

    insert into HLV values ('HLV0001',GETDATE(), 'michael jordan' ,10,5,6)