{"id":660,"date":"2019-08-22T12:47:43","date_gmt":"2019-08-22T03:47:43","guid":{"rendered":"https:\/\/took.jp\/?p=660"},"modified":"2022-08-22T06:44:54","modified_gmt":"2022-08-21T21:44:54","slug":"post-660","status":"publish","type":"post","link":"https:\/\/took.jp\/tech-blog\/post-660\/","title":{"rendered":"\u3010\u733f\u3067\u3082\u308f\u304b\u308bMysql\u3011 alter table add colum if not exists\u3092\u5b9f\u88c5\u3059\u308b"},"content":{"rendered":"<p>CREATE TABLE IF NOT EXISTS\u3063\u3066\u3042\u308a\u307e\u3059\u3088\u306d\u3002<\/p>\n<p>\u3082\u3057\u3001\u30c6\u30fc\u30d6\u30eb\u304c\u5b58\u5728\u3057\u3066\u3044\u306a\u3051\u308c\u3070\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u8ffd\u52a0\u3059\u308b\u3068\u3044\u3046\u51e6\u7406\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u30ab\u30e9\u30e0\u3067\u306f\u3001\u305d\u306e\u51e6\u7406\u304c\u884c\u3048\u306a\u3044\u305f\u3081\u306b\u3001<\/p>\n<p>\u65e2\u306b\u30ab\u30e9\u30e0\u304c\u5b58\u5728\u3057\u3066\u3044\u307e\u3059\u3068\u30a8\u30e9\u30fc\uff08ERROR 1060 (42S21): Duplicate column name ''\uff09\u304c\u8868\u793a\u3055\u308c\u3001\u5f8c\u7d9a\u51e6\u7406\u3082\u6b62\u307e\u3063\u3066\u3057\u307e\u3046\u306a\u3069\u3001<\/p>\n<p>\u56f0\u3089\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u30ab\u30e9\u30e0\u306b\u3082IF NOT EXIST\u3092\u5b9f\u88c5\u3057\u3066\u3084\u308d\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\u306b\u3057\u305f\u8a18\u4e8b\u306f\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n<p>https:\/\/qiita.com\/jas\/items\/0ffe72a96c3f8b6cf6dc<\/p>\n<p>&nbsp;<\/p>\n<p>\u51c4\u304f\u308f\u304b\u308a\u3084\u3059\u3044\u8a18\u4e8b\u306a\u306e\u3067\u3059\u304c\u3001<\/p>\n<p>\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u306a\u306b\u305d\u308c\u3068\u3044\u3046\u4eba\u306e\u305f\u3081\u306b\u89e3\u8aac\u3057\u306a\u304c\u3089\u9032\u3081\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u307e\u305a\u3001\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u3068\u306f...<\/p>\n<p>\u5024\u3092\u8fd4\u3055\u306a\u3044\u51e6\u7406\u306e\u307e\u3068\u307e\u308a\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u5024\u3092\u8fd4\u3059\u3082\u306e\u3092\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u305d\u306e\u51e6\u7406\u306e\u307e\u3068\u307e\u308a\u3092\u4f5c\u6210\u3057\u3001\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u3001<\/p>\n<p>alter table add colum if not exists\u3092\u5b9f\u73fe\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u5b9f\u88c5\u3059\u308b<\/h2>\n<p>\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u304c\u5b58\u5728\u3057\u3066\u3044\u308c\u3070\u524a\u9664\u3002<\/p>\n<p>\u3053\u308c\u304b\u3089\u4f5c\u6210\u3059\u308b\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u540d\u3068\u88ab\u3089\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">DROP PROCEDURE IF EXISTS alter_table_procedure;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u30c7\u30ea\u30df\u30bf\u3068\u306f\u533a\u5207\u308a\u6587\u5b57\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u3053\u3053\u3067\u306f\u3001\u6587\u672b\u306b\u3064\u3051\u308b\u3000; \u3092 \/\/ \u306b\u5909\u3048\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30c7\u30ea\u30df\u30bf\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u8907\u6570\u306e\u51e6\u7406\u3092\u307e\u3068\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">DELIMITER \/\/<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u30c7\u30ea\u30df\u30bf\u304c\u5909\u66f4\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001<\/p>\n<p>\/\/ \u307e\u3067\u3067\u4e00\u3064\u306e\u6587\u7ae0\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u3066\u3082\u3001\u30a8\u30e9\u30fc\u3092\u51fa\u3055\u306a\u3044\u3002\u4e0a\u66f8\u304d\u3057\u3066\u3057\u307e\u3046\u3053\u3068\u3082\u306a\u3044\u306e\u3067\u3001<\/p>\n<p>\u3082\u3057\u3001\u5b58\u5728\u3059\u308c\u3070\u3001\u3068\u3044\u3046\u51e6\u7406\u304c\u5b9f\u884c\u3067\u304d\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>ALTER TABLE ....\u306e\u90e8\u5206\u306f\u5fc5\u8981\u306b\u5fdc\u3058\u3001\u66f8\u304d\u63db\u3048\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">CREATE PROCEDURE alter_table_procedure()\r\nBEGIN\r\n  \/* SQLEXCEPTION\u3092\u7121\u8996\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a *\/\r\n  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;\r\n  \/* \u4ee5\u4e0b\u306eALTER TABLE\u3067\u300eDuplicate column name\u300f\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u3066\u3082\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u306f\u6b63\u5e38\u7d42\u4e86\u3059\u308b *\/\r\n  ALTER TABLE user ADD COLUMN age INT NULL AFTER name;\r\n  ALTER TABLE user ADD COLUMN address VARCHAR(50) NULL AFTER age;\r\nEND \/\/<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u30c7\u30ea\u30df\u30bf\u3092\u623b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u623b\u3057\u5fd8\u308c\u308b\u3068\u3001\u6587\u672b\u306b \/\/ \u3092\u3064\u3051\u306a\u3044\u3068\u51e6\u7406\u3092\u5b9f\u884c\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>mysql\u306b\u30a2\u30af\u30bb\u30b9\u3057\u306a\u304a\u3059\u3053\u3068\u3067\u3082\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306b\u623b\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">DELIMITER ;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e00\u5fdc\u3001\u4f5c\u6210\u3055\u308c\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">SHOW PROCEDURE STATUS;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">CALL alter_table_procedure();<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u3092\u524a\u9664\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">DROP PROCEDURE alter_table_procedure;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>SELECT\u3059\u308b\u3053\u3068\u3067\u3001\u3082\u3057\u30ab\u30e9\u30e0\u304c\u8ffd\u52a0\u3067\u304d\u3066\u3044\u306a\u3044\uff08\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\uff09<\/p>\n<p>ERROR 1054 (42S22): Unknown column '' in 'field list'<\/p>\n<p>\u306e\u30a8\u30e9\u30fc\u304c\u8d77\u304d\u3001\u77e5\u3089\u305b\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p>\u6700\u5f8c\u306blimit 0 \u3068\u3044\u3046\u306e\u3092\u3064\u3051\u305f\u306e\u306f\u53d6\u5f97\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u6570\u30920\u4ef6\u306b\u3059\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n<p>\u3082\u3057\u3001\u65e2\u306b\u5b58\u5728\u3057\u3066\u3044\u305f\u30ab\u30e9\u30e0\u3067\u30c7\u30fc\u30bf\u304c\u5165\u3063\u3066\u3044\u305f\u5834\u5408\u306b\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u304cSELECT\u3055\u308c\u3066\u3057\u307e\u3046\u6050\u308c\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u5b58\u5728\u3059\u308c\u3070\u3001Empty set (0.00 sec)\u304c\u8fd4\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">SELECT age, address FROM user limit 0;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CREATE TABLE IF NOT EXISTS\u3063\u3066\u3042\u308a\u307e\u3059\u3088\u306d\u3002 \u3082\u3057\u3001\u30c6\u30fc\u30d6\u30eb\u304c\u5b58\u5728\u3057\u3066\u3044\u306a\u3051\u308c\u3070\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u8ffd\u52a0\u3059\u308b\u3068\u3044\u3046\u51e6\u7406\u3002 &nbsp; \u30ab\u30e9\u30e0\u3067\u306f\u3001\u305d\u306e\u51e6\u7406\u304c\u884c\u3048\u306a\u3044\u305f\u3081\u306b\u3001 \u65e2\u306b\u30ab\u30e9\u30e0\u304c\u5b58\u5728\u3057\u3066\u3044\u307e\u3059\u3068\u30a8\u30e9\u30fc\uff08ERROR\u2026<\/p>\n","protected":false},"author":3,"featured_media":2845,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,71],"tags":[],"class_list":["post-660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-it","category-mysql"],"_links":{"self":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts\/660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/comments?post=660"}],"version-history":[{"count":0,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts\/660\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/media\/2845"}],"wp:attachment":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/media?parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/categories?post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/tags?post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}