How to add a positive integer constraint to a integer column in MySQL?

后端 未结 5 919
情深已故
情深已故 2021-01-17 12:01

How can we add a constraint which enforces a column to have only positive values.

Tried the following mysql statement but it doesn\'t work

create tab         


        
5条回答
  •  逝去的感伤
    2021-01-17 12:14

    As of MySQL 8.0.16 (MariaDB 10.2.1), CHECK constraints are enforced. (In earlier versions constraint expressions were accepted in the syntax but ignored).

    Therefore you can use:

    CREATE TABLE test (
        test_column INT CHECK (test_column > 0)
    );
    

    or

    CREATE TABLE test (
        test_column INT,
        CONSTRAINT test_column_positive CHECK (test_column > 0)
    );
    

提交回复
热议问题