1

I am new to MySQL, I want to restrict the row to only accept values from 1 to 1000 (inclusive)

CREATE TABLE Company(
    ID int NOT NULL,
    Name varchar(100),
    Employees int
);

this is the table and I need to restrict the ID row to only have values between 1 to 1000.

I have referred Want to restrict the value of a MySQL field to specific range (Decimal values) and How to restrict a column value in SQLite / MySQL all are kinda old!

I need an updated solution. Thanks in Advance

Shadow
  • 33,525
  • 10
  • 51
  • 64
MrSrv7
  • 585
  • 8
  • 22

1 Answers1

2

Since MySQL 8.0 you can use CHECK constraint in table creation statement like:

CREATE TABLE Company (
  ID int NOT NULL  CHECK (ID BETWEEN 1 AND 1000), 
  Name varchar(100), 
  Employees int
);

Test it on SQLize.online

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39