{"id":160815,"date":"2024-11-04T01:14:29","date_gmt":"2024-11-03T17:14:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160815\/"},"modified":"2024-11-04T01:14:30","modified_gmt":"2024-11-03T17:14:30","slug":"%e8%b3%87%e6%96%99%e5%ba%ab%e6%95%99%e7%a8%8b%ef%bc%9amysql-%e7%b4%84%e6%9d%9f%e6%a6%82%e8%bf%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160815\/","title":{"rendered":"\u8cc7\u6599\u5eab\u6559\u7a0b\uff1aMySQL \u7d04\u675f\u6982\u8ff0"},"content":{"rendered":"<h1 id=\"%e8%b3%87%e6%96%99%e5%ba%ab%e6%95%99%e7%a8%8b%ef%bc%9amysql-%e7%b4%84%e6%9d%9f%e6%a6%82%e8%bf%b0-MWoUeUQybi\">\u8cc7\u6599\u5eab\u6559\u7a0b\uff1aMySQL \u7d04\u675f\u6982\u8ff0<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u7d04\u675f\uff08Constraints\uff09\u662f\u7528\u4f86\u9650\u5236\u8cc7\u6599\u8868\u4e2d\u8cc7\u6599\u7684\u6709\u6548\u6027\u548c\u5b8c\u6574\u6027\u7684\u91cd\u8981\u5de5\u5177\u3002MySQL \u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u7d04\u675f\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u8cc7\u6599\u7684\u6b63\u78ba\u6027\u548c\u4e00\u81f4\u6027\u3002\u672c\u6587\u5c07\u5c0d MySQL \u4e2d\u7684\u4e3b\u8981\u7d04\u675f\u9032\u884c\u6982\u8ff0\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728\u8cc7\u6599\u5eab\u8a2d\u8a08\u4e2d\u61c9\u7528\u9019\u4e9b\u7d04\u675f\u3002<\/p>\n<h2 id=\"%e7%b4%84%e6%9d%9f%e7%9a%84%e9%a1%9e%e5%9e%8b-MWoUeUQybi\">\u7d04\u675f\u7684\u985e\u578b<\/h2>\n<p>MySQL \u652f\u63f4\u591a\u7a2e\u7d04\u675f\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u5e7e\u7a2e\uff1a<\/p>\n<ul>\n<li><strong>\u4e3b\u9375\u7d04\u675f\uff08PRIMARY KEY\uff09<\/strong><\/li>\n<li><strong>\u5916\u9375\u7d04\u675f\uff08FOREIGN KEY\uff09<\/strong><\/li>\n<li><strong>\u552f\u4e00\u7d04\u675f\uff08UNIQUE\uff09<\/strong><\/li>\n<li><strong>\u6aa2\u67e5\u7d04\u675f\uff08CHECK\uff09<\/strong><\/li>\n<li><strong>\u975e\u7a7a\u7d04\u675f\uff08NOT NULL\uff09<\/strong><\/li>\n<\/ul>\n<h3 id=\"1-%e4%b8%bb%e9%8d%b5%e7%b4%84%e6%9d%9f%ef%bc%88primary-key%ef%bc%89-MWoUeUQybi\">1. \u4e3b\u9375\u7d04\u675f\uff08PRIMARY KEY\uff09<\/h3>\n<p>\u4e3b\u9375\u7d04\u675f\u7528\u65bc\u552f\u4e00\u6a19\u8b58\u8cc7\u6599\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u3002\u6bcf\u500b\u8cc7\u6599\u8868\u53ea\u80fd\u6709\u4e00\u500b\u4e3b\u9375\uff0c\u4e14\u4e3b\u9375\u7684\u503c\u4e0d\u80fd\u70ba\u7a7a\u3002\u4e3b\u9375\u901a\u5e38\u7531\u4e00\u500b\u6216\u591a\u500b\u6b04\u4f4d\u7d44\u6210\uff0c\u9019\u4e9b\u6b04\u4f4d\u7684\u7d44\u5408\u5fc5\u9808\u662f\u552f\u4e00\u7684\u3002<\/p>\n<pre><code>CREATE TABLE users (\n    id INT AUTO_INCREMENT,\n    username VARCHAR(50) NOT NULL,\n    email VARCHAR(100) NOT NULL,\n    PRIMARY KEY (id)\n);<\/code><\/pre>\n<h3 id=\"2-%e5%a4%96%e9%8d%b5%e7%b4%84%e6%9d%9f%ef%bc%88foreign-key%ef%bc%89-MWoUeUQybi\">2. \u5916\u9375\u7d04\u675f\uff08FOREIGN KEY\uff09<\/h3>\n<p>\u5916\u9375\u7d04\u675f\u7528\u65bc\u5efa\u7acb\u5169\u500b\u8cc7\u6599\u8868\u4e4b\u9593\u7684\u95dc\u806f\u3002\u5b83\u78ba\u4fdd\u5728\u4e00\u500b\u8cc7\u6599\u8868\u4e2d\u7684\u67d0\u500b\u6b04\u4f4d\u7684\u503c\u5fc5\u9808\u5b58\u5728\u65bc\u53e6\u4e00\u500b\u8cc7\u6599\u8868\u7684\u4e3b\u9375\u6b04\u4f4d\u4e2d\u3002\u9019\u6709\u52a9\u65bc\u7dad\u8b77\u8cc7\u6599\u7684\u53c3\u7167\u5b8c\u6574\u6027\u3002<\/p>\n<pre><code>CREATE TABLE orders (\n    order_id INT AUTO_INCREMENT,\n    user_id INT,\n    order_date DATETIME,\n    PRIMARY KEY (order_id),\n    FOREIGN KEY (user_id) REFERENCES users(id)\n);<\/code><\/pre>\n<h3 id=\"3-%e5%94%af%e4%b8%80%e7%b4%84%e6%9d%9f%ef%bc%88unique%ef%bc%89-MWoUeUQybi\">3. \u552f\u4e00\u7d04\u675f\uff08UNIQUE\uff09<\/h3>\n<p>\u552f\u4e00\u7d04\u675f\u78ba\u4fdd\u8cc7\u6599\u8868\u4e2d\u7684\u67d0\u500b\u6b04\u4f4d\u6216\u6b04\u4f4d\u7d44\u5408\u7684\u503c\u662f\u552f\u4e00\u7684\uff0c\u9019\u610f\u5473\u8457\u4e0d\u53ef\u4ee5\u6709\u91cd\u8907\u7684\u503c\u3002\u8207\u4e3b\u9375\u4e0d\u540c\u7684\u662f\uff0c\u552f\u4e00\u7d04\u675f\u7684\u6b04\u4f4d\u53ef\u4ee5\u5305\u542b\u7a7a\u503c\u3002<\/p>\n<pre><code>CREATE TABLE products (\n    product_id INT AUTO_INCREMENT,\n    product_name VARCHAR(100) NOT NULL,\n    sku VARCHAR(50) UNIQUE,\n    PRIMARY KEY (product_id)\n);<\/code><\/pre>\n<h3 id=\"4-%e6%aa%a2%e6%9f%a5%e7%b4%84%e6%9d%9f%ef%bc%88check%ef%bc%89-MWoUeUQybi\">4. \u6aa2\u67e5\u7d04\u675f\uff08CHECK\uff09<\/h3>\n<p>\u6aa2\u67e5\u7d04\u675f\u7528\u65bc\u9650\u5236\u6b04\u4f4d\u7684\u503c\u5fc5\u9808\u6eff\u8db3\u7279\u5b9a\u689d\u4ef6\u3002\u9019\u5728 MySQL 8.0 \u7248\u672c\u4e2d\u5f97\u5230\u4e86\u652f\u6301\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u78ba\u4fdd\u8cc7\u6599\u7684\u6709\u6548\u6027\u3002<\/p>\n<pre><code>CREATE TABLE employees (\n    employee_id INT AUTO_INCREMENT,\n    age INT,\n    CHECK (age &gt;= 18),\n    PRIMARY KEY (employee_id)\n);<\/code><\/pre>\n<h3 id=\"5-%e9%9d%9e%e7%a9%ba%e7%b4%84%e6%9d%9f%ef%bc%88not-null%ef%bc%89-MWoUeUQybi\">5. \u975e\u7a7a\u7d04\u675f\uff08NOT NULL\uff09<\/h3>\n<p>\u975e\u7a7a\u7d04\u675f\u78ba\u4fdd\u67d0\u500b\u6b04\u4f4d\u4e0d\u80fd\u5305\u542b\u7a7a\u503c\u3002\u9019\u5c0d\u65bc\u90a3\u4e9b\u5fc5\u9808\u6709\u503c\u7684\u6b04\u4f4d\u975e\u5e38\u91cd\u8981\uff0c\u4f8b\u5982\u7528\u6236\u540d\u6216\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u3002<\/p>\n<pre><code>CREATE TABLE customers (\n    customer_id INT AUTO_INCREMENT,\n    name VARCHAR(100) NOT NULL,\n    email VARCHAR(100) NOT NULL,\n    PRIMARY KEY (customer_id)\n);<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-MWoUeUQybi\">\u7d50\u8ad6<\/h2>\n<p>\u5728 MySQL \u4e2d\u4f7f\u7528\u7d04\u675f\u662f\u8a2d\u8a08\u8cc7\u6599\u5eab\u6642\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u900f\u904e\u9069\u7576\u7684\u7d04\u675f\uff0c\u53ef\u4ee5\u78ba\u4fdd\u8cc7\u6599\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\uff0c\u5f9e\u800c\u63d0\u9ad8\u8cc7\u6599\u5eab\u7684\u53ef\u9760\u6027\u3002\u4e86\u89e3\u5404\u7a2e\u7d04\u675f\u7684\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u5c07\u6709\u52a9\u65bc\u958b\u767c\u8005\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u66f4\u6709\u6548\u5730\u7ba1\u7406\u8cc7\u6599\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 MySQL \u8cc7\u6599\u5eab\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u4e2d\u7684\u7d04\u675f\u985e\u578b\u53ca\u5176\u61c9\u7528\uff0c\u63d0\u5347\u8cc7\u6599\u5eab\u8a2d\u8a08\u7684\u5b8c\u6574\u6027\u8207\u5b89\u5168\u6027\uff0c\u638c\u63e1\u8cc7\u6599\u5eab\u6559\u7a0b\u7684\u57fa\u790e\u77e5\u8b58\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-160815","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=160815"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160815\/revisions"}],"predecessor-version":[{"id":160816,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160815\/revisions\/160816"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}