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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.