Your SlideShare is downloading. ×
メッチャ役に立つauto_incrementの話
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

メッチャ役に立つauto_incrementの話

5,108
views

Published on

2012/06/27 …

2012/06/27
MySQL Casual Talks @gumi福岡
#mysqlcf


0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,108
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. メッチャ役に立つauto_incrementの話 @kitakoh 2012
    • 2. 自己紹介@kitakoh(放置中…)大分から来ましたパン食べるパンよろしくお願いします!
    • 3. auto_increment通し番号を自動で振ってくれる機能
    • 4. user_id(auto_increment),user_name(char 10)だけのテーブルで実験
    • 5. 普通にINSERTINSERT INTO tbl (user_name)VALUES(‘user01’),(‘user02’),(‘user03’);
    • 6. 普通にINSERT +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | user01 | | 2 | user02 | | 3 | user03 | +---------+-----------+3 rows in set (0.00 sec)
    • 7. user_id=2をDELETE +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | user01 | | ! 2 | user02 | | 3 | user03 | +---------+-----------+ 3 rows in set (0.00 sec)
    • 8. この状態で新規行をINSERT +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | user01 | | 3 | user03 | +---------+-----------+ 2 rows in set (0.00 sec)
    • 9. 最後に追加された +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | user01 | | 3 | user03 | | ! 4 | userXX | +---------+-----------+3 rows in set (0.00 sec)
    • 10. 最後の行をDELETEしてINSERTすると? +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | user01 | | 3 | user03 | | ! 4 | userXX | +---------+-----------+ 3 rows in set (0.00 sec)
    • 11. 最後に追加された +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | user01 | | 3 | user03 | | ! 5 | userYY | +---------+-----------+3 rows in set (0.00 sec)
    • 12. auto_incrementで振られるシーケンスは再利用され ない
    • 13. user_idを指定してみるINSERT INTO tbl (user_id, user_name)VALUES (10, ‘user10’);
    • 14. ここに新規行をINSERT +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | user01 | | 3 | user03 | | 4 | userXX | | 5 | userYY | | 10 | user10 | +---------+-----------+
    • 15. 最後に追加された+---------+-----------+| user_id | user_name |+---------+-----------+| 1 | user01 || 3 | user03 || 4 | userXX || 5 | userYY || 10 | user10 || ! 11 | userZZ |+---------+-----------+
    • 16. 歯抜けのuser_idを指定INSERT INTO tbl (user_id, user_name)VALUES (2, ‘user2’);
    • 17. 指定場所に追加される +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | user01 | | ! 2 | user02 | | 3 | user03 | | 4 | userXX | | 5 | userYY | | 10 | user10 | | 11 | userZZ |
    • 18. シーケンスは基本的に再利用されないが、値を指定す れば再利用可能
    • 19. 全行DELETEするとどうなる?(シーケンス11まで振られている状態で)DELETE FROM tbl;INSERT INTO tbl(user_name)VALUES(‘userXX’);
    • 20. 最後に追加される +---------+-----------+ | user_id | user_name | +---------+-----------+ | 12 | userXX | +---------+-----------+1 rows in set (0.00 sec)
    • 21. テーブルをTRUNCATEするとどうなる?(シーケンスが12まで振られてる状態で)TRUNCATE TABLE tbl;INSERT INTOtbl(user_name)VALUES(‘userXX’);
    • 22. シーケンスが巻き戻った +---------+-----------+ | user_id | user_name | +---------+-----------+ | 1 | userXX | +---------+-----------+ 1 rows in set (0.00 sec) ※MySQL5.5
    • 23. DELETEでは巻き戻らないが、 TRUNCATEだと巻き戻る
    • 24. user_idが最大値の状態でINSERTINSERT INTO tbl(user_id, user_name)VALUES(127, ‘userXX’); +---------+-----------+ | user_id | user_name | +---------+-----------+ | 127 | userXX | +---------+-----------+ ↑TINYINT(SIGNED)
    • 25. ERRORになるERROR 1062 (23000): Duplicate entry127 for key PRIMARY
    • 26. シーケンス値は、ALTER文で指定できるALTER TABLE tbl auto_increment = 10;10に指定しているので、新規行INSERTすると11から入る
    • 27. 今のシーケンス値を知る方法 SHOW CREATE TABLE tbl;+-------+------------------------------------------------+| Table | Create Table |+-------+------------------------------------------------+| tbl | CREATE TABLE `tbl` ( || | `user_id` tinyint(4) NOT NULL AUTO_INCREMENT, || | `user_name` char(10) DEFAULT NULL, || | PRIMARY KEY (`user_id`) || | ) ENGINE=InnoDB AUTO_INCREMENT=10 |+-------+------------------------------------------------+
    • 28. 複合キーでauto_incrementCREATE TABLE tbl( dept_id TINYINT, ←部門IDを追加 user_id TINYINT AUTO_INCREMENT, user_name CHAR(10), PRIMARY KEY(dept_id, user_id))ENGINE=MyISAM;
    • 29. 複合キーでauto_incrementinsert into tbl(dept_id, user_name) values(1, aa);insert into tbl(dept_id, user_name) values(1, bb);insert into tbl(dept_id, user_name) values(1, cc);insert into tbl(dept_id, user_name) values(2, aa);insert into tbl(dept_id, user_name) values(2, bb);insert into tbl(dept_id, user_name) values(2, cc);1と2の部門に3件ずつINSERTしてみる
    • 30. 複合キーでauto_increment +---------+---------+-----------+ | dept_id | user_id | user_name | +---------+---------+-----------+ | 1 | 1 | aa | | 1 | 2 | bb | | 1 | 3 | cc | | 2 | 1 | aa | | 2 | 2 | bb | | 2 | 3 | cc | +---------+---------+-----------+ 6 rows in set (0.01 sec)
    • 31. 複合キーでauto_increment dept_idとuser_idで重 複しないように、自動的に user_idを連番で振ってく れます。(MyISAM限定)
    • 32. メッチャ役に立つauto_incrementの話 完 ご清聴下さった皆様、 場所をご提供下さいましたgumi様、 ありがとうございました