MySQLを用いてデータベース操作をやった話
はじめに
今回はMySQLを用いたデータの基本操作について学んだのでまとめていく。
MySQL操作について
データベースを確認
mysqlにログイン後にshowコマンドで見ることができる。
mysql> show databases;
データベースの作成と削除
create
でデータベースを作れて、drop
で消すことができる。
mysql> create database KKZoo_db01; mysql> drop database KKZoo_db01;
操作するデータベースを選択する
use
コマンドで操作対象を決めて、select
で確認する。
mysql> use database KKZoo_db01; mysql> select database();
ログイン時に操作対象を決めることもできる。
$ mysql -u root KKZoo_db01
テーブル操作について
テーブルを作成と削除
create table
でテーブルを作成でき、drop table
でテーブルを削除できる
今回はusersテーブルにid, name, age属性を付与する。
mysql> create table users(id int unsigned, name varchar(32), age int); mysql> drop table users;
テーブルの確認
show
で一覧でusersが出来ていることを確認し、desc
で中身を確認する
mysql> show tables; mysql> desc users;
データの挿入
insert
コマンドでデータを挿入することができる。
musql> insert into users (id, name, age) values (1, 'KKZ', null);
データ挿入の応用編
create table
をする際にオートインクリメント属性とプライマリーキーを付与できる。
mysql> create table users(id int unsigned auto_increment not null primary key, name varchar(32), age int); mysql> desc users; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int unsigned | NO | PRI | NULL | auto_increment | | name | varchar(32) | YES | | NULL | | | age | int | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+
上記の状態のテーブルにはidが自動で入力されるのでidを指定せずにデータベースにデータをinsert
できる。
mysql> insert into users(name, age) values('KKZoo', 20);
データベースから値を取得する
select
コマンドを用いることでデータを取得できる。
*
を用いてテーブル全体を呼び出すか、カラム名を直接指定することができる。
mysql> select * from users; mysql> select name,age from users;
条件づけられた値を取得する方法
以下のようなテーブルから20才未満のデータを取り出します。
mysql> select * from users; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | KKZoo | 20 | | 2 | higa | 25 | | 3 | sato | 18 | | 4 | arakawa | 32 | | 5 | tanaka | 30 | +----+---------+------+
where
文を用いることで可能となる。
使える比較演算子はここを参照。
mysql> select * from users where age < 20; +----+------+------+ | id | name | age | +----+------+------+ | 3 | sato | 18 | +----+------+------+
データをソートして表示
order by
を用いることでソートすることができる。
order byの使い方。
mysql> select * from users order by age asc; +----+---------+------+ | id | name | age | +----+---------+------+ | 3 | sato | 18 | | 1 | KKZoo | 20 | | 2 | higa | 25 | | 5 | tanaka | 30 | | 4 | arakawa | 32 | +----+---------+------+
表示件数に制限をつける
limit
を用いることでデータの取得件数に制限をかけることができる。
mysql> select * from users limit 2; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | KKZoo | 20 | | 2 | higa | 25 | +----+-------+------+ 2 rows in set (0.00 sec)
テーブルの任意のデータを更新
update
を用いることで任意のデータを更新することができるが、条件を指定しないと全件指定となるので、whereで条件を記載してデータを選択し、set
でカラム名を決めて更新後のデータを新規挿入するのが基本操作になる。
mysql> update users set age = 100 where id = 1; mysql> select * from users where id = 1; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | KKZoo | 100 | +----+-------+------+
テーブルの任意のデータを削除
delete
を用いることでテーブルの任意のデータを削除することができる。
mysql> delete from users where id = 1; mysql> select * from users; +----+---------+------+ | id | name | age | +----+---------+------+ | 2 | higa | 25 | | 3 | sato | 18 | | 4 | arakawa | 32 | | 5 | tanaka | 30 | +----+---------+------+
終わりに
今回はMySQLを用いることでデータベース操作を行うことで、操作の仕方を思い出すきっかけになった。