{"id":5,"date":"2012-09-21T15:18:36","date_gmt":"2012-09-21T15:18:36","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2012\/09\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/"},"modified":"2012-09-21T15:18:36","modified_gmt":"2012-09-21T15:18:36","slug":"renaming_a_mysql_column_with_a_foreign_key_constraint-shtml","status":"publish","type":"post","link":"http:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/","title":{"rendered":"Renaming A MySQL Column With A Foreign Key Constraint"},"content":{"rendered":"<p>This is more of a note to myself, but blogged in the hope it will help someone else using MySQL.<\/p>\n<p>I had to rename a column in a <a href=\"http:\/\/www.mysql.com\/\">MySQL<\/a> database earlier. However the column had a foreign key on it, so I was getting the dreaded error 150, warning me of a foreign key constraint.<\/p>\n<p>The soltuion is to first drop the foreign key, then change the column, finally adding the constraint back in.<\/p>\n<p>The SQL will look something like this&#8230;<\/p>\n<pre class=\"lang:tsql decode:true \" title=\"Renaming a column with a foreign key\" >ALTER TABLE `mytable`\nDROP FOREIGN KEY `mytable_oldname_fk`,\nCHANGE COLUMN oldname newname INT(11) DEFAULT NULL,\nADD CONSTRAINT `mytable_newname_fk` FOREIGN KEY (`newname`) REFERENCES `anothertable` (`id`)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is more of a note to myself, but blogged in the hope it will help someone else using MySQL. I had to rename a column in a MySQL database earlier. However the column had a foreign key on it, so I was getting the dreaded error 150, warning me of a foreign key constraint. &hellip; <a href=\"http:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Renaming A MySQL Column With A Foreign Key Constraint&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[42],"class_list":["post-5","post","type-post","status-publish","format-standard","hentry","category-dev","tag-mysql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Renaming A MySQL Column With A Foreign Key Constraint - Robert Price<\/title>\n<meta name=\"description\" content=\"How to rename a column in a MySQL database that has a foreign key constraint on it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Renaming A MySQL Column With A Foreign Key Constraint - Robert Price\" \/>\n<meta property=\"og:description\" content=\"How to rename a column in a MySQL database that has a foreign key constraint on it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-21T15:18:36+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Renaming A MySQL Column With A Foreign Key Constraint\",\"datePublished\":\"2012-09-21T15:18:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/\"},\"wordCount\":89,\"keywords\":[\"MySQL\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/\",\"url\":\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/\",\"name\":\"Renaming A MySQL Column With A Foreign Key Constraint - Robert Price\",\"isPartOf\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#website\"},\"datePublished\":\"2012-09-21T15:18:36+00:00\",\"author\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"How to rename a column in a MySQL database that has a foreign key constraint on it.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.robertprice.co.uk\/robblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Renaming A MySQL Column With A Foreign Key Constraint\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#website\",\"url\":\"http:\/\/www.robertprice.co.uk\/robblog\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Renaming A MySQL Column With A Foreign Key Constraint - Robert Price","description":"How to rename a column in a MySQL database that has a foreign key constraint on it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Renaming A MySQL Column With A Foreign Key Constraint - Robert Price","og_description":"How to rename a column in a MySQL database that has a foreign key constraint on it.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/","og_site_name":"Robert Price","article_published_time":"2012-09-21T15:18:36+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/"},"author":{"name":"rob","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Renaming A MySQL Column With A Foreign Key Constraint","datePublished":"2012-09-21T15:18:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/"},"wordCount":89,"keywords":["MySQL"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/","name":"Renaming A MySQL Column With A Foreign Key Constraint - Robert Price","isPartOf":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2012-09-21T15:18:36+00:00","author":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"How to rename a column in a MySQL database that has a foreign key constraint on it.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/renaming_a_mysql_column_with_a_foreign_key_constraint-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Renaming A MySQL Column With A Foreign Key Constraint"}]},{"@type":"WebSite","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#website","url":"http:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/5","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=5"}],"version-history":[{"count":0,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/5\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}