Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a custom CHECK constraint on a MySQL table?

I am having trouble with this table

CREATE TABLE `Participants` (   `meetid` int(11) NOT NULL,   `pid` varchar(15) NOT NULL,   `status` char(1) DEFAULT NULL,   PRIMARY KEY (`meetid`,`pid`),   CONSTRAINT `participants_ibfk_1` FOREIGN KEY (`meetid`) REFERENCES `Meetings` (`meetid`) ON DELETE CASCADE   CONSTRAINT `participants_ibfk_2` CHECK (status IN ('a','d','u'))   CONSTRAINT `participants_ibfk_3` CHECK (pid IN (SELECT name FROM Rooms) OR pid IN (SELECT userid FROM People)) ); 

I want to have a foreign key constraint, and that works. Then, I also want to add a constraint to the attribute status so it can only take the values 'a', 'd' and 'u'. It is not possible for me to set the field as Enum or set.

Can anyone tell me why this code does not work in MySQL?

like image 701
Mathias Bak Avatar asked Sep 22 '11 22:09

Mathias Bak


People also ask

How do I add a check constraint to an existing table?

The syntax for creating a check constraint in an ALTER TABLE statement in SQL Server (Transact-SQL) is: ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (column_name condition); table_name.

How do I apply a check constraint in MySQL?

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

How do I create a custom constraint in SQL?

Use SQL Server Management Studio In Object Explorer, expand the table to which you want to add a check constraint, right-click Constraints and select New Constraint.

How do I add a check constraint in MySQL workbench?

Here I want to create 2 CHECK constraint before the record insert to the database. ALTER TABLE SubjectEnrollment ADD CONSTRAINT register CHECK (register <= classSize AND register >=0), ADD CONSTRAINT available CHECK (available <= classSize AND available >= 0);


2 Answers

CHECK constraints are not supported by MySQL. You can define them, but they do nothing (as of MySQL 5.7).

From the manual:

The CHECK clause is parsed but ignored by all storage engines.

The workaround is to create triggers, but they aren't the easiest thing to work with.

If you want an open-source RDBMS that supports CHECK constraints, try PostgreSQL. It's actually a very good database.

like image 134
NullUserException Avatar answered Oct 09 '22 01:10

NullUserException


I don't understand why nobody here has mentioned that VIEW WITH CHECK OPTION can be a good alternative to the CHECK CONSTRAINT in MySQL:

CREATE VIEW name_of_view AS SELECT * FROM your_table WHERE <condition> WITH [LOCAL | CASCADED] CHECK OPTION; 

There is a doc on the MySQL site: The View WITH CHECK OPTION Clause

DROP TABLE `Participants`;  CREATE TABLE `Participants` (   `meetid` int(11) NOT NULL,   `pid` varchar(15) NOT NULL,   `status` char(1) DEFAULT NULL check (status IN ('a','d','u')),   PRIMARY KEY (`meetid`,`pid`) );  -- should work INSERT INTO `Participants` VALUES (1,1,'a'); -- should fail but doesn't because table check is not implemented in MySQL INSERT INTO `Participants` VALUES (2,1,'x');  DROP VIEW vParticipants; CREATE VIEW vParticipants AS    SELECT * FROM Participants WHERE status IN ('a','d','u')   WITH CHECK OPTION;  -- should work INSERT INTO vParticipants VALUES (3,1,'a'); -- will fail because view uses a WITH CHECK OPTION INSERT INTO vParticipants VALUES (4,1,'x'); 

P.S.: Keep in mind that your view should be updatable! See MySQL Updatable Views (thanks Romeo Sierra for clarification in comments).

like image 36
guzoff Avatar answered Oct 09 '22 03:10

guzoff