语法
mysql> show databases [like wild];
显示所有数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.03 sec)
显示后缀为sql的数据库
mysql> show databases like "%sql";
+-----------------+
| Database (%sql) |
+-----------------+
| mysql |
+-----------------+
1 row in set (0.00 sec)
语法
mysql> create database [if not exists] 数据库名;
mysql> create database huazi;
Query OK, 1 row affected (0.03 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| huazi |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
语法
mysql> show create database 数据库名;
语法
mysql> show warnings;
语法
mysql> drop database [if exists] 数据库名;
mysql> drop database huazi;
Query OK, 0 rows affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
语法
mysql> use 数据库名;
mysql> create database huazi;
Query OK, 1 row affected (0.03 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| huazi |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> select database(); #显示当前正在使用的数据库
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> use huazi; #切换成huazi数据库
Database changed
mysql> select database(); #显示当前正在使用的数据库
+------------+
| database() |
+------------+
| huazi |
+------------+
1 row in set (0.00 sec)
mysql> show variables;
+-------------------------------------------+------------------------------------------------------+
| Variable_name | Value
注:这里的变量太多了,博主这里就不显示了。显示变量内容:只有2列,一列是变量名,一列是变量值。
在MySQL中,行格式指的是如何组织和存储表中的数据行。不同的行格式可以影响存储、性能和空间利用率等方面的特性。MySQL支持多种行格式,其中一些常见的行格式包括:
选择适当的行格式取决于你的应用需求,需要权衡存储空间、性能和数据访问模式等因素。不同的行格式适用于不同的使用场景,因此在设计表时要根据具体情况选择合适的行格式。可以在创建表时或在表已经存在的情况下通过row_format选项来指定行格式。
mysql> show variables like "%row_format%";
+---------------------------+---------+
| Variable_name | Value |
+---------------------------+---------+
| innodb_default_row_format | dynamic |
| require_row_format | OFF |
+---------------------------+---------+
2 rows in set (0.01 sec)
mysql> select @@innodb_default_row_format;
+-----------------------------+
| @@innodb_default_row_format |
+-----------------------------+
| dynamic |
+-----------------------------+
1 row in set (0.01 sec)
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.36 |
+-----------+
1 row in set (0.00 sec)
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> select User,Host from mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster | NO | Clustered, fault-tolerant tables | NULL | NULL | NULL |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| ndbinfo | NO | MySQL Cluster system information storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.01 sec)
mysql> show tables;
语法
mysql> system 系统命令;
mysql> system ls -l;
总用量 4
drwxr-xr-x. 2 root root 6 2月 4 22:24 公共
drwxr-xr-x. 2 root root 6 2月 4 22:24 模板
drwxr-xr-x. 2 root root 6 2月 4 22:24 视频
drwxr-xr-x. 2 root root 6 2月 4 22:24 图片
drwxr-xr-x. 2 root root 6 2月 4 22:24 文档
drwxr-xr-x. 2 root root 6 2月 4 22:24 下载
drwxr-xr-x. 2 root root 6 2月 4 22:24 音乐
drwxr-xr-x. 2 root root 6 2月 4 22:24 桌面
-rw-------. 1 root root 1292 2月 4 22:10 anaconda-ks.cfg
mysql> system date;
2024年 02月 05日 星期一 16:11:27 CST
mysql字符集:
latin1支持西欧字符、希腊字符等
gbk支持中文简体字符
big5支持中文繁体字符
utf8几乎支持世界所有国家的字符。
utf8mb4是真正意义上的utf-8
mysql> show variables like 'character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.02 sec)
| character_set_client | utf8mb4 | mysql客户端字符集
| character_set_connection | utf8mb4 | 数据通信链路字符集,当mysql客户端向服务器发送请求时,客户端的请求数据以该字符集进行编码。
| character_set_database | utf8mb4 | 数据库字符集
| character_set_filesystem | binary | MySQL服务器文件系统字符集,该值是固定的binary
| character_set_results | utf8mb4 | 结果集的字符集,MySQL服务器向mysql客户端返回执行结果时,执行结果以该字符集进行编码。
| character_set_server | utf8mb4 | mysql服务器实例字符集
| character_set_system | utf8mb3 | 元数据(字段名、表名、数据库名等)的字符集
| character_sets_dir | /usr/share/mysql/charsets/ | 字符集安装的目录
mysql> show collation;
+-----------------------------+----------+-----+---------+----------+---------+---------------+
| Collation | Charset | Id | Default | Compiled | Sortlen | Pad_attribute |
+-----------------------------+----------+-----+---------+----------+---------+---------------+
| armscii8_bin | armscii8 | 64 | | Yes | 1 | PAD SPACE |
| armscii8_general_ci | armscii8 | 32 | Yes | Yes | 1 | PAD SPACE |
| utf8mb4_0900_bin | utf8mb4 | 309 | | Yes | 1 | NO PAD |
| utf8mb4_bg_0900_ai_ci | utf8mb4 | 318 | | Yes | 0 | NO PAD |
Collation 字符集校对规则名称。MySQL校对规则名称是:以对应的字符集名称开头,以国家名居中(或以general居中),以ci、cs或bin结尾。【ci表示大小写不敏感,cs表示大小写敏感,bin表示按二进制编码值比较。】
Charset 与字符集校对规则关联的字符集名称
Id 字符集校对规则编号
Default 是不是对应字符集默认的校对规则
Compiled 是否有将此字符集校对规则集成到服务器中
Sortlen 这个与字符串表示的字符集所需要的内存数量有关
Pad_attribute 控制字符串尾部空格处理方式。PAD SPACE:在排序和比较运算中,忽略字符串尾部空格;NO PAD:在排序和比较运算中,字符串尾部空格当成普通字符,不能忽略。
mysql> show charset;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| binary | Binary pseudo charset | binary | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| utf8mb3 | UTF-8 Unicode | utf8mb3_general_ci | 3 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_0900_ai_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)
或(显示结果一样)
mysql> show character set;
语法
mysql> create database 库名 character set 字符集 collate 校对规则;
或
mysql> create database 库名 charset 字符集 collate 校对规则;
或
mysql> create database 库名 charset=字符集 collate=校对规则;
mysql> create database huazi charset gbk collate gbk_chinese_ci;
Query OK, 1 row affected (0.00 sec)
或
mysql> create database huazi character set gbk collate gbk_chinese_ci;
Query OK, 1 row affected (0.01 sec)
mysql> select @@character_set_database,@@collation_database;
+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| gbk | gbk_chinese_ci |
+--------------------------+----------------------+
1 row in set (0.00 sec)
mysql> show variables like "%collation%";
mysql> show variables like "%character%";
语法
mysql> alter database 库名 charset 字符集 collate 校对规则;
或
mysql> alter database 库名 charset=字符集 collate=校对规则;
mysql> alter database huazi charset utf8mb4 collate utf8mb4_0900_ai_ci;
Query OK, 1 row affected (0.00 sec)
mysql> select @@character_set_database,@@collation_database;
+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| utf8mb4 | utf8mb4_0900_ai_ci |
+--------------------------+----------------------+
1 row in set (0.00 sec)