{"id":100413,"date":"2024-07-19T19:02:16","date_gmt":"2024-07-19T11:02:16","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=100413"},"modified":"2024-07-19T19:02:16","modified_gmt":"2024-07-19T11:02:16","slug":"mariadb-table-constraints","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mariadb-table-constraints.html","title":{"rendered":"Mariadb\u5b66\u4e60\u603b\u7ed3\uff08\u4e94\uff09\uff1a\u6570\u636e\u5e93\u8868\u7ea6\u675f\u53ca\u4e09\u8303\u5f0f"},"content":{"rendered":"
<\/p>\n
\u8303\u5f0f\uff08NF\uff09\uff1a\u8bbe\u8ba1\u5173\u7cfb\u6570\u636e\u5e93\u65f6\uff0c\u9075\u4ece\u4e0d\u540c\u7684\u89c4\u8303\u8981\u6c42\uff0c\u8bbe\u8ba1\u51fa\u5408\u7406\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u8fd9\u4e9b\u4e0d\u540c\u7684\u89c4\u8303\u8981\u6c42\u88ab\u79f0\u4e3a\u4e0d\u540c\u7684\u8303\u5f0f\uff0c\u5404\u79cd\u8303\u5f0f\u5448\u9012\u6b21\u89c4\u8303\uff0c\u8d8a\u9ad8\u7684\u8303\u5f0f\u6570\u636e\u5e93\u5197\u4f59\u8d8a\u5c0f\u3002\u4f46\u662f\u6709\u4e9b\u65f6\u5019\u4e00\u6627\u7684\u8ffd\u6c42\u8303\u5f0f\u51cf\u5c11\u5197\u4f59\uff0c\u53cd\u800c\u4f1a\u964d\u4f4e\u6570\u636e\u8bfb\u5199\u7684\u6548\u7387\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u8981\u53cd\u8303\u5f0f\uff0c\u5229\u7528\u7a7a\u95f4\u6765\u6362\u65f6\u95f4\u3002\u53ef\u4ee5\u628a\u5b83\u7c97\u7565\u5730\u7406\u89e3\u4e3a\u4e00\u5f20\u6570\u636e\u8868\u7684\u8868\u7ed3\u6784\u6240\u7b26\u5408\u7684\u67d0\u79cd\u8bbe\u8ba1\u6807\u51c6\u7684\u7ea7\u522b<\/strong>\u3002<\/p>\n \u5373\u8868\u7684\u5217\u7684\u5177\u6709\u539f\u5b50\u6027,\u4e0d\u53ef\u518d\u5206\u89e3\uff0c\u5373\u5217\u7684\u4fe1\u606f\uff0c\u4e0d\u80fd\u5206\u89e3, \u53ea\u8981\u6570\u636e\u5e93\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93(mysql\/oracle\/db2\/informix\/sysbase\/sql server)\uff0c\u5c31\u81ea\u52a8\u7684\u6ee1\u8db31NF\u3002\u6570\u636e\u5e93\u8868\u7684\u6bcf\u4e00\u5217\u90fd\u662f\u4e0d\u53ef\u5206\u5272\u7684\u539f\u5b50\u6570\u636e\u9879\uff0c\u800c\u4e0d\u80fd\u662f\u96c6\u5408\uff0c\u6570\u7ec4\uff0c\u8bb0\u5f55\u7b49\u975e\u539f\u5b50\u6570\u636e\u9879\u3002\u5982\u679c\u5b9e\u4f53\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\u6709\u591a\u4e2a\u503c\u65f6\uff0c\u5fc5\u987b\u62c6\u5206\u4e3a\u4e0d\u540c\u7684\u5c5e\u6027 \u3002\u901a\u4fd7\u7406\u89e3\u5373\u4e00\u4e2a\u5b57\u6bb5\u53ea\u5b58\u50a8\u4e00\u9879\u4fe1\u606f\u3002<\/p>\n <\/p>\n \u7b2c\u4e8c\u8303\u5f0f\uff082NF\uff09\u662f\u5728\u7b2c\u4e00\u8303\u5f0f\uff081NF\uff09\u7684\u57fa\u7840\u4e0a\u5efa\u7acb\u8d77\u6765\u7684\uff0c\u5373\u6ee1\u8db3\u7b2c\u4e8c\u8303\u5f0f\uff082NF\uff09\u5fc5\u987b\u5148\u6ee1\u8db3\u7b2c\u4e00\u8303\u5f0f\uff081NF\uff09\u3002\u6ee1\u8db31NF\u540e\uff0c\u8981\u6c42\u8868\u4e2d\u7684\u6240\u6709\u5217\uff0c\u90fd\u5fc5\u987b\u4f9d\u8d56\u4e8e\u4e3b\u952e\uff0c\u800c\u4e0d\u80fd\u6709\u4efb\u4f55\u4e00\u5217\u4e0e\u4e3b\u952e\u6ca1\u6709\u5173\u7cfb\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u8868\u53ea\u63cf\u8ff0\u4e00\u4ef6\u4e8b\u60c5\uff1b<\/p>\n \u4f8b\u5982\uff1a\u8ba2\u5355\u8868\u53ea\u63cf\u8ff0\u8ba2\u5355\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u6240\u4ee5\u6240\u6709\u5b57\u6bb5\u90fd\u5fc5\u987b\u4e0e\u8ba2\u5355id\u76f8\u5173\uff1b\u4ea7\u54c1\u8868\u53ea\u63cf\u8ff0\u4ea7\u54c1\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u6240\u4ee5\u6240\u6709\u5b57\u6bb5\u90fd\u5fc5\u987b\u4e0e\u4ea7\u54c1id\u76f8 \u5173\uff1b\u56e0\u6b64\u4e0d\u80fd\u5728\u4e00\u5f20\u8868\u4e2d\u540c\u65f6\u51fa\u73b0\u8ba2\u5355\u4fe1\u606f\u4e0e\u4ea7\u54c1\u4fe1\u606f\uff1b\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n <\/p>\n \u5fc5\u987b\u5148\u6ee1\u8db3\u7b2c\u4e8c\u8303\u5f0f\uff082NF\uff09\uff0c\u8981\u6c42\uff1a\u8868\u4e2d\u7684\u6bcf\u4e00\u5217\u53ea\u4e0e\u4e3b\u952e\u76f4\u63a5\u76f8\u5173\u800c\u4e0d\u662f\u95f4\u63a5\u76f8\u5173\uff0c\u8868\u4e2d\u7684\u6bcf\u4e00\u5217\u53ea\u80fd\u4f9d\u8d56\u4e8e\u4e3b\u952e\u3002<\/p>\n \u4f8b\u5982\uff1a\u8ba2\u5355\u8868\u4e2d\u9700\u8981\u6709\u5ba2\u6237\u76f8\u5173\u4fe1\u606f\uff0c\u5728\u5206\u79bb\u51fa\u5ba2\u6237\u8868\u4e4b\u540e\uff0c\u8ba2\u5355\u8868\u4e2d\u53ea\u9700\u8981\u6709\u4e00\u4e2a\u7528\u6237id\u5373\u53ef\uff0c\u800c\u4e0d\u80fd\u6709\u5176\u4ed6\u7684\u5ba2\u6237\u4fe1\u606f\u3002\u56e0\u4e3a\u5176\u4ed6\u7684\u5ba2\u6237\u4fe1\u606f\u76f4\u63a5\u5173\u8054\u4e8e\u7528\u6237id\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u4e0e\u8ba2\u5355id\u76f4\u63a5\u76f8\u5173\u3002<\/p>\n <\/p>\n \u7ea6\u675f\u662f\u7528\u6765\u9650\u5b9a\u8868\u4e2d\u6570\u636e\u51c6\u786e\u6027\u3001\u5b8c\u6574\u6027\u3001\u4e00\u81f4\u6027\u3001\u8054\u52a8\u6027\u7684\u4e00\u5957\u89c4\u5219\u3002\u5728Mysql\u4e2d\uff0c\u7ea6\u675f\u4fdd\u5b58\u5728information_schema\u6570\u636e\u5e93\u7684table_constraints\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8be5\u8868\u67e5\u8be2\u7ea6\u675f\u4fe1\u606f\u3002\u5982\u4e0b\u56fe\uff1a <\/p>\n <\/p>\n \u975e\u7a7a\u7ea6\u675f\uff0c\u662f\u5426\u5141\u8bb8\u8be5\u5217\u7684\u503c\u4e3aNULL\uff0c\u8fd9\u91cc\u6709\u4e00\u70b9\u5f88\u91cd\u8981\uff0c\u5f88\u591a\u5b57\u6bb5\uff08\u9664\u4e86\u65f6\u95f4\uff1f\uff09\u9ed8\u8ba4\u503c\u5982\u679c\u4e0d\u6307\u5b9a\u7684\u8bdd\u90fd\u662fNULL\uff0c\u6240\u4ee5\u9664\u4e86NULL=NULL\uff0c\u5176\u4ed6\u503c\u5e76\u4e0d\u7b49\u4e8eNULL\uff0c\u6bd4\u5982\u201c\u201d\u30010\u7b49\u3002<\/p>\n \u4fee\u6539\u4e00\u4e2a\u5b57\u6bb5\u4e3aNOT NULL\uff1a<\/p>\n \u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5bf9\u4e8e\u9ed8\u8ba4\u503c\u4e3aNULL\u4f46\u662f\u53c8\u6ca1\u6709\u6307\u5b9a\u63d2\u5165\u8fd9\u4e2a\u5b57\u6bb5\uff1a<\/p>\n \u53ef\u4ee5\u770b\u5230username\u8fd9\u4e00\u5217\u7684\u503c\u4e3a\u7a7a\u5b57\u7b26\uff0c\u800c\u5b83\u7684\u9ed8\u8ba4\u503c\u4e3aNULL\u554a\uff0c \u67e5\u4e86\u4e00\u4e0b~\u56e0\u4e3aNULL\u4e3a\u9ed8\u8ba4\u503c\uff0c\u4f46\u662f\u53c8\u4e0d\u5141\u8bb8NULL\u503c\uff0c\u6240\u4ee5\uff0c\u4e5f\u5c31\u662f\u8bf4\u73b0\u5728username\u8fd9\u4e2a\u5b57\u6bb5\u6ca1\u6709\u503c\uff0c\u56e0\u4e3aSQL_MODE\u7684\u539f\u56e0\uff0c\u53ea\u4f1a\u8b66\u544a\u4e00\u4e0b\u5e76\u4e0d\u4f1a\u76f4\u63a5\u62a5\u9519\uff0c\u5f53\u6211\u4eec\u6307\u5b9aSQL_MODE\u4e3a'STRICT_ALL_TABLES'\u65f6\uff0c\u6b64\u65f6\u63d2\u5165\u5c31\u4f1a\u62a5\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n unique\u4ee3\u8868\u552f\u4e00\u7ea6\u675f\uff1a\u552f\u4e00\u7ea6\u675f\u662f\u6307\u5b9atable\u7684\u5217\u6216\u5217\u7ec4\u5408\u4e0d\u80fd\u91cd\u590d\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u552f\u4e00\u6027\uff0c\u867d\u7136\u552f\u4e00\u7ea6\u675f\u4e0d\u5141\u8bb8\u51fa\u73b0\u91cd\u590d\u7684\u503c\uff0c\u4f46\u662f\u53ef\u4ee5\u4e3a\u591a\u4e2anull<\/strong>\uff0c\u540c\u4e00\u4e2a\u8868\u53ef\u4ee5\u6709\u591a\u4e2a\u552f\u4e00\u7ea6\u675f\uff0c\u591a\u4e2a\u5217\u7ec4\u5408\u7684\u7ea6\u675f\u3002\u5728\u521b\u5efa\u552f\u4e00\u7ea6\u675f\u7684\u65f6\u5019\uff0c\u5982\u679c\u4e0d\u7ed9\u552f\u4e00\u7ea6\u675f\u540d\u79f0\uff0c\u5c31\u9ed8\u8ba4\u548c\u5217\u540d\u76f8\u540c\uff0cMySQL\u4f1a\u7ed9\u552f\u4e00\u7ea6\u675f\u7684\u5217\u4e0a\u9ed8\u8ba4\u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u7d22\u5f15\u3002<\/p>\n \u6dfb\u52a0\u552f\u4e00\u7ea6\u675f\uff1a<\/p>\n \u4e3b\u952e\u7ea6\u675f\u76f8\u5f53\u4e8e\u552f\u4e00\u7ea6\u675f+\u975e\u7a7a\u7ea6\u675f<\/strong>\u7684\u7ec4\u5408\uff0c\u4e3b\u952e\u7ea6\u675f\u5217\u4e0d\u5141\u8bb8\u91cd\u590d\uff0c\u4e5f\u4e0d\u5141\u8bb8\u51fa\u73b0\u7a7a\u503c\u3002\u5982\u679c\u662f\u591a\u5217\u7ec4\u5408\u7684\u4e3b\u952e\u7ea6\u675f\uff0c \u90a3\u4e48\u8fd9\u4e9b\u5217\u90fd\u4e0d\u5141\u8bb8\u4e3a\u7a7a\u503c\uff0c\u5e76\u4e14\u7ec4\u5408\u7684\u503c\u4e0d\u5141\u8bb8\u91cd\u590d\u3002\u6bcf\u4e2a\u8868\u6700\u591a\u53ea\u5141\u8bb8\u4e00\u4e2a\u4e3b\u952e<\/strong>\uff0c\u5efa\u7acb\u4e3b\u952e\u7ea6\u675f\u53ef\u4ee5\u5728\u5217\u7ea7\u522b\u521b\u5efa\uff0c\u4e5f\u53ef\u4ee5\u5728\u8868\u7ea7\u522b\u4e0a\u521b\u5efa\uff0cMySQL\u7684\u4e3b\u952e\u540d\u603b\u662fPRIMARY\uff0c \u5f53\u521b\u5efa\u4e3b\u952e\u7ea6\u675f\u65f6\uff0c\u7cfb\u7edf\u9ed8\u8ba4\u4f1a\u5728\u6240\u5728\u7684\u5217\u548c\u5217\u7ec4\u5408\u4e0a\u5efa\u7acb\u5bf9\u5e94\u7684\u552f\u4e00\u7d22\u5f15\u3002<\/p>\n \u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n \u5916\u952e\u7ea6\u675f\u662f\u4fdd\u8bc1\u4e00\u4e2a\u6216\u4e24\u4e2a\u8868\u4e4b\u95f4\u7684\u53c2\u7167\u5b8c\u6574\u6027<\/strong>\uff0c\u5916\u952e\u662f\u6784\u5efa\u4e8e\u4e00\u4e2a\u8868\u7684\u4e24\u4e2a\u5b57\u6bb5\u6216\u662f\u4e24\u4e2a\u8868\u7684\u4e24\u4e2a\u5b57\u6bb5\u4e4b\u95f4\u7684\u53c2\u7167\u5173\u7cfb<\/strong>\u3002 \u4e5f\u5c31\u662f\u8bf4\u4ece\u8868\u7684\u5916\u952e\u503c\u5fc5\u987b\u5728\u4e3b\u8868\u4e2d\u80fd\u627e\u5230\u6216\u8005\u4e3a\u7a7a<\/strong>\uff0c\u5f53\u4e3b\u8868\u7684\u8bb0\u5f55\u88ab\u4ece\u8868\u53c2\u7167\u65f6\uff0c\u4e3b\u8868\u7684\u8bb0\u5f55\u5c06\u4e0d\u5141\u8bb8\u5220\u9664\uff0c\u5982\u679c\u8981\u5220\u9664\u6570\u636e\uff0c\u9700\u8981\u5148\u5220\u9664\u4ece\u8868\u4e2d\u4f9d\u8d56\u8be5\u8bb0\u5f55\u7684\u6570\u636e\uff0c\u7136\u540e\u624d\u53ef\u4ee5\u5220\u9664\u4e3b\u8868\u7684\u6570\u636e\uff0c\u8fd8\u6709\u4e00\u79cd\u5c31\u662f\u7ea7\u8054\u5220\u9664\u5b50\u8868\u6570\u636e\u3002 \u73b0\u5728\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aGROUP\u8868\u5427\uff0c\u7528\u4e8e\u8bb0\u5f55\u7528\u6237\u7684\u5206\u7ec4\u4fe1\u606f\uff0c<\/p>\n \u7136\u540e\u5462~\u4e3auser\u8868\u6dfb\u52a0\u4e00\u4e2a\u8bb0\u5f55\uff0c\u8bb0\u5f55\u7528\u6237\u5c5e\u4e8e\u90a3\u4e2a\u7ec4<\/p>\n \/\/\u6dfb\u52a0\u4e00\u4e2a\u5916\u952e<\/p>\n \/\/\u9a8c\u8bc1\u5916\u952e\u7ea6\u675f<\/p>\n \/\/\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f46\u662f\u4e0d\u53ef\u4ee5\u4e3a\u53c2\u7167\u8868\u4e2d\u6ca1\u6709\u7684\u503c<\/p>\n \u5916\u952e\u5b9a\u4e49\uff1a<\/p>\n \u5176\u4e2d\u4e00\u4e0b\u7ea7\u8054\u64cd\u4f5c\u9700\u8981\u6ce8\u610f\uff1a CHECK\u7ea6\u675f\u5c31\u662f\u5f53\u5411\u8868\u4e2d\u63d2\u5165\u4e00\u884c\u6216\u66f4\u65b0\u4e00\u884c\u6570\u636e\u65f6\u8fdb\u884cCHECK\u7ea6\u675f\u68c0\u67e5\uff0cCHECK\u63a5\u53d7\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e3aTRUE\u5219\u5141\u8bb8\u63d2\u5165\uff0c\u5982\u679c\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e3aFALSE\u5219\u62d2\u7edd\u63d2\u5165\uff0c\u5728MariaDB10.2\u7248\u672c\u624d\u5f00\u59cb\u652f\u6301CHECK\u3002<\/p>\n \u5e38\u89c1\u7684CHECK\u7ea6\u675f\u6709\uff1a<\/p>\n \u4f8b\u5b50\uff1a\u68c0\u67e5\u7528\u6237\u540d\u957f\u5ea6\u662f\u5426\u5927\u4e8e0<\/p>\n \u8fd9\u4e2a\u4e1c\u897f\u770b\u8d77\u6765\u5f88\u9e21\u808b\u7684\u6837\u5b50\uff0c\u597d\u50cf\u4e00\u822c\u90fd\u662f\u5728\u4e1a\u52a1\u5c42\u8fdb\u884c\u6570\u636e\u5224\u65ad\u4e86\uff0c\u800c\u4e14\u6570\u636e\u5e93\u561b~\u5c31\u5b58\u6570\u636e\u5c31\u597d\u4e86\u3002<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttps:\/\/100ye.wang\/linux\/mariadb-table-constraints.html<\/a><\/p>\n
\n\u4ee5\u4e0a\uff0c\u5c31\u4e0d\u7b26\u5408\u7b2c\u4e00\u8303\u5f0f\uff0c\u56e0\u4e3a\u8fdb\u8d27\u3001\u9500\u552e\u8fd8\u53ef\u4ee5\u518d\u5206\u4e3a\uff0c\u8fdb\u8d27\u6570\u91cf\u3001\u8fdb\u8d27\u5355\u4f4d\u3001\u9500\u552e\u5355\u4f4d\u3001\u9500\u552e\u6570\u91cf\u7b49\uff0c\u4ee5\u4e0b\u5219\u6ee1\u8db3\u4e86\u7b2c\u4e00\u8303\u5f0f\u3002<\/p>\n
\n<\/strong><\/div>\n\r\nMariaDB [mydb]> DESC user;\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n| Field | Type | Null | Key | Default | Extra |\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n| id | int(11) | NO | PRI | NULL | auto_increment |\r\n| username | varchar(10) | NO | | NULL | |\r\n| password | varchar(10) | NO | | NULL | |\r\n| regtime | timestamp | NO | | CURRENT_TIMESTAMP | |\r\n| logtime | timestamp | NO | | 0000-00-00 00:00:00 | |\r\n| logip | varchar(20) | YES | | NULL | |\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n6 rows in set (0.00 sec)\r\n\r\nMariaDB [mydb]> ALTER TABLE user MODIFY logip varchar(20) NOT NULL;\r\nQuery OK, 5 rows affected, 5 warnings (0.04 sec) \r\nRecords: 5 Duplicates: 0 Warnings: 5\r\n\r\nMariaDB [mydb]> DESC user;\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n| Field | Type | Null | Key | Default | Extra |\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n| id | int(11) | NO | PRI | NULL | auto_increment |\r\n| username | varchar(10) | NO | | NULL | |\r\n| password | varchar(10) | NO | | NULL | |\r\n| regtime | timestamp | NO | | CURRENT_TIMESTAMP | |\r\n| logtime | timestamp | NO | | 0000-00-00 00:00:00 | |\r\n| logip | varchar(20) | NO | | NULL | |\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n6 rows in set (0.01 sec)\r\n\r\n<\/pre>\n
\r\nMariaDB [mydb]> DESC user;\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n| Field | Type | Null | Key | Default | Extra |\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n| id | int(11) | NO | PRI | NULL | auto_increment |\r\n| username | varchar(10) | NO | | NULL | |\r\n| password | varchar(10) | NO | | NULL | |\r\n| regtime | timestamp | NO | | CURRENT_TIMESTAMP | |\r\n| logtime | timestamp | NO | | 0000-00-00 00:00:00 | |\r\n| logip | varchar(20) | YES | | NULL | |\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n\/\/\u770busername\u8fd9\u4e2a\u5b57\u6bb5\uff0c\u9ed8\u8ba4\u503c\u4e3aNULL\uff0c\u4e0d\u5141\u8bb8NULL\r\nMariaDB [mydb]> INSERT INTO user(password) VALUES('test7');\r\nQuery OK, 1 row affected, 1 warning (0.00 sec)\r\n\/\/\u8fd9\u91cc\u770b\u5230\u6211\u4eec\u63d2\u5165\u6210\u529f\u4e86\u3002\r\nMariaDB [mydb]> SELECT * FROM user WHERE password='test7';\r\n+----+----------+----------+---------------------+---------------------+-------+\r\n| id | username | password | regtime | logtime | logip |\r\n+----+----------+----------+---------------------+---------------------+-------+\r\n| 12 | | test7 | 2018-02-25 15:25:14 | 0000-00-00 00:00:00 | NULL |\r\n+----+----------+----------+---------------------+---------------------+-------+\r\n1 row in set (0.00 sec)\r\n<\/pre>\n
\n\u800clogip\u9ed8\u8ba4\u503c\u4e3aNULL\uff0c\u4f46\u662f\u5141\u8bb8\u63d2\u5165NULL\u503c\uff0c\u6240\u4ee5\u8fd9\u91cc\u663e\u793a\u4e86NULL\u503c\u3002<\/p>\n\r\nMariaDB [mydb]> INSERT INTO user(password) VALUES('test88');\r\nERROR 1364 (HY000): Field 'username' doesn't have a default value\r\n<\/pre>\n
\r\nMariaDB [mydb]> ALTER TABLE user ADD CONSTRAINT uq_username UNIQUE(username); \/\/uq_username\u4e3a\u7ea6\u675f\u540d\u79f0\uff0cUNIQUE(\u53ef\u591a\u4e2a\u5b57\u6bb5\uff09\r\n\r\n\/\/\u5f53\u63d2\u5165\u7528\u6237\u540d\u76f8\u540c\u7684\u6570\u636e\u4e8b\u5219\u4f1a\u76f4\u63a5\u62a5\u9519\r\n\r\nMariaDB [mydb]> INSERT INTO user(username,password) VALUES('test4','test123');\r\nERROR 1062 (23000): Duplicate entry 'test4' for key 'uq_username'\r\n\r\n\/\/\u5220\u9664\u6b64\u7ea6\u675f\r\nMariaDB [mydb]> ALTER TABLE user DROP KEY uq_username;\r\n\r\n\/\/\u6dfb\u52a0\u4e24\u4e2a\u5b57\u6bb5\u7684\u7ea6\u675f\r\nMariaDB [mydb]> ALTER TABLE user ADD CONSTRAINT uq_user UNIQUE(username,password);\r\n\r\n\/\/\u6d4b\u8bd5\u6dfb\u52a0\u6570\u636e\r\nMariaDB [mydb]> SELECT * FROM user; \r\n+----+----------+----------+---------------------+---------------------+-------+\r\n| id | username | password | regtime | logtime | logip |\r\n+----+----------+----------+---------------------+---------------------+-------+\r\n| 7 | test2 | test3 | 2018-02-24 16:42:48 | 0000-00-00 00:00:00 | |\r\n| 8 | test3 | test3 | 2018-02-24 16:42:48 | 0000-00-00 00:00:00 | |\r\n| 9 | test4 | test5 | 2018-02-24 16:42:48 | 0000-00-00 00:00:00 | |\r\n+----+----------+----------+---------------------+---------------------+-------+\r\n3 rows in set (0.00 sec)\r\n\r\nMariaDB [mydb]> INSERT INTO user(username,password) VALUES('test4','test123');\r\nQuery OK, 1 row affected (0.01 sec)\r\n\r\n\/\/\u4ec5\u5f53\u4e24\u4e2a\u5b57\u6bb5\u7684\u6570\u636e\u90fd\u76f8\u540c\u65f6\u624d\u8fdd\u53cd\u552f\u4e00\u7ea6\u675f\r\nMariaDB [mydb]> INSERT INTO user(username,password) VALUES('test4','test5');\r\nERROR 1062 (23000): Duplicate entry 'test4-test5' for key 'uq_user'\r\n<\/pre>\n
\r\n\/\/\u56e0\u4e3a\u73b0\u5728\u7684\u8868\u4e2d\u5df2\u7ecf\u6709\u4e3b\u952e\u4e86\uff0c\u5148\u628a\u4e3b\u952e\u5220\u6389\r\nMariaDB [mydb]> ALTER TABLE user DROP PRIMARY KEY;\r\nERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key\r\n\r\n\/\/\u544a\u8bc9\u6211\u4eec\u4e00\u5f20\u8868\u91cc\u53ea\u5141\u8bb8\u6709\u4e00\u4e2a\u5b57\u6bb5\u4e3a\u81ea\u52a8\u589e\u957f\uff0c\u4e14\u8fd9\u4e2a\u5b57\u6bb5\u5fc5\u987b\u662f\u4e3b\u952e\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u8981\u5148\u53d6\u6d88\u5b83\u7684\u81ea\u52a8\u589e\u957f\u3002\r\n\r\nMariaDB [mydb]> ALTER TABLE user MODIFY COLUMN id int(11) NOT NULL;\r\nQuery OK, 4 rows affected (0.07 sec) \r\nRecords: 4 Duplicates: 0 Warnings: 0\r\n\r\nMariaDB [mydb]> DESC user;\r\n+----------+-------------+------+-----+---------------------+-------+\r\n| Field | Type | Null | Key | Default | Extra |\r\n+----------+-------------+------+-----+---------------------+-------+\r\n| id | int(11) | NO | PRI | NULL | |\r\n\r\n\r\n\/\/\u518d\u6b21\u5220\u9664\u4e3b\u952e\r\nMariaDB [mydb]> ALTER TABLE user DROP PRIMARY KEY;\r\nQuery OK, 4 rows affected (0.03 sec) \r\nRecords: 4 Duplicates: 0 Warnings: 0\r\n\r\n\/\/\u597d\u4e86\uff0c\u518d\u8ba9\u6211\u4eec\u628a\u4e3b\u952e\u52a0\u4e0a\u5427~~~ \u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u54e6~\r\nMariaDB [mydb]> ALTER TABLE user ADD CONSTRAINT PRIMARY KEY(id);\r\nMariaDB [mydb]> ALTER TABLE user MODIFY COLUMN id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT;\r\n<\/pre>\n
\n\u6ce8\u610f\uff1a\u5916\u952e\u7ea6\u675f\u7684\u53c2\u7167\u5217\uff0c\u5728\u4e3b\u8868\u4e2d\u5f15\u7528\u7684\u53ea\u80fd\u662f\u4e3b\u952e\u6216\u552f\u4e00\u952e\u7ea6\u675f\u7684\u5217\uff0c\u5047\u5b9a\u5f15\u7528\u7684\u4e3b\u8868\u5217\u4e0d\u662f\u552f\u4e00\u7684\u8bb0\u5f55\uff0c\u90a3\u4e48\u4ece\u8868\u5f15\u7528\u7684\u6570\u636e\u5c31\u4e0d\u786e\u5b9a\u8bb0\u5f55\u7684\u4f4d\u7f6e\uff0c\u540c\u4e00\u4e2a\u8868\u53ef\u4ee5\u6709\u591a\u4e2a\u5916\u952e\u7ea6\u675f\u3002<\/p>\n\r\n CREATE TABLE `usergroup` (\r\n `id` int(3) NOT NULL AUTO_INCREMENT,\r\n `name` varchar(10) NOT NULL,\r\n `comment` varchar(100) DEFAULT NULL,\r\n PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf16 |\r\n<\/pre>\n
\r\nMariaDB [mydb]> ALTER TABLE user ADD COLUMN groupid INT(3);\r\nQuery OK, 0 rows affected (0.05 sec)\r\nRecords: 0 Duplicates: 0 Warnings: 0\r\n<\/pre>\n
\r\nALTER TABLE user ADD CONSTRAINT fk_groupid FOREIGN KEY (groupid) REFERENCES usergroup(id);\r\n<\/pre>\n
\r\nMariaDB [mydb]> INSERT INTO user(username,password,groupid) VALUES('test99','test00',1); \r\nERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`mydb`.`user`, CONSTRAINT `fk_groupid` FOREIGN KEY (`groupid`) REFERENCES `usergroup` (`id`))\r\n<\/pre>\n
\r\nMariaDB [mydb]> INSERT INTO user(username,password) VALUES('test99','test00');\r\nQuery OK, 1 row affected (0.01 sec)\r\n<\/pre>\n
\r\nreference_definition:\r\n REFERENCES tbl_name (index_col_name,...)\r\n [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]\r\n [ON DELETE reference_option]\r\n [ON UPDATE reference_option]\r\nreference_option:\r\n RESTRICT | CASCADE | SET NULL | NO ACTION\r\n<\/pre>\n
\nON DELETE CASCADE\uff1a\u5f53\u5220\u9664\u7236\uff08\u53c2\u7167\uff09\u8868\u4e2d\u7684\u884c\u65f6\uff0c\u5982\u679c\u5b50\u8868\u4e2d\u6709\u4f9d\u8d56\u4e8e\u88ab\u5220\u9664\u7236\u884c\u7684\u5b50\u884c\u5b58\u5728\uff0c\u90a3\u4e48\u8fde\u540c\u5b50\u884c\u4e00\u8d77\u5220\u9664\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002
\nON DELETE SET NULL\uff1a\u5f53\u5220\u9664\u7236\uff08\u53c2\u7167\uff09\u8868\u4e2d\u7684\u884c\u65f6\uff0c\u5982\u679c\u5b50\u8868\u4e2d\u6709\u4f9d\u8d56\u4e8e\u88ab\u5220\u9664\u7236\u884c\u7684\u5b50\u884c\u5b58\u5728\uff0c\u90a3\u4e48\u4e0d\u5220\u9664\uff0c\u800c\u662f\u5c06\u5b50\u884c\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3aNULL<\/p>\n\r\nCONSTRAINT non_empty_name CHECK (CHAR_LENGTH(name) > 0)\r\nCONSTRAINT consistent_dates CHECK (birth_date IS NULL OR death_date IS NULL OR birth_date < death_date)\r\nCONSTRAINT past_date CHECK (birth_date < NOW())<\/pre>\n
\r\nALTER TABLE user ADD CONSTRAINT non_empty_name CHECK(CHAR_LENGTH(username)>0);\r\nINSERT INTO user(id,username) VALUES(1,'');\r\n\/* SQL\u9519\u8bef\uff084025\uff09\uff1aCONSTRAINT `non_empty_name` failed for `test`.`user` *\/\r\n<\/pre>\n
\n