注意Mysqlデータ遮断
Beware of MySQL Data Truncationhttp://www.mysqlperformanceblog.com/2009/02/07/beware-of-mysql-data-truncation/
たとえば、aritcleとarticleのテーブルがあります.コメント、関連付けはarticleのid
CREATE TABLE `article` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
CREATE TABLE `article_comment` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `text` varchar(200) NOT NULL, `article_id` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `art_id` (`article_id`), CONSTRAINT `art_id` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
set sql_mode='';
insert into article values(12345678901,'name1');
insert into article_comment(text,article_id) values('text1',12345678901);
insert into article_comment(text,article_id) values('text2',12345678902);
データの表示:
articleテーブル
4294967295 name1article_コメントテーブル
1 text 1 42949672952 text 2 4294967295この中から分かるように、本来2番目に挿入されたコメントは別の文章に関連付けようとしたが、最初の文章に関連していた.これはMysqlのData Truncationのためである
show warnings表示
これにより、
(1)コメントを誤った文章に関連付ける
(2)同じ記事に多くのコメントが関連付けられている(これはパフォーマンスの問題をもたらす)
どうやって解決しますか?
set sql_mode='
insert into article_comment(text,article_id) values('text1',12345678903);
これは間違いを報告します.
[SQL] insert into article_comment(text,article_id) values('text1',12345678903);[Err] 1264 - Out of range value for column 'article_id' at row 1
そのため、上記の問題も回避された.
たとえば、aritcleとarticleのテーブルがあります.コメント、関連付けはarticleのid
CREATE TABLE `article` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
CREATE TABLE `article_comment` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `text` varchar(200) NOT NULL, `article_id` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `art_id` (`article_id`), CONSTRAINT `art_id` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
set sql_mode='';
insert into article values(12345678901,'name1');
insert into article_comment(text,article_id) values('text1',12345678901);
insert into article_comment(text,article_id) values('text2',12345678902);
データの表示:
articleテーブル
4294967295 name1article_コメントテーブル
1 text 1 42949672952 text 2 4294967295この中から分かるように、本来2番目に挿入されたコメントは別の文章に関連付けようとしたが、最初の文章に関連していた.これはMysqlのData Truncationのためである
show warnings表示
Warning | 1265 | Data truncated for column 'article_id' at row 1
これにより、
(1)コメントを誤った文章に関連付ける
(2)同じ記事に多くのコメントが関連付けられている(これはパフォーマンスの問題をもたらす)
どうやって解決しますか?
set sql_mode='
STRICT_ALL_TABLES
'; insert into article_comment(text,article_id) values('text1',12345678903);
これは間違いを報告します.
[SQL] insert into article_comment(text,article_id) values('text1',12345678903);[Err] 1264 - Out of range value for column 'article_id' at row 1
そのため、上記の問題も回避された.