-1

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Arhana
  • 41
  • 6

1 Answers1

1

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

nbk
  • 45,398
  • 8
  • 30
  • 47
  • i want to create new table and i have to add check constraint on it then what should i do? – Arhana Apr 06 '20 at 15:03
  • I tried and it's not working,actually i didnt understand the syntax , i just want to check whether age is greater than 18 i.e CHECK(age>18),can u please tell me how to implement this with the above syntax? – Arhana Apr 06 '20 at 15:27
  • please add the create code to your post, so that i can have a look – nbk Apr 06 '20 at 15:28
  • CREATE TABLE vote(id INT,age INT,CONSTRAINT `alllow1` CHECK (Type IN ('allowed','not allowed','neutral'); I want to do this CHECK(age>18) , I dont know how to. – Arhana Apr 06 '20 at 15:33
  • thank you,table is created and check constraint is applied but not working because it is taking values less than 18 – Arhana Apr 06 '20 at 15:49
  • 1
    drop the table and change the constraint to the age you want you can add more than one constraint. a cpnstraint check if its condition is met and then acceppts it or denies in case of my age can obly be over 18 , but you understand i gave you a working example on that you can build – nbk Apr 06 '20 at 16:06
  • Unfortunately MySQL does not support SQL check constraints. You can define them in your DDL query for compatibility reasons but they are just ignored. You can create BEFORE INSERT and BEFORE UPDATE triggers which either cause an error or set the field to its default value when the requirements of the data are not met. – Arhana Apr 06 '20 at 16:12