{"id":3475,"date":"2026-09-26T11:46:58","date_gmt":"2026-09-26T02:46:58","guid":{"rendered":"https:\/\/qol-kk.com\/wp2\/?p=3475"},"modified":"2026-09-26T11:46:58","modified_gmt":"2026-09-26T02:46:58","slug":"20260926-plan041","status":"publish","type":"post","link":"https:\/\/qol-kk.com\/wp2\/blog\/2026\/09\/26\/20260926-plan041\/","title":{"rendered":"\u5916\u90e8\u30ad\u30fc\u3068\u53c2\u7167\u6574\u5408\u6027\u306e\u8a2d\u8a08\uff1a\u5165\u9580\u304b\u3089\u5b9f\u52d9\u5224\u65ad\u307e\u3067"},"content":{"rendered":"<p>\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u306f\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u300c\u53c2\u7167\u300d\u3067\u7d50\u3073\u3064\u3044\u3066\u3044\u307e\u3059\u3002\u6ce8\u6587\u30c6\u30fc\u30d6\u30eb\u306e <code>user_id<\/code> \u306f\u30e6\u30fc\u30b6\u30fc\u30c6\u30fc\u30d6\u30eb\u306e\u8ab0\u304b\u3092\u6307\u3057\u3001\u6ce8\u6587\u660e\u7d30\u306e <code>order_id<\/code> \u306f\u3069\u3053\u304b\u306e\u6ce8\u6587\u3092\u6307\u3057\u307e\u3059\u3002\u3053\u306e\u300c\u6307\u3059\u30fb\u6307\u3055\u308c\u308b\u300d\u306e\u95a2\u4fc2\u304c\u58ca\u308c\u308b\u3068\u3001\u3069\u306e\u30e6\u30fc\u30b6\u30fc\u306e\u3082\u306e\u304b\u5206\u304b\u3089\u306a\u3044\u6ce8\u6587\u3084\u3001\u5b58\u5728\u3057\u306a\u3044\u6ce8\u6587\u306b\u3076\u3089\u4e0b\u304c\u3063\u305f\u660e\u7d30\u304c\u751f\u307e\u308c\u307e\u3059\u3002\u3053\u308c\u3092\u9632\u3050\u4ed5\u7d44\u307f\u304c<strong>\u5916\u90e8\u30ad\u30fc\uff08Foreign Key\uff09<\/strong>\u3067\u3042\u308a\u3001\u305d\u306e\u80cc\u5f8c\u306b\u3042\u308b\u8003\u3048\u65b9\u304c<strong>\u53c2\u7167\u6574\u5408\u6027\uff08Referential Integrity\uff09<\/strong>\u3067\u3059\u3002<\/p>\n<p>\u672c\u8a18\u4e8b\u306f\u5165\u9580\u8005\u5411\u3051\u306b\u3001\u5916\u90e8\u30ad\u30fc\u306e\u57fa\u672c\u3001<code>ON DELETE<\/code> \/ <code>ON UPDATE<\/code> \u306e\u6319\u52d5\u3001\u8cbc\u308b\u3079\u304d\u304b\u5916\u3059\u3079\u304d\u304b\u306e\u5224\u65ad\u3001\u5b9f\u52d9\u3067\u30cf\u30de\u308a\u3084\u3059\u3044\u843d\u3068\u3057\u7a74\u307e\u3067\u3092\u6574\u7406\u3057\u307e\u3059\u3002SQL \u306f PostgreSQL \/ MySQL \u3092\u60f3\u5b9a\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u8003\u3048\u65b9\u306f\u3069\u306e RDBMS \u3067\u3082\u5171\u901a\u3067\u3059\u3002<\/p>\n<h2>\u53c2\u7167\u6574\u5408\u6027\u3068\u306f\u4f55\u304b<\/h2>\n<p>\u53c2\u7167\u6574\u5408\u6027\u3068\u306f\u3001\u3072\u3068\u3053\u3068\u3067\u8a00\u3048\u3070\u300c<strong>\u53c2\u7167\u5148\u304c\u5fc5\u305a\u5b58\u5728\u3059\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b<\/strong>\u300d\u5236\u7d04\u3067\u3059\u3002<\/p>\n<ul>\n<li><code>orders.user_id<\/code> \u306b <code>42<\/code> \u304c\u5165\u3063\u3066\u3044\u308b\u306a\u3089\u3001<code>users<\/code> \u306b <code>id = 42<\/code> \u306e\u884c\u304c\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044<\/li>\n<li>\u5b58\u5728\u3057\u306a\u3044\u5024\u3092\u6307\u3059\u72b6\u614b\uff08\uff1d<strong>\u5b64\u5150\u30ec\u30b3\u30fc\u30c9 \/ orphan<\/strong>\uff09\u3092\u4f5c\u3089\u305b\u306a\u3044<\/li>\n<\/ul>\n<p>\u3053\u308c\u3092\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30b3\u30fc\u30c9\u3060\u3051\u3067\u5b88\u308d\u3046\u3068\u3059\u308b\u3068\u3001\u5fc5\u305a\u629c\u3051\u304c\u51fa\u307e\u3059\u3002\u30d0\u30b0\u3001\u4e26\u884c\u51e6\u7406\u3001\u76f4\u63a5SQL\u3067\u306e\u30c7\u30fc\u30bf\u4fee\u6b63\u3001\u30d0\u30c3\u30c1\u51e6\u7406\u2014\u2014\u3069\u3053\u304b\u4e00\u7b87\u6240\u3067\u30c1\u30a7\u30c3\u30af\u3092\u5fd8\u308c\u308c\u3070\u6574\u5408\u6027\u306f\u5d29\u308c\u307e\u3059\u3002\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306f\u3001\u3053\u306e\u4fdd\u8a3c\u3092<strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5c64\u3067\u5f37\u5236<\/strong>\u3059\u308b\u305f\u3081\u306e\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<h2>\u57fa\u672c\u69cb\u6587<\/h2>\n<h3>\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u6642\u306b\u5b9a\u7fa9\u3059\u308b<\/h3>\n<pre class=\"lang:default decode:true\">CREATE TABLE users (\r\n    id    BIGINT PRIMARY KEY,\r\n    name  VARCHAR(100) NOT NULL\r\n);\r\n\r\nCREATE TABLE orders (\r\n    id       BIGINT PRIMARY KEY,\r\n    user_id  BIGINT NOT NULL,\r\n    total    INT NOT NULL,\r\n    CONSTRAINT fk_orders_user\r\n        FOREIGN KEY (user_id) REFERENCES users (id)\r\n);<\/pre>\n<p><code>CONSTRAINT fk_orders_user<\/code> \u306e\u3088\u3046\u306b<strong>\u5236\u7d04\u306b\u540d\u524d\u3092\u4ed8\u3051\u308b<\/strong>\u306e\u304c\u304a\u3059\u3059\u3081\u3067\u3059\u3002\u540d\u524d\u3092\u4ed8\u3051\u306a\u3044\u3068DB\u304c\u81ea\u52d5\u63a1\u756a\u3057\u3001\u5f8c\u3067\u5236\u7d04\u3092\u524a\u9664\u30fb\u5909\u66f4\u3059\u308b\u3068\u304d\u306b\u540d\u524d\u3092\u8abf\u3079\u308b\u624b\u9593\u304c\u5897\u3048\u307e\u3059\u3002<\/p>\n<h3>\u65e2\u5b58\u30c6\u30fc\u30d6\u30eb\u306b\u5f8c\u304b\u3089\u8ffd\u52a0\u3059\u308b<\/h3>\n<pre class=\"lang:default decode:true\">ALTER TABLE orders\r\n    ADD CONSTRAINT fk_orders_user\r\n    FOREIGN KEY (user_id) REFERENCES users (id);<\/pre>\n<p>\u3053\u306e <code>ALTER TABLE<\/code> \u306f\u3001\u65e2\u5b58\u30c7\u30fc\u30bf\u306b\u5b64\u5150\u30ec\u30b3\u30fc\u30c9\u304c1\u4ef6\u3067\u3082\u3042\u308b\u3068<strong>\u5931\u6557\u3057\u307e\u3059<\/strong>\u3002\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u3089\u3001\u307e\u305a\u5b64\u5150\u3092\u63a2\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">SELECT o.id, o.user_id\r\nFROM orders o\r\nLEFT JOIN users u ON u.id = o.user_id\r\nWHERE u.id IS NULL;<\/pre>\n<h2>ON DELETE \/ ON UPDATE\uff1a\u89aa\u304c\u6d88\u3048\u305f\u3089\u5b50\u3092\u3069\u3046\u3059\u308b\u304b<\/h2>\n<p>\u5916\u90e8\u30ad\u30fc\u8a2d\u8a08\u306e\u809d\u306f\u3001\u300c<strong>\u89aa\u304c\u6d88\u3048\u305f\u3068\u304d\u3001\u5b50\u3092\u3069\u3046\u3059\u308b\u304b<\/strong>\u300d\u306e\u6307\u5b9a\u3067\u3059\u3002\u3053\u3053\u3092\u66d6\u6627\u306b\u3057\u305f\u307e\u307e\u8cbc\u308b\u3068\u3001\u5f8c\u3067\u300c\u6d88\u305b\u306a\u3044\u30ec\u30b3\u30fc\u30c9\u300d\u3084\u300c\u610f\u56f3\u305b\u305a\u9023\u9396\u524a\u9664\u3055\u308c\u305f\u4e8b\u6545\u300d\u306b\u60a9\u307e\u3055\u308c\u307e\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u30aa\u30d7\u30b7\u30e7\u30f3<\/th>\n<th>\u6319\u52d5<\/th>\n<th>\u4f7f\u3044\u3069\u3053\u308d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>RESTRICT<\/code> \/ <code>NO ACTION<\/code>\uff08\u30c7\u30d5\u30a9\u30eb\u30c8\uff09<\/td>\n<td>\u5b50\u304c\u6b8b\u3063\u3066\u3044\u308b\u3068\u89aa\u3092\u524a\u9664\u30fb\u66f4\u65b0\u3067\u304d\u306a\u3044<\/td>\n<td>\u8aa4\u524a\u9664\u3092\u9632\u304e\u305f\u3044\u3002\u57fa\u672c\u306f\u3053\u308c<\/td>\n<\/tr>\n<tr>\n<td><code>CASCADE<\/code><\/td>\n<td>\u89aa\u3092\u6d88\u3059\u3068\u5b50\u3082\u81ea\u52d5\u3067\u6d88\u3048\u308b\uff0f\u66f4\u65b0\u3082\u4f1d\u64ad<\/td>\n<td>\u89aa\u5b50\u304c\u4e00\u4f53\u3067\u3001\u5b50\u5358\u72ec\u3067\u306f\u610f\u5473\u3092\u6301\u305f\u306a\u3044\u3068\u304d<\/td>\n<\/tr>\n<tr>\n<td><code>SET NULL<\/code><\/td>\n<td>\u5b50\u306e\u5916\u90e8\u30ad\u30fc\u5217\u3092 NULL \u306b\u3059\u308b<\/td>\n<td>\u53c2\u7167\u304c\u4efb\u610f\u3067\u3001\u5916\u308c\u3066\u3082\u5b50\u306f\u6b8b\u3063\u3066\u3088\u3044\u3068\u304d\uff08\u5217\u304c NULL \u8a31\u5bb9\u3067\u3042\u308b\u5fc5\u8981\u3042\u308a\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>SET DEFAULT<\/code><\/td>\n<td>\u5b50\u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306b\u3059\u308b<\/td>\n<td>\u7528\u9014\u306f\u9650\u5b9a\u7684<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"lang:default decode:true\">-- \u6ce8\u6587\u304c\u6d88\u3048\u305f\u3089\u660e\u7d30\u3082\u6d88\u3059\uff08\u660e\u7d30\u306f\u6ce8\u6587\u306a\u3057\u3067\u306f\u5b58\u5728\u3057\u306a\u3044\uff09\r\nCREATE TABLE order_items (\r\n    id        BIGINT PRIMARY KEY,\r\n    order_id  BIGINT NOT NULL,\r\n    product   VARCHAR(100) NOT NULL,\r\n    CONSTRAINT fk_items_order\r\n        FOREIGN KEY (order_id) REFERENCES orders (id)\r\n        ON DELETE CASCADE\r\n);\r\n\r\n-- \u30e6\u30fc\u30b6\u30fc\u304c\u6d88\u3048\u3066\u3082\u6295\u7a3f\u306f\u6b8b\u3057\u3001\u6295\u7a3f\u8005\u3060\u3051\u300c\u4e0d\u660e\u300d\u306b\u3059\u308b\r\nCREATE TABLE posts (\r\n    id       BIGINT PRIMARY KEY,\r\n    user_id  BIGINT,               -- NULL \u8a31\u5bb9\u306b\u3057\u3066\u304a\u304f\r\n    body     TEXT NOT NULL,\r\n    CONSTRAINT fk_posts_user\r\n        FOREIGN KEY (user_id) REFERENCES users (id)\r\n        ON DELETE SET NULL\r\n);<\/pre>\n<h3>CASCADE \u306f\u4fbf\u5229\u3060\u304c\u614e\u91cd\u306b<\/h3>\n<p><code>ON DELETE CASCADE<\/code> \u306f\u9023\u9396\u524a\u9664\u3067\u3059\u3002\u89aa\u30921\u884c\u6d88\u3057\u305f\u3064\u3082\u308a\u304c\u3001\u5b50\u30fb\u5b6b\u30fb\u3072\u5b6b\u307e\u3067\u5927\u91cf\u306b\u6d88\u3048\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u7279\u306b\u591a\u6bb5\u306eCASCADE\u304c\u7e4b\u304c\u3063\u3066\u3044\u308b\u3068\u3001\u524a\u9664\u306e\u5f71\u97ff\u7bc4\u56f2\u304c\u76f4\u611f\u3088\u308a\u9065\u304b\u306b\u5e83\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u5b89\u5168\u5074\u306b\u5012\u3059\u306a\u3089\u3001<strong>\u30c7\u30d5\u30a9\u30eb\u30c8\u306f <code>RESTRICT<\/code>\uff08NO ACTION\uff09\u306b\u3057\u3066\u304a\u304d\u3001CASCADE \u306f\u300c\u5b50\u304c\u89aa\u3068\u4e00\u5fc3\u540c\u4f53\u3067\u3042\u308b\u300d\u3068\u78ba\u4fe1\u3067\u304d\u308b\u95a2\u4fc2\u306b\u3060\u3051\u4ed8\u3051\u308b<\/strong>\u306e\u304c\u5b9f\u52d9\u7684\u306a\u6307\u91dd\u3067\u3059\u3002<\/p>\n<h2>\u8ad6\u7406\u524a\u9664\u3068\u306e\u95a2\u4fc2<\/h2>\n<p>\u5b9f\u52d9\u3067\u306f\u7269\u7406\u524a\u9664\u3067\u306f\u306a\u304f <code>deleted_at<\/code>\uff08\u8ad6\u7406\u524a\u9664 \/ \u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\uff09\u3092\u4f7f\u3046\u8a2d\u8a08\u3082\u591a\u304f\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u884c\u306fDB\u4e0a\u306b\u6b8b\u308a\u7d9a\u3051\u308b\u305f\u3081\u3001<strong>\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306f\u300c\u8ad6\u7406\u524a\u9664\u3055\u308c\u305f\u89aa\u300d\u3092\u691c\u77e5\u3057\u307e\u305b\u3093<\/strong>\u3002DB\u304b\u3089\u898b\u308c\u3070\u89aa\u306f\u307e\u3060\u5b58\u5728\u3057\u3066\u3044\u308b\u304b\u3089\u3067\u3059\u3002\u300c\u524a\u9664\u6e08\u307f\u306e\u89aa\u306b\u65b0\u3057\u3044\u5b50\u3092\u7d10\u4ed8\u3051\u306a\u3044\u300d\u30eb\u30fc\u30eb\u306f\u30a2\u30d7\u30ea\u5074\u3067\u62c5\u4fdd\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5916\u90e8\u30ad\u30fc\u306f\u300c\u884c\u304c\u7269\u7406\u7684\u306b\u5b58\u5728\u3059\u308b\u304b\u300d\u3057\u304b\u898b\u306a\u3044\u3001\u3068\u7406\u89e3\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u30ed\u30c3\u30af\u306e\u843d\u3068\u3057\u7a74<\/h2>\n<h3>\u5916\u90e8\u30ad\u30fc\u5217\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092<\/h3>\n<p>\u591a\u304f\u306e\u4eba\u304c\u898b\u843d\u3068\u3057\u307e\u3059\u304c\u3001<strong>\u5916\u90e8\u30ad\u30fc\u3092\u8cbc\u3063\u305f\u304b\u3089\u3068\u3044\u3063\u3066\u3001\u5b50\u5074\u306e\u5217\u306b\u81ea\u52d5\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u5f35\u3089\u308c\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093<\/strong>\u3002<\/p>\n<ul>\n<li>PostgreSQL\uff1a\u5916\u90e8\u30ad\u30fc\u3092\u4f5c\u3063\u3066\u3082\u3001\u5b50\u5074\uff08\u53c2\u7167\u3059\u308b\u5074\uff09\u306b\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u81ea\u52d5\u751f\u6210\u3055\u308c\u306a\u3044<\/li>\n<li>MySQL\uff08InnoDB\uff09\uff1a\u5916\u90e8\u30ad\u30fc\u4f5c\u6210\u6642\u306b\u3001\u306a\u3051\u308c\u3070\u5b50\u5074\u306b\u81ea\u52d5\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f5c\u308b<\/li>\n<\/ul>\n<p>\u53c2\u7167\u5148\u306e\u89aa\u3092\u524a\u9664\u30fb\u66f4\u65b0\u3059\u308b\u969b\u3001DB\u306f\u300c\u305d\u306e\u89aa\u3092\u6307\u3059\u5b50\u304c\u3044\u306a\u3044\u304b\u300d\u3092\u5b50\u30c6\u30fc\u30d6\u30eb\u3067\u63a2\u3057\u307e\u3059\u3002\u5b50\u5074\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u7121\u3044\u3068\u3053\u306e\u63a2\u7d22\u304c\u30d5\u30eb\u30b9\u30ad\u30e3\u30f3\u306b\u306a\u308a\u3001\u89aa\u306e\u524a\u9664\u304c\u975e\u5e38\u306b\u9045\u304f\u306a\u308a\u307e\u3059\u3002<strong>PostgreSQL \u3067\u306f\u5916\u90e8\u30ad\u30fc\u5217\u306b\u624b\u52d5\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b<\/strong>\u306e\u304c\u5b9a\u77f3\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">CREATE INDEX idx_orders_user_id ON orders (user_id);<\/pre>\n<h3>\u578b\u306f\u5b8c\u5168\u306b\u4e00\u81f4\u3055\u305b\u308b<\/h3>\n<p>\u5916\u90e8\u30ad\u30fc\u306e\u5217\u3068\u53c2\u7167\u5148\u306e\u5217\u306f\u3001<strong>\u30c7\u30fc\u30bf\u578b\u3092\u53b3\u5bc6\u306b\u63c3\u3048\u307e\u3059<\/strong>\u3002<code>BIGINT<\/code> \u3068 <code>INT<\/code>\u3001<code>VARCHAR(50)<\/code> \u3068 <code>VARCHAR(100)<\/code>\u3001\u6587\u5b57\u30b3\u30fc\u30c9\u3084\u7167\u5408\u9806\u5e8f\uff08collation\uff09\u306e\u9055\u3044\u3067\u3082\u5236\u7d04\u4f5c\u6210\u306b\u5931\u6557\u3057\u305f\u308a\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u52b9\u304b\u306a\u304f\u306a\u3063\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<h2>\u5b9f\u88c5\u4f8b\uff1a\u6bb5\u968e\u7684\u306b\u5916\u90e8\u30ad\u30fc\u3092\u5c0e\u5165\u3059\u308b<\/h2>\n<p>\u904b\u7528\u4e2d\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5916\u90e8\u30ad\u30fc\u3092\u5f8c\u4ed8\u3051\u3059\u308b\u3001\u73fe\u5b9f\u7684\u306a\u624b\u9806\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">-- 1. \u307e\u305a\u5b64\u5150\u30ec\u30b3\u30fc\u30c9\u3092\u8abf\u67fb\r\nSELECT COUNT(*) AS orphan_count\r\nFROM orders o\r\nLEFT JOIN users u ON u.id = o.user_id\r\nWHERE u.id IS NULL;\r\n\r\n-- 2. \u5b64\u5150\u3092\u3069\u3046\u6271\u3046\u304b\u6c7a\u3081\u3066\u4fee\u6b63\uff08\u4f8b\uff1a\u53c2\u7167\u5148\u4e0d\u660e\u306e user_id \u3092 NULL \u306b\u5bc4\u305b\u308b\uff09\r\nUPDATE orders o\r\nSET user_id = NULL\r\nWHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.id = o.user_id);\r\n\r\n-- 3. \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5148\u306b\u7528\u610f\r\nCREATE INDEX idx_orders_user_id ON orders (user_id);\r\n\r\n-- 4. \u5916\u90e8\u30ad\u30fc\u5236\u7d04\u3092\u8ffd\u52a0\r\nALTER TABLE orders\r\n    ADD CONSTRAINT fk_orders_user\r\n    FOREIGN KEY (user_id) REFERENCES users (id)\r\n    ON DELETE RESTRICT;<\/pre>\n<p>PostgreSQL \u3067\u306f\u3001\u691c\u8a3c\u306e\u30ed\u30c3\u30af\u3092\u8efd\u304f\u3059\u308b\u305f\u3081\u306b <code>NOT VALID<\/code> \u2192 <code>VALIDATE CONSTRAINT<\/code> \u306e\u4e8c\u6bb5\u968e\u3082\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">ALTER TABLE orders\r\n    ADD CONSTRAINT fk_orders_user\r\n    FOREIGN KEY (user_id) REFERENCES users (id) NOT VALID;\r\n\r\nALTER TABLE orders VALIDATE CONSTRAINT fk_orders_user;<\/pre>\n<h2>\u78ba\u8a8d\u65b9\u6cd5<\/h2>\n<p>\u5236\u7d04\u304c\u610f\u56f3\u901a\u308a\u52b9\u3044\u3066\u3044\u308b\u304b\u306f\u3001\u5b9f\u969b\u306b\u300c\u9055\u53cd\u3059\u308b\u64cd\u4f5c\u300d\u3092\u8a66\u3057\u3066<strong>\u5f3e\u304b\u308c\u308b\u3053\u3068<\/strong>\u3067\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">-- \u5b58\u5728\u3057\u306a\u3044 user_id \u3092\u633f\u5165 \u2192 \u30a8\u30e9\u30fc\u306b\u306a\u308c\u3070\u5236\u7d04\u304c\u52b9\u3044\u3066\u3044\u308b\r\nINSERT INTO orders (id, user_id, total) VALUES (1, 999999, 1000);\r\n\r\n-- \u5b50\u304c\u6b8b\u3063\u3066\u3044\u308b\u89aa\u3092\u524a\u9664 \u2192 RESTRICT \u306a\u3089\u5f3e\u304b\u308c\u308b\r\nDELETE FROM users WHERE id = 42;<\/pre>\n<p>\u5236\u7d04\u306e\u4e00\u89a7\u306f\u3001PostgreSQL \u306a\u3089 <code>\\d orders<\/code>\u3001MySQL \u306a\u3089 <code>SHOW CREATE TABLE orders;<\/code> \u3067\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>\u5916\u90e8\u30ad\u30fc\u306f\u8cbc\u308b\u3079\u304d\u304b<\/h2>\n<p>\u4e00\u90e8\u306e\u5927\u898f\u6a21\u30b5\u30fc\u30d3\u30b9\u3067\u306f\u3001\u66f8\u304d\u8fbc\u307f\u6027\u80fd\u3084\u30b7\u30e3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u90fd\u5408\u3067\u5916\u90e8\u30ad\u30fc\u3092\u3042\u3048\u3066\u4f7f\u308f\u305a\u3001\u6574\u5408\u6027\u3092\u30a2\u30d7\u30ea\u5c64\u3067\u62c5\u4fdd\u3059\u308b\u8a2d\u8a08\u3082\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3053\u308c\u306f\u300c\u5236\u7d04\u306e\u30b3\u30b9\u30c8\u3068\u4fdd\u8a3c\u3092\u7406\u89e3\u3057\u305f\u4e0a\u3067\u306e\u610f\u56f3\u7684\u306a\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u300d\u3067\u3042\u3063\u3066\u3001\u5165\u9580\u8005\u306e\u521d\u671f\u8a2d\u8a08\u3068\u3057\u3066\u306f<strong>\u307e\u305a\u5916\u90e8\u30ad\u30fc\u3092\u8cbc\u308b<\/strong>\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002\u4fdd\u8a3c\u304c\u30bf\u30c0\u3067\u624b\u306b\u5165\u308b\u5834\u9762\u3067\u3001\u308f\u3056\u308f\u3056\u6368\u3066\u308b\u7406\u7531\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li>\u5916\u90e8\u30ad\u30fc\u306f\u300c\u53c2\u7167\u5148\u304c\u5fc5\u305a\u5b58\u5728\u3059\u308b\u300d\u3068\u3044\u3046\u53c2\u7167\u6574\u5408\u6027\u3092\u3001<strong>DB\u5c64\u3067\u5f37\u5236<\/strong>\u3059\u308b\u4ed5\u7d44\u307f<\/li>\n<li><code>ON DELETE<\/code> \u306f\u57fa\u672c <code>RESTRICT<\/code>\u3001\u4e00\u5fc3\u540c\u4f53\u306e\u89aa\u5b50\u3060\u3051 <code>CASCADE<\/code>\u3001\u5916\u308c\u3066\u3088\u3044\u53c2\u7167\u306f <code>SET NULL<\/code><\/li>\n<li>\u5b50\u5074\u306e\u5916\u90e8\u30ad\u30fc\u5217\u306b\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\uff08\u7279\u306b PostgreSQL \u306f\u624b\u52d5\u3067\uff09<\/li>\n<li>\u8ad6\u7406\u524a\u9664\u30fb\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u8981\u4ef6\u306a\u3069\u3001\u5916\u90e8\u30ad\u30fc\u3060\u3051\u3067\u306f\u5272\u308a\u5207\u308c\u306a\u3044\u9818\u57df\u304c\u3042\u308b\u3053\u3068\u3082\u77e5\u3063\u3066\u304a\u304f<\/li>\n<\/ul>\n<p>\u5165\u9580\u3068\u3057\u3066\u306f\u307e\u305a\u300c\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u5916\u90e8\u30ad\u30fc\u3092\u8cbc\u308b\u300d\u3002\u5916\u3059\u306e\u306f\u3001\u305d\u306e\u4fdd\u8a3c\u3092\u6368\u3066\u308b\u7406\u7531\u3092\u8aac\u660e\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u304b\u3089\u3067\u5341\u5206\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u306f\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u300c\u53c2\u7167\u300d\u3067\u7d50\u3073\u3064\u3044\u3066\u3044\u307e\u3059\u3002\u6ce8\u6587\u30c6\u30fc\u30d6\u30eb\u306e userid \u306f\u30e6\u30fc\u30b6\u30fc\u30c6\u30fc\u30d6\u30eb\u306e\u8ab0\u304b\u3092\u6307\u3057\u3001\u6ce8\u6587\u660e\u7d30\u306e orderid \u306f\u3069\u3053\u304b\u306e\u6ce8\u6587\u3092\u6307\u3057\u307e\u3059\u3002\u3053\u306e\u300c\u6307\u3059\u30fb\u6307\u3055\u308c\u308b\u300d\u306e\u95a2\u4fc2\u304c\u58ca\u308c\u308b\u3068\u3001\u3069\u306e\u30e6\u30fc\u2026<\/p>\n","protected":false},"author":1,"featured_media":3474,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"common","vkexunit_cta_each_option":"","_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[35],"tags":[99,1198,1083,562,1162,472,368],"class_list":["post-3475","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql-memorandum","tag-mysql","tag-postgresql","tag-1083","tag-562","tag-1162","tag-472","tag-368"],"veu_head_title_object":{"title":"","add_site_title":""},"jetpack_publicize_connections":[],"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/qol-kk.com\/wp2\/wp-content\/uploads\/2026\/09\/20260926-plan041-thumbnail.jpg","_links":{"self":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/comments?post=3475"}],"version-history":[{"count":1,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3475\/revisions"}],"predecessor-version":[{"id":3476,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3475\/revisions\/3476"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media\/3474"}],"wp:attachment":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media?parent=3475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/categories?post=3475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/tags?post=3475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}