mysqlÖÐcheckµÄÓ÷¨
mysql check Ô¼ÊøÊÇÒ»ÖÖÊý¾Ý¿âÔ¼Êø£¬ÓÃÓÚÈ·±£Êý¾Ý±íÖеÄÁÐÖª×ãÖ¸¶¨µÄÌõ¼þ¡£ËüʹÓà alter table Óï¾ä½¨É裬Ó﷨Ϊ£ºalter table table_name add constraint constraint_name check (condition)¡£ÀûÒæ°üÀ¨Êý¾ÝÍêÕûÐÔ¡¢´úÂë¿É¶ÁÐÔºÍÐÔÄÜÓÅ»¯¡£ÐèҪעÖصÄÊÇ£¬check Ô¼ÊøÖ»ÔÚÊý¾Ý²åÈë»ò¸üÐÂʱ¼ì²éÊý¾Ý£¬²»ÊÊÓÃÓÚÏÖÓеÄÊý¾Ý¡£
MySQL CHECK Ô¼Êø
ʲôÊÇ CHECK Ô¼Êø£¿
CHECK Ô¼ÊøÊÇÒ»ÖÖÊý¾Ý¿âÔ¼Êø£¬ËüÓÃÓÚÈ·±£Êý¾Ý±íÖеÄÁÐÖª×ãÖ¸¶¨µÄÌõ¼þ¡£
ÔõÑùʹÓà CHECK Ô¼Êø£º
CHECK Ô¼Êø¿ÉÒÔʹÓÃÒÔÏÂÓï·¨½¨É裺
ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (condition);
µÇ¼ºó¸´ÖÆ
ÆäÖУº
table_name ÊÇÒªÌí¼ÓÔ¼ÊøµÄÊý¾Ý±íÃû³Æ¡£
constraint_name ÊÇÔ¼ÊøµÄΨһÃû³Æ¡£
condition ÊÇÒª¼ì²éµÄÌõ¼þ£¬Ëü¿ÉÒÔÊÇÈκÎÓÐÓÃµÄ SQL ±í´ïʽ¡£
Ìõ¼þʾÀý£º
ÒÔÏÂÊÇһЩ CHECK Ô¼ÊøÌõ¼þʾÀý£º
salary > 0: È·±£ salary ÁеÄÖµ´óÓÚ 0¡£
age BETWEEN 18 AND 65: È·±£ age ÁеÄÖµ½éÓÚ 18 ºÍ 65 Ö®¼ä¡£
gender IN (‘M’, ‘F’): È·±£ gender ÁеÄֵΪ ‘M’ »ò ‘F’¡£
Óŵ㣺
ʹÓà CHECK Ô¼ÊøµÄÀûÒæ°üÀ¨£º
Êý¾ÝÍêÕûÐÔ£ºËü¿ÉÈ·±£²åÈëµ½Êý¾Ý±íÖеÄÊý¾ÝÖª×ãÖ¸¶¨µÄÌõ¼þ¡£
´úÂë¿É¶ÁÐÔ£ºËüÔÊÐíÔÚ½¨Éè±íʱָ¶¨Ìõ¼þ£¬´Ó¶øÌá¸ß´úÂëµÄ¿É¶ÁÐÔ¡£
ÐÔÄÜÓÅ»¯£ºÊý¾Ý¿â¿ÉÒÔʹÓà CHECK Ô¼ÊøÀ´¿ìËÙ¼ì²é²åÈëµÄÊý¾Ý£¬´Ó¶øÌá¸ßÐÔÄÜ¡£
×¢ÖØÊÂÏ
ÔÚʹÓà CHECK Ô¼Êøʱ£¬ÐèҪ˼Á¿ÒÔÏÂ×¢ÖØÊÂÏ
CHECK Ô¼ÊøÖ»ÔÚÊý¾Ý²åÈë»ò¸üÐÂʱ¼ì²éÊý¾Ý¡£
ÈôÊÇÎ¥·´ CHECK Ô¼Êø£¬½«Òý·¢¹ýʧ£¬×èÖ¹²åÈë»ò¸üвÙ×÷¡£
CHECK Ô¼Êø²»ÊÊÓÃÓÚÏÖÓеÄÊý¾Ý£¬ËüÖ»ÊÊÓÃÓÚÔÚ½¨ÉèÔ¼Êøºó²åÈëµÄÐÂÊý¾Ý¡£
ÒÔÉϾÍÊÇmysqlÖÐcheckµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡