初心者の自分ための忘却録
すべてのデータベースの一覧を取得
書式 SHOW DATABASES [LIKE 'pattern' | WHERE expr]
MySQL8.0CommandLineClient
show databases;
実行結果
MySQL8.0CommandLineClient
+--------------------+
| Database |
+--------------------+
| test1 |
+--------------------+
使用するデータベースを選択
書式 USE db_name
test1 を選択
MySQL8.0CommandLineClient
use test1;
作成済のテーブル一覧を確認する
書式
SHOW [EXTENDED] [FULL] TABLES [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
カレントデータベース(作業中のデータベース)であるtest1のテーブル確認
MySQL8.0CommandLineClient
show tables;
実行結果
MySQL8.0CommandLineClient
+-----------------+
| Tables_in_test1 |
+-----------------+
| users1 |
+-----------------+
テーブル作成
書式(DB選択の場合)
CREATE TABLE [IF NOT EXISTS] tbl_name (col_name data_type, ...)
選択中のDBであるtest1にテーブル"input"を作成
MySQL8.0CommandLineClient
create table input (
id int not null auto_increment,
name char(30) not null,
text char(255),
date datetime,
primary key(id)
);
auto_increment:新しい行に一意の識別子を生成できる。
primary key:主キーの設定
テーブルのカラムと設定内容確認
書式
SHOW FULL COLUMNS FROM [TABLE_NAME];
MySQL8.0CommandLineClient
show full columns input
実行結果
MySQL8.0CommandLineClient
+-------+-----------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+-----------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| name | char(30) | utf8mb4_0900_ai_ci | NO | | NULL | | select,insert,update,references | |
| text | char(255) | utf8mb4_0900_ai_ci | YES | | NULL | | select,insert,update,references | |
| date | datetime | NULL | YES | | NULL | | select,insert,update,references | |
+-------+-----------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
テーブルにデータを追加
書式
INSERT [INTO] tbl_name [(col_name [, col_name] ...)] {VALUES | VALUE} (value_list) [, (value_list)] ...
1行データを追加
MySQL8.0CommandLineClient
insert into input(name,text,date) values('taro','test1','2021-05-30 22:00::00');
複数行データを追加
MySQL8.0CommandLineClient
insert into input(name,text,date) values
('yama','test2','2021-05-30 22:01:00'),
('taro','test3','2021-05-30 22:02:00'),
('yama','test4','2021-05-30 22:03:00');
テーブルデータ取得
MySQL8.0CommandLineClient
select * from input;
実行結果
MySQL8.0CommandLineClient
+----+------+-------+---------------------+
| id | name | text | date |
+----+------+-------+---------------------+
| 1 | taro | test1 | 2021-05-30 22:00:00 |
| 2 | yama | test2 | 2021-05-30 22:01:00 |
| 3 | taro | test3 | 2021-05-30 22:02:00 |
| 4 | yama | test4 | 2021-05-30 22:03:00 |
+----+------+-------+---------------------+