マイクロサービスの本を読んでたら
思ってるよりView機能は重要だと知った
マスタカは使ったことないので試しに触ってみた
・作ったテーブル
mysql> desc table_test; +------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | created_at | datetime | YES | | NULL | | | updated_at | datetime | YES | | NULL | | +------------+-------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)
・データのの中身
mysql> select * from table_test; +----+----------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+----------+---------------------+---------------------+ | 1 | masterka | 2020-09-20 17:42:08 | 2020-09-20 17:42:08 | +----+----------+---------------------+---------------------+ 1 row in set (0.00 sec)
・Viewの作成
create view view_test as select name, created_at from table_test; mysql> select * from view_test; +----------+---------------------+ | name | created_at | +----------+---------------------+ | masterka | 2020-09-20 17:42:08 | +----------+---------------------+ 1 row in set (0.01 sec)
・Tableの確認
Viewも一緒に確認できる
mysql> show tables; +-------------------------+ | Tables_in_test_database | +-------------------------+ | table_test | | view_test | +-------------------------+
・Viewだけ確認
mysql> select * from information_schema.views \G; *************************** 1. row *************************** TABLE_CATALOG: def TABLE_SCHEMA: test_database TABLE_NAME: view_test VIEW_DEFINITION: select <code>test_database</code>.<code>table_test</code>.<code>name</code> AS <code>name</code>,<code>test_database</code>.<code>table_test</code>.<code>created_at</code> AS <code>created_at</code> from <code>test_database</code>.<code>table_test</code> CHECK_OPTION: NONE IS_UPDATABLE: YES DEFINER: docker@% SECURITY_TYPE: DEFINER CHARACTER_SET_CLIENT: utf8 COLLATION_CONNECTION: utf8_general_ci 1 row in set (0.01 sec)
・Viewを削除
mysql> drop view view_test; Query OK, 0 rows affected (0.02 sec)
こうしてMySQLのViewを使ってみました
確かにこれならスキーマの変更を相手に知らせなくても大丈夫かなと思います
関連記事:
- MySQL8.0のdelete文でロックになるか調べてみた
- Cassandra: The Definitive Guideの7章を読んだ
- Docker ComposeでMySQL + phpMyAdminをやってみた