mysqlÖÐΨһµÄË÷ÒýÒªº¦×ÖÊÇ
ΨһË÷ÒýÒªº¦×ÖÊÇ unique£¬ÓÃÓÚÈ·±£±íÖÐÖ¸¶¨ÁлòÁÐ×éºÏ¾ßÓÐΨһֵ¡£½¨ÉèΨһË÷ÒýµÄÀûÒæ°üÀ¨£ºÊý¾ÝÍêÕûÐÔ¡¢ÅÌÎÊÐÔÄÜÓÅ»¯ºÍΨһԼÊøʵÑé¡£
MySQL ÖÐΨһµÄË÷ÒýÒªº¦×Ö
MySQL ÖУ¬ÓÃÓÚ½¨ÉèΨһË÷ÒýµÄÒªº¦×ÖÊÇ UNIQUE¡£
ʲôÊÇΨһË÷Òý£¿
ΨһË÷ÒýÊÇÒ»ÖÖÌØÊâÀàÐ͵ÄË÷Òý£¬¿ÉÈ·±£±íÖÐÿ¸ö¼Í¼ÖеÄÌض¨ÁлòÁÐ×éºÏ¶¼ÊÇΨһµÄ¡£»»¾ä»°Ëµ£¬Ëü²»ÔÊÐí±íÖзºÆðÖظ´µÄÖµ¡£
ΪʲôҪʹÓÃΨһË÷Òý£¿
ʹÓÃΨһË÷Òý¾ßÓÐÒÔÏÂÀûÒ棺
È·±£Êý¾ÝÍêÕûÐÔ£º±ÜÃâͳһÁлòÁÐ×éºÏ·ºÆðÖظ´Öµ¡£
ÓÅ»¯ÅÌÎÊÐÔÄÜ£ºÎ¨Ò»Ë÷Òý¿ÉÒÔ¼ÓËÙ»ùÓÚÏìÓ¦ÁеÄÅÌÎÊËÙÂÊ£¬ÓÉÓÚËü¿ÉÒÔ¿ìËÙÕÒµ½ËùÐè¼Í¼¡£
ʵÑéΨһԼÊø£ºÎ¨Ò»Ë÷Òý¿ÉÒÔͨ¹ýեȡÖظ´ÖµÀ´ÊµÑéΨһԼÊø¡£
ÔõÑù½¨ÉèΨһË÷Òý£¿
ÒÔÏ SQL Óï¾äÑÝʾÁËÔõÑù½¨ÉèΨһË÷Òý£º
CREATE UNIQUE INDEX index_name ON table_name (column_name1, column_name2, ...);
µÇ¼ºó¸´ÖÆ
ÀýÈ磬ÒÔÏÂÓï¾äÔÚ customers ±íµÄ name ºÍ email ÁÐÉϽ¨ÉèÒ»¸öΨһË÷Òý£º
CREATE UNIQUE INDEX unique_name_email ON customers (name, email);
µÇ¼ºó¸´ÖÆ
×¢ÖØ£º
ÿ¸ö±í×î¶à¿ÉÒÔÓÐ 16 ¸öΨһË÷Òý¡£
ΨһË÷Òý¿ÉÒÔÓ¦ÓÃÓÚ¶à¸öÁÐÉÏ£¬Ðγɸ´ºÏΨһË÷Òý¡£
ÈôÊÇÊÔͼÏò°üÀ¨Öظ´ÖµµÄ±íÖвåÈë¼Í¼£¬MySQL ½«Òý·¢¹ýʧ¡£
ÒÔÉϾÍÊÇmysqlÖÐΨһµÄË÷ÒýÒªº¦×ÖÊǵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡