MySQLの備忘録です
目次
テーブルの情報を取得
データベースを選択
USE データベース名;
Database changed
テーブル名を表示
データベースを選択している場合
USE データベース名;で使用するデータベースを選択している場合は以下を入力
SHOW TABLES;
+-------------------------------------+ | Tables_in_world | +-------------------------------------+ | city | | country | | countrylanguage | +-------------------------------------+
LIKEを使って特定のテーブルを抽出
ワイルドカード(%)を使って特定のテーブルを絞り込めます
| ‘%~’ | ~で終わる |
| ‘~%’ | ~で始まる |
| ‘%〇%’ | ~を含む |
SHOW TABLES LIKE ‘%y’;
”y”で終わるテーブルが抽出されました
+-------------------------------------+ | Tables_in_world (%y) | +-------------------------------------+ | city | | country | +-------------------------------------+
データベースを選択していない場合
使用するデータベースを選択していない場合は以下を入力
SHOW TABLES FROM データベース名;
+-------------------------------------+ | Tables_in_world | +-------------------------------------+ | city | | country | | countrylanguage | +-------------------------------------+
テーブル名とテーブルの種類を取得
SHOW FULL TABLES;
+-----------------+------------+ | Tables_in_world | Table_type | +-----------------+------------+ | city | BASE TABLE | | country | BASE TABLE | | countrylanguage | BASE TABLE | +-----------------+------------+
データベースのテーブル情報を取得
SHOW TABLE STATUS FROM データベース名\G
*************************** 1. row ***************************
Name: city
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 4047
Avg_row_length: 101
Data_length: 409600
Max_data_length: 0
Index_length: 114688
Data_free: 0
Auto_increment: 4080
Create_time: 2023-06-01 16:54:56
Update_time: NULL
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options:
Comment:
*************************** 2. row ***************************
Name: country
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 239
Avg_row_length: 479
Data_length: 114688
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2023-06-01 16:54:56
Update_time: NULL
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options:
Comment:
*************************** 3. row ***************************
Name: countrylanguage
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 984
Avg_row_length: 99
Data_length: 98304
Max_data_length: 0
Index_length: 65536
Data_free: 0
Auto_increment: NULL
Create_time: 2023-06-01 16:54:56
Update_time: NULL
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options:
Comment:
3 rows in set (0.00 sec)
LIKEを使用して特定のテーブルを検索
ワイルドカードを使用して、特定のテーブルを絞り込めます
| ‘%~’ | ~で終わる |
| ‘~%’ | ~で始まる |
| ‘%〇%’ | ~を含む |
SHOW TABLE STATUS LIKE ‘%it%’;
‘it’を含むテーブルの情報を取得しました
*************************** 1. row ***************************
Name: city
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 4047
Avg_row_length: 101
Data_length: 409600
Max_data_length: 0
Index_length: 114688
Data_free: 0
Auto_increment: 4080
Create_time: 2023-06-01 16:54:56
Update_time: NULL
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
テーブル定義を確認
作成したテーブルの項目を表示
SHOW CREATE TABLE テーブル名;(\G)
*************************** 1. row ***************************
Table: city
Create Table: CREATE TABLE `city` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` char(35) NOT NULL DEFAULT '',
`CountryCode` char(3) NOT NULL DEFAULT '',
`District` char(20) NOT NULL DEFAULT '',
`Population` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `CountryCode` (`CountryCode`),
CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=utf8mb4
1 row in set (0.01 sec)
特定のテーブル内の項目情報を表示
SHOW FULL COLUMNS FROM テーブル名\G
*************************** 1. row ***************************
Field: ID
Type: int(11)
Collation: NULL
Null: NO
Key: PRI
Default: NULL
Extra: auto_increment
Privileges: select,insert,update,references
Comment:
*************************** 2. row ***************************
Field: Name
Type: char(35)
Collation: utf8mb4_general_ci
Null: NO
Key:
Default:
Extra:
Privileges: select,insert,update,references
Comment:
*************************** 3. row ***************************
Field: CountryCode
Type: char(3)
Collation: utf8mb4_general_ci
Null: NO
Key: MUL
Default:
Extra:
Privileges: select,insert,update,references
Comment:
*************************** 4. row ***************************
Field: District
Type: char(20)
Collation: utf8mb4_general_ci
Null: NO
Key:
Default:
Extra:
Privileges: select,insert,update,references
Comment:
*************************** 5. row ***************************
Field: Population
Type: int(11)
Collation: NULL
Null: NO
Key:
Default: 0
Extra:
Privileges: select,insert,update,references
Comment:
5 rows in set (0.12 sec)
テーブル構造またはクエリ実行計画に関する情報を表示
DESC テーブル名;
+-------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+----------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | Name | char(35) | NO | | | | | CountryCode | char(3) | NO | MUL | | | | District | char(20) | NO | | | | | Population | int(11) | NO | | 0 | | +-------------+----------+------+-----+---------+----------------+


コメント