mysqlconstraintssqlyog

How to add check constraint in 'SQLyog community edition - Mysql GUI'?


There is no check constraint tab in SQLyog community edition - Mysql GUI.


Solution

  • The same as in mysql workbench or phpmyadmin or console

    Open a sql Window and run a query

    see https://sqlyogkb.webyog.com/article/45-sql-window

    A example Query is following

    To alter your existing table use

    ALTER TABLE SomeTable 
    ADD CONSTRAINT `alllow1` CHECK (Type IN 
    ('allowed','not allowed','neutral'))
    

    Or add following to the CREATE TABLE separated by comma

    CONSTRAINT `alllow1` CHECK (Type IN 
    ('allowed','not allowed','neutral')
    

    For your table:

    CREATE TABLE vote (
        id INT,
        age INT,
        CONSTRAINT checkage CHECK (age > 18)
    ); 
    

    This works only in mysql 8.x.

    If you have a mysql 5.x You need and BEFORE INSERT and BEFORE UPDATE TRIGGER