<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>tech-memo &#187; MySQL</title>
	<atom:link href="http://tech.hapicky.com/archives/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://tech.hapicky.com</link>
	<description>ソフトウェアエンジニアリングに関するメモ書き</description>
	<lastBuildDate>Tue, 02 Feb 2010 05:42:30 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.hapicky.com/archives/tag/mysql/feed" />
		<item>
		<title>[MySQL] NOT NULLかつDEFAULTなしの列を追加した場合、データタイプデフォルト値が設定される。</title>
		<link>http://tech.hapicky.com/archives/234</link>
		<comments>http://tech.hapicky.com/archives/234#comments</comments>
		<pubDate>Tue, 15 Sep 2009 05:16:27 +0000</pubDate>
		<dc:creator>hapicky</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://tech.hapicky.com/?p=234</guid>
		<description><![CDATA[MySQL（5.0系）で、すでにレコードが存在するテーブルにNOT NULLかつDEFAULTなしの列を追加してもエラーにならなかった。きちんと理解していないので調べておく。 以下のテーブルを作成し、レコードを追加しておく。 ?View Code MYSQLmysql&#62; CREATE TABLE users &#40;id INTEGER PRIMARY KEY&#41;; mysql&#62; DESC users; +-------+---------+------+-----+---------+----------------+ &#124; Field &#124; Type &#124; Null &#124; Key &#124; Default &#124; Extra &#124; +-------+---------+------+-----+---------+----------------+ &#124; id &#124; int&#40;11&#41; &#124; NO &#124; PRI &#124; NULL &#124; &#124; +-------+---------+------+-----+---------+----------------+ mysql&#62; INSERT INTO users VALUES&#40;1&#41;; NOT NULLでDEFAULTなしの列を追加してみる。 ?View Code MYSQLmysql&#62; ALTER [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL（5.0系）で、すでにレコードが存在するテーブルにNOT NULLかつDEFAULTなしの列を追加してもエラーにならなかった。きちんと理解していないので調べておく。</p>
<p>以下のテーブルを作成し、レコードを追加しておく。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p234code5'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2345"><td class="code" id="p234code5"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> users <span style="color: #FF00FF;">&#40;</span>id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">DESC</span> users<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+-------+---------+------+-----+---------+----------------+</span>
<span style="color: #CC0099;">|</span> <span style="color: #000099;">Field</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Type</span>    <span style="color: #CC0099;">|</span> <span style="color: #9900FF; font-weight: bold;">Null</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Key</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Default</span> <span style="color: #CC0099;">|</span> Extra          <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-------+---------+------+-----+---------+----------------+</span>
<span style="color: #CC0099;">|</span> id    <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">NO</span>   <span style="color: #CC0099;">|</span> PRI <span style="color: #CC0099;">|</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>    <span style="color: #CC0099;">|</span>                <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-------+---------+------+-----+---------+----------------+</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> users <span style="color: #990099; font-weight: bold;">VALUES</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>NOT NULLでDEFAULTなしの列を追加してみる。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p234code6'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2346"><td class="code" id="p234code6"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> users <span style="color: #990099; font-weight: bold;">ADD</span> <span style="color: #FF00FF;">&#40;</span>name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span> 
age <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span> created_at <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>エラーにならず、以下の値が設定された。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p234code7'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2347"><td class="code" id="p234code7"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> USERS<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+------+-----+---------------------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> name <span style="color: #CC0099;">|</span> age <span style="color: #CC0099;">|</span> created_at          <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+-----+---------------------+</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span>      <span style="color: #CC0099;">|</span>   <span style="color: #008080;">0</span> <span style="color: #CC0099;">|</span> <span style="color: #008080;">0000</span><span style="color: #CC0099;">-</span><span style="color: #008080;">00</span><span style="color: #CC0099;">-</span><span style="color: #008080;">00</span> <span style="color: #008080;">00</span>:<span style="color: #008080;">00</span>:<span style="color: #008080;">00</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+----+------+-----+---------------------+</span></pre></td></tr></table></div>

<p>どうやら「データタイプデフォルト値」が設定されるもよう。<br />
これは「ストリクトSQLモード」が有効な場合でも同じ結果となる。試してみる。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p234code8'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2348"><td class="code" id="p234code8"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- いったん列を削除</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> users <span style="color: #990099; font-weight: bold;">DROP</span> name<span style="color: #000033;">;</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> users <span style="color: #990099; font-weight: bold;">DROP</span> age<span style="color: #000033;">;</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> users <span style="color: #990099; font-weight: bold;">DROP</span> created_at<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- ストリクトSQLモードを有効に</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SET</span> sql_mode <span style="color: #CC0099;">=</span> <span style="color: #008000;">'STRICT<span style="color: #008080; font-weight: bold;">_</span>ALL<span style="color: #008080; font-weight: bold;">_</span>TABLES'</span><span style="color: #000033;">;</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> @@sql_mode<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+-------------------+</span>
<span style="color: #CC0099;">|</span> @@sql_mode        <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-------------------+</span>
<span style="color: #CC0099;">|</span> STRICT_ALL_TABLES <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-------------------+</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- ストリクトSQLモードでもエラーにならない</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> users <span style="color: #990099; font-weight: bold;">ADD</span> <span style="color: #FF00FF;">&#40;</span>name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span> 
age <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span> created_at <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> USERS<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+------+-----+---------------------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> name <span style="color: #CC0099;">|</span> age <span style="color: #CC0099;">|</span> created_at          <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+------+-----+---------------------+</span>
<span style="color: #CC0099;">|</span>  <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span>      <span style="color: #CC0099;">|</span>   <span style="color: #008080;">0</span> <span style="color: #CC0099;">|</span> <span style="color: #008080;">0000</span><span style="color: #CC0099;">-</span><span style="color: #008080;">00</span><span style="color: #CC0099;">-</span><span style="color: #008080;">00</span> <span style="color: #008080;">00</span>:<span style="color: #008080;">00</span>:<span style="color: #008080;">00</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+----+------+-----+---------------------+</span></pre></td></tr></table></div>

<p>せめてストリクトSQLモードが有効な場合にはエラーにしてほしいですね。要注意です。</p>
<p>参照ドキュメント: </p>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html">Data Type Default Values</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_all_tables">Server SQL Modes</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://tech.hapicky.com/archives/234/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.hapicky.com/archives/234" />
	</item>
		<item>
		<title>[MySQL] int(11)の11は最大桁数ではない</title>
		<link>http://tech.hapicky.com/archives/225</link>
		<comments>http://tech.hapicky.com/archives/225#comments</comments>
		<pubDate>Tue, 01 Sep 2009 09:17:39 +0000</pubDate>
		<dc:creator>hapicky</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://tech.hapicky.com/?p=225</guid>
		<description><![CDATA[危なかった。勘違いしてた。 int(11)の「11」って表示桁数を揃えるためのものであって、格納できるのはあくまで4バイトなんですね。 参照: 本家ドキュメント 試してみる。 ?View Code MYSQLmysql&#62; create table int_test &#40;id int&#40;11&#41; primary key&#41;; Query OK, 0 rows affected &#40;0.04 sec&#41; &#160; mysql&#62; desc int_test; +-------+---------+------+-----+---------+-------+ &#124; Field &#124; Type &#124; Null &#124; Key &#124; Default &#124; Extra &#124; +-------+---------+------+-----+---------+-------+ &#124; id &#124; int&#40;11&#41; &#124; NO &#124; PRI &#124; NULL &#124; &#124; +-------+---------+------+-----+---------+-------+ 1 [...]]]></description>
			<content:encoded><![CDATA[<p>危なかった。勘違いしてた。<br />
int(11)の「11」って表示桁数を揃えるためのものであって、格納できるのはあくまで4バイトなんですね。<br />
参照: <a href="http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html">本家ドキュメント</a></p>
<p>試してみる。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p225code10'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p22510"><td class="code" id="p225code10"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">create</span> <span style="color: #990099; font-weight: bold;">table</span> int_test <span style="color: #FF00FF;">&#40;</span>id <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">primary key</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.04</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">desc</span> int_test<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+-------+---------+------+-----+---------+-------+</span>
<span style="color: #CC0099;">|</span> <span style="color: #000099;">Field</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Type</span>    <span style="color: #CC0099;">|</span> <span style="color: #9900FF; font-weight: bold;">Null</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Key</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Default</span> <span style="color: #CC0099;">|</span> Extra <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-------+---------+------+-----+---------+-------+</span>
<span style="color: #CC0099;">|</span> id    <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">NO</span>   <span style="color: #CC0099;">|</span> PRI <span style="color: #CC0099;">|</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>    <span style="color: #CC0099;">|</span>       <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-------+---------+------+-----+---------+-------+</span>
<span style="color: #008080;">1</span> row <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">insert</span> <span style="color: #990099; font-weight: bold;">into</span> int_test <span style="color: #990099; font-weight: bold;">values</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2147483647</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">1</span> row affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">insert</span> <span style="color: #990099; font-weight: bold;">into</span> int_test <span style="color: #990099; font-weight: bold;">values</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2147483648</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
ERROR <span style="color: #008080;">1062</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">23000</span><span style="color: #FF00FF;">&#41;</span>: Duplicate entry <span style="color: #008000;">'2147483647'</span> for <span style="color: #990099; font-weight: bold;">key</span> <span style="color: #008080;">1</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://tech.hapicky.com/archives/225/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.hapicky.com/archives/225" />
	</item>
		<item>
		<title>MySQLのvarcharは、最大サイズ65,535（バージョン5.0.3以降）</title>
		<link>http://tech.hapicky.com/archives/93</link>
		<comments>http://tech.hapicky.com/archives/93#comments</comments>
		<pubDate>Tue, 24 Mar 2009 03:58:28 +0000</pubDate>
		<dc:creator>hapicky</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://tech.hapicky.com/?p=93</guid>
		<description><![CDATA[いろいろ勘違いしていたので整理。 varchar で指定するサイズは、バイト数ではなく文字数。 varchar(5)なフィールドに「あいうえお」を格納できる。 （クライアント文字コードが正しく設定されている前提の話） varcharの最大サイズは65,535。 ただし行にも最大サイズ（65,535「バイト」）があり、これに収まる限り。 blob、text型については別格納となるので、行サイズとしてはポインタ分のみ消費。 参照リファレンスは以下 The CHAR and VARCHAR Types The Maximum Number of Columns Per Table]]></description>
			<content:encoded><![CDATA[<p>いろいろ勘違いしていたので整理。</p>
<ul>
<li>varchar で指定するサイズは、バイト数ではなく文字数。</li>
<ul>
<li>varchar(5)なフィールドに「あいうえお」を格納できる。<br />
（クライアント文字コードが正しく設定されている前提の話）</li>
</ul>
<li>varcharの最大サイズは65,535。</li>
<ul>
<li>ただし行にも最大サイズ（65,535<strong>「バイト」</strong>）があり、これに収まる限り。</li>
</ul>
<li>blob、text型については別格納となるので、行サイズとしてはポインタ分のみ消費。</li>
</ul>
<p>参照リファレンスは以下</p>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/char.html">The CHAR and VARCHAR Types</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/column-count-limit.html">The Maximum Number of Columns Per Table</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://tech.hapicky.com/archives/93/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.hapicky.com/archives/93" />
	</item>
		<item>
		<title>MySQLのGROUP BYは、寛容すぎて気持ちが悪い。</title>
		<link>http://tech.hapicky.com/archives/71</link>
		<comments>http://tech.hapicky.com/archives/71#comments</comments>
		<pubDate>Wed, 25 Feb 2009 09:41:24 +0000</pubDate>
		<dc:creator>hapicky</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://tech.hapicky.com/?p=71</guid>
		<description><![CDATA[以前の職場ではOracleばかり使っていたのですが、MySQLを使うようになってちょっとカルチャーショック、というお話。 以下のようなテーブルがあってですよ、 ?View Code MYSQLmysql&#62; SELECT table_a.* FROM table_a; +-------+-------+-------+ &#124; col_a &#124; col_b &#124; col_c &#124; +-------+-------+-------+ &#124; 1 &#124; 1 &#124; 1 &#124; &#124; 2 &#124; 1 &#124; 2 &#124; &#124; 3 &#124; 2 &#124; 2 &#124; &#124; 4 &#124; 2 &#124; 1 &#124; +-------+-------+-------+ 4 rows in set &#40;0.00 sec&#41; 以下のように、GROUP BYに含まれない列でもSELECTできちゃうんですね。 [...]]]></description>
			<content:encoded><![CDATA[<p>以前の職場ではOracleばかり使っていたのですが、MySQLを使うようになってちょっとカルチャーショック、というお話。</p>
<p>以下のようなテーブルがあってですよ、</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p71code14'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7114"><td class="code" id="p71code14"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> table_a.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> table_a<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+-------+-------+-------+</span>
<span style="color: #CC0099;">|</span> col_a <span style="color: #CC0099;">|</span> col_b <span style="color: #CC0099;">|</span> col_c <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-------+-------+-------+</span>
<span style="color: #CC0099;">|</span>     <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>     <span style="color: #008080;">2</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">2</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>     <span style="color: #008080;">3</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">2</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">2</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>     <span style="color: #008080;">4</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">2</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-------+-------+-------+</span>
<span style="color: #008080;">4</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p>以下のように、GROUP BYに含まれない列でもSELECTできちゃうんですね。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p71code15'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7115"><td class="code" id="p71code15"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> col_b<span style="color: #000033;">,</span> col_c <span style="color: #990099; font-weight: bold;">FROM</span> table_a <span style="color: #990099; font-weight: bold;">GROUP BY</span> col_b<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+-------+-------+</span>
<span style="color: #CC0099;">|</span> col_b <span style="color: #CC0099;">|</span> col_c <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+-------+-------+</span>
<span style="color: #CC0099;">|</span>     <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">1</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>     <span style="color: #008080;">2</span> <span style="color: #CC0099;">|</span>     <span style="color: #008080;">2</span> <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+-------+-------+</span>
<span style="color: #008080;">2</span> rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p>col_cの値はどれがSELECTされるのだろう…。物理的な先頭行の値？</p>
<p>Oracleだとたしか構文エラーになったよなーなんて思っていたのですが、ちゃんと「MySQLによる拡張です」って書いてありました。<br />
本家ドキュメント：<a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-columns.html">MySQL extends the use of GROUP BY&#8230;</a></p>
<p>前述のようなケースは「やっちゃだめ」とも書いてありますね。</p>
<blockquote><p>Do not use this feature if the columns you omit from the GROUP BY part are not constant in the group.</p></blockquote>
<p>さらに環境変数 <a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html">sql_mode</a> に <a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_only_full_group_by">ONLY_FULL_GROUP_BY</a> を設定すれば、この拡張は抑制されるみたい。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p71code16'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7116"><td class="code" id="p71code16"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SET</span> sql_mode <span style="color: #CC0099;">=</span> <span style="color: #008000;">'ONLY<span style="color: #008080; font-weight: bold;">_</span>FULL<span style="color: #008080; font-weight: bold;">_</span>GROUP<span style="color: #008080; font-weight: bold;">_</span>BY'</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> col_b<span style="color: #000033;">,</span> col_c <span style="color: #990099; font-weight: bold;">FROM</span> table_a <span style="color: #990099; font-weight: bold;">GROUP BY</span> col_b<span style="color: #000033;">;</span>
ERROR <span style="color: #008080;">1055</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">42000</span><span style="color: #FF00FF;">&#41;</span>: <span style="color: #008000;">'database<span style="color: #008080; font-weight: bold;">_</span>a.table<span style="color: #008080; font-weight: bold;">_</span>a.col<span style="color: #008080; font-weight: bold;">_</span>c'</span> isn<span style="color: #008000;">'t in GROUP BY</span></pre></td></tr></table></div>

<p>あいまいなSQLを未然に防止するためには、設定しておいた方がいい気がする…。</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.hapicky.com/archives/71/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.hapicky.com/archives/71" />
	</item>
	</channel>
</rss>

