mysqlÖÐcheckÔ¼ÊøÔõôÓÃ
mysql ÖÐµÄ check Ô¼ÊøÓÃÓÚÏÞÖƱíÖÐÊý¾ÝÖµµÄ¹æÄ££¬Ê¹ÓÃÓï·¨£ºalter table table_name add constraint constraint_name check (column_name expression)¡£Óŵã°üÀ¨Êý¾ÝÍêÕûÐÔ¡¢ÐÔÄܺͿÉά»¤ÐÔ£¬×¢ÖØÊÂÏîÊÇÖ»ÊÊÓÃÓÚµ¥¸öÁУ¬±í´ïʽ±ØÐè·µ»Ø²¼¶ûÖµ£¬Ô¼ÊøÃû³Æ±ØÐèΨһ¡£
MySQL ÖÐµÄ CHECK Ô¼Êø
ÔÚ MySQL ÖУ¬CHECK Ô¼ÊøÓÃÓÚÏÞÖƱíÖеÄÊý¾ÝÖµ¡£Ëüͨ¹ýÖ¸¶¨Ò»¸ö²¼¶û±í´ïʽÀ´½ç˵ÔÊÐí´æ´¢ÔÚ±íÖеÄÖµ¹æÄ£¡£
ÔõÑùʹÓà CHECK Ô¼Êø
Òª½¨Éè CHECK Ô¼Êø£¬ÇëʹÓÃÒÔÏÂÓï·¨£º
ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (column_name expression);
µÇ¼ºó¸´ÖÆ
ÆäÖУº
table_name ÊÇÒªÌí¼ÓÔ¼ÊøµÄ±íÃû¡£
constraint_name ÊÇÔ¼ÊøµÄΨһÃû³Æ¡£
column_name ÊÇÒªÓ¦ÓÃÔ¼ÊøµÄÁÐÃû¡£
expression ÊÇÒ»¸ö²¼¶û±í´ïʽ£¬Ëü½ç˵ÁËÔÊÐí´æ´¢ÔÚ¸ÃÁÐÖеÄÖµ¡£
ÔõÑùʹÓà CHECK Ô¼ÊøʾÀý
ÀýÈ磬Ҫ½¨ÉèÒ»¸ö½öÔÊÐíÕýÊýµÄ±íÃûΪ employees µÄ±íÖÐ salary ÁУ¬¿ÉÒÔʹÓÃÒÔÏÂÔ¼Êø£º
ALTER TABLE employees ADD CONSTRAINT positive_salary CHECK (salary > 0);
µÇ¼ºó¸´ÖÆ
CHECK Ô¼ÊøµÄÓŵã
ʹÓà CHECK Ô¼ÊøÓÐÒÔÏÂÓŵ㣺
Êý¾ÝÍêÕûÐÔ£ºËüÈ·±£Ö»ÓÐÇкÏÔ¼ÊøÌõ¼þµÄÖµ²Å»ª²åÈëµ½±íÖС£
ÐÔÄÜ£ºËü¿ÉÒÔÔÚ²åÈëʱ¿ìËÙÑéÖ¤Êý¾Ý£¬´Ó¶øïÔÌÎÞЧÊý¾ÝµÄ´æ´¢¡£
¿Éά»¤ÐÔ£ºËüʹÁÐÔ¼Êø¸üÒ×ÓÚÃ÷È·ºÍÖÎÀí¡£
CHECK Ô¼ÊøµÄ×¢ÖØÊÂÏî
ÒÔÏÂÐèҪעÖØһЩÊÂÏ
CHECK Ô¼ÊøÖ»ÄÜÓ¦ÓÃÓÚµ¥¸öÁС£
±í´ïʽ±ØÐèʼÖÕ·µ»Ø TRUE »ò FALSE Öµ¡£
Ô¼ÊøÃû³Æ±ØÐèÔÚ±íÖÐΨһ¡£
ÒÔÉϾÍÊÇmysqlÖÐcheckÔ¼ÊøÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡